1 11 package org.eclipse.ui.internal.themes; 12 13 import java.util.Collection ; 14 import java.util.HashMap ; 15 import java.util.HashSet ; 16 import java.util.Map ; 17 import java.util.ResourceBundle ; 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 34 public class ThemeRegistryReader extends RegistryReader { 35 36 39 private final static ResourceBundle RESOURCE_BUNDLE = ResourceBundle 40 .getBundle(ThemeRegistryReader.class.getName()); 41 42 private Collection categoryDefinitions = new HashSet (); 43 44 private Collection colorDefinitions = new HashSet (); 45 46 private Collection fontDefinitions = new HashSet (); 47 48 private ThemeDescriptor themeDescriptor = null; 49 50 private ThemeRegistry themeRegistry; 51 52 private Map dataMap = new HashMap (); 53 54 57 public ThemeRegistryReader() { 58 super(); 59 } 60 61 66 public Collection getCategoryDefinitions() { 67 return categoryDefinitions; 68 } 69 70 75 public Collection getColorDefinitions() { 76 return colorDefinitions; 77 } 78 79 84 public Map getData() { 85 return dataMap; 86 } 87 88 93 public Collection getFontDefinitions() { 94 return fontDefinitions; 95 } 96 97 103 private ThemeElementCategory readCategory(IConfigurationElement element) { 104 String name = element.getAttribute(IWorkbenchRegistryConstants.ATT_LABEL); 105 106 String id = element.getAttribute(IWorkbenchRegistryConstants.ATT_ID); 107 String parentId = element.getAttribute(IWorkbenchRegistryConstants.ATT_PARENT_ID); 108 109 String 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 128 private ColorDefinition readColor(IConfigurationElement element) { 129 String name = element.getAttribute(IWorkbenchRegistryConstants.ATT_LABEL); 130 131 String id = element.getAttribute(IWorkbenchRegistryConstants.ATT_ID); 132 133 String defaultMapping = element.getAttribute(IWorkbenchRegistryConstants.ATT_DEFAULTS_TO); 134 135 String 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")); return null; 146 } 147 148 String categoryId = element.getAttribute(IWorkbenchRegistryConstants.ATT_CATEGORY_ID); 149 150 String description = null; 151 boolean isEditable = true; 152 String 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 176 private String getColorValue(IConfigurationElement element) { 177 if (element == null) { 178 return null; 179 } 180 181 String value = element.getAttribute(IWorkbenchRegistryConstants.ATT_VALUE); 182 if (value == null) { 183 value = checkColorFactory(element); 184 } 185 return value; 186 } 187 188 195 private String getPlatformSpecificColorValue( 196 IConfigurationElement[] elements) { 197 return getColorValue(getBestPlatformMatch(elements)); 198 } 199 200 207 private IConfigurationElement getBestPlatformMatch( 208 IConfigurationElement[] elements) { 209 IConfigurationElement match = null; 210 211 String osname = Platform.getOS(); 212 String wsname = Platform.getWS(); 213 214 for (int i = 0; i < elements.length; i++) { 215 IConfigurationElement element = elements[i]; 216 String elementOs = element.getAttribute(IWorkbenchRegistryConstants.ATT_OS); 217 String elementWs = element.getAttribute(IWorkbenchRegistryConstants.ATT_WS); 218 219 if (osname.equalsIgnoreCase(elementOs)) { 220 if (wsname.equalsIgnoreCase(elementWs)) { 221 return element; 223 } 224 match = element; 225 } else if (wsname.equalsIgnoreCase(elementWs)) { 226 match = element; 227 } 228 } 229 230 return match; 231 } 232 233 236 public boolean readElement(IConfigurationElement element) { 237 String 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")); } 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 name = element.getAttribute(IWorkbenchRegistryConstants.ATT_NAME); 299 String value = element.getAttribute(IWorkbenchRegistryConstants.ATT_VALUE); 300 if (name == null || value == null) { 301 logError(element, RESOURCE_BUNDLE.getString("Data.badData")); } 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 categoryId = element.getAttribute(IWorkbenchRegistryConstants.ATT_CATEGORY_ID); 315 String presentationId = element.getAttribute(IWorkbenchRegistryConstants.ATT_PRESENTATIONID); 316 themeRegistry.addCategoryPresentationBinding(categoryId, 317 presentationId); 318 return true; 319 } 320 321 return false; 322 } 323 324 330 private FontDefinition readFont(IConfigurationElement element) { 331 String name = element.getAttribute(IWorkbenchRegistryConstants.ATT_LABEL); 332 333 String id = element.getAttribute(IWorkbenchRegistryConstants.ATT_ID); 334 335 String defaultMapping = element.getAttribute(IWorkbenchRegistryConstants.ATT_DEFAULTS_TO); 336 337 String 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")); return null; 346 } 347 348 String categoryId = element.getAttribute(IWorkbenchRegistryConstants.ATT_CATEGORY_ID); 349 350 boolean isEditable = true; 351 String isEditableString = element.getAttribute(IWorkbenchRegistryConstants.ATT_IS_EDITABLE); 352 if (isEditableString != null) { 353 isEditable = Boolean.valueOf(isEditableString).booleanValue(); 354 } 355 356 String 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 376 private String getPlatformSpecificFontValue(IConfigurationElement[] elements) { 377 return getFontValue(getBestPlatformMatch(elements)); 378 } 379 380 386 private String getFontValue(IConfigurationElement element) { 387 if (element == null) { 388 return null; 389 } 390 391 return element.getAttribute(IWorkbenchRegistryConstants.ATT_VALUE); 392 } 393 394 400 private String checkColorFactory(IConfigurationElement element) { 401 String 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 e) { 409 WorkbenchPlugin.log(RESOURCE_BUNDLE 410 .getString("Colors.badFactory"), WorkbenchPlugin.getStatus(e)); 412 } 413 } 414 return value; 415 } 416 417 423 protected ThemeDescriptor readTheme(IConfigurationElement element) { 424 ThemeDescriptor desc = null; 425 426 String id = element.getAttribute(ThemeDescriptor.ATT_ID); 427 if (id == null) { 428 return null; 429 } 430 desc = (ThemeDescriptor) themeRegistry.findTheme(id); 432 if (desc == null) { 434 desc = new ThemeDescriptor(id); 435 themeRegistry.add(desc); 436 } 437 desc.extractName(element); 439 440 return desc; 441 } 442 443 449 public void readThemes(IExtensionRegistry in, ThemeRegistry out) { 450 setRegistry(out); 452 readRegistry(in, PlatformUI.PLUGIN_ID, IWorkbenchRegistryConstants.PL_THEMES); 453 454 readRegistry(in, PlatformUI.PLUGIN_ID, 456 IWorkbenchRegistryConstants.PL_FONT_DEFINITIONS); 457 } 458 459 464 public void setRegistry(ThemeRegistry out) { 465 themeRegistry = out; 466 } 467 } 468 | Popular Tags |