1 11 package org.eclipse.ui.internal.themes; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Collections ; 16 import java.util.HashMap ; 17 import java.util.HashSet ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Map ; 21 import java.util.Set ; 22 23 import org.eclipse.ui.themes.IThemeManager; 24 25 30 public class ThemeRegistry implements IThemeRegistry { 31 32 private List themes; 33 34 private List colors; 35 36 private List fonts; 37 38 private List categories; 39 40 private Map dataMap; 41 42 45 private Map categoryBindingMap; 46 47 50 public ThemeRegistry() { 51 themes = new ArrayList (); 52 colors = new ArrayList (); 53 fonts = new ArrayList (); 54 categories = new ArrayList (); 55 dataMap = new HashMap (); 56 categoryBindingMap = new HashMap (); 57 } 58 59 62 void add(IThemeDescriptor desc) { 63 if (findTheme(desc.getId()) != null) { 64 return; 65 } 66 themes.add(desc); 67 } 68 69 72 void add(ColorDefinition desc) { 73 if (findColor(desc.getId()) != null) { 74 return; 75 } 76 colors.add(desc); 77 } 78 79 82 public ThemeElementCategory findCategory(String id) { 83 return (ThemeElementCategory) findDescriptor(getCategories(), id); 84 } 85 86 89 public ColorDefinition findColor(String id) { 90 return (ColorDefinition) findDescriptor(getColors(), id); 91 } 92 93 96 public IThemeDescriptor findTheme(String id) { 97 return (IThemeDescriptor) findDescriptor(getThemes(), id); 98 } 99 100 105 private IThemeElementDefinition findDescriptor( 106 IThemeElementDefinition[] descriptors, String 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 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 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 139 public ColorDefinition[] getColorsFor(String 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 153 public FontDefinition[] getFontsFor(String 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 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 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 207 void add(FontDefinition definition) { 208 if (findFont(definition.getId()) != null) { 209 return; 210 } 211 fonts.add(definition); 212 } 213 214 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 228 public FontDefinition findFont(String id) { 229 return (FontDefinition) findDescriptor(getFonts(), id); 230 } 231 232 235 void add(ThemeElementCategory definition) { 236 if (findCategory(definition.getId()) != null) { 237 return; 238 } 239 categories.add(definition); 240 } 241 242 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 257 void setData(String name, String value) { 258 if (dataMap.containsKey(name)) { 259 return; 260 } 261 262 dataMap.put(name, value); 263 } 264 265 268 public Map getData() { 269 return Collections.unmodifiableMap(dataMap); 270 } 271 272 277 public void addData(Map otherData) { 278 for (Iterator i = otherData.keySet().iterator(); i.hasNext();) { 279 Object key = i.next(); 280 if (dataMap.containsKey(key)) { 281 continue; 282 } 283 dataMap.put(key, otherData.get(key)); 284 } 285 } 286 287 294 public void addCategoryPresentationBinding(String categoryId, 295 String presentationId) { 296 Set presentations = (Set ) categoryBindingMap.get(categoryId); 297 if (presentations == null) { 298 presentations = new HashSet (); 299 categoryBindingMap.put(categoryId, presentations); 300 } 301 presentations.add(presentationId); 302 } 303 304 307 public Set getPresentationsBindingsFor(ThemeElementCategory category) { 308 return (Set ) categoryBindingMap.get(category.getId()); 309 } 310 } 311 | Popular Tags |