KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.suberic.util.swing;
2
3 import javax.swing.*;
4 import java.util.*;
5 import javax.swing.plaf.metal.*;
6
7 import net.suberic.util.*;
8
9 /**
10  * A class that allows one to apply arbitrary styles to individual
11  * Components.
12  */

13 public class ThemeManager implements ValueChangeListener, ItemCreator, ItemListChangeListener {
14
15   private ItemManager manager;
16   private WeakHashMap listenerList = new WeakHashMap();
17   private VariableBundle sourceBundle;
18   private String JavaDoc resourceString;
19   
20   /**
21    * Creates a ThemeManager.
22    */

23   public ThemeManager(String JavaDoc newResourceString, VariableBundle bundle) {
24     resourceString = newResourceString;
25     sourceBundle = bundle;
26     createThemes();
27   }
28
29   /**
30    * Creates the Theme entries.
31    */

32   private void createThemes() {
33     manager = new ItemManager(resourceString, sourceBundle, this);
34     manager.addItemListChangeListener(this);
35   }
36   
37   /**
38    * Applies the given Theme to the component.
39    */

40   public void applyTheme(MetalTheme theme, java.awt.Component JavaDoc component) throws UnsupportedLookAndFeelException {
41     LookAndFeel laf = UIManager.getLookAndFeel();
42     if (laf instanceof MetalLookAndFeel) {
43       MetalLookAndFeel oldMlaf = ((MetalLookAndFeel)laf);
44       MetalTheme oldMt = getDefaultTheme();
45       // not that that really matters.
46

47       if (theme != null) {
48     oldMlaf.setCurrentTheme(theme);
49     MetalLookAndFeel newMlaf = new MetalLookAndFeel();
50     UIManager.setLookAndFeel(newMlaf);
51     SwingUtilities.updateComponentTreeUI(component);
52     oldMlaf.setCurrentTheme(oldMt);
53     UIManager.setLookAndFeel(oldMlaf);
54       } else {
55     SwingUtilities.updateComponentTreeUI(component);
56       }
57     } else {
58       throw new UnsupportedLookAndFeelException("Expected MetalLookAndFeel, got " + laf.getClass().getName());
59     }
60   }
61
62   /**
63    * updates the given Component with the configuration from the given
64    * ThemeSupporter.
65    */

66   public void updateUI(ThemeSupporter ui, java.awt.Component JavaDoc component) throws UnsupportedLookAndFeelException {
67     updateUI(ui, component, false);
68   }
69
70   /**
71    * updates the given Component with the configuration from the given
72    * ThemeSupporter.
73    */

74   public void updateUI(ThemeSupporter ui, java.awt.Component JavaDoc component, boolean force) throws UnsupportedLookAndFeelException {
75     MetalTheme newTheme = ui.getTheme(this);
76     MetalTheme oldTheme = ui.getCurrentTheme();
77     if (! force) {
78       if (newTheme != oldTheme) {
79     applyTheme(newTheme, component);
80     ui.setCurrentTheme(newTheme);
81       }
82     } else {
83       applyTheme(newTheme, component);
84       if (newTheme != oldTheme) {
85     ui.setCurrentTheme(newTheme);
86       }
87     }
88   }
89
90
91   /**
92    * Gets the default configuration for the system.
93    */

94   public MetalTheme getDefaultTheme() {
95     String JavaDoc defaultString = sourceBundle.getProperty(resourceString + "._default", "");
96     if (defaultString != null && ! defaultString.equals("")) {
97       return getTheme(defaultString);
98     }
99
100     return null;
101   }
102
103   /**
104    * Gets the named configuration, or null if no such configuration
105    * exists.
106    */

107   public MetalTheme getTheme(String JavaDoc configID) {
108     if (configID == null)
109       return null;
110
111     Item returnValue = manager.getItem(configID);
112     if (returnValue == null)
113       return null;
114     else if (returnValue instanceof MetalTheme)
115       return (MetalTheme) returnValue;
116     else if (returnValue instanceof ThemeWrapperItem)
117       return ((ThemeWrapperItem) returnValue).getWrappedTheme();
118     else
119       return null;
120
121     //return (MetalTheme) manager.getItem(configID);
122
}
123
124   /**
125    * Called when a ui value changes.
126    */

127   public void valueChanged(String JavaDoc changedValue) {
128
129   }
130
131   /**
132    * As defined in net.suberic.util.ItemListChangeListener.
133    *
134    * This listens for ItemListChangeEvents, which result from changes to the
135    * resourceString property. The result is just that the event is passed
136    * to listeners to this object.
137    */

138   public void itemListChanged(ItemListChangeEvent e) {
139     fireItemListChanged(e);
140   }
141
142   /**
143    * This notifies all listeners that the Theme list has changed.
144    */

145   public void fireItemListChanged(ItemListChangeEvent e) {
146     Iterator iter = listenerList.keySet().iterator();
147     while (iter.hasNext())
148       ((ItemListChangeListener)iter.next()).itemListChanged(e);
149   }
150
151   /**
152    * This adds a listener.
153    */

154   public void addItemListListener(ItemListChangeListener newListener) {
155     listenerList.put(newListener, null);
156   }
157   
158   /**
159    * This removes a listener.
160    */

161   public void removeItemListListener(ItemListChangeListener oldListener) {
162     listenerList.remove(oldListener);
163   }
164
165   /**
166    * Creates an item from the given sourceBundle, resourceString, and itemId.
167    *
168    * Creates a new Theme object.
169    */

170   public Item createItem(VariableBundle sourceBundle, String JavaDoc resourceString, String JavaDoc itemId) {
171     if (itemId != null && itemId.equals("Ocean")) {
172       ThemeWrapperItem wrapper = new ThemeWrapperItem(sourceBundle, resourceString, itemId);
173       try {
174     Class JavaDoc oceanThemeClass = Class.forName("javax.swing.plaf.metal.OceanTheme");
175     MetalTheme oceanTheme = (MetalTheme) oceanThemeClass.newInstance();
176     wrapper.setWrappedTheme(oceanTheme);
177     return wrapper;
178       } catch (Exception JavaDoc e) {
179     // probably not in jdk 1.5. ignore.
180
}
181     }
182     
183     return new ConfigurableMetalTheme(sourceBundle, resourceString, itemId);
184   }
185
186 }
187
Popular Tags