1 11 package org.eclipse.pde.internal.ui.wizards.product; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.Locale ; 15 16 import org.eclipse.core.resources.IFile; 17 import org.eclipse.core.resources.IProject; 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IPath; 20 import org.eclipse.core.runtime.IProgressMonitor; 21 import org.eclipse.core.runtime.IStatus; 22 import org.eclipse.core.runtime.Path; 23 import org.eclipse.core.runtime.Status; 24 import org.eclipse.osgi.util.NLS; 25 import org.eclipse.pde.core.IBaseModel; 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.core.plugin.PluginRegistry; 33 import org.eclipse.pde.internal.core.TargetPlatformHelper; 34 import org.eclipse.pde.internal.core.iproduct.IAboutInfo; 35 import org.eclipse.pde.internal.core.iproduct.IProduct; 36 import org.eclipse.pde.internal.core.iproduct.ISplashInfo; 37 import org.eclipse.pde.internal.core.iproduct.IWindowImages; 38 import org.eclipse.pde.internal.core.plugin.WorkspacePluginModelBase; 39 import org.eclipse.pde.internal.core.product.SplashInfo; 40 import org.eclipse.pde.internal.core.text.plugin.PluginElementNode; 41 import org.eclipse.pde.internal.ui.PDEPlugin; 42 import org.eclipse.pde.internal.ui.PDEUIMessages; 43 import org.eclipse.pde.internal.ui.util.ModelModification; 44 import org.eclipse.pde.internal.ui.util.PDEModelUtility; 45 import org.eclipse.pde.internal.ui.util.TemplateFileGenerator; 46 import org.eclipse.swt.widgets.Shell; 47 import org.eclipse.ui.branding.IProductConstants; 48 49 public class ProductDefinitionOperation extends BaseManifestOperation { 50 51 private String fProductId; 52 private String fApplication; 53 private IProduct fProduct; 54 55 protected IProject fProject; 56 57 private UpdateSplashHandlerAction fUpdateSplashAction; 58 59 private RemoveSplashHandlerBindingAction fRemoveSplashAction; 60 61 private UpdateSplashProgressOperation fUpdateSplashProgressOperation; 62 63 public ProductDefinitionOperation(IProduct product, String pluginId, String productId, String application, Shell shell) { 64 super(shell, pluginId); 65 fProductId = productId; 66 fApplication = application; 67 fProduct = product; 68 fProject = null; 69 } 70 71 79 public ProductDefinitionOperation(IProduct product, String pluginId, 80 String productId, String application, Shell shell, IProject project) { 81 super(shell, pluginId); 82 fProductId = productId; 83 fApplication = application; 84 fProduct = product; 85 fProject = project; 87 } 88 89 93 protected String getFormattedPackageName(String id){ 94 StringBuffer buffer = new StringBuffer (); 95 for (int i = 0; i < id.length(); i++) { 96 char ch = id.charAt(i); 97 if (buffer.length() == 0) { 98 if (Character.isJavaIdentifierStart(ch)) 99 buffer.append(Character.toLowerCase(ch)); 100 } else { 101 if (Character.isJavaIdentifierPart(ch) || ch == '.') 102 buffer.append(ch); 103 } 104 } 105 return buffer.toString().toLowerCase(Locale.ENGLISH); 106 } 107 108 111 protected String createTargetPackage() { 112 String packageName = getFormattedPackageName(fPluginId); 115 if (packageName.length() == 0) { 117 return ISplashHandlerConstants.F_UNQUALIFIED_EXTENSION_ID; 118 } 119 return packageName + '.' + ISplashHandlerConstants.F_UNQUALIFIED_EXTENSION_ID; 121 } 122 123 126 private String createAttributeValueClass() { 127 String targetPackage = createTargetPackage(); 128 String targetClass = createTargetClass(); 129 if (targetClass == null) { 131 return null; 132 } 133 134 return targetPackage + 135 "." + targetClass; 137 } 138 139 142 private String createTargetClass() { 143 String splashHandlerType = getSplashHandlerType(); 145 if (splashHandlerType == null) { 147 return null; 148 } 149 for (int i = 0; i < ISplashHandlerConstants.F_SPLASH_SCREEN_TYPE_CHOICES.length; i++) { 151 String choice = ISplashHandlerConstants.F_SPLASH_SCREEN_TYPE_CHOICES[i][0]; 152 if (splashHandlerType.equals(choice)) { 153 return ISplashHandlerConstants.F_SPLASH_SCREEN_CLASSES[i]; 154 } 155 } 156 return null; 157 } 158 159 162 private String createAttributeValueID() { 163 return createTargetPackage() + 165 "." + getSplashHandlerType(); 167 } 168 169 172 private UpdateSplashProgressOperation getUpdateSplashProgressOperation() { 173 if (fUpdateSplashProgressOperation == null) { 174 fUpdateSplashProgressOperation = new UpdateSplashProgressOperation(); 175 } else { 176 fUpdateSplashProgressOperation.reset(); 177 } 178 return fUpdateSplashProgressOperation; 179 } 180 181 186 private void updateSplashProgress(IPluginModelBase model, 187 IProgressMonitor monitor) throws CoreException { 188 if (fProject == null) { 190 return; 191 } else if (model == null) { 192 return; 193 } else if (monitor == null) { 194 return; 195 } 196 UpdateSplashProgressOperation operation = getUpdateSplashProgressOperation(); 198 operation.setModel(model); 199 operation.setShowProgress(isProgressDefined()); 200 operation.setProject(fProject); 201 operation.setProductID(fProduct.getId()); 202 operation.setPluginID(fPluginId); 203 operation.run(monitor); 205 } 206 207 210 private boolean isProgressDefined() { 211 ISplashInfo info = fProduct.getProduct().getSplashInfo(); 213 if (info == null) { 215 return false; 216 } 217 return info.isDefinedGeometry(); 219 } 220 221 224 private String getSplashHandlerType() { 225 ISplashInfo info = fProduct.getProduct().getSplashInfo(); 227 if (info == null) { 229 return null; 230 } 231 if (info.isDefinedSplashHandlerType() == false) { 233 return null; 234 } 235 return info.getFieldSplashHandlerType(); 236 } 237 238 243 private void updateSplashHandler(IPluginModelBase model, 244 IProgressMonitor monitor) throws CoreException { 245 updateSplashHandlerFiles(model, monitor); 249 updateSplashHandlerModel(model, monitor); 252 } 253 254 259 private void updateSplashHandlerFiles(IPluginModelBase model, 260 IProgressMonitor monitor) throws CoreException { 261 if (fProject == null) { 263 return; 264 } 265 String splashHandlerType = getSplashHandlerType(); 267 if (splashHandlerType == null) { 269 return; 270 } 271 TemplateFileGenerator generator = new TemplateFileGenerator( 276 fProject, model, fPluginId, createTargetPackage(), 277 createTargetClass(), splashHandlerType); 278 generator.generateFiles(monitor); 280 } 281 282 287 private void updateSplashHandlerModel(IPluginModelBase model, 288 IProgressMonitor monitor) throws CoreException { 289 String splashHandlerType = getSplashHandlerType(); 291 if (splashHandlerType == null) { 293 runRemoveSplashAction(model, monitor); 294 } else { 295 runUpdateSplashAction(model, monitor, splashHandlerType); 296 } 297 } 298 299 303 private void runRemoveSplashAction(IPluginModelBase model, 304 IProgressMonitor monitor) throws CoreException { 305 fRemoveSplashAction = new RemoveSplashHandlerBindingAction(); 307 fRemoveSplashAction.setFieldProductID(fProduct.getId()); 309 fRemoveSplashAction.setFieldTargetPackage(createTargetPackage()); 310 311 fRemoveSplashAction.setModel(model); 312 fRemoveSplashAction.setMonitor(monitor); 313 fRemoveSplashAction.run(); 315 fRemoveSplashAction.hasException(); 317 } 318 319 325 private void runUpdateSplashAction(IPluginModelBase model, 326 IProgressMonitor monitor, String splashHandlerType) 327 throws CoreException { 328 fUpdateSplashAction = new UpdateSplashHandlerAction(); 330 String id = createAttributeValueID(); 332 fUpdateSplashAction.setFieldID(id); 333 fUpdateSplashAction.setFieldClass(createAttributeValueClass()); 334 fUpdateSplashAction.setFieldSplashID(id); 335 fUpdateSplashAction.setFieldProductID(fProduct.getId()); 336 fUpdateSplashAction.setFieldTemplate(splashHandlerType); 337 fUpdateSplashAction.setFieldPluginID(fPluginId); 338 339 fUpdateSplashAction.setModel(model); 340 fUpdateSplashAction.setMonitor(monitor); 341 fUpdateSplashAction.run(); 343 fUpdateSplashAction.hasException(); 345 } 346 347 public void run(IProgressMonitor monitor) throws InvocationTargetException , 348 InterruptedException { 349 try { 350 IFile file = getFile(); 351 if (!file.exists()) { 352 createNewFile(file, monitor); 353 } else { 354 modifyExistingFile(file, monitor); 355 } 356 updateSingleton(monitor); 357 } catch (CoreException e) { 358 throw new InvocationTargetException (e); 359 } 360 } 361 362 private void createNewFile(IFile file, IProgressMonitor monitor) throws CoreException { 363 WorkspacePluginModelBase model = (WorkspacePluginModelBase)getModel(file); 364 IPluginBase base = model.getPluginBase(); 365 base.setSchemaVersion(TargetPlatformHelper.getTargetVersion() < 3.2 ? "3.0" : "3.2"); base.add(createExtension(model)); 367 updateSplashHandler(model, monitor); 369 updateSplashProgress(model, monitor); 371 372 model.save(); 373 } 374 375 private IPluginExtension createExtension(IPluginModelBase model) throws CoreException{ 376 IPluginExtension extension = model.getFactory().createExtension(); 377 extension.setPoint("org.eclipse.core.runtime.products"); extension.setId(fProductId); 379 extension.add(createExtensionContent(extension)); 380 return extension; 381 } 382 383 private IPluginElement createExtensionContent(IPluginExtension extension) throws CoreException { 384 IPluginElement element = extension.getModel().getFactory().createElement(extension); 385 element.setName("product"); element.setAttribute("name", fProduct.getName()); element.setAttribute("application", fApplication); 389 IPluginElement child = createElement(element, IProductConstants.WINDOW_IMAGES, getWindowImagesString()); 390 if (child != null) 391 element.add(child); 392 393 child = createElement(element, IProductConstants.ABOUT_TEXT, getAboutText()); 394 if (child != null) 395 element.add(child); 396 397 child = createElement(element, IProductConstants.ABOUT_IMAGE, getAboutImage()); 398 if (child != null) 399 element.add(child); 400 401 child = createElement(element, IProductConstants.STARTUP_FOREGROUND_COLOR, getForegroundColor()); 402 if (child != null) 403 element.add(child); 404 405 child = createElement(element, IProductConstants.STARTUP_PROGRESS_RECT, getProgressRect()); 406 if (child != null) 407 element.add(child); 408 409 child = createElement(element, IProductConstants.STARTUP_MESSAGE_RECT, getMessageRect()); 410 if (child != null) 411 element.add(child); 412 413 return element; 414 } 415 416 private IPluginElement createElement(IPluginElement parent, String name, String value) throws CoreException { 417 IPluginElement element = null; 418 if (value != null && value.length() > 0) { 419 element = parent.getModel().getFactory().createElement(parent); 420 element.setName("property"); element.setAttribute("name", name); element.setAttribute("value", value); } 424 return element; 425 } 426 427 private String getAboutText() { 428 IAboutInfo info = fProduct.getAboutInfo(); 429 if (info != null) { 430 String text = info.getText(); 431 return text == null || text.length() == 0 ? null : text; 432 } 433 return null; 434 } 435 436 private String getAboutImage() { 437 IAboutInfo info = fProduct.getAboutInfo(); 438 return info != null ? getURL(info.getImagePath()) : null; 439 } 440 441 private String getURL(String location) { 442 if (location == null || location.trim().length() == 0) 443 return null; 444 IPath path = new Path(location); 445 if (!path.isAbsolute()) 446 return location; 447 String projectName = path.segment(0); 448 IProject project = PDEPlugin.getWorkspace().getRoot().getProject(projectName); 449 if (project.exists()) { 450 IPluginModelBase model = PluginRegistry.findModel(project); 451 if (model != null) { 452 String id = model.getPluginBase().getId(); 453 if (fPluginId.equals(id)) 454 return path.removeFirstSegments(1).toString(); 455 return "platform:/plugin/" + id + "/" + path.removeFirstSegments(1); } 457 } 458 return location; 459 } 460 461 private String getWindowImagesString() { 462 IWindowImages images = fProduct.getWindowImages(); 463 StringBuffer buffer = new StringBuffer (); 464 if (images != null) { 465 for (int i = 0; i < IWindowImages.TOTAL_IMAGES; i++) { 466 String image = getURL(images.getImagePath(i)); 467 if (image != null) { 468 if (buffer.length() > 0) 469 buffer.append(","); buffer.append(image); 471 } 472 473 } 474 } 475 return buffer.length() == 0 ? null : buffer.toString(); } 477 478 private String getForegroundColor() { 479 ISplashInfo info = fProduct.getSplashInfo(); 480 return info != null ? info.getForegroundColor() : null; 481 } 482 483 private String getProgressRect() { 484 ISplashInfo info = fProduct.getSplashInfo(); 485 return info != null ? SplashInfo.getGeometryString(info.getProgressGeometry()) : null; 486 } 487 488 private String getMessageRect() { 489 ISplashInfo info = fProduct.getSplashInfo(); 490 return info != null ? SplashInfo.getGeometryString(info.getMessageGeometry()) : null; 491 } 492 493 private void modifyExistingFile(IFile file, IProgressMonitor monitor) throws CoreException { 494 IStatus status = PDEPlugin.getWorkspace().validateEdit(new IFile[] {file}, getShell()); 495 if (status.getSeverity() != IStatus.OK) 496 throw new CoreException(new Status(IStatus.ERROR, "org.eclipse.pde.ui", IStatus.ERROR, NLS.bind(PDEUIMessages.ProductDefinitionOperation_readOnly, fPluginId), null)); 498 ModelModification mod = new ModelModification(file) { 499 protected void modifyModel(IBaseModel model, IProgressMonitor monitor) throws CoreException { 500 if (!(model instanceof IPluginModelBase)) 501 return; 502 IPluginExtension extension = findProductExtension((IPluginModelBase)model); 503 if (extension == null) 504 insertNewExtension((IPluginModelBase)model); 505 else 506 modifyExistingExtension(extension); 507 updateSplashHandler((IPluginModelBase)model, monitor); 509 updateSplashProgress((IPluginModelBase)model, monitor); 511 } 512 }; 513 PDEModelUtility.modifyModel(mod, monitor); 514 } 515 516 private IPluginExtension findProductExtension(IPluginModelBase model) { 517 IPluginExtension[] extensions = model.getPluginBase().getExtensions(); 518 for (int i = 0; i < extensions.length; i++) { 519 String point = extensions[i].getPoint(); 520 String id = extensions[i].getId(); 521 if (fProductId.equals(id) && "org.eclipse.core.runtime.products".equals(point)) { return extensions[i]; 523 } 524 } 525 return null; 526 } 527 528 private void insertNewExtension(IPluginModelBase model) throws CoreException { 529 IPluginExtension extension = createExtension(model); 530 model.getPluginBase().add(extension); 531 } 532 533 private void modifyExistingExtension(IPluginExtension extension) throws CoreException { 534 if (extension.getChildCount() == 0) { 535 insertNewProductElement(extension); 536 return; 537 } 538 539 PluginElementNode element = (PluginElementNode)extension.getChildren()[0]; 540 541 if (!"product".equals(element.getName())) { insertNewProductElement(extension); 543 return; 544 } 545 546 element.setAttribute("application", fApplication); element.setAttribute("name", fProduct.getName()); synchronizeChild(element, IProductConstants.APP_NAME, fProduct.getName()); 549 550 synchronizeChild(element, IProductConstants.ABOUT_IMAGE, getAboutImage()); 551 synchronizeChild(element, IProductConstants.ABOUT_TEXT, getAboutText()); 552 synchronizeChild(element, IProductConstants.WINDOW_IMAGES, getWindowImagesString()); 553 synchronizeChild(element, IProductConstants.STARTUP_FOREGROUND_COLOR, getForegroundColor()); 554 synchronizeChild(element, IProductConstants.STARTUP_MESSAGE_RECT, getMessageRect()); 555 synchronizeChild(element, IProductConstants.STARTUP_PROGRESS_RECT, getProgressRect()); 556 557 } 558 559 private void synchronizeChild(IPluginElement element, String propertyName, String value) throws CoreException { 560 IPluginElement child = null; 561 IPluginObject[] children = element.getChildren(); 562 for (int i = 0; i < children.length; i++) { 563 IPluginElement candidate = (IPluginElement)children[i]; 564 if (candidate.getName().equals("property")) { IPluginAttribute attr = candidate.getAttribute("name"); if (attr != null && attr.getValue().equals(propertyName)) { 567 child = candidate; 568 break; 569 } 570 } 571 } 572 if (child != null && value == null) 573 element.remove(child); 574 575 if (value == null) 576 return; 577 578 if (child == null) { 579 child = element.getModel().getFactory().createElement(element); 580 child.setName("property"); element.add(child); 582 } 583 child.setAttribute("value", value); child.setAttribute("name", propertyName); } 586 587 private void insertNewProductElement(IPluginExtension extension) throws CoreException { 588 IPluginElement element = createExtensionContent(extension); 589 extension.add(element); 590 } 591 592 } 593 | Popular Tags |