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 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 resourceString; 19 20 23 public ThemeManager(String newResourceString, VariableBundle bundle) { 24 resourceString = newResourceString; 25 sourceBundle = bundle; 26 createThemes(); 27 } 28 29 32 private void createThemes() { 33 manager = new ItemManager(resourceString, sourceBundle, this); 34 manager.addItemListChangeListener(this); 35 } 36 37 40 public void applyTheme(MetalTheme theme, java.awt.Component component) throws UnsupportedLookAndFeelException { 41 LookAndFeel laf = UIManager.getLookAndFeel(); 42 if (laf instanceof MetalLookAndFeel) { 43 MetalLookAndFeel oldMlaf = ((MetalLookAndFeel)laf); 44 MetalTheme oldMt = getDefaultTheme(); 45 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 66 public void updateUI(ThemeSupporter ui, java.awt.Component component) throws UnsupportedLookAndFeelException { 67 updateUI(ui, component, false); 68 } 69 70 74 public void updateUI(ThemeSupporter ui, java.awt.Component 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 94 public MetalTheme getDefaultTheme() { 95 String defaultString = sourceBundle.getProperty(resourceString + "._default", ""); 96 if (defaultString != null && ! defaultString.equals("")) { 97 return getTheme(defaultString); 98 } 99 100 return null; 101 } 102 103 107 public MetalTheme getTheme(String 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 } 123 124 127 public void valueChanged(String changedValue) { 128 129 } 130 131 138 public void itemListChanged(ItemListChangeEvent e) { 139 fireItemListChanged(e); 140 } 141 142 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 154 public void addItemListListener(ItemListChangeListener newListener) { 155 listenerList.put(newListener, null); 156 } 157 158 161 public void removeItemListListener(ItemListChangeListener oldListener) { 162 listenerList.remove(oldListener); 163 } 164 165 170 public Item createItem(VariableBundle sourceBundle, String resourceString, String itemId) { 171 if (itemId != null && itemId.equals("Ocean")) { 172 ThemeWrapperItem wrapper = new ThemeWrapperItem(sourceBundle, resourceString, itemId); 173 try { 174 Class oceanThemeClass = Class.forName("javax.swing.plaf.metal.OceanTheme"); 175 MetalTheme oceanTheme = (MetalTheme) oceanThemeClass.newInstance(); 176 wrapper.setWrappedTheme(oceanTheme); 177 return wrapper; 178 } catch (Exception e) { 179 } 181 } 182 183 return new ConfigurableMetalTheme(sourceBundle, resourceString, itemId); 184 } 185 186 } 187 | Popular Tags |