KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > util > swing > ConfigurableMetalTheme


1 package net.suberic.util.swing;
2
3 import net.suberic.util.*;
4 import java.util.*;
5 import javax.swing.*;
6 import javax.swing.plaf.metal.*;
7 import javax.swing.plaf.*;
8
9
10 /**
11  * A Theme which allows you to set all of your colors for a Metal
12  * application.
13  */

14 public class ConfigurableMetalTheme extends DefaultMetalTheme implements Item, ValueChangeListener {
15
16   private String JavaDoc itemId;
17   private String JavaDoc resourceString;
18
19   private VariableBundle bundle = null;
20
21   private WeakHashMap themeListenerList = new WeakHashMap();
22
23   protected ColorUIResource subPrimary1 = null;
24   protected ColorUIResource subPrimary2 = null;
25   protected ColorUIResource subPrimary3 = null;
26     
27
28   protected ColorUIResource subSecondary1 = null;
29   protected ColorUIResource subSecondary2 = null;
30   protected ColorUIResource subSecondary3 = null;
31
32   protected ColorUIResource subBlack = null;
33   protected ColorUIResource subWhite = null;
34   
35   protected FontUIResource subControlFont = null;
36   protected FontUIResource subSystemFont = null;
37   protected FontUIResource subUserFont = null;
38   protected FontUIResource subSmallFont = null;
39   protected FontUIResource subMonospacedFont = null;
40   
41   
42   /**
43    * Creates a new ConfigurableMetalTheme from the given property.
44    */

45   public ConfigurableMetalTheme(VariableBundle sourceBundle, String JavaDoc newResourceString, String JavaDoc newItemId) {
46     itemId = newItemId;
47     resourceString = newResourceString;
48     
49     bundle = sourceBundle;
50
51     loadTheme(getItemProperty(), sourceBundle);
52
53     sourceBundle.addValueChangeListener(this, getItemProperty() + ".*");
54   }
55   
56   /**
57    * The Item ID. For example, if you were to have a list of users, a
58    * given user's itemID may be "defaultUser".
59    */

60   public String JavaDoc getItemID() {
61     return itemId;
62   }
63   
64   /**
65    * The Item property. For example, if you were to have a list of users, a
66    * given user's itemProperty may be "Users.defaultUser".
67    */

68   public String JavaDoc getItemProperty() {
69     return resourceString + "." + itemId;
70   }
71
72   /**
73    * Called when a ui value changes.
74    */

75   public void valueChanged(String JavaDoc changedValue) {
76     loadTheme(getItemProperty(), bundle);
77     fireThemeChangedEvent();
78   }
79
80   /**
81    * Adds a ThemeListener to the ListenerList.
82    */

83   public void addThemeListener(ThemeListener tl) {
84     if (! themeListenerList.containsKey(tl))
85       themeListenerList.put(tl, null);
86   }
87   
88   /**
89    * Removes a ThemeListener from the ListenerList.
90    */

91   public void removeThemeListener(ThemeListener tl) {
92     themeListenerList.remove(tl);
93   }
94
95   /**
96    * Notifies all registered ThemeListeners that this Theme has changed.
97    */

98   public void fireThemeChangedEvent() {
99     Iterator iter = themeListenerList.keySet().iterator();
100     while (iter.hasNext()) {
101       ThemeListener current = (ThemeListener) iter.next();
102       current.themeChanged(this);
103     }
104   }
105
106   /**
107    * This loads the theme from the given property and bundle.
108    */

109   protected void loadTheme(String JavaDoc property, VariableBundle sourceBundle) {
110     subPrimary1 = createColorUIResource(property + ".primary1", sourceBundle);
111     subPrimary2 = createColorUIResource(property + ".primary2", sourceBundle);
112     subPrimary3 = createColorUIResource(property + ".primary3", sourceBundle);
113
114     subSecondary1 = createColorUIResource(property + ".secondary1", sourceBundle);
115     subSecondary2 = createColorUIResource(property + ".secondary2", sourceBundle);
116     subSecondary3 = createColorUIResource(property + ".secondary3", sourceBundle);
117     subWhite = createColorUIResource(property + ".white", sourceBundle);
118     subBlack = createColorUIResource(property + ".black", sourceBundle);
119
120     subControlFont = createFontUIResource(property + ".controlFont", sourceBundle);
121     subSystemFont = createFontUIResource(property + ".systemFont", sourceBundle);
122     subUserFont = createFontUIResource(property + ".userFont", sourceBundle);
123     subSmallFont = createFontUIResource(property + ".smallFont", sourceBundle);
124     subMonospacedFont = createFontUIResource(property + ".monospacedFont", sourceBundle);
125     
126   }
127   
128   protected ColorUIResource createColorUIResource(String JavaDoc property, VariableBundle sourceBundle) {
129     String JavaDoc enabled = sourceBundle.getProperty(property + "._enabled", "false");
130     if (enabled.equalsIgnoreCase("true")) {
131       String JavaDoc rgbString = sourceBundle.getProperty(property + ".rgb", "");
132       if (rgbString != null && ! rgbString.equals("")) {
133     try {
134       int rgbValue = Integer.parseInt(rgbString);
135       ColorUIResource returnValue = new ColorUIResource(rgbValue);
136       return returnValue;
137     } catch (Exception JavaDoc e) {
138       return null;
139     }
140       }
141     }
142
143     return null;
144   }
145
146   protected FontUIResource createFontUIResource(String JavaDoc property, VariableBundle sourceBundle) {
147     String JavaDoc enabled = sourceBundle.getProperty(property + "._enabled", "false");
148     if (enabled.equalsIgnoreCase("true")) {
149       String JavaDoc fontString = sourceBundle.getProperty(property, "");
150       if (fontString != null && ! fontString.equals("")) {
151     try {
152       FontUIResource returnValue = new FontUIResource(java.awt.Font.decode(fontString));
153       return returnValue;
154     } catch (Exception JavaDoc e) {
155     return null;
156     }
157       }
158     }
159
160     return null;
161   }
162
163   public String JavaDoc getName() { return getItemID(); }
164   
165   
166   protected ColorUIResource getPrimary1() {
167     if (subPrimary1 != null)
168       return subPrimary1;
169     else
170       return super.getPrimary1();
171   }
172   protected ColorUIResource getPrimary2() {
173     if (subPrimary2 != null)
174       return subPrimary2;
175     else
176       return super.getPrimary2();
177   }
178   protected ColorUIResource getPrimary3() {
179     if (subPrimary3 != null)
180       return subPrimary3;
181     else
182       return super.getPrimary3();
183   }
184   
185   protected ColorUIResource getSecondary1() {
186     if (subSecondary1 != null)
187       return subSecondary1;
188     else
189       return super.getSecondary1();
190   }
191     protected ColorUIResource getSecondary2() {
192     if (subSecondary2 != null)
193       return subSecondary2;
194     else
195       return super.getSecondary2();
196     }
197     protected ColorUIResource getSecondary3() {
198     if (subSecondary3 != null)
199       return subSecondary3;
200     else
201       return super.getSecondary3();
202     }
203     protected ColorUIResource getWhite() {
204     if (subWhite != null)
205       return subWhite;
206     else
207       return super.getWhite();
208     }
209     protected ColorUIResource getBlack() {
210       if (subBlack != null) {
211       
212     return subBlack;
213       } else {
214     return super.getBlack();
215       }
216     }
217
218     public FontUIResource getControlTextFont() {
219     if (subControlFont != null)
220       return subControlFont;
221     else
222       return super.getControlTextFont();
223     }
224     public FontUIResource getSystemTextFont() {
225     if (subSystemFont != null)
226       return subSystemFont;
227     else
228       return super.getSystemTextFont();
229     }
230     public FontUIResource getUserTextFont() {
231     if (subUserFont != null)
232       return subUserFont;
233     else
234       return super.getUserTextFont();
235     }
236     public FontUIResource getMenuTextFont() {
237     if (subControlFont != null)
238       return subControlFont;
239     else
240       return super.getMenuTextFont();
241     }
242     public FontUIResource getWindowTitleFont() {
243     if (subControlFont != null)
244       return subControlFont;
245     else
246       return super.getWindowTitleFont();
247     }
248     public FontUIResource getSubTextFont() {
249       if (subSmallFont != null)
250     return subSmallFont;
251       else
252     return super.getSubTextFont();
253     }
254     public FontUIResource getMonospacedFont() {
255       return subMonospacedFont;
256     }
257 }
258
Popular Tags