1 11 12 package org.eclipse.pde.internal.ui.wizards.product; 13 14 import org.eclipse.core.runtime.CoreException; 15 import org.eclipse.core.runtime.IProgressMonitor; 16 import org.eclipse.jface.action.Action; 17 import org.eclipse.osgi.util.NLS; 18 import org.eclipse.pde.core.plugin.IPluginAttribute; 19 import org.eclipse.pde.core.plugin.IPluginElement; 20 import org.eclipse.pde.core.plugin.IPluginExtension; 21 import org.eclipse.pde.core.plugin.IPluginExtensionPoint; 22 import org.eclipse.pde.core.plugin.IPluginModelBase; 23 import org.eclipse.pde.core.plugin.IPluginObject; 24 import org.eclipse.pde.internal.core.util.PDETextHelper; 25 import org.eclipse.pde.internal.ui.PDEUIMessages; 26 27 31 public class UpdateSplashHandlerAction extends Action implements ISplashHandlerConstants { 32 33 private IPluginModelBase fModel; 34 35 private IProgressMonitor fMonitor; 36 37 private CoreException fException; 38 39 private String fFieldID; 40 41 private String fFieldSplashID; 42 43 private String fFieldProductID; 44 45 private String fFieldClass; 46 47 private String fFieldTemplate; 48 49 private String fFieldPluginID; 50 51 54 public UpdateSplashHandlerAction() { 55 reset(); 56 } 57 58 61 public void setFieldID(String fieldID) { 62 fFieldID = fieldID; 63 } 64 65 68 public void setFieldSplashID(String fieldSplashID) { 69 fFieldSplashID = fieldSplashID; 70 } 71 72 75 public void setFieldProductID(String fieldProductID) { 76 fFieldProductID = fieldProductID; 77 } 78 79 82 public void setFieldClass(String fieldClass) { 83 fFieldClass = fieldClass; 84 } 85 86 89 public void setFieldTemplate(String fieldTemplate) { 90 fFieldTemplate = fieldTemplate; 91 } 92 93 96 public void setFieldPluginID(String fieldPluginID) { 97 fFieldPluginID = fieldPluginID; 98 } 99 100 103 public void reset() { 104 fModel = null; 105 fMonitor = null; 106 fException = null; 107 108 fFieldID = null; 109 fFieldClass = null; 110 fFieldSplashID = null; 111 fFieldProductID = null; 112 fFieldTemplate = null; 113 fFieldPluginID = null; 114 } 115 116 117 120 public void setModel(IPluginModelBase model) { 121 fModel = model; 122 } 123 124 127 public void setMonitor(IProgressMonitor monitor) { 128 fMonitor = monitor; 129 } 130 131 134 public void run() { 135 try { 136 updateModel(); 137 } catch (CoreException e) { 138 fException = e; 139 } 140 } 141 142 145 public void hasException() throws CoreException { 146 if (fException != null) { 148 throw fException; 149 } 150 } 151 152 155 private void updateModel() throws CoreException { 156 IPluginExtension extension = 158 findFirstExtension(F_SPLASH_HANDLERS_EXTENSION); 159 if (extension == null) { 161 addExtensionSplashHandlers(); 163 } else { 164 modifyExtensionSplashHandlers(extension); 166 } 167 if (isExtensibleTemplateSelected(fFieldTemplate)) { 169 IPluginExtensionPoint extensionPoint = 174 findFirstExtensionPoint(F_SPLASH_EXTENSION_POINT); 175 if (extensionPoint == null) { 178 addExtensionPointSplashExtension(); 180 } 181 String fullExtensionPointID = fFieldPluginID + 183 '.' + F_SPLASH_EXTENSION_POINT; 184 IPluginExtension extensionSplash = 185 findFirstExtension(fullExtensionPointID); 186 if (extensionSplash == null) { 189 addExtensionSplash(); 191 } 192 } 193 } 194 195 198 private void addExtensionSplash() throws CoreException { 199 String fullExtensionPointID = fFieldPluginID + 201 '.' + F_SPLASH_EXTENSION_POINT; 202 fMonitor.beginTask(NLS.bind(PDEUIMessages.UpdateSplashHandlerInModelAction_msgAddingExtension, 203 fullExtensionPointID), 1); 204 IPluginExtension extension = createExtensionSplash(); 206 fModel.getPluginBase().add(extension); 208 fMonitor.done(); 210 } 211 212 215 private void addExtensionPointSplashExtension() throws CoreException { 216 fMonitor.beginTask(NLS.bind(PDEUIMessages.UpdateSplashHandlerInModelAction_msgAddingExtensionPoint, 218 F_SPLASH_EXTENSION_POINT), 1); 219 IPluginExtensionPoint extensionPoint = createExtensionPointSplash(); 221 fModel.getPluginBase().add(extensionPoint); 223 fMonitor.done(); 225 } 226 227 231 private IPluginExtensionPoint createExtensionPointSplash() 232 throws CoreException { 233 IPluginExtensionPoint extensionPoint = 235 fModel.getFactory().createExtensionPoint(); 236 extensionPoint.setId(F_SPLASH_EXTENSION_POINT); 238 extensionPoint.setName(PDEUIMessages.UpdateSplashHandlerInModelAction_splashExtensionPointName); 240 extensionPoint.setSchema("schema/splashExtension.exsd"); 243 return extensionPoint; 244 } 245 246 247 251 private IPluginExtension findFirstExtension( 252 String extensionPointID) { 253 IPluginExtension[] extensions = fModel.getPluginBase().getExtensions(); 255 for (int i = 0; i < extensions.length; i++) { 257 String point = extensions[i].getPoint(); 258 if (extensionPointID.equals(point)) { 259 return extensions[i]; 260 } 261 } 262 return null; 263 } 264 265 266 270 private IPluginExtensionPoint findFirstExtensionPoint( 271 String extensionPointID) { 272 IPluginExtensionPoint[] extensionPoints = 274 fModel.getPluginBase().getExtensionPoints(); 275 for (int i = 0; i < extensionPoints.length; i++) { 278 String point = extensionPoints[i].getId(); 280 if (extensionPointID.equals(point)) { 281 return extensionPoints[i]; 282 } 283 } 284 return null; 285 } 286 287 290 private void addExtensionSplashHandlers() throws CoreException { 291 fMonitor.beginTask(NLS.bind(PDEUIMessages.UpdateSplashHandlerInModelAction_msgAddingExtension, 293 F_SPLASH_HANDLERS_EXTENSION), 1); 294 IPluginExtension extension = createExtensionSplashHandlers(); 296 fModel.getPluginBase().add(extension); 297 fMonitor.done(); 299 } 300 301 305 private IPluginExtension createExtensionSplashHandlers() 306 throws CoreException { 307 IPluginExtension extension = fModel.getFactory().createExtension(); 309 extension.setPoint(F_SPLASH_HANDLERS_EXTENSION); 311 createExtensionChildrenSplashHandlers(extension); 315 316 return extension; 317 } 318 319 323 private void createExtensionChildrenSplashHandlers( 324 IPluginExtension extension) throws CoreException { 325 addElementSplashHandler(extension); 327 addElementProductBinding(extension); 329 } 330 331 335 private void addElementSplashHandler(IPluginExtension extension) 336 throws CoreException { 337 IPluginElement splashHandlerElement = 339 createElementSplashHandler(extension); 340 if (splashHandlerElement != null) { 342 extension.add(0, splashHandlerElement); 346 } 347 } 348 349 353 private void addElementProductBinding(IPluginExtension extension) 354 throws CoreException { 355 IPluginElement productBindingElement = 357 createElementProductBinding(extension); 358 if (productBindingElement != null) { 360 extension.add(1, productBindingElement); 364 } 365 } 366 367 372 private IPluginElement createElementSplashHandler( 373 IPluginExtension extension) throws CoreException{ 374 IPluginElement element = 376 extension.getModel().getFactory().createElement(extension); 377 element.setName(F_ELEMENT_SPLASH_HANDLER); 379 element.setAttribute(F_ATTRIBUTE_ID, fFieldID); 381 element.setAttribute(F_ATTRIBUTE_CLASS, fFieldClass); 383 384 return element; 385 } 386 387 392 private IPluginElement createElementProductBinding( 393 IPluginExtension extension) throws CoreException { 394 IPluginElement element = 396 extension.getModel().getFactory().createElement(extension); 397 element.setName(F_ELEMENT_PRODUCT_BINDING); 399 element.setAttribute(F_ATTRIBUTE_PRODUCT_ID, fFieldProductID); 401 element.setAttribute(F_ATTRIBUTE_SPLASH_ID, fFieldSplashID); 403 404 return element; 405 } 406 407 411 private void modifyExtensionSplashHandlers(IPluginExtension extension) throws CoreException { 412 fMonitor.beginTask(NLS.bind(PDEUIMessages.UpdateSplashHandlerInModelAction_msgModifyingExtension, 414 F_SPLASH_HANDLERS_EXTENSION), 1); 415 modifyExtensionChildrenSplashHandlers(extension); 417 fMonitor.done(); 419 } 420 421 425 private void modifyExtensionChildrenSplashHandlers( 426 IPluginExtension extension) throws CoreException { 427 IPluginElement splashHandlerElement = findSplashHandlerElement(extension); 429 if (splashHandlerElement == null) { 431 addElementSplashHandler(extension); 433 } else { 434 syncSplashHandlerElement(splashHandlerElement); 436 } 437 IPluginElement productBindingElement = findProductBindingElement(extension); 439 removeMatchingProductBindingElements(extension); 447 if (productBindingElement == null) { 449 addElementProductBinding(extension); 451 } else { 452 syncProductBindingElement(productBindingElement); 454 } 455 } 456 457 460 private void removeMatchingProductBindingElements(IPluginExtension extension) 461 throws CoreException { 462 if (extension.getChildCount() == 0) { 464 return; 466 } 467 IPluginObject[] pluginObjects = extension.getChildren(); 468 for (int j = 0; j < pluginObjects.length; j++) { 470 if (pluginObjects[j] instanceof IPluginElement) { 471 IPluginElement element = (IPluginElement)pluginObjects[j]; 472 if (element.getName().equals(F_ELEMENT_PRODUCT_BINDING)) { 474 IPluginAttribute splashIDAttribute = 476 element.getAttribute(F_ATTRIBUTE_SPLASH_ID); 477 IPluginAttribute productIDAttribute = 479 element.getAttribute(F_ATTRIBUTE_PRODUCT_ID); 480 if ((productIDAttribute == null) || 485 (PDETextHelper.isDefined(productIDAttribute.getValue()) == false) || 486 (splashIDAttribute == null) || 487 (PDETextHelper.isDefined(splashIDAttribute.getValue()) == false)) { 488 extension.remove(element); 490 } else if (productIDAttribute.getValue().equals(fFieldProductID) && 491 (splashIDAttribute.getValue().equals(fFieldSplashID) == false)) { 492 extension.remove(element); 494 } 495 } 496 } 497 } 498 } 499 500 504 private IPluginElement findSplashHandlerElement(IPluginExtension extension) { 505 if (extension.getChildCount() == 0) { 507 return null; 509 } 510 IPluginObject[] pluginObjects = extension.getChildren(); 511 for (int j = 0; j < pluginObjects.length; j++) { 513 if (pluginObjects[j] instanceof IPluginElement) { 514 IPluginElement element = (IPluginElement)pluginObjects[j]; 515 if (element.getName().equals(F_ELEMENT_SPLASH_HANDLER)) { 517 IPluginAttribute idAttribute = 519 element.getAttribute(F_ATTRIBUTE_ID); 520 if ((idAttribute != null) && 522 PDETextHelper.isDefined(idAttribute.getValue()) && 523 idAttribute.getValue().equals(fFieldID)) { 524 return element; 526 } 527 } 528 } 529 } 530 return null; 531 } 532 533 537 private void syncSplashHandlerElement(IPluginElement element) throws CoreException { 538 IPluginAttribute classAttribute = 540 element.getAttribute(F_ATTRIBUTE_CLASS); 541 if ((classAttribute != null) && 543 PDETextHelper.isDefined(classAttribute.getValue()) && 544 classAttribute.getValue().equals(fFieldClass)) { 545 return; 547 } 548 element.setAttribute(F_ATTRIBUTE_CLASS, fFieldClass); 550 } 551 552 556 private void syncProductBindingElement(IPluginElement element) throws CoreException { 557 IPluginAttribute productIDAttribute = 559 element.getAttribute(F_ATTRIBUTE_PRODUCT_ID); 560 if ((productIDAttribute != null) && 562 PDETextHelper.isDefined(productIDAttribute.getValue()) && 563 productIDAttribute.getValue().equals(fFieldProductID)) { 564 return; 566 } 567 element.setAttribute(F_ATTRIBUTE_PRODUCT_ID, fFieldProductID); 569 } 570 571 575 private IPluginElement findProductBindingElement(IPluginExtension extension) { 576 if (extension.getChildCount() == 0) { 578 return null; 580 } 581 IPluginObject[] pluginObjects = extension.getChildren(); 582 for (int j = 0; j < pluginObjects.length; j++) { 584 if (pluginObjects[j] instanceof IPluginElement) { 585 IPluginElement element = (IPluginElement)pluginObjects[j]; 586 if (element.getName().equals(F_ELEMENT_PRODUCT_BINDING)) { 588 IPluginAttribute splashIDAttribute = 590 element.getAttribute(F_ATTRIBUTE_SPLASH_ID); 591 if ((splashIDAttribute != null) && 593 PDETextHelper.isDefined(splashIDAttribute.getValue()) && 594 splashIDAttribute.getValue().equals(fFieldSplashID)) { 595 return element; 597 } 598 } 599 } 600 } 601 return null; 602 } 603 604 608 private IPluginExtension createExtensionSplash() throws CoreException { 609 610 String fullExtensionPointID = fFieldPluginID + 611 '.' + F_SPLASH_EXTENSION_POINT; 612 IPluginExtension extension = fModel.getFactory().createExtension(); 614 extension.setPoint(fullExtensionPointID); 616 createExtensionChildrenSplash(extension); 620 621 return extension; 622 } 623 624 628 private void createExtensionChildrenSplash(IPluginExtension extension) throws CoreException { 629 630 String iconsDir = "icons" + '/'; 632 IPluginElement splashElementAf = 634 createElementSplash(extension, "af", iconsDir + "af.png", PDEUIMessages.UpdateSplashHandlerInModelAction_nameApplicationFramework); if (splashElementAf != null) { 636 extension.add(splashElementAf); 637 } 638 IPluginElement splashElementEmbedded = 640 createElementSplash(extension, "embedded", iconsDir + "embedded.png", PDEUIMessages.UpdateSplashHandlerInModelAction_nameEmbedded); if (splashElementEmbedded != null) { 642 extension.add(splashElementEmbedded); 643 } 644 IPluginElement splashElementEnterprise = 646 createElementSplash(extension, "enterprise", iconsDir + "enterprise.png", PDEUIMessages.UpdateSplashHandlerInModelAction_nameEnterprise); if (splashElementEnterprise != null) { 648 extension.add(splashElementEnterprise); 649 } 650 IPluginElement splashElementLanguages = 652 createElementSplash(extension, "languages", iconsDir + "languages.png", PDEUIMessages.UpdateSplashHandlerInModelAction_nameLanguages); if (splashElementLanguages != null) { 654 extension.add(splashElementLanguages); 655 } 656 IPluginElement splashElementRCP = 658 createElementSplash(extension, "rcp", iconsDir + "rcp.png", PDEUIMessages.UpdateSplashHandlerInModelAction_nameRCP); if (splashElementRCP != null) { 660 extension.add(splashElementRCP); 661 } 662 } 663 664 672 private IPluginElement createElementSplash( 673 IPluginExtension extension, String id, String icon, 674 String tooltip) throws CoreException { 675 IPluginElement element = 677 extension.getModel().getFactory().createElement(extension); 678 element.setName(F_ELEMENT_SPLASH); 680 element.setAttribute(F_ATTRIBUTE_ID, id); 682 element.setAttribute(F_ATTRIBUTE_ICON, icon); 684 element.setAttribute(F_ATTRIBUTE_TOOLTIP, tooltip); 686 687 return element; 688 } 689 690 694 public static boolean isExtensibleTemplateSelected(String template) { 695 if (template.equals(F_SPLASH_SCREEN_TYPE_CHOICES[2][0])) { 696 return true; 697 } 698 return false; 699 } 700 701 } 702 | Popular Tags |