| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2004, 2008 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; |
| 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.jface.resource.JFaceResources; |
| 22 | import org.eclipse.swt.graphics.FontData; |
| 23 | import org.eclipse.swt.graphics.RGB; |
| 24 | import org.eclipse.swt.widgets.Display; |
| 25 | import org.eclipse.ui.internal.Workbench; |
| 26 | import org.eclipse.ui.internal.WorkbenchPlugin; |
| 27 | import org.eclipse.ui.themes.ITheme; |
| 28 | import org.eclipse.ui.themes.IThemeManager; |
| 29 | |
| 30 | /** |
| 31 | * @since 3.0 |
| 32 | */ |
| 33 | public final class ThemeElementHelper { |
| 34 | |
| 35 | public static void populateRegistry(ITheme theme, |
| 36 | FontDefinition[] definitions, IPreferenceStore store) { |
| 37 | // sort the definitions by dependant ordering so that we process |
| 38 | // ancestors before children. |
| 39 | FontDefinition[] copyOfDefinitions = null; |
| 40 | |
| 41 | // the colors to set a default value for, but not a registry value |
| 42 | FontDefinition[] defaults = null; |
| 43 | if (!theme.getId().equals(IThemeManager.DEFAULT_THEME)) { |
| 44 | definitions = addDefaulted(definitions); |
| 45 | //compute the defaults only if we're setting preferences at this time |
| 46 | if (store != null) { |
| 47 | defaults = getDefaults(definitions); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | copyOfDefinitions = new FontDefinition[definitions.length]; |
| 52 | System.arraycopy(definitions, 0, copyOfDefinitions, 0, |
| 53 | definitions.length); |
| 54 | Arrays.sort(copyOfDefinitions, new IThemeRegistry.HierarchyComparator( |
| 55 | definitions)); |
| 56 | |
| 57 | for (int i = 0; i < copyOfDefinitions.length; i++) { |
| 58 | FontDefinition definition = copyOfDefinitions[i]; |
| 59 | installFont(definition, theme, store, true); |
| 60 | } |
| 61 | |
| 62 | if (defaults != null) { |
| 63 | for (int i = 0; i < defaults.length; i++) { |
| 64 | installFont(defaults[i], theme, store, false); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @param definitions |
| 71 | * @return |
| 72 | */ |
| 73 | private static FontDefinition[] addDefaulted(FontDefinition[] definitions) { |
| 74 | IThemeRegistry registry = WorkbenchPlugin.getDefault() |
| 75 | .getThemeRegistry(); |
| 76 | FontDefinition[] allDefs = registry.getFonts(); |
| 77 | |
| 78 | SortedSet set = addDefaulted(definitions, allDefs); |
| 79 | return (FontDefinition[]) set.toArray(new FontDefinition[set.size()]); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Installs the given font in the preference store and optionally the font |
| 84 | * registry. |
| 85 | * |
| 86 | * @param definition |
| 87 | * the font definition |
| 88 | * @param registry |
| 89 | * the font registry |
| 90 | * @param store |
| 91 | * the preference store from which to set and obtain font data |
| 92 | * @param setInRegistry |
| 93 | * whether the color should be put into the registry as well as |
| 94 | * having its default preference set |
| 95 | */ |
| 96 | private static void installFont(FontDefinition definition, ITheme theme, |
| 97 | IPreferenceStore store, boolean setInRegistry) { |
| 98 | FontRegistry registry = theme.getFontRegistry(); |
| 99 | |
| 100 | String id = definition.getId(); |
| 101 | String key = createPreferenceKey(theme, id); |
| 102 | FontData[] prefFont = store != null ? PreferenceConverter |
| 103 | .getFontDataArray(store, key) : null; |
| 104 | FontData[] defaultFont = null; |
| 105 | if (definition.getValue() != null) { |
| 106 | defaultFont = definition.getValue(); |
| 107 | } else if (definition.getDefaultsTo() != null) { |
| 108 | defaultFont = registry.filterData(registry |
| 109 | .getFontData(definition.getDefaultsTo()), Workbench |
| 110 | .getInstance().getDisplay()); |
| 111 | } else { |
| 112 | // values pushed in from jface property files. Very ugly. |
| 113 | Display display = Workbench.getInstance().getDisplay(); |
| 114 | |
| 115 | //If in high contrast, ignore the defaults in jface and use the default (system) font. |
| 116 | //This is a hack to address bug #205474. See bug #228207 for a future fix. |
| 117 | FontData[] fontData = JFaceResources.getFontRegistry().getFontData( |
| 118 | display.getHighContrast() |
| 119 | ? JFaceResources.DEFAULT_FONT |
| 120 | : id |
| 121 | ); |
| 122 | defaultFont = registry.bestDataArray(fontData, display); |
| 123 | } |
| 124 | |
| 125 | if (setInRegistry) { |
| 126 | if (prefFont == null |
| 127 | || prefFont == PreferenceConverter.FONTDATA_ARRAY_DEFAULT_DEFAULT) { |
| 128 | prefFont = defaultFont; |
| 129 | } |
| 130 | |
| 131 | if (prefFont != null) { |
| 132 | registry.put(id, prefFont); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | if (defaultFont != null && store != null) { |
| 137 | PreferenceConverter.setDefault(store, key, defaultFont); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | public static void populateRegistry(ITheme theme, |
| 142 | ColorDefinition[] definitions, IPreferenceStore store) { |
| 143 | // sort the definitions by dependant ordering so that we process |
| 144 | // ancestors before children. |
| 145 | |
| 146 | ColorDefinition[] copyOfDefinitions = null; |
| 147 | |
| 148 | // the colors to set a default value for, but not a registry value |
| 149 | ColorDefinition[] defaults = null; |
| 150 | if (!theme.getId().equals(IThemeManager.DEFAULT_THEME)) { |
| 151 | definitions = addDefaulted(definitions); |
| 152 | //compute defaults only if we're setting preferences |
| 153 | if (store != null) { |
| 154 | defaults = getDefaults(definitions); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | copyOfDefinitions = new ColorDefinition[definitions.length]; |
| 159 | System.arraycopy(definitions, 0, copyOfDefinitions, 0, |
| 160 | definitions.length); |
| 161 | Arrays.sort(copyOfDefinitions, new IThemeRegistry.HierarchyComparator( |
| 162 | definitions)); |
| 163 | |
| 164 | for (int i = 0; i < copyOfDefinitions.length; i++) { |
| 165 | ColorDefinition definition = copyOfDefinitions[i]; |
| 166 | installColor(definition, theme, store, true); |
| 167 | } |
| 168 | |
| 169 | if (defaults != null) { |
| 170 | for (int i = 0; i < defaults.length; i++) { |
| 171 | installColor(defaults[i], theme, store, false); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Return the definitions that should have their default preference value |
| 178 | * set but nothing else. |
| 179 | * |
| 180 | * @param definitions the definitions that will be fully handled |
| 181 | * @return the remaining definitions that should be defaulted |
| 182 | */ |
| 183 | private static ColorDefinition[] getDefaults(ColorDefinition[] definitions) { |
| 184 | IThemeRegistry registry = WorkbenchPlugin.getDefault() |
| 185 | .getThemeRegistry(); |
| 186 | ColorDefinition[] allDefs = registry.getColors(); |
| 187 | |
| 188 | SortedSet set = new TreeSet(IThemeRegistry.ID_COMPARATOR); |
| 189 | set.addAll(Arrays.asList(allDefs)); |
| 190 | set.removeAll(Arrays.asList(definitions)); |
| 191 | return (ColorDefinition[]) set.toArray(new ColorDefinition[set.size()]); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Return the definitions that should have their default preference value |
| 196 | * set but nothing else. |
| 197 | * |
| 198 | * @param definitions the definitions that will be fully handled |
| 199 | * @return the remaining definitions that should be defaulted |
| 200 | */ |
| 201 | private static FontDefinition[] getDefaults(FontDefinition[] definitions) { |
| 202 | IThemeRegistry registry = WorkbenchPlugin.getDefault() |
| 203 | .getThemeRegistry(); |
| 204 | FontDefinition[] allDefs = registry.getFonts(); |
| 205 | |
| 206 | SortedSet set = new TreeSet(IThemeRegistry.ID_COMPARATOR); |
| 207 | set.addAll(Arrays.asList(allDefs)); |
| 208 | set.removeAll(Arrays.asList(definitions)); |
| 209 | return (FontDefinition[]) set.toArray(new FontDefinition[set.size()]); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * @param definitions |
| 214 | * @return |
| 215 | */ |
| 216 | private static ColorDefinition[] addDefaulted(ColorDefinition[] definitions) { |
| 217 | IThemeRegistry registry = WorkbenchPlugin.getDefault() |
| 218 | .getThemeRegistry(); |
| 219 | ColorDefinition[] allDefs = registry.getColors(); |
| 220 | |
| 221 | SortedSet set = addDefaulted(definitions, allDefs); |
| 222 | return (ColorDefinition[]) set.toArray(new ColorDefinition[set.size()]); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * @param definitions |
| 227 | * @param allDefs |
| 228 | * @return |
| 229 | */ |
| 230 | private static SortedSet addDefaulted( |
| 231 | IHierarchalThemeElementDefinition[] definitions, |
| 232 | IHierarchalThemeElementDefinition[] allDefs) { |
| 233 | SortedSet set = new TreeSet(IThemeRegistry.ID_COMPARATOR); |
| 234 | set.addAll(Arrays.asList(definitions)); |
| 235 | |
| 236 | IHierarchalThemeElementDefinition copy [] = new IHierarchalThemeElementDefinition[allDefs.length]; |
| 237 | System.arraycopy(allDefs, 0, copy, 0, allDefs.length); |
| 238 | |
| 239 | Arrays.sort(allDefs, new IThemeRegistry.HierarchyComparator(copy)); |
| 240 | for (int i = 0; i < allDefs.length; i++) { |
| 241 | IHierarchalThemeElementDefinition def = allDefs[i]; |
| 242 | if (def.getDefaultsTo() != null) { |
| 243 | if (set.contains(def.getDefaultsTo())) { |
| 244 | set.add(def); |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | return set; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Installs the given color in the preference store and optionally the color |
| 253 | * registry. |
| 254 | * |
| 255 | * @param definition |
| 256 | * the color definition |
| 257 | * @param theme |
| 258 | * the theme defining the color |
| 259 | * @param store |
| 260 | * the preference store from which to set and obtain color data |
| 261 | * @param setInRegistry |
| 262 | * whether the color should be put into the registry |
| 263 | */ |
| 264 | |
| 265 | private static void installColor(ColorDefinition definition, ITheme theme, |
| 266 | IPreferenceStore store, boolean setInRegistry) { |
| 267 | |
| 268 | //TODO: store shouldn't be null, should assert instead of checking null all over |
| 269 | |
| 270 | ColorRegistry registry = theme.getColorRegistry(); |
| 271 | |
| 272 | String id = definition.getId(); |
| 273 | String key = createPreferenceKey(theme, id); |
| 274 | RGB prefColor = store != null |
| 275 | ? PreferenceConverter.getColor(store, key) |
| 276 | : null; |
| 277 | RGB defaultColor = (definition.getValue() != null) |
| 278 | ? definition.getValue() |
| 279 | : registry.getRGB(definition.getDefaultsTo()); |
| 280 | |
| 281 | if (defaultColor == null) { |
| 282 | // default is null, likely because we have a bad definition - the |
| 283 | // defaultsTo color doesn't exist. We still need a sensible default, |
| 284 | // however. |
| 285 | defaultColor = PreferenceConverter.COLOR_DEFAULT_DEFAULT; |
| 286 | } |
| 287 | |
| 288 | if (prefColor == null |
| 289 | || prefColor == PreferenceConverter.COLOR_DEFAULT_DEFAULT) { |
| 290 | prefColor = defaultColor; |
| 291 | } |
| 292 | |
| 293 | //if the preference value isn't the default then retain that pref value |
| 294 | RGB colorToUse = ! store.isDefault(key) |
| 295 | ? prefColor |
| 296 | : defaultColor; |
| 297 | |
| 298 | if (setInRegistry) { |
| 299 | registry.put(id, colorToUse); |
| 300 | } |
| 301 | |
| 302 | if (store != null) { |
| 303 | PreferenceConverter.setDefault(store, key, defaultColor); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * @param theme |
| 309 | * @param id |
| 310 | * @return |
| 311 | */ |
| 312 | public static String createPreferenceKey(ITheme theme, String id) { |
| 313 | String themeId = theme.getId(); |
| 314 | if (themeId.equals(IThemeManager.DEFAULT_THEME)) { |
| 315 | return id; |
| 316 | } |
| 317 | |
| 318 | return themeId + '.' + id; |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * @param theme |
| 323 | * @param property |
| 324 | * @return |
| 325 | */ |
| 326 | public static String[] splitPropertyName(Theme theme, String property) { |
| 327 | IThemeDescriptor[] descriptors = WorkbenchPlugin.getDefault() |
| 328 | .getThemeRegistry().getThemes(); |
| 329 | for (int i = 0; i < descriptors.length; i++) { |
| 330 | IThemeDescriptor themeDescriptor = descriptors[i]; |
| 331 | String id = themeDescriptor.getId(); |
| 332 | if (property.startsWith(id + '.')) { // the property starts with |
| 333 | // a known theme ID - |
| 334 | // extract and return it and |
| 335 | // the remaining property |
| 336 | return new String[] { property.substring(0, id.length()), |
| 337 | property.substring(id.length() + 1) }; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | // default is simply return the default theme ID and the raw property |
| 342 | return new String[] { IThemeManager.DEFAULT_THEME, property }; |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Not intended to be instantiated. |
| 347 | */ |
| 348 | private ThemeElementHelper() { |
| 349 | // no-op |
| 350 | } |
| 351 | } |