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