KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Set JavaDoc;
22
23 import org.eclipse.ui.themes.IThemeManager;
24
25 /**
26  * The central manager for Theme descriptors.
27  *
28  * @since 3.0
29  */

30 public class ThemeRegistry implements IThemeRegistry {
31
32     private List JavaDoc themes;
33
34     private List JavaDoc colors;
35
36     private List JavaDoc fonts;
37
38     private List JavaDoc categories;
39
40     private Map JavaDoc dataMap;
41
42     /**
43      * Map from String (categoryId) -> Set (presentationIds)
44      */

45     private Map JavaDoc categoryBindingMap;
46
47     /**
48      * Create a new ThemeRegistry.
49      */

50     public ThemeRegistry() {
51         themes = new ArrayList JavaDoc();
52         colors = new ArrayList JavaDoc();
53         fonts = new ArrayList JavaDoc();
54         categories = new ArrayList JavaDoc();
55         dataMap = new HashMap JavaDoc();
56         categoryBindingMap = new HashMap JavaDoc();
57     }
58
59     /**
60      * Add a descriptor to the registry.
61      */

62     void add(IThemeDescriptor desc) {
63         if (findTheme(desc.getId()) != null) {
64             return;
65         }
66         themes.add(desc);
67     }
68
69     /**
70      * Add a descriptor to the registry.
71      */

72     void add(ColorDefinition desc) {
73         if (findColor(desc.getId()) != null) {
74             return;
75         }
76         colors.add(desc);
77     }
78
79     /* (non-Javadoc)
80      * @see org.eclipse.ui.internal.themes.IThemeRegistry#findCategory(java.lang.String)
81      */

82     public ThemeElementCategory findCategory(String JavaDoc id) {
83         return (ThemeElementCategory) findDescriptor(getCategories(), id);
84     }
85
86     /* (non-Javadoc)
87      * @see org.eclipse.ui.internal.themes.IThemeRegistry#findColor(java.lang.String)
88      */

89     public ColorDefinition findColor(String JavaDoc id) {
90         return (ColorDefinition) findDescriptor(getColors(), id);
91     }
92
93     /* (non-Javadoc)
94      * @see org.eclipse.ui.internal.registry.IThemeRegistry#find(java.lang.String)
95      */

96     public IThemeDescriptor findTheme(String JavaDoc id) {
97         return (IThemeDescriptor) findDescriptor(getThemes(), id);
98     }
99
100     /**
101      * @param descriptors
102      * @param id
103      * @return
104      */

105     private IThemeElementDefinition findDescriptor(
106             IThemeElementDefinition[] descriptors, String JavaDoc id) {
107         int idx = Arrays.binarySearch(descriptors, id, ID_COMPARATOR);
108         if (idx < 0) {
109             return null;
110         }
111         return descriptors[idx];
112     }
113
114     /* (non-Javadoc)
115      * @see org.eclipse.ui.internal.registry.IThemeRegistry#getLookNFeels()
116      */

117     public IThemeDescriptor[] getThemes() {
118         int nSize = themes.size();
119         IThemeDescriptor[] retArray = new IThemeDescriptor[nSize];
120         themes.toArray(retArray);
121         Arrays.sort(retArray, ID_COMPARATOR);
122         return retArray;
123     }
124
125     /* (non-Javadoc)
126      * @see org.eclipse.ui.internal.registry.IThemeRegistry#getLookNFeels()
127      */

128     public ColorDefinition[] getColors() {
129         int nSize = colors.size();
130         ColorDefinition[] retArray = new ColorDefinition[nSize];
131         colors.toArray(retArray);
132         Arrays.sort(retArray, ID_COMPARATOR);
133         return retArray;
134     }
135
136     /* (non-Javadoc)
137      * @see org.eclipse.ui.internal.themes.IThemeRegistry#getColorsFor(java.lang.String)
138      */

139     public ColorDefinition[] getColorsFor(String JavaDoc themeId) {
140         ColorDefinition[] defs = getColors();
141         if (themeId.equals(IThemeManager.DEFAULT_THEME)) {
142             return defs;
143         }
144
145         IThemeDescriptor desc = findTheme(themeId);
146         ColorDefinition[] overrides = desc.getColors();
147         return (ColorDefinition[]) overlay(defs, overrides);
148     }
149
150     /* (non-Javadoc)
151      * @see org.eclipse.ui.internal.themes.IThemeRegistry#getFontsFor(java.lang.String)
152      */

153     public FontDefinition[] getFontsFor(String JavaDoc themeId) {
154         FontDefinition[] defs = getFonts();
155         if (themeId.equals(IThemeManager.DEFAULT_THEME)) {
156             return defs;
157         }
158
159         IThemeDescriptor desc = findTheme(themeId);
160         FontDefinition[] overrides = desc.getFonts();
161         return (FontDefinition[]) overlay(defs, overrides);
162     }
163
164     /**
165      * Overlay the overrides onto the base definitions.
166      *
167      * @param defs the base definitions
168      * @param overrides the overrides
169      * @return the overlayed elements
170      */

171     private IThemeElementDefinition[] overlay(IThemeElementDefinition[] defs,
172             IThemeElementDefinition[] overrides) {
173         for (int i = 0; i < overrides.length; i++) {
174             int idx = Arrays.binarySearch(defs, overrides[i],
175                     IThemeRegistry.ID_COMPARATOR);
176             if (idx >= 0) {
177                 defs[idx] = overlay(defs[idx], overrides[i]);
178             }
179         }
180         return defs;
181     }
182
183     /**
184      * Overlay the override onto the base definition.
185      *
186      * @param defs the base definition
187      * @param overrides the override
188      * @return the overlayed element
189      */

190     private IThemeElementDefinition overlay(IThemeElementDefinition original,
191             IThemeElementDefinition overlay) {
192         if (original instanceof ColorDefinition) {
193             ColorDefinition originalColor = (ColorDefinition) original;
194             ColorDefinition overlayColor = (ColorDefinition) overlay;
195             return new ColorDefinition(originalColor, overlayColor.getValue());
196         } else if (original instanceof FontDefinition) {
197             FontDefinition originalFont = (FontDefinition) original;
198             FontDefinition overlayFont = (FontDefinition) overlay;
199             return new FontDefinition(originalFont, overlayFont.getValue());
200         }
201         return null;
202     }
203
204     /**
205      * @param definition
206      */

207     void add(FontDefinition definition) {
208         if (findFont(definition.getId()) != null) {
209             return;
210         }
211         fonts.add(definition);
212     }
213
214     /* (non-Javadoc)
215      * @see org.eclipse.ui.internal.themes.IThemeRegistry#getGradients()
216      */

217     public FontDefinition[] getFonts() {
218         int nSize = fonts.size();
219         FontDefinition[] retArray = new FontDefinition[nSize];
220         fonts.toArray(retArray);
221         Arrays.sort(retArray, ID_COMPARATOR);
222         return retArray;
223     }
224
225     /* (non-Javadoc)
226      * @see org.eclipse.ui.internal.themes.IThemeRegistry#findFont(java.lang.String)
227      */

228     public FontDefinition findFont(String JavaDoc id) {
229         return (FontDefinition) findDescriptor(getFonts(), id);
230     }
231
232     /**
233      * @param definition
234      */

235     void add(ThemeElementCategory definition) {
236         if (findCategory(definition.getId()) != null) {
237             return;
238         }
239         categories.add(definition);
240     }
241
242     /* (non-Javadoc)
243      * @see org.eclipse.ui.internal.themes.IThemeRegistry#getCategories()
244      */

245     public ThemeElementCategory[] getCategories() {
246         int nSize = categories.size();
247         ThemeElementCategory[] retArray = new ThemeElementCategory[nSize];
248         categories.toArray(retArray);
249         Arrays.sort(retArray, ID_COMPARATOR);
250         return retArray;
251     }
252
253     /**
254      * @param name
255      * @param value
256      */

257     void setData(String JavaDoc name, String JavaDoc value) {
258         if (dataMap.containsKey(name)) {
259             return;
260         }
261         
262         dataMap.put(name, value);
263     }
264
265     /* (non-Javadoc)
266      * @see org.eclipse.ui.internal.themes.IThemeRegistry#getData()
267      */

268     public Map JavaDoc getData() {
269         return Collections.unmodifiableMap(dataMap);
270     }
271
272     /**
273      * Add the data from another map to this data
274      *
275      * @param otherData the other data to add
276      */

277     public void addData(Map JavaDoc otherData) {
278         for (Iterator JavaDoc i = otherData.keySet().iterator(); i.hasNext();) {
279             Object JavaDoc key = i.next();
280             if (dataMap.containsKey(key)) {
281                 continue;
282             }
283             dataMap.put(key, otherData.get(key));
284         }
285     }
286
287     /**
288      * Add a category presentation binding. The given category will only be
289      * availible if the given presentation is active.
290      *
291      * @param categoryId the category id
292      * @param presentationId the presentation id
293      */

294     public void addCategoryPresentationBinding(String JavaDoc categoryId,
295             String JavaDoc presentationId) {
296         Set JavaDoc presentations = (Set JavaDoc) categoryBindingMap.get(categoryId);
297         if (presentations == null) {
298             presentations = new HashSet JavaDoc();
299             categoryBindingMap.put(categoryId, presentations);
300         }
301         presentations.add(presentationId);
302     }
303
304     /* (non-Javadoc)
305      * @see org.eclipse.ui.internal.themes.IThemeRegistry#getPresentationsBindingsFor(org.eclipse.ui.internal.themes.ThemeElementCategory)
306      */

307     public Set JavaDoc getPresentationsBindingsFor(ThemeElementCategory category) {
308         return (Set JavaDoc) categoryBindingMap.get(category.getId());
309     }
310 }
311
Popular Tags