1 11 package org.eclipse.ui.internal.intro.impl.swt; 12 13 import java.util.Enumeration ; 14 import java.util.Hashtable ; 15 import java.util.Properties ; 16 17 import org.eclipse.core.runtime.FileLocator; 18 import org.eclipse.core.runtime.Path; 19 import org.eclipse.jface.resource.JFaceResources; 20 import org.eclipse.swt.graphics.Color; 21 import org.eclipse.swt.graphics.Font; 22 import org.eclipse.swt.graphics.Image; 23 import org.eclipse.ui.forms.widgets.FormToolkit; 24 import org.eclipse.ui.internal.intro.impl.model.AbstractBaseIntroElement; 25 import org.eclipse.ui.internal.intro.impl.model.AbstractIntroContainer; 26 import org.eclipse.ui.internal.intro.impl.model.AbstractIntroElement; 27 import org.eclipse.ui.internal.intro.impl.model.AbstractIntroIdElement; 28 import org.eclipse.ui.internal.intro.impl.model.AbstractIntroPage; 29 import org.eclipse.ui.internal.intro.impl.model.IntroGroup; 30 import org.eclipse.ui.internal.intro.impl.model.IntroImage; 31 import org.eclipse.ui.internal.intro.impl.model.IntroLink; 32 import org.eclipse.ui.internal.intro.impl.model.IntroModelRoot; 33 import org.eclipse.ui.internal.intro.impl.model.IntroText; 34 import org.eclipse.ui.internal.intro.impl.model.loader.ModelLoaderUtil; 35 import org.eclipse.ui.internal.intro.impl.util.ImageUtil; 36 import org.eclipse.ui.internal.intro.impl.util.Log; 37 import org.osgi.framework.Bundle; 38 39 public class PageStyleManager extends SharedStyleManager { 40 41 private AbstractIntroPage page; 42 private Hashtable altStyleContexts = new Hashtable (); 43 private IntroModelRoot root; 44 45 46 54 public PageStyleManager(AbstractIntroPage page, Properties sharedProperties) { 55 this.page = page; 56 context = new StyleContext(); 57 context.bundle = page.getBundle(); 58 59 if (page.injectSharedStyle()) 61 properties = new Properties (sharedProperties); 62 else 63 properties = new Properties (); 64 String altStyle = page.getAltStyle(); 65 if (altStyle != null) { 66 load(properties, altStyle, context); 67 } 68 69 Hashtable altStyles = page.getAltStyles(); 72 if (altStyles != null) { 73 Enumeration styles = altStyles.keys(); 74 while (styles.hasMoreElements()) { 75 String style = (String ) styles.nextElement(); 76 Properties inheritedProperties = new Properties (); 77 Bundle bundle = (Bundle) altStyles.get(style); 78 StyleContext sc = new StyleContext(); 79 sc.bundle = bundle; 80 load(inheritedProperties, style, sc); 81 altStyleContexts.put(inheritedProperties, sc); 82 } 83 } 84 85 root = (IntroModelRoot) page.getParentPage().getParent(); 87 } 88 89 90 public String getProperty(String key) { 92 return getProperty(key, true); 93 } 94 95 private String getProperty(String key, boolean useImplicitKey) { 98 Properties aProperties = findPropertyOwner(key); 99 String value = super.doGetProperty(aProperties, key); 100 if (useImplicitKey) { 101 if (value == null && page.getId() != null 102 && key.startsWith(page.getId())) { 103 String relativeKey = key.substring(page.getId().length()); 105 return getProperty(relativeKey); 106 } 107 } 108 return value; 109 } 110 111 112 121 private Properties findPropertyOwner(String key) { 122 if (properties.containsKey(key)) 124 return properties; 125 126 Enumeration inheritedPageProperties = altStyleContexts.keys(); 128 while (inheritedPageProperties.hasMoreElements()) { 129 Properties aProperties = (Properties ) inheritedPageProperties 130 .nextElement(); 131 if (aProperties.containsKey(key)) 132 return aProperties; 133 } 134 return properties; 136 } 137 138 139 140 147 148 protected StyleContext getAssociatedContext(String key) { 149 Properties aProperties = findPropertyOwner(key); 150 StyleContext context = (StyleContext) altStyleContexts.get(aProperties); 151 if (context != null) 152 return context; 153 return super.getAssociatedContext(key); 154 } 155 156 157 163 public int getPageNumberOfColumns() { 164 return getIntProperty(page, ".layout.ncolumns", 0); } 166 167 168 public int getNumberOfColumns(IntroGroup group) { 169 return getIntProperty(group, ".layout.ncolumns", 0); } 171 172 public boolean getEqualWidth(IntroGroup group) { 173 return getBooleanProperty(group, ".layout.equalWidth", false); } 175 176 public int getPageVerticalSpacing() { 177 return getIntProperty(page, ".layout.vspacing", 5); } 179 180 public int getVerticalSpacing(IntroGroup group) { 181 return getIntProperty(group, ".layout.vspacing", 5); } 183 184 public int getPageHorizantalSpacing() { 185 return getIntProperty(page, ".layout.hspacing", 5); } 187 188 public int getHorizantalSpacing(IntroGroup group) { 189 return getIntProperty(group, ".layout.hspacing", 5); } 191 192 public int getColSpan(AbstractBaseIntroElement element) { 193 return getIntProperty(element, ".layout.colspan", 1); } 195 196 public int getRowSpan(AbstractBaseIntroElement element) { 197 return getIntProperty(element, ".layout.rowspan", 1); } 199 200 private int getIntProperty(AbstractBaseIntroElement element, 201 String qualifier, int defaultValue) { 202 StringBuffer buff = ModelLoaderUtil.createPathToElementKey(element, true); 203 if (buff == null) 204 return defaultValue; 205 String key = buff.append(qualifier).toString(); 206 return getIntProperty(key, defaultValue); 207 } 208 209 private boolean getBooleanProperty(AbstractBaseIntroElement element, 210 String qualifier, boolean defaultValue) { 211 StringBuffer buff = ModelLoaderUtil.createPathToElementKey(element, true); 212 if (buff == null) 213 return defaultValue; 214 String key = buff.append(qualifier).toString(); 215 return getBooleanProperty(key, defaultValue); 216 } 217 218 private int getIntProperty(String key, int defaulValue) { 219 int intValue = defaulValue; 220 String value = getProperty(key); 221 if (value == null) 222 return intValue; 223 224 try { 225 intValue = Integer.parseInt(value); 226 } catch (NumberFormatException e) { 227 Log.error("Failed to parse key: " + key + " as an integer.", e); } 229 return intValue; 230 } 231 232 private boolean getBooleanProperty(String key, boolean defaultValue) { 233 boolean booleanValue = defaultValue; 234 String value = getProperty(key); 235 if (value != null) 236 booleanValue = value.equalsIgnoreCase("true"); return booleanValue; 238 } 239 240 241 255 public String getDescription(IntroGroup group) { 256 StringBuffer buff = ModelLoaderUtil.createPathToElementKey(group, true); 257 if (buff == null) 258 return null; 259 String key = buff.append(".description-id").toString(); return doGetDescription(group, key); 261 } 262 263 264 277 public String getPageDescription() { 278 if (page.getId() == null) 279 return null; 280 String key = page.getId() + ".description-id"; return doGetDescription(page, key); 282 } 283 284 private String doGetDescription(AbstractIntroContainer parent, String key) { 285 String path = getProperty(key); 286 String description = null; 287 if (path != null) 288 description = findTextFromPath(parent, path); 289 if (description != null) 290 return description; 291 return findTextFromStyleId(parent, getDescriptionStyleId()); 292 } 293 294 private String getDescriptionStyleId() { 295 String key = "description-style-id"; return getProperty(key); 297 } 298 299 310 public String getPageSubTitle() { 311 String key = page.getId() + ".subtitle-id"; String path = getProperty(key); 313 String description = null; 314 if (path != null) 315 description = findTextFromPath(page, path); 316 if (description != null) 317 return description; 318 return findTextFromStyleId(page, getPageSubTitleStyleId()); 319 } 320 321 private String getPageSubTitleStyleId() { 322 String key = "subtitle-style-id"; return getProperty(key); 324 } 325 326 private String findTextFromPath(AbstractIntroContainer parent, String path) { 327 AbstractIntroElement child = parent.findTarget(root, path); 328 if (child != null && child.isOfType(AbstractIntroElement.TEXT)) { 329 makeFiltered(child); 330 return ((IntroText) child).getText(); 331 } 332 return null; 333 } 334 335 340 private String findTextFromStyleId(AbstractIntroContainer parent, 341 String styleId) { 342 IntroText[] allText = (IntroText[]) parent 343 .getChildrenOfType(AbstractIntroElement.TEXT); 344 for (int i = 0; i < allText.length; i++) { 345 if (allText[i].getStyleId() == null) 346 continue; 348 if (allText[i].getStyleId().equals(styleId)) { 349 makeFiltered(allText[i]); 350 return allText[i].getText(); 351 } 352 } 353 return null; 354 } 355 356 362 private AbstractIntroElement makeFiltered(AbstractIntroElement element) { 363 if (element.isOfType(AbstractIntroElement.BASE_ELEMENT)) 364 ((AbstractBaseIntroElement) element).setFilterState(true); 365 return element; 366 } 367 368 369 370 public boolean getShowLinkDescription() { 371 String key = page.getId() + ".show-link-description"; String value = getProperty(key); 373 if (value == null) { 374 key = ".show-link-description"; value = getProperty(key); 376 } 377 if (value == null) 378 value = "true"; return value.toLowerCase().equals("true"); } 381 382 public boolean showHomePageNavigation() { 383 String key = page.getId() + ".show-home-page-navigation"; String value = getProperty(key); 385 if (value == null) { 386 key = ".show-home-page-navigation"; value = getProperty(key); 388 } 389 if (value == null) 390 value = "true"; return value.equalsIgnoreCase("true"); } 393 394 395 public Color getColor(FormToolkit toolkit, AbstractBaseIntroElement element) { 396 StringBuffer buff = ModelLoaderUtil.createPathToElementKey(element, true); 397 if (buff == null) 398 return null; 399 String key = buff.append(".font.fg").toString(); return getColor(toolkit, key); 401 } 402 403 public Color getBackgrond(FormToolkit toolkit, AbstractBaseIntroElement element) { 404 StringBuffer buff = ModelLoaderUtil.createPathToElementKey(element, true); 405 if (buff == null) 406 return null; 407 String key = buff.append(".bg").toString(); return getColor(toolkit, key); 409 } 410 411 public boolean isBold(IntroText text) { 412 String value = null; 413 425 value = getPropertyValue(text, ".font.bold"); if (value == null) { 427 value = getProperty("bold-style-id"); if (value != null && text.getStyleId() != null) 431 return text.getStyleId().equals(value); 432 } 433 return false; 434 } 435 436 private String getPropertyValue(AbstractIntroIdElement element, String suffix) { 437 StringBuffer buff = ModelLoaderUtil.createPathToElementKey(element, true); 438 if (buff != null) { 439 String key = buff.append(suffix).toString(); 440 String value = getProperty(key); 441 if (value != null) 442 return value; 443 buff = ModelLoaderUtil.createPathToElementKey(element, false); 445 if (buff!= null) { 446 key = buff.append(suffix).toString(); 447 value = getProperty(key); 448 return value; 449 } 450 } 451 return null; 452 } 453 454 public static Font getBannerFont() { 455 return JFaceResources.getBannerFont(); 456 } 457 458 public static Font getHeaderFont() { 459 return JFaceResources.getHeaderFont(); 460 } 461 462 470 public Image getImage(IntroLink link, String qualifier, String defaultKey) { 471 String key = createImageByIdKey(page, link, qualifier); 473 String value = getProperty(key, false); 474 if (value==null) { 475 key = createImageKey(page, link, qualifier); 476 value = getProperty(key, false); 479 } 480 if (value == null && page.getId() != null 481 && key.startsWith(page.getId())) 482 key = key.substring(page.getId().length()); 484 485 String pageKey = createImageKey(page, null, qualifier); 487 488 return getImage(key, pageKey, defaultKey); 489 } 490 491 private String createImageKey(AbstractIntroPage page, IntroLink link, 492 String qualifier) { 493 StringBuffer buff = null; 494 if (link != null) { 495 buff = ModelLoaderUtil.createPathToElementKey(link, true); 496 if (buff == null) 497 return ""; } else { 499 buff = new StringBuffer (); 500 buff.append(page.getId()); 501 } 502 buff.append("."); buff.append(qualifier); 504 return buff.toString(); 505 } 506 507 private String createImageByIdKey(AbstractIntroPage page, IntroLink link, 508 String qualifier) { 509 if (link==null || link.getId()==null) 510 return ""; StringBuffer buff = new StringBuffer (); 512 buff.append(page.getId()); 513 buff.append("."); buff.append(link.getId()); 515 buff.append("."); buff.append(qualifier); 517 return buff.toString(); 518 } 519 520 public Image getImage(IntroImage introImage) { 521 String imageLocation = introImage.getSrcAsIs(); 522 StringBuffer buff = ModelLoaderUtil.createPathToElementKey(introImage, true); 523 String key; 524 if (buff == null) { 525 key = "//" + imageLocation; } else { 527 key = buff!=null?buff.toString():null; 528 } 529 if (ImageUtil.hasImage(key)) 530 return ImageUtil.getImage(key); 531 if (buff != null) { 533 StyleContext acontext = getAssociatedContext(key); 534 if (acontext.inTheme) { 535 ImageUtil.registerImage(key, acontext.path, imageLocation); 536 return ImageUtil.getImage(key); 537 } 538 } 539 Bundle bundle = introImage.getBundle(); 540 if (FileLocator.find(bundle, new Path(imageLocation), null) == null) { 541 return null; 542 } 543 ImageUtil.registerImage(key, bundle, imageLocation); 544 return ImageUtil.getImage(key); 545 546 } 547 548 549 } 550 | Popular Tags |