001: package net.suberic.util.swing;
002:
003: import net.suberic.util.*;
004: import java.util.*;
005: import javax.swing.*;
006: import javax.swing.plaf.metal.*;
007: import javax.swing.plaf.*;
008:
009: /**
010: * A Theme which allows you to set all of your colors for a Metal
011: * application.
012: */
013: public class ConfigurableMetalTheme extends DefaultMetalTheme implements
014: Item, ValueChangeListener {
015:
016: private String itemId;
017: private String resourceString;
018:
019: private VariableBundle bundle = null;
020:
021: private WeakHashMap themeListenerList = new WeakHashMap();
022:
023: protected ColorUIResource subPrimary1 = null;
024: protected ColorUIResource subPrimary2 = null;
025: protected ColorUIResource subPrimary3 = null;
026:
027: protected ColorUIResource subSecondary1 = null;
028: protected ColorUIResource subSecondary2 = null;
029: protected ColorUIResource subSecondary3 = null;
030:
031: protected ColorUIResource subBlack = null;
032: protected ColorUIResource subWhite = null;
033:
034: protected FontUIResource subControlFont = null;
035: protected FontUIResource subSystemFont = null;
036: protected FontUIResource subUserFont = null;
037: protected FontUIResource subSmallFont = null;
038: protected FontUIResource subMonospacedFont = null;
039:
040: /**
041: * Creates a new ConfigurableMetalTheme from the given property.
042: */
043: public ConfigurableMetalTheme(VariableBundle sourceBundle,
044: String newResourceString, String newItemId) {
045: itemId = newItemId;
046: resourceString = newResourceString;
047:
048: bundle = sourceBundle;
049:
050: loadTheme(getItemProperty(), sourceBundle);
051:
052: sourceBundle.addValueChangeListener(this , getItemProperty()
053: + ".*");
054: }
055:
056: /**
057: * The Item ID. For example, if you were to have a list of users, a
058: * given user's itemID may be "defaultUser".
059: */
060: public String getItemID() {
061: return itemId;
062: }
063:
064: /**
065: * The Item property. For example, if you were to have a list of users, a
066: * given user's itemProperty may be "Users.defaultUser".
067: */
068: public String getItemProperty() {
069: return resourceString + "." + itemId;
070: }
071:
072: /**
073: * Called when a ui value changes.
074: */
075: public void valueChanged(String changedValue) {
076: loadTheme(getItemProperty(), bundle);
077: fireThemeChangedEvent();
078: }
079:
080: /**
081: * Adds a ThemeListener to the ListenerList.
082: */
083: public void addThemeListener(ThemeListener tl) {
084: if (!themeListenerList.containsKey(tl))
085: themeListenerList.put(tl, null);
086: }
087:
088: /**
089: * Removes a ThemeListener from the ListenerList.
090: */
091: public void removeThemeListener(ThemeListener tl) {
092: themeListenerList.remove(tl);
093: }
094:
095: /**
096: * Notifies all registered ThemeListeners that this Theme has changed.
097: */
098: public void fireThemeChangedEvent() {
099: 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 property,
110: VariableBundle sourceBundle) {
111: subPrimary1 = createColorUIResource(property + ".primary1",
112: sourceBundle);
113: subPrimary2 = createColorUIResource(property + ".primary2",
114: sourceBundle);
115: subPrimary3 = createColorUIResource(property + ".primary3",
116: sourceBundle);
117:
118: subSecondary1 = createColorUIResource(property + ".secondary1",
119: sourceBundle);
120: subSecondary2 = createColorUIResource(property + ".secondary2",
121: sourceBundle);
122: subSecondary3 = createColorUIResource(property + ".secondary3",
123: sourceBundle);
124: subWhite = createColorUIResource(property + ".white",
125: sourceBundle);
126: subBlack = createColorUIResource(property + ".black",
127: sourceBundle);
128:
129: subControlFont = createFontUIResource(
130: property + ".controlFont", sourceBundle);
131: subSystemFont = createFontUIResource(property + ".systemFont",
132: sourceBundle);
133: subUserFont = createFontUIResource(property + ".userFont",
134: sourceBundle);
135: subSmallFont = createFontUIResource(property + ".smallFont",
136: sourceBundle);
137: subMonospacedFont = createFontUIResource(property
138: + ".monospacedFont", sourceBundle);
139:
140: }
141:
142: protected ColorUIResource createColorUIResource(String property,
143: VariableBundle sourceBundle) {
144: String enabled = sourceBundle.getProperty(property
145: + "._enabled", "false");
146: if (enabled.equalsIgnoreCase("true")) {
147: String rgbString = sourceBundle.getProperty(property
148: + ".rgb", "");
149: if (rgbString != null && !rgbString.equals("")) {
150: try {
151: int rgbValue = Integer.parseInt(rgbString);
152: ColorUIResource returnValue = new ColorUIResource(
153: rgbValue);
154: return returnValue;
155: } catch (Exception e) {
156: return null;
157: }
158: }
159: }
160:
161: return null;
162: }
163:
164: protected FontUIResource createFontUIResource(String property,
165: VariableBundle sourceBundle) {
166: String enabled = sourceBundle.getProperty(property
167: + "._enabled", "false");
168: if (enabled.equalsIgnoreCase("true")) {
169: String fontString = sourceBundle.getProperty(property, "");
170: if (fontString != null && !fontString.equals("")) {
171: try {
172: FontUIResource returnValue = new FontUIResource(
173: java.awt.Font.decode(fontString));
174: return returnValue;
175: } catch (Exception e) {
176: return null;
177: }
178: }
179: }
180:
181: return null;
182: }
183:
184: public String getName() {
185: return getItemID();
186: }
187:
188: protected ColorUIResource getPrimary1() {
189: if (subPrimary1 != null)
190: return subPrimary1;
191: else
192: return super .getPrimary1();
193: }
194:
195: protected ColorUIResource getPrimary2() {
196: if (subPrimary2 != null)
197: return subPrimary2;
198: else
199: return super .getPrimary2();
200: }
201:
202: protected ColorUIResource getPrimary3() {
203: if (subPrimary3 != null)
204: return subPrimary3;
205: else
206: return super .getPrimary3();
207: }
208:
209: protected ColorUIResource getSecondary1() {
210: if (subSecondary1 != null)
211: return subSecondary1;
212: else
213: return super .getSecondary1();
214: }
215:
216: protected ColorUIResource getSecondary2() {
217: if (subSecondary2 != null)
218: return subSecondary2;
219: else
220: return super .getSecondary2();
221: }
222:
223: protected ColorUIResource getSecondary3() {
224: if (subSecondary3 != null)
225: return subSecondary3;
226: else
227: return super .getSecondary3();
228: }
229:
230: protected ColorUIResource getWhite() {
231: if (subWhite != null)
232: return subWhite;
233: else
234: return super .getWhite();
235: }
236:
237: protected ColorUIResource getBlack() {
238: if (subBlack != null) {
239:
240: return subBlack;
241: } else {
242: return super .getBlack();
243: }
244: }
245:
246: public FontUIResource getControlTextFont() {
247: if (subControlFont != null)
248: return subControlFont;
249: else
250: return super .getControlTextFont();
251: }
252:
253: public FontUIResource getSystemTextFont() {
254: if (subSystemFont != null)
255: return subSystemFont;
256: else
257: return super .getSystemTextFont();
258: }
259:
260: public FontUIResource getUserTextFont() {
261: if (subUserFont != null)
262: return subUserFont;
263: else
264: return super .getUserTextFont();
265: }
266:
267: public FontUIResource getMenuTextFont() {
268: if (subControlFont != null)
269: return subControlFont;
270: else
271: return super .getMenuTextFont();
272: }
273:
274: public FontUIResource getWindowTitleFont() {
275: if (subControlFont != null)
276: return subControlFont;
277: else
278: return super .getWindowTitleFont();
279: }
280:
281: public FontUIResource getSubTextFont() {
282: if (subSmallFont != null)
283: return subSmallFont;
284: else
285: return super .getSubTextFont();
286: }
287:
288: public FontUIResource getMonospacedFont() {
289: return subMonospacedFont;
290: }
291: }
|