KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > themes > WorkbenchThemeManager


1 /*******************************************************************************
2  * Copyright (c) 2004, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.themes;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
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 /**
34  * Theme manager for the Workbench.
35  *
36  * @since 3.0
37  */

38 public class WorkbenchThemeManager extends EventManager implements
39         IThemeManager {
40
41     private static final String JavaDoc SYSTEM_DEFAULT_THEME = "org.eclipse.ui.ide.systemDefault";//$NON-NLS-1$
42

43     private static WorkbenchThemeManager instance;
44
45     /**
46      * Returns the singelton instance of the WorkbenchThemeManager
47      *
48      * @return singleton instance
49      */

50     public static synchronized WorkbenchThemeManager getInstance() {
51         if (instance == null) {
52             instance = new WorkbenchThemeManager();
53             instance.getCurrentTheme(); // initialize the current theme
54
}
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 JavaDoc themes = new HashMap JavaDoc(7);
81
82     /*
83      * Call dispose when we close
84      */

85     private WorkbenchThemeManager() {
86         defaultThemeColorRegistry = new ColorRegistry(PlatformUI.getWorkbench()
87                 .getDisplay());
88
89         defaultThemeFontRegistry = new FontRegistry(PlatformUI.getWorkbench()
90                 .getDisplay());
91
92         // copy the font values from preferences.
93
FontRegistry jfaceFonts = JFaceResources.getFontRegistry();
94         for (Iterator JavaDoc i = jfaceFonts.getKeySet().iterator(); i.hasNext();) {
95             String JavaDoc key = (String JavaDoc) i.next();
96             defaultThemeFontRegistry.put(key, jfaceFonts.getFontData(key));
97         }
98
99         String JavaDoc themeId = IThemeManager.DEFAULT_THEME;
100         // Check if we are in high contrast mode. If so then set the theme to
101
// the system default
102
if (PlatformUI.getWorkbench().getDisplay() != null) {
103             // Determine the high contrast setting before
104
// any access to preferences
105
final boolean[] highContrast = new boolean[] { false };
106             PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable JavaDoc() {
107
108                 /*
109                  * (non-Javadoc)
110                  *
111                  * @see java.lang.Runnable#run()
112                  */

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     /*
127      * (non-Javadoc)
128      *
129      * @see org.eclipse.ui.themes.IThemeManager#addPropertyChangeListener(org.eclipse.jface.util.IPropertyChangeListener)
130      */

131     public void addPropertyChangeListener(IPropertyChangeListener listener) {
132         addListenerObject(listener);
133     }
134
135     /**
136      * Disposes all ThemeEntries.
137      */

138     public void dispose() {
139         for (Iterator JavaDoc 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 JavaDoc 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 JavaDoc[] 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 JavaDoc changeId, ITheme oldTheme,
167             ITheme newTheme) {
168
169         PropertyChangeEvent event = new PropertyChangeEvent(this, changeId,
170                 oldTheme, newTheme);
171         firePropertyChange(event);
172     }
173
174     /*
175      * (non-Javadoc)
176      *
177      * @see org.eclipse.ui.themes.IThemeManager#getCurrentTheme()
178      */

179     public ITheme getCurrentTheme() {
180         if (currentTheme == null) {
181             String JavaDoc themeId = PrefUtil.getAPIPreferenceStore().getString(
182                     IWorkbenchPreferenceConstants.CURRENT_THEME_ID);
183
184             if (themeId == null) // bad preference
185
setCurrentTheme(IThemeManager.DEFAULT_THEME);
186
187             else
188                 setCurrentTheme(themeId);
189         }
190         return currentTheme;
191     }
192
193     /**
194      * Return the default color registry.
195      *
196      * @return the default color registry
197      */

198     public ColorRegistry getDefaultThemeColorRegistry() {
199         return defaultThemeColorRegistry;
200     }
201
202     /**
203      * Return the default font registry.
204      *
205      * @return the default font registry
206      */

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     /*
221      * (non-Javadoc)
222      *
223      * @see org.eclipse.ui.themes.IThemeManager#getTheme(java.lang.String)
224      */

225     public ITheme getTheme(String JavaDoc 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     /**
238      * Answer the IThemeRegistry for the Workbench
239      */

240     private IThemeRegistry getThemeRegistry() {
241         if (themeRegistry == null) {
242             themeRegistry = WorkbenchPlugin.getDefault().getThemeRegistry();
243         }
244         return themeRegistry;
245     }
246
247     /*
248      * (non-Javadoc)
249      *
250      * @see org.eclipse.ui.themes.IThemeManager#removePropertyChangeListener(org.eclipse.jface.util.IPropertyChangeListener)
251      */

252     public void removePropertyChangeListener(IPropertyChangeListener listener) {
253         removeListenerObject(listener);
254     }
255
256     /*
257      * (non-Javadoc)
258      *
259      * @see org.eclipse.ui.themes.IThemeManager#setCurrentTheme(java.lang.String)
260      */

261     public void setCurrentTheme(String JavaDoc 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             // update the preference if required.
272
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             // update the jface registries
280
{
281                 ColorRegistry jfaceColors = JFaceResources.getColorRegistry();
282                 ColorRegistry themeColors = currentTheme.getColorRegistry();
283                 for (Iterator JavaDoc i = themeColors.getKeySet().iterator(); i
284                         .hasNext();) {
285                     String JavaDoc key = (String JavaDoc) 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 JavaDoc i = themeFonts.getKeySet().iterator(); i
293                         .hasNext();) {
294                     String JavaDoc key = (String JavaDoc) i.next();
295                     jfaceFonts.put(key, themeFonts.getFontData(key));
296                 }
297             }
298         }
299     }
300 }
301
Popular Tags