1 11 package org.eclipse.ui.internal.themes; 12 13 import java.util.ResourceBundle ; 14 import java.util.Set ; 15 16 import org.eclipse.core.commands.common.EventManager; 17 import org.eclipse.jface.preference.IPreferenceStore; 18 import org.eclipse.jface.preference.PreferenceConverter; 19 import org.eclipse.jface.resource.ColorRegistry; 20 import org.eclipse.jface.resource.DataFormatException; 21 import org.eclipse.jface.resource.FontRegistry; 22 import org.eclipse.jface.resource.StringConverter; 23 import org.eclipse.jface.util.IPropertyChangeListener; 24 import org.eclipse.jface.util.PropertyChangeEvent; 25 import org.eclipse.swt.graphics.FontData; 26 import org.eclipse.swt.graphics.RGB; 27 import org.eclipse.ui.IWorkbench; 28 import org.eclipse.ui.IWorkbenchPreferenceConstants; 29 import org.eclipse.ui.PlatformUI; 30 import org.eclipse.ui.internal.WorkbenchPlugin; 31 import org.eclipse.ui.internal.util.PrefUtil; 32 import org.eclipse.ui.internal.util.Util; 33 import org.eclipse.ui.themes.ITheme; 34 import org.eclipse.ui.themes.IThemeManager; 35 36 39 public class Theme extends EventManager implements ITheme { 40 41 44 private final static ResourceBundle RESOURCE_BUNDLE = ResourceBundle 45 .getBundle(Theme.class.getName()); 46 47 private CascadingColorRegistry themeColorRegistry; 48 49 private CascadingFontRegistry themeFontRegistry; 50 51 private IThemeDescriptor descriptor; 52 53 private IPropertyChangeListener themeListener; 54 55 private CascadingMap dataMap; 56 57 private ThemeRegistry themeRegistry; 58 59 private IPropertyChangeListener propertyListener; 60 61 64 public Theme(IThemeDescriptor descriptor) { 65 themeRegistry = ((ThemeRegistry) WorkbenchPlugin.getDefault() 66 .getThemeRegistry()); 67 this.descriptor = descriptor; 68 IWorkbench workbench = PlatformUI.getWorkbench(); 69 if (descriptor != null) { 70 ITheme defaultTheme = workbench.getThemeManager().getTheme( 71 IThemeManager.DEFAULT_THEME); 72 73 ColorDefinition[] colorDefinitions = this.descriptor.getColors(); 74 themeColorRegistry = new CascadingColorRegistry(defaultTheme 75 .getColorRegistry()); 76 if (colorDefinitions.length > 0) { 77 ThemeElementHelper.populateRegistry(this, colorDefinitions, 78 PrefUtil.getInternalPreferenceStore()); 79 } 80 81 FontDefinition[] fontDefinitions = this.descriptor.getFonts(); 82 themeFontRegistry = new CascadingFontRegistry(defaultTheme 83 .getFontRegistry()); 84 if (fontDefinitions.length > 0) { 85 ThemeElementHelper.populateRegistry(this, fontDefinitions, 86 PrefUtil.getInternalPreferenceStore()); 87 } 88 89 dataMap = new CascadingMap(((ThemeRegistry) WorkbenchPlugin 90 .getDefault().getThemeRegistry()).getData(), descriptor 91 .getData()); 92 } 93 94 getColorRegistry().addListener(getCascadeListener()); 95 getFontRegistry().addListener(getCascadeListener()); 96 PrefUtil.getInternalPreferenceStore().addPropertyChangeListener( 97 getPropertyListener()); 98 } 99 100 105 private IPropertyChangeListener getPropertyListener() { 106 if (propertyListener == null) { 107 propertyListener = new IPropertyChangeListener() { 108 109 112 public void propertyChange(PropertyChangeEvent event) { 113 String [] split = ThemeElementHelper.splitPropertyName( 114 Theme.this, event.getProperty()); 115 String key = split[1]; 116 String theme = split[0]; 117 if (key.equals(IWorkbenchPreferenceConstants.CURRENT_THEME_ID)) { 118 return; 119 } 120 try { 121 String thisTheme = getId(); 122 123 if (Util.equals(thisTheme, theme)) { 124 if (getFontRegistry().hasValueFor(key)) { 125 FontData[] data = PreferenceConverter 126 .basicGetFontData((String ) event 127 .getNewValue()); 128 129 getFontRegistry().put(key, data); 130 processDefaultsTo(key, data); 131 return; 132 } 133 else if (getColorRegistry().hasValueFor(key)) { 134 RGB rgb = StringConverter.asRGB((String ) event 135 .getNewValue()); 136 getColorRegistry().put(key, rgb); 137 processDefaultsTo(key, rgb); 138 return; 139 } 140 } 141 } catch (DataFormatException e) { 142 } 144 } 145 146 152 private void processDefaultsTo(String key, FontData[] fd) { 153 FontDefinition[] defs = WorkbenchPlugin.getDefault() 154 .getThemeRegistry().getFontsFor(getId()); 155 for (int i = 0; i < defs.length; i++) { 156 String defaultsTo = defs[i].getDefaultsTo(); 157 if (defaultsTo != null && defaultsTo.equals(key)) { 158 IPreferenceStore store = WorkbenchPlugin 159 .getDefault().getPreferenceStore(); 160 if (store.isDefault(ThemeElementHelper 161 .createPreferenceKey(Theme.this, defs[i] 162 .getId()))) { 163 getFontRegistry().put(defs[i].getId(), fd); 164 processDefaultsTo(defs[i].getId(), fd); 165 } 166 } 167 } 168 } 169 170 176 private void processDefaultsTo(String key, RGB rgb) { 177 ColorDefinition[] defs = WorkbenchPlugin.getDefault() 178 .getThemeRegistry().getColorsFor(getId()); 179 for (int i = 0; i < defs.length; i++) { 180 String defaultsTo = defs[i].getDefaultsTo(); 181 if (defaultsTo != null && defaultsTo.equals(key)) { 182 IPreferenceStore store = WorkbenchPlugin 183 .getDefault().getPreferenceStore(); 184 if (store.isDefault(ThemeElementHelper 185 .createPreferenceKey(Theme.this, defs[i] 186 .getId()))) { 187 getColorRegistry().put(defs[i].getId(), rgb); 188 processDefaultsTo(defs[i].getId(), rgb); 189 } 190 } 191 } 192 } 193 }; 194 } 195 return propertyListener; 196 } 197 198 201 private IPropertyChangeListener getCascadeListener() { 202 if (themeListener == null) { 203 themeListener = new IPropertyChangeListener() { 204 205 208 public void propertyChange(PropertyChangeEvent event) { 209 firePropertyChange(event); 210 } 211 }; 212 } 213 return themeListener; 214 } 215 216 public ColorRegistry getColorRegistry() { 217 if (themeColorRegistry != null) { 218 return themeColorRegistry; 219 } 220 221 return WorkbenchThemeManager.getInstance() 222 .getDefaultThemeColorRegistry(); 223 } 224 225 public FontRegistry getFontRegistry() { 226 if (themeFontRegistry != null) { 227 return themeFontRegistry; 228 } 229 230 return WorkbenchThemeManager.getInstance() 231 .getDefaultThemeFontRegistry(); 232 } 233 234 public void dispose() { 235 if (themeColorRegistry != null) { 236 themeColorRegistry.removeListener(themeListener); 237 themeColorRegistry.dispose(); 238 } 239 if (themeFontRegistry != null) { 240 themeFontRegistry.removeListener(themeListener); 241 themeFontRegistry.dispose(); 242 } 243 PrefUtil.getInternalPreferenceStore() 244 .removePropertyChangeListener(getPropertyListener()); 245 } 246 247 250 public String getId() { 251 return descriptor == null ? IThemeManager.DEFAULT_THEME : descriptor 252 .getId(); 253 } 254 255 258 public void addPropertyChangeListener(IPropertyChangeListener listener) { 259 addListenerObject(listener); 260 } 261 262 265 public void removePropertyChangeListener(IPropertyChangeListener listener) { 266 removeListenerObject(listener); 267 } 268 269 private void firePropertyChange(PropertyChangeEvent event) { 270 Object [] listeners = getListeners(); 271 for (int i = 0; i < listeners.length; i++) { 272 ((IPropertyChangeListener) listeners[i]).propertyChange(event); 273 } 274 } 275 276 279 public String getLabel() { 280 return descriptor == null ? RESOURCE_BUNDLE 281 .getString("DefaultTheme.label") : descriptor.getName(); } 283 284 287 public String getString(String key) { 288 if (dataMap != null) { 289 return (String ) dataMap.get(key); 290 } 291 return (String ) themeRegistry.getData().get(key); 292 } 293 294 297 public Set keySet() { 298 if (dataMap != null) { 299 return dataMap.keySet(); 300 } 301 302 return themeRegistry.getData().keySet(); 303 } 304 305 308 public int getInt(String key) { 309 String string = getString(key); 310 if (string == null) { 311 return 0; 312 } 313 try { 314 return Integer.parseInt(string); 315 } catch (NumberFormatException e) { 316 return 0; 317 } 318 } 319 320 323 public boolean getBoolean(String key) { 324 String string = getString(key); 325 if (string == null) { 326 return false; 327 } 328 329 return Boolean.valueOf(getString(key)).booleanValue(); 330 } 331 } 332 | Popular Tags |