1 11 12 package org.eclipse.pde.internal.ui.wizards.cheatsheet; 13 14 import java.lang.reflect.InvocationTargetException ; 15 16 import org.eclipse.core.resources.IFile; 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IProgressMonitor; 19 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.core.runtime.Status; 21 import org.eclipse.core.runtime.jobs.ISchedulingRule; 22 import org.eclipse.pde.core.IBaseModel; 23 import org.eclipse.pde.core.build.IBuild; 24 import org.eclipse.pde.core.build.IBuildEntry; 25 import org.eclipse.pde.core.build.IBuildModel; 26 import org.eclipse.pde.core.plugin.IPluginAttribute; 27 import org.eclipse.pde.core.plugin.IPluginBase; 28 import org.eclipse.pde.core.plugin.IPluginElement; 29 import org.eclipse.pde.core.plugin.IPluginExtension; 30 import org.eclipse.pde.core.plugin.IPluginModelBase; 31 import org.eclipse.pde.core.plugin.IPluginObject; 32 import org.eclipse.pde.internal.core.ClasspathUtilCore; 33 import org.eclipse.pde.internal.core.ICoreConstants; 34 import org.eclipse.pde.internal.core.TargetPlatformHelper; 35 import org.eclipse.pde.internal.core.build.BuildObject; 36 import org.eclipse.pde.internal.core.build.WorkspaceBuildModel; 37 import org.eclipse.pde.internal.core.ibundle.IBundle; 38 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase; 39 import org.eclipse.pde.internal.core.ibundle.IManifestHeader; 40 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSConstants; 41 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSConstants; 42 import org.eclipse.pde.internal.core.plugin.WorkspaceFragmentModel; 43 import org.eclipse.pde.internal.core.plugin.WorkspacePluginModel; 44 import org.eclipse.pde.internal.core.plugin.WorkspacePluginModelBase; 45 import org.eclipse.pde.internal.core.text.bundle.BundleSymbolicNameHeader; 46 import org.eclipse.pde.internal.core.text.bundle.RequireBundleHeader; 47 import org.eclipse.pde.internal.core.util.PDETextHelper; 48 import org.eclipse.pde.internal.ui.IPDEUIConstants; 49 import org.eclipse.pde.internal.ui.PDEPlugin; 50 import org.eclipse.pde.internal.ui.PDEUIMessages; 51 import org.eclipse.pde.internal.ui.util.ModelModification; 52 import org.eclipse.pde.internal.ui.util.PDEModelUtility; 53 import org.eclipse.swt.widgets.Shell; 54 import org.eclipse.ui.actions.WorkspaceModifyOperation; 55 import org.osgi.framework.Constants; 56 57 61 public class RegisterCSOperation extends WorkspaceModifyOperation { 62 63 public static final String F_CS_EXTENSION_ID = "org.eclipse.ui.cheatsheets"; 65 public static final String F_CS_ATTRIBUTE_CONTENT_FILE = "contentFile"; 67 public static final String F_CS_ATTRIBUTE_COMPOSITE = "composite"; 69 private IRegisterCSData fRegisterCSData; 70 71 private Shell fShell; 72 73 76 public RegisterCSOperation(IRegisterCSData registerCSData, Shell shell) { 77 fRegisterCSData = registerCSData; 78 fShell = shell; 79 } 80 81 84 public RegisterCSOperation(ISchedulingRule rule) { 85 super(rule); 86 } 87 88 91 protected void execute(IProgressMonitor monitor) throws CoreException, 92 InvocationTargetException , InterruptedException { 93 94 try { 95 IFile file = fRegisterCSData.getPluginFile(); 96 if (fRegisterCSData.pluginExists()) { 99 modifyExistingPluginFile(file, monitor); 100 } else { 101 createNewPluginFile(file, monitor); 102 } 103 } catch (CoreException e) { 104 throw new InvocationTargetException (e); 105 } 106 } 107 108 112 private static class FindCSExtensionResult { 113 114 public IPluginExtension fCSExtension; 115 116 public IPluginElement fCSElement; 117 118 121 public FindCSExtensionResult() { 122 fCSExtension = null; 123 fCSElement = null; 124 } 125 126 129 public boolean foundCSExtension() { 130 return (fCSExtension != null); 131 } 132 133 136 public boolean foundExactCSElement() { 137 return (fCSElement != null); 138 } 139 } 140 141 146 private void modifyExistingPluginFile(IFile file, IProgressMonitor monitor) 147 throws CoreException { 148 149 IStatus status = 153 PDEPlugin.getWorkspace().validateEdit(new IFile[] {file}, fShell); 154 if (status.getSeverity() != IStatus.OK) { 155 throw new CoreException(new Status(IStatus.ERROR, 156 IPDEUIConstants.PLUGIN_ID, IStatus.ERROR, 157 PDEUIMessages.RegisterCSOperation_errorManifestReadOnly, null)); 158 } 159 ModelModification mod = new ModelModification(fRegisterCSData.getPluginProject()) { 161 protected void modifyModel(IBaseModel model, IProgressMonitor monitor) throws CoreException { 162 doModifyPluginModel(model, monitor); 163 doModifyManifestModel(model); 164 } 165 }; 166 PDEModelUtility.modifyModel(mod, monitor); 167 } 168 169 174 private void doModifyPluginModel(IBaseModel model, 175 IProgressMonitor monitor) throws CoreException { 176 if ((model instanceof IPluginModelBase) == false) { 177 return; 178 } 179 IPluginModelBase modelBase = (IPluginModelBase)model; 180 FindCSExtensionResult result = findCheatSheetExtension(modelBase); 182 if (result.foundCSExtension() && 184 result.foundExactCSElement()) { 185 modifyExistingElement(result.fCSElement, monitor); 189 IPluginElement categoryElement = createElementCategory(result.fCSExtension); 192 if (categoryElement != null) { 193 result.fCSExtension.add(categoryElement); 194 } 195 } else if (result.foundCSExtension()) { 196 modifyExistingExtension(result.fCSExtension, monitor); 201 } else { 202 insertNewExtension(modelBase, monitor); 205 } 206 } 207 208 211 private void insertNewExtension(IPluginModelBase modelBase, 212 IProgressMonitor monitor) throws CoreException { 213 monitor.beginTask(PDEUIMessages.RegisterCSOperation_newCSExtensionExistingPlugin, 1); 215 IPluginExtension extension = createExtensionCheatSheet(modelBase); 217 modelBase.getPluginBase().add(extension); 218 monitor.done(); 220 } 221 222 225 private void modifyExistingExtension(IPluginExtension extension, 226 IProgressMonitor monitor) throws CoreException { 227 monitor.beginTask(PDEUIMessages.RegisterCSOperation_modCSExtensionExistingPlugin, 1); 229 createExtensionChildren(extension); 231 monitor.done(); 233 } 234 235 239 private void modifyExistingElement(IPluginElement csElement, 240 IProgressMonitor monitor) throws CoreException { 241 monitor.beginTask(PDEUIMessages.RegisterCSOperation_modCSElementExistingPlugin, 1); 243 csElement.setAttribute(ICompCSConstants.ATTRIBUTE_NAME, 247 fRegisterCSData.getDataCheatSheetName()); 248 String categoryID = fRegisterCSData.getDataCategoryID(); 252 if (categoryID == null) { 253 categoryID = ""; } 255 csElement.setAttribute(RegisterCSWizardPage.F_CS_ELEMENT_CATEGORY, 256 categoryID); 257 IPluginElement descriptionElement = findExistingDescription(csElement); 263 if (descriptionElement == null) { 264 descriptionElement = createElementDescription(csElement); 266 if (descriptionElement != null) { 267 csElement.add(descriptionElement); 268 } 269 } else { 270 boolean modified = modifyExistingDescription(descriptionElement); 272 if (modified == false) { 273 csElement.remove(descriptionElement); 276 } 277 } 278 monitor.done(); 280 } 281 282 285 private boolean modifyExistingDescription(IPluginElement element) throws CoreException { 286 if (PDETextHelper.isDefinedAfterTrim(fRegisterCSData.getDataDescription())) { 289 element.setText(fRegisterCSData.getDataDescription().trim()); 290 return true; 291 } 292 return false; 293 } 294 295 299 private IPluginElement findExistingDescription(IPluginElement csElement) 300 throws CoreException { 301 302 if (csElement.getChildCount() > 0) { 303 IPluginObject pluginObject = csElement.getChildren()[0]; 304 if (pluginObject instanceof IPluginElement) { 305 IPluginElement element = (IPluginElement)pluginObject; 306 if (element.getName().equals(RegisterCSWizardPage.F_CS_ELEMENT_DESCRIPTION)) { 307 return element; 308 } 309 } 310 } 311 return null; 312 } 313 314 320 private FindCSExtensionResult findCheatSheetExtension(IPluginModelBase model) { 321 FindCSExtensionResult result = new FindCSExtensionResult(); 323 IPluginExtension[] extensions = fRegisterCSData.findExtensions(model, 325 RegisterCSWizardPage.F_CS_EXTENSION_POINT_ID); 326 for (int i = 0; i < extensions.length; i++) { 334 result.fCSExtension = extensions[i]; 336 if (extensions[i].getChildCount() == 0) { 338 continue; 340 } 341 IPluginObject[] pluginObjects = extensions[i].getChildren(); 342 for (int j = 0; j < pluginObjects.length; j++) { 344 if (pluginObjects[j] instanceof IPluginElement) { 345 IPluginElement element = (IPluginElement)pluginObjects[j]; 346 if (element.getName().equals(RegisterCSWizardPage.F_CS_ELEMENT_CHEATSHEET)) { 348 IPluginAttribute idAttribute = 351 element.getAttribute(ICompCSConstants.ATTRIBUTE_ID); 352 if ((idAttribute != null) && 355 PDETextHelper.isDefined(idAttribute.getValue()) && 356 fRegisterCSData.getDataCheatSheetID().equals( 357 idAttribute.getValue())) { 358 result.fCSElement = element; 360 return result; 361 } 362 } 363 364 } 365 } 366 } 367 return result; 368 } 369 370 374 private void createNewPluginFile(IFile file, IProgressMonitor monitor) 375 throws CoreException { 376 377 monitor.beginTask(PDEUIMessages.RegisterCSOperation_addNewCSExtensionNewPlugin, 4); 379 WorkspacePluginModelBase model = 381 (WorkspacePluginModelBase)createModel(file); 382 monitor.worked(1); 384 385 IPluginBase base = model.getPluginBase(); 386 double targetVersion = TargetPlatformHelper.getTargetVersion(); 388 String version = null; 389 if (targetVersion < 3.2) { 390 version = ICoreConstants.TARGET30; 391 } else { 392 version = ICoreConstants.TARGET32; 393 } 394 base.setSchemaVersion(version); 395 base.add(createExtensionCheatSheet(model)); 397 monitor.worked(1); 399 model.save(); 401 monitor.worked(1); 403 modifyExistingManifestFile(file); 406 monitor.done(); 408 } 409 410 413 private void modifyExistingManifestFile(IFile file) 414 throws CoreException { 415 IStatus status = 419 PDEPlugin.getWorkspace().validateEdit(new IFile[] { file }, fShell); 420 if (status.getSeverity() != IStatus.OK) { 421 throw new CoreException( 422 new Status(IStatus.ERROR, IPDEUIConstants.PLUGIN_ID, 423 IStatus.ERROR, PDEUIMessages.RegisterCSOperation_errorManifestReadOnly, null)); 424 } 425 ModelModification mod = 427 new ModelModification(fRegisterCSData.getPluginProject()) { 428 protected void modifyModel(IBaseModel model, IProgressMonitor monitor) throws CoreException { 429 doModifyManifestModel(model); 430 doModifyBuildModel(model); 431 } 432 }; 433 PDEModelUtility.modifyModel(mod, null); 434 } 435 436 439 private void doModifyManifestModel(IBaseModel model) { 440 if ((model instanceof IBundlePluginModelBase) == false) { 442 return; 443 } 444 IBundlePluginModelBase modelBase = (IBundlePluginModelBase)model; 445 IBundle bundle = modelBase.getBundleModel().getBundle(); 446 IManifestHeader header = bundle.getManifestHeader(Constants.BUNDLE_SYMBOLICNAME); 448 if (header instanceof BundleSymbolicNameHeader) { 449 BundleSymbolicNameHeader symbolic = (BundleSymbolicNameHeader)header; 450 if (symbolic.isSingleton() == false) { 454 symbolic.setSingleton(true); 455 } 456 } 457 header = bundle.getManifestHeader(Constants.REQUIRE_BUNDLE); 459 if (header instanceof RequireBundleHeader) { 460 RequireBundleHeader require = (RequireBundleHeader)header; 461 if (require.hasElement(F_CS_EXTENSION_ID) == false) { 462 require.addBundle(F_CS_EXTENSION_ID); 463 } 464 } 465 } 466 467 470 private void doModifyBuildModel(IBaseModel model) throws CoreException { 471 if ((model instanceof IPluginModelBase) == false) { 473 return; 474 } 475 IPluginModelBase modelBase = (IPluginModelBase)model; 476 IBuild build = ClasspathUtilCore.getBuild(modelBase); 477 if (build == null) { 479 return; 480 } 481 IBuildEntry entry = build.getEntry(IBuildEntry.BIN_INCLUDES); 483 if (entry == null) { 484 return; 487 } 488 if (entry.contains(ICoreConstants.PLUGIN_FILENAME_DESCRIPTOR) == false) { 491 entry.addToken(ICoreConstants.PLUGIN_FILENAME_DESCRIPTOR); 492 } 493 if (build instanceof BuildObject) { 497 IBuildModel buildModel = ((BuildObject)build).getModel(); 498 if (buildModel instanceof WorkspaceBuildModel) { 499 ((WorkspaceBuildModel)buildModel).save(); 500 } 501 } 502 } 503 504 508 private IPluginModelBase createModel(IFile file) { 509 if (fRegisterCSData.isFragment()) { 510 return new WorkspaceFragmentModel(file, false); 511 } 512 return new WorkspacePluginModel(file, false); 513 } 514 515 520 private IPluginExtension createExtensionCheatSheet(IPluginModelBase model) 521 throws CoreException { 522 IPluginExtension extension = model.getFactory().createExtension(); 523 extension.setPoint(RegisterCSWizardPage.F_CS_EXTENSION_POINT_ID); 525 528 createExtensionChildren(extension); 529 530 return extension; 531 } 532 533 537 private void createExtensionChildren(IPluginExtension extension) 538 throws CoreException { 539 IPluginElement categoryElement = createElementCategory(extension); 541 if (categoryElement != null) { 542 extension.add(categoryElement); 543 } 544 IPluginElement cheatSheetElement = createElementCheatSheet(extension); 546 if (cheatSheetElement != null) { 547 extension.add(cheatSheetElement); 548 } 549 } 550 551 556 private IPluginElement createElementCategory(IPluginExtension extension) 557 throws CoreException { 558 String categoryID = fRegisterCSData.getDataCategoryID(); 560 if (categoryID == null) { 561 return null; 562 } 563 int type = fRegisterCSData.getDataCategoryType(); 565 if (type != CSCategoryTrackerUtil.F_TYPE_NEW_CATEGORY) { 566 return null; 567 } 568 IPluginElement element = 570 extension.getModel().getFactory().createElement(extension); 571 element.setName(RegisterCSWizardPage.F_CS_ELEMENT_CATEGORY); 573 element.setAttribute(ICompCSConstants.ATTRIBUTE_ID, 575 categoryID); 576 element.setAttribute(ICompCSConstants.ATTRIBUTE_NAME, 579 fRegisterCSData.getDataCategoryName()); 580 581 return element; 582 } 583 584 589 private IPluginElement createElementCheatSheet(IPluginExtension extension) 590 throws CoreException { 591 592 IPluginElement element = 593 extension.getModel().getFactory().createElement(extension); 594 element.setName(RegisterCSWizardPage.F_CS_ELEMENT_CHEATSHEET); 596 element.setAttribute(ICompCSConstants.ATTRIBUTE_ID, 598 fRegisterCSData.getDataCheatSheetID()); 599 element.setAttribute(ICompCSConstants.ATTRIBUTE_NAME, 601 fRegisterCSData.getDataCheatSheetName()); 602 String categoryID = fRegisterCSData.getDataCategoryID(); 605 if (categoryID != null) { 606 element.setAttribute(RegisterCSWizardPage.F_CS_ELEMENT_CATEGORY, 607 categoryID); 608 } 609 element.setAttribute(F_CS_ATTRIBUTE_CONTENT_FILE, 611 fRegisterCSData.getDataContentFile()); 612 element.setAttribute(F_CS_ATTRIBUTE_COMPOSITE, 614 Boolean.toString(fRegisterCSData.isCompositeCheatSheet())); 615 IPluginElement descriptionElement = createElementDescription(element); 617 if (descriptionElement != null) { 618 element.add(descriptionElement); 619 } 620 621 return element; 622 } 623 624 629 private IPluginElement createElementDescription(IPluginElement parentElement) 630 throws CoreException { 631 String descriptionText = fRegisterCSData.getDataDescription(); 634 if (PDETextHelper.isDefinedAfterTrim(descriptionText) == false) { 635 return null; 636 } 637 IPluginElement element = 639 parentElement.getModel().getFactory().createElement(parentElement); 640 element.setName(ISimpleCSConstants.ELEMENT_DESCRIPTION); 642 element.setText(descriptionText.trim()); 644 645 return element; 646 } 647 648 649 } 650 | Popular Tags |