1 11 package org.eclipse.ui.internal.themes; 12 13 import java.util.Arrays ; 14 import java.util.SortedSet ; 15 import java.util.TreeSet ; 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 31 public final class ThemeElementHelper { 32 33 public static void populateRegistry(ITheme theme, 34 FontDefinition[] definitions, IPreferenceStore store) { 35 FontDefinition[] copyOfDefinitions = null; 38 39 FontDefinition[] defaults = null; 41 if (!theme.getId().equals(IThemeManager.DEFAULT_THEME)) { 42 definitions = addDefaulted(definitions); 43 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 71 private static FontDefinition[] addDefaulted(FontDefinition[] definitions) { 72 IThemeRegistry registry = WorkbenchPlugin.getDefault() 73 .getThemeRegistry(); 74 FontDefinition[] allDefs = registry.getFonts(); 75 76 SortedSet set = addDefaulted(definitions, allDefs); 77 return (FontDefinition[]) set.toArray(new FontDefinition[set.size()]); 78 } 79 80 94 private static void installFont(FontDefinition definition, ITheme theme, 95 IPreferenceStore store, boolean setInRegistry) { 96 FontRegistry registry = theme.getFontRegistry(); 97 98 String id = definition.getId(); 99 String 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 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 136 ColorDefinition[] copyOfDefinitions = null; 137 138 ColorDefinition[] defaults = null; 140 if (!theme.getId().equals(IThemeManager.DEFAULT_THEME)) { 141 definitions = addDefaulted(definitions); 142 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 173 private static ColorDefinition[] getDefaults(ColorDefinition[] definitions) { 174 IThemeRegistry registry = WorkbenchPlugin.getDefault() 175 .getThemeRegistry(); 176 ColorDefinition[] allDefs = registry.getColors(); 177 178 SortedSet set = new TreeSet (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 191 private static FontDefinition[] getDefaults(FontDefinition[] definitions) { 192 IThemeRegistry registry = WorkbenchPlugin.getDefault() 193 .getThemeRegistry(); 194 FontDefinition[] allDefs = registry.getFonts(); 195 196 SortedSet set = new TreeSet (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 206 private static ColorDefinition[] addDefaulted(ColorDefinition[] definitions) { 207 IThemeRegistry registry = WorkbenchPlugin.getDefault() 208 .getThemeRegistry(); 209 ColorDefinition[] allDefs = registry.getColors(); 210 211 SortedSet set = addDefaulted(definitions, allDefs); 212 return (ColorDefinition[]) set.toArray(new ColorDefinition[set.size()]); 213 } 214 215 220 private static SortedSet addDefaulted( 221 IHierarchalThemeElementDefinition[] definitions, 222 IHierarchalThemeElementDefinition[] allDefs) { 223 SortedSet set = new TreeSet (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 255 private static void installColor(ColorDefinition definition, ITheme theme, 256 IPreferenceStore store, boolean setInRegistry) { 257 258 ColorRegistry registry = theme.getColorRegistry(); 259 260 String id = definition.getId(); 261 String 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 292 public static String createPreferenceKey(ITheme theme, String id) { 293 String themeId = theme.getId(); 294 if (themeId.equals(IThemeManager.DEFAULT_THEME)) { 295 return id; 296 } 297 298 return themeId + '.' + id; 299 } 300 301 306 public static String [] splitPropertyName(Theme theme, String 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 id = themeDescriptor.getId(); 312 if (property.startsWith(id + '.')) { return new String [] { property.substring(0, id.length()), 317 property.substring(id.length() + 1) }; 318 } 319 } 320 321 return new String [] { IThemeManager.DEFAULT_THEME, property }; 323 } 324 325 328 private ThemeElementHelper() { 329 } 331 } 332 | Popular Tags |