1 11 package org.eclipse.ui.internal.themes; 12 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.Map ; 16 17 import org.eclipse.core.commands.common.EventManager; 18 import org.eclipse.jface.resource.ColorRegistry; 19 import org.eclipse.jface.resource.FontRegistry; 20 import org.eclipse.jface.resource.JFaceResources; 21 import org.eclipse.jface.util.IPropertyChangeListener; 22 import org.eclipse.jface.util.PropertyChangeEvent; 23 import org.eclipse.swt.graphics.FontData; 24 import org.eclipse.swt.graphics.RGB; 25 import org.eclipse.swt.widgets.Display; 26 import org.eclipse.ui.IWorkbenchPreferenceConstants; 27 import org.eclipse.ui.PlatformUI; 28 import org.eclipse.ui.internal.WorkbenchPlugin; 29 import org.eclipse.ui.internal.util.PrefUtil; 30 import org.eclipse.ui.themes.ITheme; 31 import org.eclipse.ui.themes.IThemeManager; 32 33 38 public class WorkbenchThemeManager extends EventManager implements 39 IThemeManager { 40 41 private static final String SYSTEM_DEFAULT_THEME = "org.eclipse.ui.ide.systemDefault"; 43 private static WorkbenchThemeManager instance; 44 45 50 public static synchronized WorkbenchThemeManager getInstance() { 51 if (instance == null) { 52 instance = new WorkbenchThemeManager(); 53 instance.getCurrentTheme(); } 55 return instance; 56 } 57 58 private ITheme currentTheme; 59 60 private IPropertyChangeListener currentThemeListener = new IPropertyChangeListener() { 61 62 public void propertyChange(PropertyChangeEvent event) { 63 firePropertyChange(event); 64 if (event.getSource() instanceof FontRegistry) { 65 JFaceResources.getFontRegistry().put(event.getProperty(), 66 (FontData[]) event.getNewValue()); 67 } else if (event.getSource() instanceof ColorRegistry) { 68 JFaceResources.getColorRegistry().put(event.getProperty(), 69 (RGB) event.getNewValue()); 70 } 71 } 72 }; 73 74 private ColorRegistry defaultThemeColorRegistry; 75 76 private FontRegistry defaultThemeFontRegistry; 77 78 private IThemeRegistry themeRegistry; 79 80 private Map themes = new HashMap (7); 81 82 85 private WorkbenchThemeManager() { 86 defaultThemeColorRegistry = new ColorRegistry(PlatformUI.getWorkbench() 87 .getDisplay()); 88 89 defaultThemeFontRegistry = new FontRegistry(PlatformUI.getWorkbench() 90 .getDisplay()); 91 92 FontRegistry jfaceFonts = JFaceResources.getFontRegistry(); 94 for (Iterator i = jfaceFonts.getKeySet().iterator(); i.hasNext();) { 95 String key = (String ) i.next(); 96 defaultThemeFontRegistry.put(key, jfaceFonts.getFontData(key)); 97 } 98 99 String themeId = IThemeManager.DEFAULT_THEME; 100 if (PlatformUI.getWorkbench().getDisplay() != null) { 103 final boolean[] highContrast = new boolean[] { false }; 106 PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable () { 107 108 113 public void run() { 114 highContrast[0] = Display.getCurrent().getHighContrast(); 115 116 } 117 }); 118 if (highContrast[0]) 119 themeId = SYSTEM_DEFAULT_THEME; 120 } 121 122 PrefUtil.getAPIPreferenceStore().setDefault( 123 IWorkbenchPreferenceConstants.CURRENT_THEME_ID, themeId); 124 } 125 126 131 public void addPropertyChangeListener(IPropertyChangeListener listener) { 132 addListenerObject(listener); 133 } 134 135 138 public void dispose() { 139 for (Iterator i = themes.values().iterator(); i.hasNext();) { 140 ITheme theme = (ITheme) i.next(); 141 theme.removePropertyChangeListener(currentThemeListener); 142 theme.dispose(); 143 } 144 themes.clear(); 145 } 146 147 private boolean doSetCurrentTheme(String id) { 148 ITheme oldTheme = currentTheme; 149 ITheme newTheme = getTheme(id); 150 if (oldTheme != newTheme && newTheme != null) { 151 currentTheme = newTheme; 152 return true; 153 } 154 155 return false; 156 } 157 158 protected void firePropertyChange(PropertyChangeEvent event) { 159 Object [] listeners = getListeners(); 160 161 for (int i = 0; i < listeners.length; i++) { 162 ((IPropertyChangeListener) listeners[i]).propertyChange(event); 163 } 164 } 165 166 protected void firePropertyChange(String changeId, ITheme oldTheme, 167 ITheme newTheme) { 168 169 PropertyChangeEvent event = new PropertyChangeEvent(this, changeId, 170 oldTheme, newTheme); 171 firePropertyChange(event); 172 } 173 174 179 public ITheme getCurrentTheme() { 180 if (currentTheme == null) { 181 String themeId = PrefUtil.getAPIPreferenceStore().getString( 182 IWorkbenchPreferenceConstants.CURRENT_THEME_ID); 183 184 if (themeId == null) setCurrentTheme(IThemeManager.DEFAULT_THEME); 186 187 else 188 setCurrentTheme(themeId); 189 } 190 return currentTheme; 191 } 192 193 198 public ColorRegistry getDefaultThemeColorRegistry() { 199 return defaultThemeColorRegistry; 200 } 201 202 207 public FontRegistry getDefaultThemeFontRegistry() { 208 return defaultThemeFontRegistry; 209 } 210 211 private ITheme getTheme(IThemeDescriptor td) { 212 ITheme theme = (ITheme) themes.get(td); 213 if (theme == null) { 214 theme = new Theme(td); 215 themes.put(td, theme); 216 } 217 return theme; 218 } 219 220 225 public ITheme getTheme(String id) { 226 if (id.equals(IThemeManager.DEFAULT_THEME)) { 227 return getTheme((IThemeDescriptor) null); 228 } 229 230 IThemeDescriptor td = getThemeRegistry().findTheme(id); 231 if (td == null) { 232 return null; 233 } 234 return getTheme(td); 235 } 236 237 240 private IThemeRegistry getThemeRegistry() { 241 if (themeRegistry == null) { 242 themeRegistry = WorkbenchPlugin.getDefault().getThemeRegistry(); 243 } 244 return themeRegistry; 245 } 246 247 252 public void removePropertyChangeListener(IPropertyChangeListener listener) { 253 removeListenerObject(listener); 254 } 255 256 261 public void setCurrentTheme(String id) { 262 ITheme oldTheme = currentTheme; 263 if (WorkbenchThemeManager.getInstance().doSetCurrentTheme(id)) { 264 firePropertyChange(CHANGE_CURRENT_THEME, oldTheme, 265 getCurrentTheme()); 266 if (oldTheme != null) { 267 oldTheme.removePropertyChangeListener(currentThemeListener); 268 } 269 currentTheme.addPropertyChangeListener(currentThemeListener); 270 271 if (!PrefUtil.getAPIPreferenceStore().getString( 273 IWorkbenchPreferenceConstants.CURRENT_THEME_ID).equals(id)) { 274 PrefUtil.getAPIPreferenceStore().setValue( 275 IWorkbenchPreferenceConstants.CURRENT_THEME_ID, id); 276 PrefUtil.saveAPIPrefs(); 277 } 278 279 { 281 ColorRegistry jfaceColors = JFaceResources.getColorRegistry(); 282 ColorRegistry themeColors = currentTheme.getColorRegistry(); 283 for (Iterator i = themeColors.getKeySet().iterator(); i 284 .hasNext();) { 285 String key = (String ) i.next(); 286 jfaceColors.put(key, themeColors.getRGB(key)); 287 } 288 } 289 { 290 FontRegistry jfaceFonts = JFaceResources.getFontRegistry(); 291 FontRegistry themeFonts = currentTheme.getFontRegistry(); 292 for (Iterator i = themeFonts.getKeySet().iterator(); i 293 .hasNext();) { 294 String key = (String ) i.next(); 295 jfaceFonts.put(key, themeFonts.getFontData(key)); 296 } 297 } 298 } 299 } 300 } 301 | Popular Tags |