KickJava   Java API By Example, From Geeks To Geeks.

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


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.ResourceBundle JavaDoc;
14 import java.util.Set JavaDoc;
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 /**
37  * @since 3.0
38  */

39 public class Theme extends EventManager implements ITheme {
40
41     /**
42      * The translation bundle in which to look up internationalized text.
43      */

44     private final static ResourceBundle JavaDoc 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     /**
62      * @param descriptor
63      */

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     /**
101      * Listener that is responsible for responding to preference changes.
102      *
103      * @return the property change listener
104      */

105     private IPropertyChangeListener getPropertyListener() {
106         if (propertyListener == null) {
107             propertyListener = new IPropertyChangeListener() {
108
109                 /* (non-Javadoc)
110                  * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
111                  */

112                 public void propertyChange(PropertyChangeEvent event) {
113                     String JavaDoc[] split = ThemeElementHelper.splitPropertyName(
114                             Theme.this, event.getProperty());
115                     String JavaDoc key = split[1];
116                     String JavaDoc theme = split[0];
117                     if (key.equals(IWorkbenchPreferenceConstants.CURRENT_THEME_ID)) {
118                         return;
119                     }
120                     try {
121                         String JavaDoc thisTheme = getId();
122                                           
123                         if (Util.equals(thisTheme, theme)) {
124                             if (getFontRegistry().hasValueFor(key)) {
125                                 FontData[] data = PreferenceConverter
126                                         .basicGetFontData((String JavaDoc) 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 JavaDoc) event
135                                         .getNewValue());
136                                 getColorRegistry().put(key, rgb);
137                                 processDefaultsTo(key, rgb);
138                                 return;
139                             }
140                         }
141                     } catch (DataFormatException e) {
142                         //no-op
143
}
144                 }
145
146                 /**
147                  * Process all fonts that default to the given ID.
148                  *
149                  * @param key the font ID
150                  * @param fd the new FontData for defaulted fonts
151                  */

152                 private void processDefaultsTo(String JavaDoc key, FontData[] fd) {
153                     FontDefinition[] defs = WorkbenchPlugin.getDefault()
154                             .getThemeRegistry().getFontsFor(getId());
155                     for (int i = 0; i < defs.length; i++) {
156                         String JavaDoc 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                 /**
171                  * Process all colors that default to the given ID.
172                  *
173                  * @param key the color ID
174                  * @param rgb the new RGB value for defaulted colors
175                  */

176                 private void processDefaultsTo(String JavaDoc key, RGB rgb) {
177                     ColorDefinition[] defs = WorkbenchPlugin.getDefault()
178                             .getThemeRegistry().getColorsFor(getId());
179                     for (int i = 0; i < defs.length; i++) {
180                         String JavaDoc 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     /**
199      * Listener that is responsible for rebroadcasting events fired from the base font/color registry
200      */

201     private IPropertyChangeListener getCascadeListener() {
202         if (themeListener == null) {
203             themeListener = new IPropertyChangeListener() {
204
205                 /* (non-Javadoc)
206                  * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
207                  */

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     /* (non-Javadoc)
248      * @see org.eclipse.ui.internal.themes.ITheme#getId()
249      */

250     public String JavaDoc getId() {
251         return descriptor == null ? IThemeManager.DEFAULT_THEME : descriptor
252                 .getId();
253     }
254
255     /* (non-Javadoc)
256      * @see org.eclipse.ui.IWorkbench#addPropertyChangeListener(org.eclipse.jface.util.IPropertyChangeListener)
257      */

258     public void addPropertyChangeListener(IPropertyChangeListener listener) {
259         addListenerObject(listener);
260     }
261
262     /* (non-Javadoc)
263      * @see org.eclipse.ui.IWorkbench#removePropertyChangeListener(org.eclipse.jface.util.IPropertyChangeListener)
264      */

265     public void removePropertyChangeListener(IPropertyChangeListener listener) {
266         removeListenerObject(listener);
267     }
268
269     private void firePropertyChange(PropertyChangeEvent event) {
270         Object JavaDoc[] listeners = getListeners();
271         for (int i = 0; i < listeners.length; i++) {
272             ((IPropertyChangeListener) listeners[i]).propertyChange(event);
273         }
274     }
275
276     /* (non-Javadoc)
277      * @see org.eclipse.ui.internal.themes.ITheme#getLabel()
278      */

279     public String JavaDoc getLabel() {
280         return descriptor == null ? RESOURCE_BUNDLE
281                 .getString("DefaultTheme.label") : descriptor.getName(); //$NON-NLS-1$
282
}
283
284     /* (non-Javadoc)
285      * @see org.eclipse.ui.themes.ITheme#getString(java.lang.String)
286      */

287     public String JavaDoc getString(String JavaDoc key) {
288         if (dataMap != null) {
289             return (String JavaDoc) dataMap.get(key);
290         }
291         return (String JavaDoc) themeRegistry.getData().get(key);
292     }
293
294     /* (non-Javadoc)
295      * @see org.eclipse.ui.themes.ITheme#keySet()
296      */

297     public Set JavaDoc keySet() {
298         if (dataMap != null) {
299             return dataMap.keySet();
300         }
301
302         return themeRegistry.getData().keySet();
303     }
304
305     /* (non-Javadoc)
306      * @see org.eclipse.ui.themes.ITheme#getInt(java.lang.String)
307      */

308     public int getInt(String JavaDoc key) {
309         String JavaDoc string = getString(key);
310         if (string == null) {
311             return 0;
312         }
313         try {
314             return Integer.parseInt(string);
315         } catch (NumberFormatException JavaDoc e) {
316             return 0;
317         }
318     }
319
320     /* (non-Javadoc)
321      * @see org.eclipse.ui.themes.ITheme#getBoolean(java.lang.String)
322      */

323     public boolean getBoolean(String JavaDoc key) {
324         String JavaDoc string = getString(key);
325         if (string == null) {
326             return false;
327         }
328
329         return Boolean.valueOf(getString(key)).booleanValue();
330     }
331 }
332
Popular Tags