1. /*
  2. * @(#)DefaultMetalTheme.java 1.27 04/03/03
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.plaf.metal;
  8. import javax.swing.plaf.*;
  9. import javax.swing.*;
  10. import java.awt.*;
  11. import sun.security.action.GetPropertyAction;
  12. /**
  13. * This class describes the default Metal Theme.
  14. * <p>
  15. * <strong>Warning:</strong>
  16. * Serialized objects of this class will not be compatible with
  17. * future Swing releases. The current serialization support is
  18. * appropriate for short term storage or RMI between applications running
  19. * the same version of Swing. As of 1.4, support for long term storage
  20. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  21. * has been added to the <code>java.beans</code> package.
  22. * Please see {@link java.beans.XMLEncoder}.
  23. *
  24. * @version 1.27 03/03/04
  25. * @author Steve Wilson
  26. */
  27. public class DefaultMetalTheme extends MetalTheme {
  28. /**
  29. * Whether or not fonts should be plain. This is only used if
  30. * the defaults property 'swing.boldMetal' == "false".
  31. */
  32. private static final boolean PLAIN_FONTS;
  33. /**
  34. * Names of the fonts to use.
  35. */
  36. private static final String[] fontNames = {
  37. "Dialog", "Dialog", "Dialog", "Dialog", "Dialog", "Dialog"
  38. };
  39. /**
  40. * Styles for the fonts. This is ignored if the defaults property
  41. * <code>swing.boldMetal</code> is false, or PLAIN_FONTS is true.
  42. */
  43. private static final int[] fontStyles = {
  44. Font.BOLD, Font.PLAIN, Font.PLAIN, Font.BOLD, Font.BOLD, Font.PLAIN
  45. };
  46. /**
  47. * Sizes for the fonts.
  48. */
  49. private static final int[] fontSizes = {
  50. 12, 12, 12, 12, 12, 10
  51. };
  52. // note the properties listed here can currently be used by people
  53. // providing runtimes to hint what fonts are good. For example the bold
  54. // dialog font looks bad on a Mac, so Apple could use this property to
  55. // hint at a good font.
  56. //
  57. // However, we don't promise to support these forever. We may move
  58. // to getting these from the swing.properties file, or elsewhere.
  59. /**
  60. * System property names used to look up fonts.
  61. */
  62. private static final String[] defaultNames = {
  63. "swing.plaf.metal.controlFont",
  64. "swing.plaf.metal.systemFont",
  65. "swing.plaf.metal.userFont",
  66. "swing.plaf.metal.controlFont",
  67. "swing.plaf.metal.controlFont",
  68. "swing.plaf.metal.smallFont"
  69. };
  70. /**
  71. * Returns the ideal font name for the font identified by key.
  72. */
  73. static String getDefaultFontName(int key) {
  74. return fontNames[key];
  75. }
  76. /**
  77. * Returns the ideal font size for the font identified by key.
  78. */
  79. static int getDefaultFontSize(int key) {
  80. return fontSizes[key];
  81. }
  82. /**
  83. * Returns the ideal font style for the font identified by key.
  84. */
  85. static int getDefaultFontStyle(int key) {
  86. if (key != WINDOW_TITLE_FONT) {
  87. Object boldMetal = UIManager.get("swing.boldMetal");
  88. if (boldMetal != null) {
  89. if (Boolean.FALSE.equals(boldMetal)) {
  90. return Font.PLAIN;
  91. }
  92. }
  93. else if (PLAIN_FONTS) {
  94. return Font.PLAIN;
  95. }
  96. }
  97. return fontStyles[key];
  98. }
  99. /**
  100. * Returns the default used to look up the specified font.
  101. */
  102. static String getDefaultPropertyName(int key) {
  103. return defaultNames[key];
  104. }
  105. static {
  106. Object boldProperty = java.security.AccessController.doPrivileged(
  107. new GetPropertyAction("swing.boldMetal"));
  108. if (boldProperty == null || !"false".equals(boldProperty)) {
  109. PLAIN_FONTS = false;
  110. }
  111. else {
  112. PLAIN_FONTS = true;
  113. }
  114. }
  115. private static final ColorUIResource primary1 = new ColorUIResource(
  116. 102, 102, 153);
  117. private static final ColorUIResource primary2 = new ColorUIResource(153,
  118. 153, 204);
  119. private static final ColorUIResource primary3 = new ColorUIResource(
  120. 204, 204, 255);
  121. private static final ColorUIResource secondary1 = new ColorUIResource(
  122. 102, 102, 102);
  123. private static final ColorUIResource secondary2 = new ColorUIResource(
  124. 153, 153, 153);
  125. private static final ColorUIResource secondary3 = new ColorUIResource(
  126. 204, 204, 204);
  127. private FontDelegate fontDelegate;
  128. public String getName() { return "Steel"; }
  129. public DefaultMetalTheme() {
  130. install();
  131. }
  132. // these are blue in Metal Default Theme
  133. protected ColorUIResource getPrimary1() { return primary1; }
  134. protected ColorUIResource getPrimary2() { return primary2; }
  135. protected ColorUIResource getPrimary3() { return primary3; }
  136. // these are gray in Metal Default Theme
  137. protected ColorUIResource getSecondary1() { return secondary1; }
  138. protected ColorUIResource getSecondary2() { return secondary2; }
  139. protected ColorUIResource getSecondary3() { return secondary3; }
  140. public FontUIResource getControlTextFont() {
  141. return getFont(CONTROL_TEXT_FONT);
  142. }
  143. public FontUIResource getSystemTextFont() {
  144. return getFont(SYSTEM_TEXT_FONT);
  145. }
  146. public FontUIResource getUserTextFont() {
  147. return getFont(USER_TEXT_FONT);
  148. }
  149. public FontUIResource getMenuTextFont() {
  150. return getFont(MENU_TEXT_FONT);
  151. }
  152. public FontUIResource getWindowTitleFont() {
  153. return getFont(WINDOW_TITLE_FONT);
  154. }
  155. public FontUIResource getSubTextFont() {
  156. return getFont(SUB_TEXT_FONT);
  157. }
  158. private FontUIResource getFont(int key) {
  159. return fontDelegate.getFont(key);
  160. }
  161. void install() {
  162. if (MetalLookAndFeel.isWindows() &&
  163. MetalLookAndFeel.useSystemFonts()) {
  164. fontDelegate = new WindowsFontDelegate();
  165. }
  166. else {
  167. fontDelegate = new FontDelegate();
  168. }
  169. }
  170. /**
  171. * Returns true if this is a theme provided by the core platform.
  172. */
  173. boolean isSystemTheme() {
  174. return (getClass() == DefaultMetalTheme.class);
  175. }
  176. /**
  177. * FontDelegates add an extra level of indirection to obtaining fonts.
  178. */
  179. private static class FontDelegate {
  180. private static int[] defaultMapping = {
  181. CONTROL_TEXT_FONT, SYSTEM_TEXT_FONT,
  182. USER_TEXT_FONT, CONTROL_TEXT_FONT,
  183. CONTROL_TEXT_FONT, SUB_TEXT_FONT
  184. };
  185. FontUIResource fonts[];
  186. // menu and window are mapped to controlFont
  187. public FontDelegate() {
  188. fonts = new FontUIResource[6];
  189. }
  190. public FontUIResource getFont(int type) {
  191. int mappedType = defaultMapping[type];
  192. if (fonts[type] == null) {
  193. Font f = getPrivilegedFont(mappedType);
  194. if (f == null) {
  195. f = new Font(getDefaultFontName(type),
  196. getDefaultFontStyle(type),
  197. getDefaultFontSize(type));
  198. }
  199. fonts[type] = new FontUIResource(f);
  200. }
  201. return fonts[type];
  202. }
  203. /**
  204. * This is the same as invoking
  205. * <code>Font.getFont(key)</code>, with the exception
  206. * that it is wrapped inside a <code>doPrivileged</code> call.
  207. */
  208. protected Font getPrivilegedFont(final int key) {
  209. return (Font)java.security.AccessController.doPrivileged(
  210. new java.security.PrivilegedAction() {
  211. public Object run() {
  212. return Font.getFont(getDefaultPropertyName(key));
  213. }
  214. }
  215. );
  216. }
  217. }
  218. /**
  219. * The WindowsFontDelegate uses DesktopProperties to obtain fonts.
  220. */
  221. private static class WindowsFontDelegate extends FontDelegate {
  222. private MetalFontDesktopProperty[] props;
  223. private boolean[] checkedPriviledged;
  224. public WindowsFontDelegate() {
  225. props = new MetalFontDesktopProperty[6];
  226. checkedPriviledged = new boolean[6];
  227. }
  228. public FontUIResource getFont(int type) {
  229. if (fonts[type] != null) {
  230. return fonts[type];
  231. }
  232. if (!checkedPriviledged[type]) {
  233. Font f = getPrivilegedFont(type);
  234. checkedPriviledged[type] = true;
  235. if (f != null) {
  236. fonts[type] = new FontUIResource(f);
  237. return fonts[type];
  238. }
  239. }
  240. if (props[type] == null) {
  241. props[type] = new MetalFontDesktopProperty(type);
  242. }
  243. // While passing null may seem bad, we don't actually use
  244. // the table and looking it up is rather expensive.
  245. return (FontUIResource)props[type].createValue(null);
  246. }
  247. }
  248. }