KickJava   Java API By Example, From Geeks To Geeks.

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


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.Arrays JavaDoc;
14 import java.util.SortedSet JavaDoc;
15 import java.util.TreeSet JavaDoc;
16
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.FontRegistry;
21 import org.eclipse.swt.graphics.FontData;
22 import org.eclipse.swt.graphics.RGB;
23 import org.eclipse.ui.internal.Workbench;
24 import org.eclipse.ui.internal.WorkbenchPlugin;
25 import org.eclipse.ui.themes.ITheme;
26 import org.eclipse.ui.themes.IThemeManager;
27
28 /**
29  * @since 3.0
30  */

31 public final class ThemeElementHelper {
32
33     public static void populateRegistry(ITheme theme,
34             FontDefinition[] definitions, IPreferenceStore store) {
35         // sort the definitions by dependant ordering so that we process
36
// ancestors before children.
37
FontDefinition[] copyOfDefinitions = null;
38
39         // the colors to set a default value for, but not a registry value
40
FontDefinition[] defaults = null;
41         if (!theme.getId().equals(IThemeManager.DEFAULT_THEME)) {
42             definitions = addDefaulted(definitions);
43             //compute the defaults only if we're setting preferences at this time
44
if (store != null) {
45                 defaults = getDefaults(definitions);
46             }
47         }
48
49         copyOfDefinitions = new FontDefinition[definitions.length];
50         System.arraycopy(definitions, 0, copyOfDefinitions, 0,
51                 definitions.length);
52         Arrays.sort(copyOfDefinitions, new IThemeRegistry.HierarchyComparator(
53                 definitions));
54
55         for (int i = 0; i < copyOfDefinitions.length; i++) {
56             FontDefinition definition = copyOfDefinitions[i];
57             installFont(definition, theme, store, true);
58         }
59
60         if (defaults != null) {
61             for (int i = 0; i < defaults.length; i++) {
62                 installFont(defaults[i], theme, store, false);
63             }
64         }
65     }
66
67     /**
68      * @param definitions
69      * @return
70      */

71     private static FontDefinition[] addDefaulted(FontDefinition[] definitions) {
72         IThemeRegistry registry = WorkbenchPlugin.getDefault()
73                 .getThemeRegistry();
74         FontDefinition[] allDefs = registry.getFonts();
75
76         SortedSet JavaDoc set = addDefaulted(definitions, allDefs);
77         return (FontDefinition[]) set.toArray(new FontDefinition[set.size()]);
78     }
79
80     /**
81      * Installs the given font in the preference store and optionally the font
82      * registry.
83      *
84      * @param definition
85      * the font definition
86      * @param registry
87      * the font registry
88      * @param store
89      * the preference store from which to set and obtain font data
90      * @param setInRegistry
91      * whether the color should be put into the registry as well as
92      * having its default preference set
93      */

94     private static void installFont(FontDefinition definition, ITheme theme,
95             IPreferenceStore store, boolean setInRegistry) {
96         FontRegistry registry = theme.getFontRegistry();
97
98         String JavaDoc id = definition.getId();
99         String JavaDoc key = createPreferenceKey(theme, id);
100         FontData[] prefFont = store != null ? PreferenceConverter
101                 .getFontDataArray(store, key) : null;
102         FontData[] defaultFont = null;
103         if (definition.getValue() != null) {
104             defaultFont = definition.getValue();
105         } else if (definition.getDefaultsTo() != null) {
106             defaultFont = registry.filterData(registry
107                     .getFontData(definition.getDefaultsTo()), Workbench
108                     .getInstance().getDisplay());
109         } else {
110             // values pushed in from jface property files. Very ugly.
111
defaultFont = registry.bestDataArray(registry.getFontData(key),
112                     Workbench.getInstance().getDisplay());
113         }
114
115         if (setInRegistry) {
116             if (prefFont == null
117                     || prefFont == PreferenceConverter.FONTDATA_ARRAY_DEFAULT_DEFAULT) {
118                 prefFont = defaultFont;
119             }
120
121             if (prefFont != null) {
122                 registry.put(id, prefFont);
123             }
124         }
125
126         if (defaultFont != null && store != null) {
127             PreferenceConverter.setDefault(store, key, defaultFont);
128         }
129     }
130
131     public static void populateRegistry(ITheme theme,
132             ColorDefinition[] definitions, IPreferenceStore store) {
133         // sort the definitions by dependant ordering so that we process
134
// ancestors before children.
135

136         ColorDefinition[] copyOfDefinitions = null;
137
138         // the colors to set a default value for, but not a registry value
139
ColorDefinition[] defaults = null;
140         if (!theme.getId().equals(IThemeManager.DEFAULT_THEME)) {
141             definitions = addDefaulted(definitions);
142             //compute defaults only if we're setting preferences
143
if (store != null) {
144                 defaults = getDefaults(definitions);
145             }
146         }
147
148         copyOfDefinitions = new ColorDefinition[definitions.length];
149         System.arraycopy(definitions, 0, copyOfDefinitions, 0,
150                 definitions.length);
151         Arrays.sort(copyOfDefinitions, new IThemeRegistry.HierarchyComparator(
152                 definitions));
153
154         for (int i = 0; i < copyOfDefinitions.length; i++) {
155             ColorDefinition definition = copyOfDefinitions[i];
156             installColor(definition, theme, store, true);
157         }
158
159         if (defaults != null) {
160             for (int i = 0; i < defaults.length; i++) {
161                 installColor(defaults[i], theme, store, false);
162             }
163         }
164     }
165
166     /**
167      * Return the definitions that should have their default preference value
168      * set but nothing else.
169      *
170      * @param definitions the definitions that will be fully handled
171      * @return the remaining definitions that should be defaulted
172      */

173     private static ColorDefinition[] getDefaults(ColorDefinition[] definitions) {
174         IThemeRegistry registry = WorkbenchPlugin.getDefault()
175                 .getThemeRegistry();
176         ColorDefinition[] allDefs = registry.getColors();
177
178         SortedSet JavaDoc set = new TreeSet JavaDoc(IThemeRegistry.ID_COMPARATOR);
179         set.addAll(Arrays.asList(allDefs));
180         set.removeAll(Arrays.asList(definitions));
181         return (ColorDefinition[]) set.toArray(new ColorDefinition[set.size()]);
182     }
183
184     /**
185      * Return the definitions that should have their default preference value
186      * set but nothing else.
187      *
188      * @param definitions the definitions that will be fully handled
189      * @return the remaining definitions that should be defaulted
190      */

191     private static FontDefinition[] getDefaults(FontDefinition[] definitions) {
192         IThemeRegistry registry = WorkbenchPlugin.getDefault()
193                 .getThemeRegistry();
194         FontDefinition[] allDefs = registry.getFonts();
195
196         SortedSet JavaDoc set = new TreeSet JavaDoc(IThemeRegistry.ID_COMPARATOR);
197         set.addAll(Arrays.asList(allDefs));
198         set.removeAll(Arrays.asList(definitions));
199         return (FontDefinition[]) set.toArray(new FontDefinition[set.size()]);
200     }
201
202     /**
203      * @param definitions
204      * @return
205      */

206     private static ColorDefinition[] addDefaulted(ColorDefinition[] definitions) {
207         IThemeRegistry registry = WorkbenchPlugin.getDefault()
208                 .getThemeRegistry();
209         ColorDefinition[] allDefs = registry.getColors();
210
211         SortedSet JavaDoc set = addDefaulted(definitions, allDefs);
212         return (ColorDefinition[]) set.toArray(new ColorDefinition[set.size()]);
213     }
214
215     /**
216      * @param definitions
217      * @param allDefs
218      * @return
219      */

220     private static SortedSet JavaDoc addDefaulted(
221             IHierarchalThemeElementDefinition[] definitions,
222             IHierarchalThemeElementDefinition[] allDefs) {
223         SortedSet JavaDoc set = new TreeSet JavaDoc(IThemeRegistry.ID_COMPARATOR);
224         set.addAll(Arrays.asList(definitions));
225         
226         IHierarchalThemeElementDefinition copy [] = new IHierarchalThemeElementDefinition[allDefs.length];
227         System.arraycopy(allDefs, 0, copy, 0, allDefs.length);
228         
229         Arrays.sort(allDefs, new IThemeRegistry.HierarchyComparator(copy));
230         for (int i = 0; i < allDefs.length; i++) {
231             IHierarchalThemeElementDefinition def = allDefs[i];
232             if (def.getDefaultsTo() != null) {
233                 if (set.contains(def.getDefaultsTo())) {
234                     set.add(def);
235                 }
236             }
237         }
238         return set;
239     }
240
241     /**
242      * Installs the given color in the preference store and optionally the color
243      * registry.
244      *
245      * @param definition
246      * the color definition
247      * @param registry
248      * the color registry
249      * @param store
250      * the preference store from which to set and obtain color data
251      * @param setInRegistry
252      * whether the color should be put into the registry as well as
253      * having its default preference set
254      */

255     private static void installColor(ColorDefinition definition, ITheme theme,
256             IPreferenceStore store, boolean setInRegistry) {
257
258         ColorRegistry registry = theme.getColorRegistry();
259
260         String JavaDoc id = definition.getId();
261         String JavaDoc key = createPreferenceKey(theme, id);
262         RGB prefColor = store != null ? PreferenceConverter
263                 .getColor(store, key) : null;
264         RGB defaultColor = null;
265         if (definition.getValue() != null) {
266             defaultColor = definition.getValue();
267         } else {
268             defaultColor = registry.getRGB(definition.getDefaultsTo());
269         }
270
271         if (setInRegistry) {
272             if (prefColor == null
273                     || prefColor == PreferenceConverter.COLOR_DEFAULT_DEFAULT) {
274                 prefColor = defaultColor;
275             }
276
277             if (prefColor != null) {
278                 registry.put(id, prefColor);
279             }
280         }
281
282         if (defaultColor != null && store != null) {
283             PreferenceConverter.setDefault(store, key, defaultColor);
284         }
285     }
286
287     /**
288      * @param theme
289      * @param id
290      * @return
291      */

292     public static String JavaDoc createPreferenceKey(ITheme theme, String JavaDoc id) {
293         String JavaDoc themeId = theme.getId();
294         if (themeId.equals(IThemeManager.DEFAULT_THEME)) {
295             return id;
296         }
297
298         return themeId + '.' + id;
299     }
300
301     /**
302      * @param theme
303      * @param property
304      * @return
305      */

306     public static String JavaDoc[] splitPropertyName(Theme theme, String JavaDoc property) {
307         IThemeDescriptor[] descriptors = WorkbenchPlugin.getDefault()
308                 .getThemeRegistry().getThemes();
309         for (int i = 0; i < descriptors.length; i++) {
310             IThemeDescriptor themeDescriptor = descriptors[i];
311             String JavaDoc id = themeDescriptor.getId();
312             if (property.startsWith(id + '.')) { // the property starts with
313
// a known theme ID -
314
// extract and return it and
315
// the remaining property
316
return new String JavaDoc[] { property.substring(0, id.length()),
317                         property.substring(id.length() + 1) };
318             }
319         }
320
321         // default is simply return the default theme ID and the raw property
322
return new String JavaDoc[] { IThemeManager.DEFAULT_THEME, property };
323     }
324
325     /**
326      * Not intended to be instantiated.
327      */

328     private ThemeElementHelper() {
329         // no-op
330
}
331 }
332
Popular Tags