1 11 12 package org.eclipse.pde.internal.ui.wizards.product; 13 14 import org.eclipse.core.filebuffers.FileBuffers; 15 import org.eclipse.core.filebuffers.ITextFileBuffer; 16 import org.eclipse.core.filebuffers.ITextFileBufferManager; 17 import org.eclipse.core.filebuffers.LocationKind; 18 import org.eclipse.core.resources.IFile; 19 import org.eclipse.core.resources.IProject; 20 import org.eclipse.core.resources.IResource; 21 import org.eclipse.core.resources.IWorkspaceRunnable; 22 import org.eclipse.core.runtime.CoreException; 23 import org.eclipse.core.runtime.IPath; 24 import org.eclipse.core.runtime.IProgressMonitor; 25 import org.eclipse.core.runtime.IStatus; 26 import org.eclipse.core.runtime.NullProgressMonitor; 27 import org.eclipse.core.runtime.Status; 28 import org.eclipse.core.runtime.SubProgressMonitor; 29 import org.eclipse.jface.text.BadLocationException; 30 import org.eclipse.jface.text.IDocument; 31 import org.eclipse.pde.core.build.IBuildEntry; 32 import org.eclipse.pde.core.build.IBuildModel; 33 import org.eclipse.pde.core.plugin.IExtensionsModelFactory; 34 import org.eclipse.pde.core.plugin.IPluginAttribute; 35 import org.eclipse.pde.core.plugin.IPluginElement; 36 import org.eclipse.pde.core.plugin.IPluginExtension; 37 import org.eclipse.pde.core.plugin.IPluginModelBase; 38 import org.eclipse.pde.core.plugin.IPluginObject; 39 import org.eclipse.pde.internal.core.build.WorkspaceBuildModel; 40 import org.eclipse.pde.internal.core.text.build.BuildModel; 41 import org.eclipse.pde.internal.core.text.build.PropertiesTextChangeListener; 42 import org.eclipse.pde.internal.core.util.PDETextHelper; 43 import org.eclipse.pde.internal.ui.IPDEUIConstants; 44 import org.eclipse.pde.internal.ui.PDEUIMessages; 45 import org.eclipse.text.edits.MalformedTreeException; 46 import org.eclipse.text.edits.MultiTextEdit; 47 import org.eclipse.text.edits.TextEdit; 48 49 53 public class UpdateSplashProgressOperation implements IWorkspaceRunnable { 54 55 public static final String F_EXTENSION_PRODUCT = "org.eclipse.core.runtime.products"; 57 public static final String F_ELEMENT_PRODUCT = "product"; 59 public static final String F_ELEMENT_PROPERTY = "property"; 61 public static final String F_ATTRIBUTE_NAME = "name"; 63 public static final String F_ATTRIBUTE_VALUE = "value"; 65 public static final String F_ATTRIBUTE_NAME_PREFCUST = "preferenceCustomization"; 67 public static final String F_KEY_SHOW_PROGRESS = "org.eclipse.ui/SHOW_PROGRESS_ON_STARTUP"; 69 public static final String F_FILE_NAME_PLUGIN_CUSTOM = "plugin_customization.ini"; 71 private IPluginModelBase fModel; 72 73 private IProgressMonitor fMonitor; 74 75 private boolean fShowProgress; 76 77 private IProject fProject; 78 79 private String fProductID; 80 81 protected String fPluginId; 82 83 private ITextFileBufferManager fTextFileBufferManager; 84 85 private ITextFileBuffer fTextFileBuffer; 86 87 private PropertiesTextChangeListener fPropertiesListener; 88 89 92 public UpdateSplashProgressOperation() { 93 reset(); 94 } 95 96 99 public void reset() { 100 fModel = null; 102 fMonitor = null; 103 fProductID = null; 104 fShowProgress = true; 105 fProject = null; 106 fPluginId = null; 107 fTextFileBufferManager = null; 109 fPropertiesListener = null; 110 fTextFileBuffer = null; 111 } 112 113 116 public void setPluginID(String pluginID) { 117 fPluginId = pluginID; 118 } 119 120 123 public void setModel(IPluginModelBase model) { 124 fModel = model; 125 } 126 127 130 private void setMonitor(IProgressMonitor monitor) { 131 if (monitor == null) { 132 monitor = new NullProgressMonitor(); 133 } 134 fMonitor = monitor; 135 } 136 137 140 public void setShowProgress(boolean showProgress) { 141 fShowProgress = showProgress; 142 } 143 144 147 public void setProductID(String productID) { 148 fProductID = productID; 149 } 150 151 154 public void setProject(IProject project) { 155 fProject = project; 156 } 157 158 161 public void run(IProgressMonitor monitor) throws CoreException { 162 setMonitor(monitor); 164 fMonitor.beginTask(PDEUIMessages.UpdateSplashProgressAction_msgProgressCustomizingSplash, 10); 166 try { 167 update(); 168 } finally { 169 fMonitor.done(); 170 } 171 } 172 173 176 private void update() throws CoreException { 177 IPluginExtension productExtension = findProductExtension(); 179 fMonitor.worked(1); 180 if (productExtension == null) { 182 return; 184 } 185 IPluginElement productElement = findProductElement(productExtension); 187 fMonitor.worked(1); 188 if (productElement == null) { 190 return; 192 } 193 IPluginElement propertyElement = findPrefCustPropertyElement(productElement); 195 fMonitor.worked(1); 196 if ((propertyElement == null) && 197 fShowProgress) { 198 addPreferenceCustomizationElement(productElement); 202 } else if (propertyElement == null) { 203 updateDefaultPluginCustomizationFile(); 212 } else { 213 updatePreferenceCustomizationElement(propertyElement); 217 } 218 fMonitor.worked(4); 219 } 220 221 225 private boolean isAttributeValueDefined(IPluginAttribute valueAttribute) { 226 if (valueAttribute == null) { 227 return false; 228 } 229 return PDETextHelper.isDefined(valueAttribute.getValue()); 230 } 231 232 236 private boolean isFileExist(IResource resource) { 237 if (resource == null) { 238 return false; 239 } 240 return (resource instanceof IFile); 241 } 242 243 247 private void updatePreferenceCustomizationElement( 248 IPluginElement propertyElement) throws CoreException { 249 IPluginAttribute valueAttribute = propertyElement.getAttribute(F_ATTRIBUTE_VALUE); 251 boolean isAttributeValueNotDefined = !isAttributeValueDefined(valueAttribute); 253 if (isAttributeValueNotDefined && 254 fShowProgress) { 255 createDefaultPluginCustomizationFile(propertyElement); 259 return; 260 } else if (isAttributeValueNotDefined) { 261 updateDefaultPluginCustomizationFile(); 264 return; 265 } 266 String pluginCustomizationFileName = valueAttribute.getValue(); 268 IResource resource = fProject.findMember(pluginCustomizationFileName); 270 boolean isFileNotExist = !isFileExist(resource); 272 if (isFileNotExist && 273 fShowProgress) { 274 createDefaultPluginCustomizationFile(propertyElement); 278 return; 279 } else if (isFileNotExist) { 280 return; 283 } 284 updatePluginCustomizationFile((IFile)resource); 288 } 289 290 295 private CoreException createCoreException(String message, Throwable exception) { 296 IStatus status = new Status(IStatus.ERROR, IPDEUIConstants.PLUGIN_ID, message, exception); 297 return new CoreException(status); 298 } 299 300 304 private CoreException createCoreException(String message) { 305 IStatus status = new Status(IStatus.ERROR, IPDEUIConstants.PLUGIN_ID, message); 306 return new CoreException(status); 307 } 308 309 313 private ITextFileBufferManager getTextFileBufferManager() throws CoreException { 314 if (fTextFileBufferManager == null) { 315 fTextFileBufferManager = FileBuffers.getTextFileBufferManager(); 317 } 318 if (fTextFileBufferManager == null) { 320 throw createCoreException(PDEUIMessages.UpdateSplashProgressAction_msgErrorTextFileBufferManager); 321 } 322 return fTextFileBufferManager; 323 } 324 325 330 private ITextFileBuffer getPluginCustomizationBuffer(IFile file) throws CoreException { 331 IPath path = file.getFullPath(); 332 LocationKind kind = LocationKind.IFILE; 333 fTextFileBuffer = 335 getTextFileBufferManager().getTextFileBuffer(path, kind); 336 if (fTextFileBuffer == null) { 338 throw createCoreException(PDEUIMessages.UpdateSplashProgressAction_msgErrorTextFileBuffer); 339 } 340 return fTextFileBuffer; 341 } 342 343 348 private BuildModel getBuildModel(IFile file) throws CoreException { 349 IDocument document = getPluginCustomizationBuffer(file).getDocument(); 352 BuildModel pluginCustomModel = new BuildModel(document, false); 354 pluginCustomModel.setUnderlyingResource(file); 355 pluginCustomModel.setCharset(file.getCharset()); 356 fPropertiesListener = new PropertiesTextChangeListener(document); 360 pluginCustomModel.addModelChangedListener(fPropertiesListener); 361 362 return pluginCustomModel; 363 } 364 365 369 private void updatePluginCustomizationFile(IFile file) throws CoreException { 370 IPath path = file.getFullPath(); 371 LocationKind kind = LocationKind.IFILE; 372 getTextFileBufferManager().connect(path, kind, new SubProgressMonitor(fMonitor, 1)); 374 try { 375 BuildModel pluginCustomModel = getBuildModel(file); 377 pluginCustomModel.load(); 379 IBuildEntry showProgressEntry = 381 pluginCustomModel.getBuild().getEntry(F_KEY_SHOW_PROGRESS); 382 if (showProgressEntry == null) { 384 addShowProgressEntry(pluginCustomModel); 387 } else { 388 updateShowProgressEntry(showProgressEntry); 391 } 392 savePluginCustomFileChanges(pluginCustomModel); 394 } catch (MalformedTreeException e) { 395 throw createCoreException(PDEUIMessages.UpdateSplashProgressAction_msgErrorCustomFileSaveFailed, e); 396 } catch (BadLocationException e) { 397 throw createCoreException(PDEUIMessages.UpdateSplashProgressAction_msgErrorCustomFileSaveFailed, e); 398 } finally { 399 getTextFileBufferManager().disconnect(path, kind, new SubProgressMonitor(fMonitor, 1)); 401 } 402 } 403 404 410 private void savePluginCustomFileChanges(BuildModel pluginCustomModel) throws CoreException, MalformedTreeException, BadLocationException { 411 if (pluginCustomModel.isDirty() == false) { 413 return; 415 } else if (fPropertiesListener == null) { 416 return; 418 } else if (fTextFileBuffer == null) { 419 return; 421 } 422 TextEdit[] edits = fPropertiesListener.getTextOperations(); 424 if (edits.length == 0) { 425 return; 427 } 428 MultiTextEdit multi = new MultiTextEdit(); 430 multi.addChildren(edits); 431 multi.apply(pluginCustomModel.getDocument()); 432 if (fTextFileBuffer.isDirty() == false) { 434 return; 436 } 437 fTextFileBuffer.commit(new SubProgressMonitor(fMonitor, 1), true); 439 } 440 441 445 private String getBooleanValue(boolean value) { 446 if (value) { 447 return Boolean.TRUE.toString(); 448 } 449 return Boolean.FALSE.toString(); 450 } 451 452 456 private void updateShowProgressEntry(IBuildEntry showProgressEntry) throws CoreException { 457 String newBooleanValue = getBooleanValue(fShowProgress); 459 String [] values = showProgressEntry.getTokens(); 461 if (values.length == 0) { 463 showProgressEntry.addToken(newBooleanValue); 466 return; 467 } else if (values.length > 1) { 468 removeEntryTokens(showProgressEntry, values); 471 showProgressEntry.addToken(newBooleanValue); 472 return; 473 } 474 String oldBooleanValue = values[0]; 476 if (oldBooleanValue.equals(newBooleanValue) == false) { 479 showProgressEntry.renameToken(oldBooleanValue, newBooleanValue); 480 } 481 } 483 484 489 private void removeEntryTokens(IBuildEntry showProgressEntry, String [] values) throws CoreException { 490 for (int i = 0; i < values.length; i++) { 492 showProgressEntry.removeToken(values[i]); 493 } 494 } 495 496 500 private void addShowProgressEntry(IBuildModel pluginCustomModel) throws CoreException { 501 IBuildEntry showProgressEntry = 503 pluginCustomModel.getFactory().createEntry(F_KEY_SHOW_PROGRESS); 504 showProgressEntry.addToken(getBooleanValue(fShowProgress)); 506 pluginCustomModel.getBuild().add(showProgressEntry); 508 } 509 510 513 private void createPluginCustomizationFile() throws CoreException { 514 IFile file = fProject.getFile(F_FILE_NAME_PLUGIN_CUSTOM); 517 WorkspaceBuildModel pluginCustomModel = 519 new WorkspaceBuildModel(file); 520 addShowProgressEntry(pluginCustomModel); 522 pluginCustomModel.save(); 524 } 525 526 530 private void addPreferenceCustomizationElement(IPluginElement productElement) throws CoreException { 531 IExtensionsModelFactory factory = productElement.getModel().getFactory(); 533 IPluginElement propertyElement = 535 factory.createElement(productElement); 536 propertyElement.setName(F_ELEMENT_PROPERTY); 537 propertyElement.setAttribute(F_ATTRIBUTE_NAME, F_ATTRIBUTE_NAME_PREFCUST); 539 productElement.add(propertyElement); 541 createDefaultPluginCustomizationFile(propertyElement); 543 } 544 545 548 private void createDefaultPluginCustomizationFile(IPluginElement propertyElement) throws CoreException { 549 propertyElement.setAttribute(F_ATTRIBUTE_VALUE, F_FILE_NAME_PLUGIN_CUSTOM); 551 IResource resource = fProject.findMember(F_FILE_NAME_PLUGIN_CUSTOM); 553 if (isFileExist(resource)) { 555 updatePluginCustomizationFile((IFile)resource); 558 } else { 559 createPluginCustomizationFile(); 562 } 563 } 564 565 568 private void updateDefaultPluginCustomizationFile() throws CoreException { 569 IResource resource = fProject.findMember(F_FILE_NAME_PLUGIN_CUSTOM); 571 if (isFileExist(resource)) { 572 updatePluginCustomizationFile((IFile)resource); 575 } 576 } 577 578 582 private IPluginElement findPrefCustPropertyElement( 583 IPluginElement productElement) { 584 if (productElement.getChildCount() == 0) { 586 return null; 587 } 588 IPluginObject[] objects = productElement.getChildren(); 590 for (int i = 0; i < objects.length; i++) { 592 if ((objects[i] instanceof IPluginElement) == false) { 594 continue; 595 } 596 if (objects[i].getName().equals(F_ELEMENT_PROPERTY) == false) { 598 continue; 599 } 600 IPluginElement element = (IPluginElement)objects[i]; 601 IPluginAttribute nameAttribute = element.getAttribute(F_ATTRIBUTE_NAME); 603 if (nameAttribute == null) { 605 continue; 606 } else if (PDETextHelper.isDefined(nameAttribute.getValue()) == false) { 607 continue; 608 } else if (nameAttribute.getValue().equals(F_ATTRIBUTE_NAME_PREFCUST) == false) { 609 continue; 610 } 611 612 return element; 613 } 614 return null; 615 } 616 617 621 private IPluginElement findProductElement(IPluginExtension extension) { 622 if (extension.getChildCount() != 1) { 624 return null; 625 } 626 IPluginObject pluginObject = extension.getChildren()[0]; 628 if ((pluginObject instanceof IPluginElement) == false) { 630 return null; 631 } 632 if (pluginObject.getName().equals(F_ELEMENT_PRODUCT) == false) { 634 return null; 635 } 636 return (IPluginElement)pluginObject; 637 } 638 639 642 private IPluginExtension findProductExtension() { 643 IPluginExtension[] extensions = fModel.getPluginBase().getExtensions(); 645 for (int i = 0; i < extensions.length; i++) { 648 String point = extensions[i].getPoint(); 650 if (point.equals(F_EXTENSION_PRODUCT) == false) { 652 continue; 653 } 654 String id = fPluginId + 657 '.' + 658 extensions[i].getId(); 659 if (id.equals(fProductID) == false) { 660 continue; 661 } 662 return extensions[i]; 663 } 664 return null; 665 } 666 667 } 668 | Popular Tags |