KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > metal > DefaultMetalTheme


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  
8 package javax.swing.plaf.metal;
9
10 import javax.swing.plaf.*;
11 import javax.swing.*;
12 import java.awt.*;
13
14 import sun.security.action.GetPropertyAction;
15
16 /**
17  * This class describes the default Metal Theme.
18  * <p>
19  * <strong>Warning:</strong>
20  * Serialized objects of this class will not be compatible with
21  * future Swing releases. The current serialization support is
22  * appropriate for short term storage or RMI between applications running
23  * the same version of Swing. As of 1.4, support for long term storage
24  * of all JavaBeans<sup><font size="-2">TM</font></sup>
25  * has been added to the <code>java.beans</code> package.
26  * Please see {@link java.beans.XMLEncoder}.
27  *
28  * @version 1.27 03/03/04
29  * @author Steve Wilson
30  */

31 public class DefaultMetalTheme extends MetalTheme JavaDoc {
32     /**
33      * Whether or not fonts should be plain. This is only used if
34      * the defaults property 'swing.boldMetal' == "false".
35      */

36     private static final boolean PLAIN_FONTS;
37
38     /**
39      * Names of the fonts to use.
40      */

41     private static final String JavaDoc[] fontNames = {
42         "Dialog", "Dialog", "Dialog", "Dialog", "Dialog", "Dialog"
43     };
44     /**
45      * Styles for the fonts. This is ignored if the defaults property
46      * <code>swing.boldMetal</code> is false, or PLAIN_FONTS is true.
47      */

48     private static final int[] fontStyles = {
49         Font.BOLD, Font.PLAIN, Font.PLAIN, Font.BOLD, Font.BOLD, Font.PLAIN
50     };
51     /**
52      * Sizes for the fonts.
53      */

54     private static final int[] fontSizes = {
55         12, 12, 12, 12, 12, 10
56     };
57
58     // note the properties listed here can currently be used by people
59
// providing runtimes to hint what fonts are good. For example the bold
60
// dialog font looks bad on a Mac, so Apple could use this property to
61
// hint at a good font.
62
//
63
// However, we don't promise to support these forever. We may move
64
// to getting these from the swing.properties file, or elsewhere.
65
/**
66      * System property names used to look up fonts.
67      */

68     private static final String JavaDoc[] defaultNames = {
69         "swing.plaf.metal.controlFont",
70         "swing.plaf.metal.systemFont",
71         "swing.plaf.metal.userFont",
72         "swing.plaf.metal.controlFont",
73         "swing.plaf.metal.controlFont",
74         "swing.plaf.metal.smallFont"
75     };
76
77     /**
78      * Returns the ideal font name for the font identified by key.
79      */

80     static String JavaDoc getDefaultFontName(int key) {
81         return fontNames[key];
82     }
83
84     /**
85      * Returns the ideal font size for the font identified by key.
86      */

87     static int getDefaultFontSize(int key) {
88         return fontSizes[key];
89     }
90
91     /**
92      * Returns the ideal font style for the font identified by key.
93      */

94     static int getDefaultFontStyle(int key) {
95         if (key != WINDOW_TITLE_FONT) {
96             Object JavaDoc boldMetal = UIManager.get("swing.boldMetal");
97             if (boldMetal != null) {
98                 if (Boolean.FALSE.equals(boldMetal)) {
99                     return Font.PLAIN;
100                 }
101             }
102             else if (PLAIN_FONTS) {
103                 return Font.PLAIN;
104             }
105         }
106         return fontStyles[key];
107     }
108
109     /**
110      * Returns the default used to look up the specified font.
111      */

112     static String JavaDoc getDefaultPropertyName(int key) {
113         return defaultNames[key];
114     }
115
116     static {
117         Object JavaDoc boldProperty = java.security.AccessController.doPrivileged(
118             new GetPropertyAction("swing.boldMetal"));
119         if (boldProperty == null || !"false".equals(boldProperty)) {
120             PLAIN_FONTS = false;
121         }
122         else {
123             PLAIN_FONTS = true;
124         }
125     }
126
127     private static final ColorUIResource primary1 = new ColorUIResource(
128                               102, 102, 153);
129     private static final ColorUIResource primary2 = new ColorUIResource(153,
130                               153, 204);
131     private static final ColorUIResource primary3 = new ColorUIResource(
132                               204, 204, 255);
133     private static final ColorUIResource secondary1 = new ColorUIResource(
134                               102, 102, 102);
135     private static final ColorUIResource secondary2 = new ColorUIResource(
136                               153, 153, 153);
137     private static final ColorUIResource secondary3 = new ColorUIResource(
138                               204, 204, 204);
139
140     private FontDelegate fontDelegate;
141
142     public String JavaDoc getName() { return "Steel"; }
143
144     public DefaultMetalTheme() {
145         install();
146     }
147
148     // these are blue in Metal Default Theme
149
protected ColorUIResource getPrimary1() { return primary1; }
150     protected ColorUIResource getPrimary2() { return primary2; }
151     protected ColorUIResource getPrimary3() { return primary3; }
152
153     // these are gray in Metal Default Theme
154
protected ColorUIResource getSecondary1() { return secondary1; }
155     protected ColorUIResource getSecondary2() { return secondary2; }
156     protected ColorUIResource getSecondary3() { return secondary3; }
157
158
159     public FontUIResource getControlTextFont() {
160         return getFont(CONTROL_TEXT_FONT);
161     }
162
163     public FontUIResource getSystemTextFont() {
164         return getFont(SYSTEM_TEXT_FONT);
165     }
166
167     public FontUIResource getUserTextFont() {
168         return getFont(USER_TEXT_FONT);
169     }
170
171     public FontUIResource getMenuTextFont() {
172         return getFont(MENU_TEXT_FONT);
173     }
174
175     public FontUIResource getWindowTitleFont() {
176         return getFont(WINDOW_TITLE_FONT);
177     }
178
179     public FontUIResource getSubTextFont() {
180         return getFont(SUB_TEXT_FONT);
181     }
182
183     private FontUIResource getFont(int key) {
184         return fontDelegate.getFont(key);
185     }
186
187     void install() {
188         if (MetalLookAndFeel.isWindows() &&
189                              MetalLookAndFeel.useSystemFonts()) {
190             fontDelegate = new WindowsFontDelegate();
191         }
192         else {
193             fontDelegate = new FontDelegate();
194         }
195     }
196
197     /**
198      * Returns true if this is a theme provided by the core platform.
199      */

200     boolean isSystemTheme() {
201         return (getClass() == DefaultMetalTheme JavaDoc.class);
202     }
203
204     /**
205      * FontDelegates add an extra level of indirection to obtaining fonts.
206      */

207     private static class FontDelegate {
208         private static int[] defaultMapping = {
209             CONTROL_TEXT_FONT, SYSTEM_TEXT_FONT,
210             USER_TEXT_FONT, CONTROL_TEXT_FONT,
211             CONTROL_TEXT_FONT, SUB_TEXT_FONT
212         };
213         FontUIResource fonts[];
214
215         // menu and window are mapped to controlFont
216
public FontDelegate() {
217             fonts = new FontUIResource[6];
218         }
219
220         public FontUIResource getFont(int type) {
221             int mappedType = defaultMapping[type];
222             if (fonts[type] == null) {
223                 Font f = getPrivilegedFont(mappedType);
224
225                 if (f == null) {
226                     f = new Font(getDefaultFontName(type),
227                              getDefaultFontStyle(type),
228                              getDefaultFontSize(type));
229                 }
230                 fonts[type] = new FontUIResource(f);
231             }
232             return fonts[type];
233         }
234
235         /**
236          * This is the same as invoking
237          * <code>Font.getFont(key)</code>, with the exception
238          * that it is wrapped inside a <code>doPrivileged</code> call.
239          */

240         protected Font getPrivilegedFont(final int key) {
241             return (Font)java.security.AccessController.doPrivileged(
242                 new java.security.PrivilegedAction JavaDoc() {
243                     public Object JavaDoc run() {
244                         return Font.getFont(getDefaultPropertyName(key));
245                     }
246                 }
247                 );
248         }
249     }
250
251     /**
252      * The WindowsFontDelegate uses DesktopProperties to obtain fonts.
253      */

254     private static class WindowsFontDelegate extends FontDelegate {
255         private MetalFontDesktopProperty JavaDoc[] props;
256         private boolean[] checkedPriviledged;
257
258         public WindowsFontDelegate() {
259             props = new MetalFontDesktopProperty JavaDoc[6];
260             checkedPriviledged = new boolean[6];
261         }
262
263         public FontUIResource getFont(int type) {
264             if (fonts[type] != null) {
265                 return fonts[type];
266             }
267             if (!checkedPriviledged[type]) {
268                 Font f = getPrivilegedFont(type);
269
270                 checkedPriviledged[type] = true;
271                 if (f != null) {
272                     fonts[type] = new FontUIResource(f);
273                     return fonts[type];
274                 }
275             }
276             if (props[type] == null) {
277                 props[type] = new MetalFontDesktopProperty JavaDoc(type);
278             }
279             // While passing null may seem bad, we don't actually use
280
// the table and looking it up is rather expensive.
281
return (FontUIResource)props[type].createValue(null);
282         }
283     }
284 }
285
Popular Tags