1 11 12 package org.eclipse.ui.internal.intro.impl.model; 13 14 import java.util.ArrayList ; 15 import java.util.Map ; 16 import java.util.StringTokenizer ; 17 import java.util.Vector ; 18 19 import org.eclipse.core.runtime.IConfigurationElement; 20 import org.eclipse.core.runtime.IRegistryChangeEvent; 21 import org.eclipse.core.runtime.Platform; 22 import org.eclipse.swt.SWTError; 23 import org.eclipse.swt.widgets.Composite; 24 import org.eclipse.ui.IMemento; 25 import org.eclipse.ui.internal.intro.impl.model.loader.ModelLoaderUtil; 26 import org.eclipse.ui.internal.intro.impl.model.util.ModelUtil; 27 import org.eclipse.ui.internal.intro.impl.presentations.BrowserIntroPartImplementation; 28 import org.eclipse.ui.internal.intro.impl.presentations.FormIntroPartImplementation; 29 import org.eclipse.ui.internal.intro.impl.presentations.TextIntroPartImplementation; 30 import org.eclipse.ui.internal.intro.impl.util.Log; 31 import org.eclipse.ui.internal.intro.impl.util.StringUtil; 32 import org.eclipse.ui.intro.IIntroPart; 33 34 53 public class IntroPartPresentation extends AbstractIntroElement { 54 55 protected static final String TAG_PRESENTATION = "presentation"; private static final String TAG_IMPLEMENTATION = "implementation"; 58 private static final String ATT_KIND = "kind"; private static final String ATT_STYLE = "style"; private static final String ATT_OS = "os"; private static final String ATT_WS = "ws"; protected static final String ATT_HOME_PAGE_ID = "home-page-id"; protected static final String ATT_STANDBY_PAGE_ID = "standby-page-id"; 65 public static final String BROWSER_IMPL_KIND = "html"; public static final String FORMS_IMPL_KIND = "swt"; private static final String TEXT_IMPL_KIND = "text"; 71 72 private String [] implementationStyles; 74 private String implementationKind; 75 private String homePageId; 76 private String standbyPageId; 77 78 private IntroHead head; 81 82 private AbstractIntroPartImplementation implementation; 83 84 private IntroLaunchBarElement launchBar; 85 86 private IIntroPart introPart; 89 private IMemento memento; 90 91 94 IntroPartPresentation(IConfigurationElement element) { 95 super(element); 96 homePageId = element.getAttribute(ATT_HOME_PAGE_ID); 97 standbyPageId = element.getAttribute(ATT_STANDBY_PAGE_ID); 98 } 99 100 private void updatePresentationAttributes(IConfigurationElement element) { 101 if (element != null) { 102 String value = element.getAttribute(ATT_STYLE); 105 if (value!=null) { 106 IntroModelRoot root = getModelRoot(); 107 ArrayList list = new ArrayList (); 108 StringTokenizer stok = new StringTokenizer (value, ","); for (;stok.hasMoreTokens();) { 110 String oneStyle = stok.nextToken().trim(); 111 if (root!=null) 112 oneStyle = root.resolveVariables(oneStyle); 113 list.add(oneStyle); 114 } 115 implementationStyles = (String [])list.toArray(new String [list.size()]); 116 } 117 implementationKind = element.getAttribute(ATT_KIND); 118 head = getHead(element); 121 if (implementationStyles!=null) { 123 for (int i=0; i<implementationStyles.length; i++) { 124 implementationStyles[i] = ModelUtil.resolveURL(implementationStyles[i], element); 125 } 126 } 127 } 128 } 129 130 136 public String [] getImplementationStyles() { 137 return implementationStyles; 138 } 139 140 145 public String getImplementationKind() { 146 return implementationKind; 147 } 148 149 public AbstractIntroPartImplementation getIntroPartImplementation() { 150 return implementation; 151 } 152 153 154 161 private IntroHead getHead(IConfigurationElement element) { 162 try { 163 IConfigurationElement[] headElements = element.getChildren(IntroHead.TAG_HEAD); 166 if (headElements.length == 0) 167 return null; 169 IntroHead head = new IntroHead(headElements[0]); 170 head.setParent(this); 171 return head; 172 } catch (Exception e) { 173 Log.error(e.getMessage(), e); 174 return null; 175 } 176 } 177 178 185 186 public IntroLaunchBarElement getLaunchBarElement() { 187 if (launchBar != null) 188 return launchBar; 189 IConfigurationElement[] children = getCfgElement().getChildren("launchBar"); if (children.length > 0) { 191 launchBar = new IntroLaunchBarElement(children[0]); 192 launchBar.setParent(this); 193 if (children.length > 1) 194 Log 195 .warning("Mutiple Intro Launch bars defined when only one is allowed. Only first one was loaded. "); } 197 return launchBar; 198 } 199 200 203 public void init(IIntroPart introPart, IMemento memento) { 204 this.introPart = introPart; 208 this.memento = memento; 209 } 210 211 216 public void createPartControl(Composite parent) { 217 Vector validImplementations = getValidImplementationElements(getCfgElement()); 218 IConfigurationElement implementationElement = null; 219 for (int i = 0; i < validImplementations.size(); i++) { 220 implementationElement = (IConfigurationElement) validImplementations.elementAt(i); 221 updatePresentationAttributes(implementationElement); 223 try { 224 implementation = createIntroPartImplementation(getImplementationKind()); 225 if (implementation == null) 226 continue; 228 229 implementation.init(introPart, memento); 230 implementation.createPartControl(parent); 231 IntroModelRoot model = getModelRoot(); 232 if (model != null && model.getConfigurer() != null) { 233 IntroTheme theme = model.getTheme(); 234 Map properties = theme!=null?theme.getProperties():null; 235 model.getConfigurer().init(introPart.getIntroSite(), properties); 236 } 237 if (Log.logInfo) 238 Log.info("Loading Intro UI implementation from: " + ModelLoaderUtil.getLogString(implementationElement, "kind")); break; 241 } catch (SWTError e) { 242 Log.warning("Failed to create Intro UI implementation from: " + ModelLoaderUtil.getLogString(implementationElement, "kind") + e.getMessage()); implementation = null; 245 implementationElement = null; 246 } catch (Exception e) { 247 Log.error("Failed to create Intro UI implementation from: " + ModelLoaderUtil.getLogString(implementationElement, "kind"), e); implementation = null; 250 implementationElement = null; 251 } 252 } 253 254 if (implementationElement == null) { 255 implementation = new FormIntroPartImplementation(); 257 try { 258 implementation.init(introPart, memento); 259 implementationKind = FORMS_IMPL_KIND; 262 } catch (Exception e) { 263 Log.error(e.getMessage(), e); 265 return; 266 } 267 implementation.createPartControl(parent); 268 Log.warning("Loaded UI Forms implementation as a default UI implementation."); } 270 } 271 272 278 private Vector getValidImplementationElements(IConfigurationElement configElement) { 279 280 Vector validList = new Vector (); 281 282 IConfigurationElement[] implementationElements = configElement.getChildren(TAG_IMPLEMENTATION); 285 287 if (implementationElements.length == 0) 288 return validList; 290 291 String currentOS = Platform.getOS(); 292 String currentWS = Platform.getWS(); 293 294 for (int i = 0; i < implementationElements.length; i++) { 297 String os = implementationElements[i].getAttribute(ATT_OS); 298 if (os == null) 299 continue; 301 302 if (listValueHasValue(os, currentOS)) { 303 String ws = implementationElements[i].getAttribute(ATT_WS); 306 if (ws == null) { 307 validList.add(implementationElements[i]); 310 } else { 311 if (listValueHasValue(ws, currentWS)) 313 validList.add(implementationElements[i]); 314 } 315 } 316 } 317 318 for (int i = 0; i < implementationElements.length; i++) { 321 String os = implementationElements[i].getAttribute(ATT_OS); 322 if (os == null) { 323 String ws = implementationElements[i].getAttribute(ATT_WS); 326 if (ws == null) { 327 validList.add(implementationElements[i]); 330 } else { 331 if (listValueHasValue(ws, currentWS)) 333 validList.add(implementationElements[i]); 334 } 335 336 } 337 } 338 339 return validList; 340 341 } 342 343 348 private boolean listValueHasValue(String stringValue, String value) { 349 String [] attributeValues = StringUtil.split(stringValue, ","); for (int i = 0; i < attributeValues.length; i++) { 351 if (attributeValues[i].equalsIgnoreCase(value)) 352 return true; 353 } 354 return false; 355 } 356 357 360 public String getSharedStyle(String kind) { 361 IConfigurationElement[] implementationElements = getCfgElement().getChildren(TAG_IMPLEMENTATION); 363 365 if (implementationElements.length == 0) 366 return null; 368 369 for (int i = 0; i < implementationElements.length; i++) { 371 String aKind = implementationElements[i].getAttribute(ATT_KIND); 372 if (aKind.equals(kind)) { 373 String style = implementationElements[i].getAttribute(ATT_STYLE); 375 return ModelUtil.resolveURL(style, getCfgElement()); 376 } 377 } 378 return null; 379 } 380 381 382 386 private AbstractIntroPartImplementation createIntroPartImplementation(String implementationType) { 387 if (implementationType == null) 389 return null; 390 if (!implementationType.equals(BROWSER_IMPL_KIND) && !implementationType.equals(FORMS_IMPL_KIND) 391 && !implementationType.equals(TEXT_IMPL_KIND)) 392 return null; 393 394 AbstractIntroPartImplementation implementation = null; 395 try { 396 if (implementationType.equals(BROWSER_IMPL_KIND)) 397 implementation = new BrowserIntroPartImplementation(); 398 else if (implementationType.equals(FORMS_IMPL_KIND)) 399 implementation = new FormIntroPartImplementation(); 400 else 401 implementation = new TextIntroPartImplementation(); 402 } catch (Exception e) { 403 Log.error("Could not instantiate implementation " + implementationType, e); 405 } 406 return implementation; 407 } 408 409 415 public IIntroPart getIntroPart() { 416 return introPart; 417 } 418 419 426 public void saveState(IMemento memento) { 427 if (implementation != null) 428 implementation.saveState(memento); 429 } 430 431 432 public void setFocus() { 433 if (implementation != null) 434 implementation.setFocus(); 435 } 436 437 public void standbyStateChanged(boolean standby, boolean isStandbyPartNeeded) { 438 if (implementation != null) 439 implementation.standbyStateChanged(standby, isStandbyPartNeeded); 440 } 441 442 public void updateHistory(AbstractIntroPage page) { 443 if (implementation != null) 444 implementation.updateHistory(page); 445 } 446 447 448 449 public boolean navigateForward() { 450 if (implementation != null) 451 return implementation.navigateForward(); 452 return false; 453 } 454 455 public boolean navigateBackward() { 456 if (implementation != null) 457 return implementation.navigateBackward(); 458 return false; 459 } 460 461 public boolean navigateHome() { 462 if (implementation != null) 463 return implementation.navigateHome(); 464 return false; 465 } 466 467 468 471 public void dispose() { 472 if (implementation != null) 473 implementation.dispose(); 474 } 475 476 482 public void registryChanged(IRegistryChangeEvent event) { 483 if (implementation != null) 484 implementation.registryChanged(event); 485 } 486 487 490 public String getHomePageId() { 491 return homePageId; 492 } 493 494 497 public String getStandbyPageId() { 498 return standbyPageId; 499 } 500 501 506 public int getType() { 507 return AbstractIntroElement.PRESENTATION; 508 } 509 510 514 public IntroHead getHead() { 515 return head; 516 } 517 518 519 520 521 } 522 | Popular Tags |