KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 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.Collection JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.ResourceBundle JavaDoc;
18
19 import org.eclipse.core.runtime.IConfigurationElement;
20 import org.eclipse.core.runtime.IExtensionRegistry;
21 import org.eclipse.core.runtime.Platform;
22 import org.eclipse.jface.resource.StringConverter;
23 import org.eclipse.ui.PlatformUI;
24 import org.eclipse.ui.internal.WorkbenchPlugin;
25 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
26 import org.eclipse.ui.internal.registry.RegistryReader;
27 import org.eclipse.ui.themes.IColorFactory;
28
29 /**
30  * Registry reader for themes.
31  *
32  * @since 3.0
33  */

34 public class ThemeRegistryReader extends RegistryReader {
35
36     /**
37      * The translation bundle in which to look up internationalized text.
38      */

39     private final static ResourceBundle JavaDoc RESOURCE_BUNDLE = ResourceBundle
40             .getBundle(ThemeRegistryReader.class.getName());
41
42     private Collection JavaDoc categoryDefinitions = new HashSet JavaDoc();
43
44     private Collection JavaDoc colorDefinitions = new HashSet JavaDoc();
45
46     private Collection JavaDoc fontDefinitions = new HashSet JavaDoc();
47
48     private ThemeDescriptor themeDescriptor = null;
49
50     private ThemeRegistry themeRegistry;
51
52     private Map JavaDoc dataMap = new HashMap JavaDoc();
53
54     /**
55      * ThemeRegistryReader constructor comment.
56      */

57     public ThemeRegistryReader() {
58         super();
59     }
60
61     /**
62      * Returns the category definitions.
63      *
64      * @return the category definitions
65      */

66     public Collection JavaDoc getCategoryDefinitions() {
67         return categoryDefinitions;
68     }
69
70     /**
71      * Returns the color definitions.
72      *
73      * @return the color definitions
74      */

75     public Collection JavaDoc getColorDefinitions() {
76         return colorDefinitions;
77     }
78
79     /**
80      * Returns the data map.
81      *
82      * @return the data map
83      */

84     public Map JavaDoc getData() {
85         return dataMap;
86     }
87
88     /**
89      * Returns the font definitions.
90      *
91      * @return the font definitions
92      */

93     public Collection JavaDoc getFontDefinitions() {
94         return fontDefinitions;
95     }
96
97     /**
98      * Read a category.
99      *
100      * @param element the element to read
101      * @return the new category
102      */

103     private ThemeElementCategory readCategory(IConfigurationElement element) {
104         String JavaDoc name = element.getAttribute(IWorkbenchRegistryConstants.ATT_LABEL);
105
106         String JavaDoc id = element.getAttribute(IWorkbenchRegistryConstants.ATT_ID);
107         String JavaDoc parentId = element.getAttribute(IWorkbenchRegistryConstants.ATT_PARENT_ID);
108
109         String JavaDoc description = null;
110
111         IConfigurationElement[] descriptions = element
112                 .getChildren(IWorkbenchRegistryConstants.TAG_DESCRIPTION);
113
114         if (descriptions.length > 0) {
115             description = descriptions[0].getValue();
116         }
117
118         return new ThemeElementCategory(name, id, parentId, description,
119                 element.getNamespace(), element);
120     }
121
122     /**
123      * Read a color.
124      *
125      * @param element the element to read
126      * @return the new color
127      */

128     private ColorDefinition readColor(IConfigurationElement element) {
129         String JavaDoc name = element.getAttribute(IWorkbenchRegistryConstants.ATT_LABEL);
130
131         String JavaDoc id = element.getAttribute(IWorkbenchRegistryConstants.ATT_ID);
132
133         String JavaDoc defaultMapping = element.getAttribute(IWorkbenchRegistryConstants.ATT_DEFAULTS_TO);
134
135         String JavaDoc value = getPlatformSpecificColorValue(element
136                 .getChildren(IWorkbenchRegistryConstants.TAG_COLORVALUE));
137
138         if (value == null) {
139             value = getColorValue(element);
140         }
141
142         if ((value == null && defaultMapping == null)
143                 || (value != null && defaultMapping != null)) {
144             logError(element, RESOURCE_BUNDLE.getString("Colors.badDefault")); //$NON-NLS-1$
145
return null;
146         }
147
148         String JavaDoc categoryId = element.getAttribute(IWorkbenchRegistryConstants.ATT_CATEGORY_ID);
149
150         String JavaDoc description = null;
151         boolean isEditable = true;
152         String JavaDoc isEditableString = element.getAttribute(IWorkbenchRegistryConstants.ATT_IS_EDITABLE);
153         if (isEditableString != null) {
154             isEditable = Boolean.valueOf(isEditableString).booleanValue();
155         }
156
157         IConfigurationElement[] descriptions = element
158                 .getChildren(IWorkbenchRegistryConstants.TAG_DESCRIPTION);
159
160         if (descriptions.length > 0) {
161             description = descriptions[0].getValue();
162         }
163
164         return new ColorDefinition(name, id, defaultMapping, value, categoryId,
165                 isEditable, description, element.getDeclaringExtension()
166                         .getNamespace());
167     }
168
169     /**
170      * Gets the color value, either via the value attribute or from a color
171      * factory.
172      *
173      * @param element the element to check
174      * @return the color string
175      */

176     private String JavaDoc getColorValue(IConfigurationElement element) {
177         if (element == null) {
178             return null;
179         }
180
181         String JavaDoc value = element.getAttribute(IWorkbenchRegistryConstants.ATT_VALUE);
182         if (value == null) {
183             value = checkColorFactory(element);
184         }
185         return value;
186     }
187
188     /**
189      * Check for platform specific color values. This will return the
190      * "best match" for the current platform.
191      *
192      * @param elements the elements to check
193      * @return the platform specific color, if any
194      */

195     private String JavaDoc getPlatformSpecificColorValue(
196             IConfigurationElement[] elements) {
197         return getColorValue(getBestPlatformMatch(elements));
198     }
199
200     /**
201      * Get the element that has os/ws attributes that best match the current
202      * platform.
203      *
204      * @param elements the elements to check
205      * @return the best match, if any
206      */

207     private IConfigurationElement getBestPlatformMatch(
208             IConfigurationElement[] elements) {
209         IConfigurationElement match = null;
210
211         String JavaDoc osname = Platform.getOS();
212         String JavaDoc wsname = Platform.getWS();
213
214         for (int i = 0; i < elements.length; i++) {
215             IConfigurationElement element = elements[i];
216             String JavaDoc elementOs = element.getAttribute(IWorkbenchRegistryConstants.ATT_OS);
217             String JavaDoc elementWs = element.getAttribute(IWorkbenchRegistryConstants.ATT_WS);
218
219             if (osname.equalsIgnoreCase(elementOs)) {
220                 if (wsname.equalsIgnoreCase(elementWs)) {
221                     // best possible match. Return
222
return element;
223                 }
224                 match = element;
225             } else if (wsname.equalsIgnoreCase(elementWs)) {
226                 match = element;
227             }
228         }
229
230         return match;
231     }
232
233     /* (non-Javadoc)
234      * @see org.eclipse.ui.internal.registry.RegistryReader#readElement(org.eclipse.core.runtime.IConfigurationElement)
235      */

236     public boolean readElement(IConfigurationElement element) {
237         String JavaDoc elementName = element.getName();
238         if (themeDescriptor == null && elementName.equals(IWorkbenchRegistryConstants.TAG_COLORDEFINITION)) {
239             ColorDefinition definition = readColor(element);
240             if (definition != null) {
241                 if (!colorDefinitions.contains(definition)) {
242                     colorDefinitions.add(definition);
243                     themeRegistry.add(definition);
244                 }
245             }
246             return true;
247         } else if (themeDescriptor != null
248                 && elementName.equals(IWorkbenchRegistryConstants.TAG_COLOROVERRIDE)) {
249             ColorDefinition definition = readColor(element);
250             if (definition != null) {
251                 themeDescriptor.add(definition);
252             }
253             return true;
254         } else if (themeDescriptor == null
255                 && elementName.equals(IWorkbenchRegistryConstants.TAG_FONTDEFINITION)) {
256             FontDefinition definition = readFont(element);
257             if (definition != null) {
258                 if (!fontDefinitions.contains(definition)) {
259                     fontDefinitions.add(definition);
260                     themeRegistry.add(definition);
261                 }
262             }
263             return true;
264         } else if (themeDescriptor != null
265                 && elementName.equals(IWorkbenchRegistryConstants.TAG_FONTOVERRIDE)) {
266             FontDefinition definition = readFont(element);
267             if (definition != null) {
268                 themeDescriptor.add(definition);
269             }
270             return true;
271         } else if (themeDescriptor == null
272                 && elementName.equals(IWorkbenchRegistryConstants.TAG_CATEGORYDEFINITION)) {
273             ThemeElementCategory definition = readCategory(element);
274             if (definition != null) {
275                 if (!categoryDefinitions.contains(definition)) {
276                     categoryDefinitions.add(definition);
277                     themeRegistry.add(definition);
278                 }
279             }
280             return true;
281         } else if (element.getName().equals(IWorkbenchRegistryConstants.TAG_THEME)) {
282             if (themeDescriptor != null) {
283                 logError(element, RESOURCE_BUNDLE
284                         .getString("Themes.badNesting")); //$NON-NLS-1$
285
} else {
286                 themeDescriptor = readTheme(element);
287                 if (themeDescriptor != null) {
288                     readElementChildren(element);
289                     themeDescriptor = null;
290                 }
291                 return true;
292             }
293         } else if (themeDescriptor != null
294                 && elementName.equals(IWorkbenchRegistryConstants.TAG_DESCRIPTION)) {
295             themeDescriptor.setDescription(element.getValue());
296             return true;
297         } else if (elementName.equals(IWorkbenchRegistryConstants.TAG_DATA)) {
298             String JavaDoc name = element.getAttribute(IWorkbenchRegistryConstants.ATT_NAME);
299             String JavaDoc value = element.getAttribute(IWorkbenchRegistryConstants.ATT_VALUE);
300             if (name == null || value == null) {
301                 logError(element, RESOURCE_BUNDLE.getString("Data.badData")); //$NON-NLS-1$
302
} else {
303                 if (themeDescriptor != null) {
304                     themeDescriptor.setData(name, value);
305                 } else {
306                     themeRegistry.setData(name, value);
307                     if (!dataMap.containsKey(name)) {
308                         dataMap.put(name, value);
309                     }
310                 }
311             }
312             return true;
313         } else if (elementName.equals(IWorkbenchRegistryConstants.TAG_CATEGORYPRESENTATIONBINDING)) {
314             String JavaDoc categoryId = element.getAttribute(IWorkbenchRegistryConstants.ATT_CATEGORY_ID);
315             String JavaDoc presentationId = element.getAttribute(IWorkbenchRegistryConstants.ATT_PRESENTATIONID);
316             themeRegistry.addCategoryPresentationBinding(categoryId,
317                     presentationId);
318             return true;
319         }
320
321         return false;
322     }
323
324     /**
325      * Read a font.
326      *
327      * @param element the element to read
328      * @return the new font
329      */

330     private FontDefinition readFont(IConfigurationElement element) {
331         String JavaDoc name = element.getAttribute(IWorkbenchRegistryConstants.ATT_LABEL);
332
333         String JavaDoc id = element.getAttribute(IWorkbenchRegistryConstants.ATT_ID);
334
335         String JavaDoc defaultMapping = element.getAttribute(IWorkbenchRegistryConstants.ATT_DEFAULTS_TO);
336
337         String JavaDoc value = getPlatformSpecificFontValue(element
338                 .getChildren(IWorkbenchRegistryConstants.TAG_FONTVALUE));
339         if (value == null) {
340             value = element.getAttribute(IWorkbenchRegistryConstants.ATT_VALUE);
341         }
342
343         if (value != null && defaultMapping != null) {
344             logError(element, RESOURCE_BUNDLE.getString("Fonts.badDefault")); //$NON-NLS-1$
345
return null;
346         }
347
348         String JavaDoc categoryId = element.getAttribute(IWorkbenchRegistryConstants.ATT_CATEGORY_ID);
349
350         boolean isEditable = true;
351         String JavaDoc isEditableString = element.getAttribute(IWorkbenchRegistryConstants.ATT_IS_EDITABLE);
352         if (isEditableString != null) {
353             isEditable = Boolean.valueOf(isEditableString).booleanValue();
354         }
355
356         String JavaDoc description = null;
357
358         IConfigurationElement[] descriptions = element
359                 .getChildren(IWorkbenchRegistryConstants.TAG_DESCRIPTION);
360
361         if (descriptions.length > 0) {
362             description = descriptions[0].getValue();
363         }
364
365         return new FontDefinition(name, id, defaultMapping, value, categoryId,
366                 isEditable, description);
367     }
368
369     /**
370      * Check for platform specific font values. This will return the
371      * "best match" for the current platform.
372      *
373      * @param elements the elements to check
374      * @return the platform specific font, if any
375      */

376     private String JavaDoc getPlatformSpecificFontValue(IConfigurationElement[] elements) {
377         return getFontValue(getBestPlatformMatch(elements));
378     }
379
380     /**
381      * Gets the font valu from the value attribute.
382      *
383      * @param element the element to check
384      * @return the font string
385      */

386     private String JavaDoc getFontValue(IConfigurationElement element) {
387         if (element == null) {
388             return null;
389         }
390
391         return element.getAttribute(IWorkbenchRegistryConstants.ATT_VALUE);
392     }
393
394     /**
395      * Attempt to load the color value from the colorFactory attribute.
396      *
397      * @param element the element to load from
398      * @return the value, or null if it could not be obtained
399      */

400     private String JavaDoc checkColorFactory(IConfigurationElement element) {
401         String JavaDoc value = null;
402         if (element.getAttribute(IWorkbenchRegistryConstants.ATT_COLORFACTORY) != null
403                 || element.getChildren(IWorkbenchRegistryConstants.ATT_COLORFACTORY).length > 0) {
404             try {
405                 IColorFactory factory = (IColorFactory) element
406                         .createExecutableExtension(IWorkbenchRegistryConstants.ATT_COLORFACTORY);
407                 value = StringConverter.asString(factory.createColor());
408             } catch (Exception JavaDoc e) {
409                 WorkbenchPlugin.log(RESOURCE_BUNDLE
410                         .getString("Colors.badFactory"), //$NON-NLS-1$
411
WorkbenchPlugin.getStatus(e));
412             }
413         }
414         return value;
415     }
416
417     /**
418      * Read a theme.
419      *
420      * @param element the element to read
421      * @return the new theme
422      */

423     protected ThemeDescriptor readTheme(IConfigurationElement element) {
424         ThemeDescriptor desc = null;
425
426         String JavaDoc id = element.getAttribute(ThemeDescriptor.ATT_ID);
427         if (id == null) {
428             return null;
429         }
430         //see if the theme has already been created in another extension
431
desc = (ThemeDescriptor) themeRegistry.findTheme(id);
432         //if not, create it
433
if (desc == null) {
434             desc = new ThemeDescriptor(id);
435             themeRegistry.add(desc);
436         }
437         //set the name as applicable
438
desc.extractName(element);
439     
440         return desc;
441     }
442
443     /**
444      * Read the theme extensions within a registry.
445      *
446      * @param in the registry to read
447      * @param out the registry to write to
448      */

449     public void readThemes(IExtensionRegistry in, ThemeRegistry out) {
450         // this does not seem to really ever be throwing an the exception
451
setRegistry(out);
452         readRegistry(in, PlatformUI.PLUGIN_ID, IWorkbenchRegistryConstants.PL_THEMES);
453
454         // support for old font definitions
455
readRegistry(in, PlatformUI.PLUGIN_ID,
456                 IWorkbenchRegistryConstants.PL_FONT_DEFINITIONS);
457     }
458
459     /**
460      * Set the output registry.
461      *
462      * @param out the output registry
463      */

464     public void setRegistry(ThemeRegistry out) {
465         themeRegistry = out;
466     }
467 }
468
Popular Tags