1 11 package org.eclipse.ui.internal; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.HashMap ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.ListIterator ; 20 import java.util.Map ; 21 22 import org.eclipse.core.commands.AbstractHandler; 23 import org.eclipse.core.commands.ExecutionEvent; 24 import org.eclipse.core.commands.IHandler; 25 import org.eclipse.core.runtime.Assert; 26 import org.eclipse.core.runtime.CoreException; 27 import org.eclipse.core.runtime.IConfigurationElement; 28 import org.eclipse.core.runtime.IExtension; 29 import org.eclipse.core.runtime.IPath; 30 import org.eclipse.core.runtime.IProgressMonitor; 31 import org.eclipse.core.runtime.IStatus; 32 import org.eclipse.core.runtime.MultiStatus; 33 import org.eclipse.core.runtime.SafeRunner; 34 import org.eclipse.core.runtime.Status; 35 import org.eclipse.core.runtime.SubProgressMonitor; 36 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler; 37 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker; 38 import org.eclipse.jface.dialogs.IDialogConstants; 39 import org.eclipse.jface.dialogs.MessageDialog; 40 import org.eclipse.jface.internal.provisional.action.ICoolBarManager2; 41 import org.eclipse.jface.operation.IRunnableContext; 42 import org.eclipse.jface.operation.IRunnableWithProgress; 43 import org.eclipse.jface.preference.IPreferenceStore; 44 import org.eclipse.jface.resource.ImageDescriptor; 45 import org.eclipse.jface.resource.ImageRegistry; 46 import org.eclipse.jface.resource.JFaceResources; 47 import org.eclipse.jface.util.IPropertyChangeListener; 48 import org.eclipse.jface.util.PropertyChangeEvent; 49 import org.eclipse.jface.util.SafeRunnable; 50 import org.eclipse.jface.viewers.ArrayContentProvider; 51 import org.eclipse.jface.window.IShellProvider; 52 import org.eclipse.osgi.util.NLS; 53 import org.eclipse.swt.custom.BusyIndicator; 54 import org.eclipse.swt.program.Program; 55 import org.eclipse.swt.widgets.Display; 56 import org.eclipse.swt.widgets.Shell; 57 import org.eclipse.ui.ActiveShellExpression; 58 import org.eclipse.ui.IEditorActionBarContributor; 59 import org.eclipse.ui.IEditorDescriptor; 60 import org.eclipse.ui.IEditorInput; 61 import org.eclipse.ui.IEditorLauncher; 62 import org.eclipse.ui.IEditorMatchingStrategy; 63 import org.eclipse.ui.IEditorPart; 64 import org.eclipse.ui.IEditorReference; 65 import org.eclipse.ui.IEditorRegistry; 66 import org.eclipse.ui.IEditorSite; 67 import org.eclipse.ui.IMemento; 68 import org.eclipse.ui.IPathEditorInput; 69 import org.eclipse.ui.IPersistableEditor; 70 import org.eclipse.ui.IPersistableElement; 71 import org.eclipse.ui.ISaveablePart; 72 import org.eclipse.ui.ISaveablePart2; 73 import org.eclipse.ui.ISaveablesLifecycleListener; 74 import org.eclipse.ui.ISaveablesSource; 75 import org.eclipse.ui.IViewPart; 76 import org.eclipse.ui.IWorkbenchPage; 77 import org.eclipse.ui.IWorkbenchPart; 78 import org.eclipse.ui.IWorkbenchPart3; 79 import org.eclipse.ui.IWorkbenchPartReference; 80 import org.eclipse.ui.IWorkbenchWindow; 81 import org.eclipse.ui.PartInitException; 82 import org.eclipse.ui.PlatformUI; 83 import org.eclipse.ui.Saveable; 84 import org.eclipse.ui.dialogs.ListSelectionDialog; 85 import org.eclipse.ui.handlers.IHandlerActivation; 86 import org.eclipse.ui.handlers.IHandlerService; 87 import org.eclipse.ui.internal.StartupThreading.StartupRunnable; 88 import org.eclipse.ui.internal.dialogs.EventLoopProgressMonitor; 89 import org.eclipse.ui.internal.editorsupport.ComponentSupport; 90 import org.eclipse.ui.internal.misc.ExternalEditor; 91 import org.eclipse.ui.internal.misc.StatusUtil; 92 import org.eclipse.ui.internal.misc.UIStats; 93 import org.eclipse.ui.internal.part.NullEditorInput; 94 import org.eclipse.ui.internal.registry.EditorDescriptor; 95 import org.eclipse.ui.internal.registry.EditorRegistry; 96 import org.eclipse.ui.internal.tweaklets.TabBehaviour; 97 import org.eclipse.ui.internal.tweaklets.Tweaklets; 98 import org.eclipse.ui.internal.util.Util; 99 import org.eclipse.ui.model.WorkbenchPartLabelProvider; 100 import org.eclipse.ui.part.MultiEditor; 101 import org.eclipse.ui.part.MultiEditorInput; 102 import org.eclipse.ui.statushandlers.StatusManager; 103 104 119 public class EditorManager implements IExtensionChangeHandler { 120 EditorAreaHelper editorPresentation; 121 122 WorkbenchWindow window; 123 124 WorkbenchPage page; 125 126 private Map actionCache = new HashMap (); 127 128 private static final String PIN_EDITOR_KEY = "PIN_EDITOR"; 130 private static final String PIN_EDITOR = "ovr16/pinned_ovr.gif"; 132 private IPropertyChangeListener editorPropChangeListnener = null; 135 136 private IHandlerActivation pinEditorHandlerActivation = null; 138 139 static final String RESOURCES_TO_SAVE_MESSAGE = WorkbenchMessages.EditorManager_saveResourcesMessage; 140 141 static final String SAVE_RESOURCES_TITLE = WorkbenchMessages.EditorManager_saveResourcesTitle; 142 143 146 public EditorManager(WorkbenchWindow window, WorkbenchPage workbenchPage, 147 EditorAreaHelper pres) { 148 Assert.isNotNull(window); 149 Assert.isNotNull(workbenchPage); 150 Assert.isNotNull(pres); 151 this.window = window; 152 this.page = workbenchPage; 153 this.editorPresentation = pres; 154 155 page.getExtensionTracker().registerHandler(this, null); 156 } 157 158 163 void checkDeleteEditorResources() { 164 IEditorReference[] editors = page.getEditorReferences(); 166 if (editors.length == 0) { 168 if (editorPropChangeListnener != null) { 169 IPreferenceStore prefStore = WorkbenchPlugin.getDefault() 171 .getPreferenceStore(); 172 prefStore 173 .removePropertyChangeListener(editorPropChangeListnener); 174 editorPropChangeListnener = null; 175 } 176 if (pinEditorHandlerActivation != null) { 177 final IHandlerService handlerService = (IHandlerService) window.getWorkbench().getService(IHandlerService.class); 179 handlerService.deactivateHandler(pinEditorHandlerActivation); 180 pinEditorHandlerActivation = null; 181 } 182 } 183 } 184 185 189 void checkCreateEditorPropListener() { 190 if (editorPropChangeListnener == null) { 191 editorPropChangeListnener = new IPropertyChangeListener() { 195 public void propertyChange(PropertyChangeEvent event) { 196 if (event.getProperty().equals( 197 IPreferenceConstants.REUSE_EDITORS_BOOLEAN)) { 198 IEditorReference[] editors = getEditors(); 199 for (int i = 0; i < editors.length; i++) { 200 ((EditorReference) editors[i]).pinStatusUpdated(); 201 } 202 } 203 } 204 }; 205 WorkbenchPlugin.getDefault().getPreferenceStore() 206 .addPropertyChangeListener(editorPropChangeListnener); 207 } 208 } 209 210 214 void checkCreatePinEditorShortcutKeyHandler() { 215 if (pinEditorHandlerActivation == null) { 216 final Shell shell = window.getShell(); 217 final IHandler pinEditorHandler = new AbstractHandler() { 218 public final Object execute(final ExecutionEvent event) { 219 IPreferenceStore store = WorkbenchPlugin.getDefault().getPreferenceStore(); 222 if (store 223 .getBoolean(IPreferenceConstants.REUSE_EDITORS_BOOLEAN) 224 || ((TabBehaviour)Tweaklets.get(TabBehaviour.KEY)).alwaysShowPinAction()) { 225 226 IWorkbenchPartReference ref = editorPresentation 227 .getVisibleEditor(); 228 if (ref instanceof WorkbenchPartReference) { 229 WorkbenchPartReference concreteRef = (WorkbenchPartReference) ref; 230 231 concreteRef.setPinned(concreteRef.isPinned()); 232 } 233 } 234 return null; 235 } 236 }; 237 238 final IHandlerService handlerService = (IHandlerService) window.getWorkbench().getService(IHandlerService.class); 240 pinEditorHandlerActivation = handlerService.activateHandler( 241 "org.eclipse.ui.window.pinEditor", pinEditorHandler, new ActiveShellExpression(shell)); 243 } 244 } 245 246 251 ImageDescriptor getEditorPinImageDesc() { 252 ImageRegistry registry = JFaceResources.getImageRegistry(); 253 ImageDescriptor pinDesc = registry.getDescriptor(PIN_EDITOR_KEY); 254 if (pinDesc == null) { 256 pinDesc = WorkbenchImages.getWorkbenchImageDescriptor(PIN_EDITOR); 257 registry.put(PIN_EDITOR_KEY, pinDesc); 258 259 } 260 return pinDesc; 261 } 262 263 266 private List collectDirtyEditors() { 267 List result = new ArrayList (3); 268 IEditorReference[] editors = page.getEditorReferences(); 269 for (int i = 0; i < editors.length; i++) { 270 IEditorPart part = (IEditorPart) editors[i].getPart(false); 271 if (part != null && part.isDirty()) { 272 result.add(part); 273 } 274 275 } 276 return result; 277 } 278 279 282 public boolean containsEditor(IEditorReference ref) { 283 IEditorReference[] editors = page.getEditorReferences(); 284 for (int i = 0; i < editors.length; i++) { 285 if (ref == editors[i]) { 286 return true; 287 } 288 } 289 return false; 290 } 291 292 297 private EditorActionBars createEditorActionBars(EditorDescriptor desc, 298 final IEditorSite site) { 299 String type = desc.getId(); 301 302 EditorActionBars actionBars = (EditorActionBars) actionCache.get(type); 304 if (actionBars != null) { 305 actionBars.addRef(); 306 return actionBars; 307 } 308 309 actionBars = new EditorActionBars(page, site.getWorkbenchWindow(), type); 311 actionBars.addRef(); 312 actionCache.put(type, actionBars); 313 314 IEditorActionBarContributor contr = desc.createActionBarContributor(); 316 if (contr != null) { 317 actionBars.setEditorContributor(contr); 318 contr.init(actionBars, page); 319 } 320 321 EditorActionBuilder builder = new EditorActionBuilder(); 323 contr = builder.readActionExtensions(desc); 324 if (contr != null) { 325 actionBars.setExtensionContributor(contr); 326 contr.init(actionBars, page); 327 } 328 329 return actionBars; 331 } 332 333 336 private EditorActionBars createEmptyEditorActionBars(final IEditorSite site) { 337 String type = String.valueOf(System.currentTimeMillis()); 339 340 EditorActionBars actionBars = new EditorActionBars(page, site.getWorkbenchWindow(), type); 343 actionBars.addRef(); 344 actionCache.put(type, actionBars); 345 346 return actionBars; 348 } 349 350 353 void disposeEditorActionBars(EditorActionBars actionBars) { 354 actionBars.removeRef(); 355 if (actionBars.getRef() <= 0) { 356 String type = actionBars.getEditorType(); 357 actionCache.remove(type); 358 ICoolBarManager2 coolBar = (ICoolBarManager2) window.getCoolBarManager2(); 360 if (coolBar != null) { 361 coolBar.refresh(); 362 } 363 actionBars.dispose(); 364 } 365 } 366 367 375 public IEditorPart findEditor(IEditorInput input) { 376 return findEditor(null, input, IWorkbenchPage.MATCH_INPUT); 377 } 378 379 392 public IEditorPart findEditor(String editorId, IEditorInput input, 393 int matchFlags) { 394 IEditorReference[] refs = findEditors(input, editorId, matchFlags); 395 if (refs.length == 0) { 396 return null; 397 } 398 return refs[0].getEditor(true); 399 } 400 401 415 public IEditorReference[] findEditors(IEditorInput input, String editorId, 416 int matchFlags) { 417 if (matchFlags == IWorkbenchPage.MATCH_NONE) { 418 return new IEditorReference[0]; 419 } 420 List result = new ArrayList (); 421 ArrayList othersList = new ArrayList (Arrays.asList(page 422 .getEditorReferences())); 423 if (!othersList.isEmpty()) { 424 IEditorReference active = page.getActiveEditorReference(); 425 if (active != null) { 426 othersList.remove(active); 427 ArrayList activeList = new ArrayList (1); 428 activeList.add(active); 429 findEditors(activeList, input, editorId, matchFlags, result); 430 } 431 findEditors(othersList, input, editorId, matchFlags, result); 432 } 433 return (IEditorReference[]) result.toArray(new IEditorReference[result 434 .size()]); 435 } 436 437 452 private void findEditors(List editorList, IEditorInput input, 453 String editorId, int matchFlags, List result) { 454 if (matchFlags == IWorkbenchPage.MATCH_NONE) { 455 return; 456 } 457 458 if (((matchFlags & IWorkbenchPage.MATCH_ID) != 0) && editorId != null) { 460 for (Iterator i = editorList.iterator(); i.hasNext();) { 461 EditorReference editor = (EditorReference) i.next(); 462 if (!editorId.equals(editor.getId())) { 463 i.remove(); 464 } 465 } 466 } 467 468 if ((matchFlags & IWorkbenchPage.MATCH_INPUT) == 0) { 471 result.addAll(editorList); 472 return; 473 } 474 475 for (Iterator i = editorList.iterator(); i.hasNext();) { 477 EditorReference editor = (EditorReference) i.next(); 478 IEditorDescriptor desc = editor.getDescriptor(); 479 if (desc != null) { 480 IEditorMatchingStrategy matchingStrategy = desc 481 .getEditorMatchingStrategy(); 482 if (matchingStrategy != null) { 483 i.remove(); if (matchingStrategy.matches(editor, input)) { 486 result.add(editor); 487 } 488 } 489 } 490 } 491 492 for (Iterator i = editorList.iterator(); i.hasNext();) { 495 IEditorReference editor = (IEditorReference) i.next(); 496 IEditorPart part = (IEditorPart) editor.getPart(false); 497 if (part != null) { 498 i.remove(); if (part.getEditorInput() != null 501 && part.getEditorInput().equals(input)) { 502 result.add(editor); 503 } 504 } 505 } 506 507 String name = input.getName(); 512 IPersistableElement persistable = input.getPersistable(); 513 if (name == null || persistable == null) { 514 return; 515 } 516 String id = persistable.getFactoryId(); 517 if (id == null) { 518 return; 519 } 520 for (Iterator i = editorList.iterator(); i.hasNext();) { 521 EditorReference editor = (EditorReference) i.next(); 522 if (name.equals(editor.getName()) 523 && id.equals(editor.getFactoryId())) { 524 IEditorInput restoredInput; 525 try { 526 restoredInput = editor.getEditorInput(); 527 if (Util.equals(restoredInput, input)) { 528 result.add(editor); 529 } 530 } catch (PartInitException e1) { 531 WorkbenchPlugin.log(e1); 532 } 533 } 534 } 535 } 536 537 540 private Display getDisplay() { 541 return window.getShell().getDisplay(); 542 } 543 544 547 public int getEditorCount() { 548 return page.getEditorReferences().length; 549 } 550 551 554 private IEditorRegistry getEditorRegistry() { 555 return WorkbenchPlugin.getDefault().getEditorRegistry(); 556 } 557 558 561 public IEditorPart[] getDirtyEditors() { 562 List dirtyEditors = collectDirtyEditors(); 563 return (IEditorPart[]) dirtyEditors 564 .toArray(new IEditorPart[dirtyEditors.size()]); 565 } 566 567 570 public IEditorReference[] getEditors() { 571 return page.getEditorReferences(); 572 } 573 574 577 public IEditorPart getVisibleEditor() { 578 IEditorReference ref = editorPresentation.getVisibleEditor(); 579 if (ref == null) { 580 return null; 581 } 582 return (IEditorPart) ref.getPart(true); 583 } 584 585 588 public boolean isSaveAllNeeded() { 589 IEditorReference[] editors = page.getEditorReferences(); 590 for (int i = 0; i < editors.length; i++) { 591 IEditorReference ed = editors[i]; 592 if (ed.isDirty()) { 593 return true; 594 } 595 } 596 return false; 597 } 598 599 603 private IEditorReference findReusableEditor(EditorDescriptor desc) { 604 return ((TabBehaviour)Tweaklets.get(TabBehaviour.KEY)).findReusableEditor(page); 605 } 606 607 620 public IEditorReference openEditor(String editorId, IEditorInput input, 621 boolean setVisible, IMemento editorState) throws PartInitException { 622 if (editorId == null || input == null) { 623 throw new IllegalArgumentException (); 624 } 625 626 IEditorRegistry reg = getEditorRegistry(); 627 EditorDescriptor desc = (EditorDescriptor) reg.findEditor(editorId); 628 if (desc == null) { 629 throw new PartInitException(NLS.bind( 630 WorkbenchMessages.EditorManager_unknownEditorIDMessage, 631 editorId)); 632 } 633 634 IEditorReference result = openEditorFromDescriptor(desc, input, editorState); 635 return result; 636 } 637 638 641 public IEditorReference openEditorFromDescriptor(EditorDescriptor desc, 642 IEditorInput input, IMemento editorState) throws PartInitException { 643 IEditorReference result = null; 644 if (desc.isInternal()) { 645 result = reuseInternalEditor(desc, input); 646 if (result == null) { 647 result = new EditorReference(this, input, desc, editorState); 648 } 649 } else if (desc.getId() 650 .equals(IEditorRegistry.SYSTEM_INPLACE_EDITOR_ID)) { 651 if (ComponentSupport.inPlaceEditorSupported()) { 652 result = new EditorReference(this, input, desc); 653 } 654 } else if (desc.getId().equals( 655 IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID)) { 656 IPathEditorInput pathInput = getPathEditorInput(input); 657 if (pathInput != null) { 658 result = openSystemExternalEditor(pathInput.getPath()); 659 } else { 660 throw new PartInitException( 661 WorkbenchMessages.EditorManager_systemEditorError); 662 } 663 } else if (desc.isOpenExternal()) { 664 result = openExternalEditor(desc, input); 665 } else { 666 throw new PartInitException(NLS.bind( 668 WorkbenchMessages.EditorManager_invalidDescriptor, desc 669 .getId())); 670 } 671 672 if (result != null) { 673 createEditorTab((EditorReference) result, ""); } 675 676 Workbench wb = (Workbench) window.getWorkbench(); 677 wb.getEditorHistory().add(input, desc); 678 return result; 679 } 680 681 684 private IEditorReference openExternalEditor(final EditorDescriptor desc, 685 IEditorInput input) throws PartInitException { 686 final CoreException ex[] = new CoreException[1]; 687 688 final IPathEditorInput pathInput = getPathEditorInput(input); 689 if (pathInput != null && pathInput.getPath() != null) { 690 BusyIndicator.showWhile(getDisplay(), new Runnable () { 691 public void run() { 692 try { 693 if (desc.getLauncher() != null) { 694 Object launcher = WorkbenchPlugin.createExtension( 696 desc.getConfigurationElement(), "launcher"); ((IEditorLauncher) launcher).open(pathInput 698 .getPath()); 699 } else { 700 ExternalEditor oEditor = new ExternalEditor( 702 pathInput.getPath(), desc); 703 oEditor.open(); 704 } 705 } catch (CoreException e) { 706 ex[0] = e; 707 } 708 } 709 }); 710 } else { 711 throw new PartInitException(NLS.bind( 712 WorkbenchMessages.EditorManager_errorOpeningExternalEditor, 713 desc.getFileName(), desc.getId())); 714 } 715 716 if (ex[0] != null) { 717 throw new PartInitException(NLS.bind( 718 WorkbenchMessages.EditorManager_errorOpeningExternalEditor, 719 desc.getFileName(), desc.getId()), ex[0]); 720 } 721 722 return null; 724 } 725 726 737 IEditorReference[] openMultiEditor(final IEditorReference ref, 738 final MultiEditor part, final MultiEditorInput input) 739 throws PartInitException { 740 741 String [] editorArray = input.getEditors(); 742 IEditorInput[] inputArray = input.getInput(); 743 744 EditorDescriptor[] descArray = new EditorDescriptor[editorArray.length]; 746 IEditorReference refArray[] = new IEditorReference[editorArray.length]; 747 IEditorPart partArray[] = new IEditorPart[editorArray.length]; 748 749 IEditorRegistry reg = getEditorRegistry(); 750 for (int i = 0; i < editorArray.length; i++) { 751 EditorDescriptor innerDesc = (EditorDescriptor) reg 752 .findEditor(editorArray[i]); 753 if (innerDesc == null) { 754 throw new PartInitException(NLS.bind( 755 WorkbenchMessages.EditorManager_unknownEditorIDMessage, 756 editorArray[i])); 757 } 758 descArray[i] = innerDesc; 759 InnerEditor innerRef = new InnerEditor(ref, inputArray[i], 760 descArray[i]); 761 refArray[i] = innerRef; 762 partArray[i] = innerRef.getEditor(true); 763 } 764 part.setChildren(partArray); 765 return refArray; 766 } 767 768 771 private void createEditorTab(final EditorReference ref, 772 final String workbookId) throws PartInitException { 773 774 editorPresentation.addEditor(ref, workbookId); 775 776 } 777 778 781 EditorSite createSite(final IEditorReference ref, final IEditorPart part, 782 final EditorDescriptor desc, final IEditorInput input) 783 throws PartInitException { 784 EditorSite site = new EditorSite(ref, part, page, desc); 785 if (desc != null) { 786 site.setActionBars(createEditorActionBars(desc, site)); 787 } else { 788 site.setActionBars(createEmptyEditorActionBars(site)); 789 } 790 final String label = part.getTitle(); try { 792 try { 793 UIStats.start(UIStats.INIT_PART, label); 794 part.init(site, input); 795 } finally { 796 UIStats.end(UIStats.INIT_PART, part, label); 797 } 798 799 if (part.getSite() != site || part.getEditorSite() != site) { 801 throw new PartInitException(NLS.bind( 802 WorkbenchMessages.EditorManager_siteIncorrect, desc 803 .getId())); 804 } 805 806 } catch (Exception e) { 807 disposeEditorActionBars((EditorActionBars) site.getActionBars()); 808 site.dispose(); 809 if (e instanceof PartInitException) { 810 throw (PartInitException) e; 811 } 812 813 throw new PartInitException( 814 WorkbenchMessages.EditorManager_errorInInit, e); 815 } 816 817 return site; 818 } 819 820 823 private IEditorReference reuseInternalEditor(EditorDescriptor desc, 824 IEditorInput input) throws PartInitException { 825 826 Assert.isNotNull(desc, "descriptor must not be null"); Assert.isNotNull(input, "input must not be null"); 829 IEditorReference reusableEditorRef = findReusableEditor(desc); 830 if (reusableEditorRef != null) { 831 return ((TabBehaviour) Tweaklets.get(TabBehaviour.KEY)) 832 .reuseInternalEditor(page, this, editorPresentation, desc, 833 input, reusableEditorRef); 834 } 835 return null; 836 } 837 838 IEditorPart createPart(final EditorDescriptor desc) 839 throws PartInitException { 840 try { 841 IEditorPart result = desc.createEditor(); 842 IConfigurationElement element = desc.getConfigurationElement(); 843 if (element != null) { 844 page.getExtensionTracker().registerObject( 845 element.getDeclaringExtension(), result, 846 IExtensionTracker.REF_WEAK); 847 } 848 return result; 849 } catch (CoreException e) { 850 throw new PartInitException(StatusUtil.newStatus( 851 desc.getPluginID(), 852 WorkbenchMessages.EditorManager_instantiationError, e)); 853 } 854 } 855 856 859 private IEditorReference openSystemExternalEditor(final IPath location) 860 throws PartInitException { 861 if (location == null) { 862 throw new IllegalArgumentException (); 863 } 864 865 final boolean result[] = { false }; 866 BusyIndicator.showWhile(getDisplay(), new Runnable () { 867 public void run() { 868 if (location != null) { 869 result[0] = Program.launch(location.toOSString()); 870 } 871 } 872 }); 873 874 if (!result[0]) { 875 throw new PartInitException(NLS.bind( 876 WorkbenchMessages.EditorManager_unableToOpenExternalEditor, 877 location)); 878 } 879 880 return null; 882 } 883 884 ImageDescriptor findImage(EditorDescriptor desc, IPath path) { 885 if (desc == null) { 886 return ImageDescriptor.getMissingImageDescriptor(); 888 } 889 890 if (desc.isOpenExternal() && path != null) { 891 return PlatformUI.getWorkbench().getEditorRegistry() 892 .getImageDescriptor(path.toOSString()); 893 } 894 895 return desc.getImageDescriptor(); 896 } 897 898 901 public IStatus restoreState(IMemento memento) { 902 final MultiStatus result = new MultiStatus(PlatformUI.PLUGIN_ID, 904 IStatus.OK, 905 WorkbenchMessages.EditorManager_problemsRestoringEditors, null); 906 final String activeWorkbookID[] = new String [1]; 907 final ArrayList visibleEditors = new ArrayList (5); 908 final IEditorReference activeEditor[] = new IEditorReference[1]; 909 910 IMemento areaMem = memento.getChild(IWorkbenchConstants.TAG_AREA); 911 if (areaMem != null) { 912 result.add(editorPresentation.restoreState(areaMem)); 913 activeWorkbookID[0] = areaMem 914 .getString(IWorkbenchConstants.TAG_ACTIVE_WORKBOOK); 915 } 916 917 919 IMemento[] editorMems = memento 920 .getChildren(IWorkbenchConstants.TAG_EDITOR); 921 for (int x = 0; x < editorMems.length; x++) { 922 restoreEditorState(editorMems[x], visibleEditors, activeEditor, 925 result); 926 } 927 928 if (areaMem != null) { 930 result.add(editorPresentation.restorePresentationState(areaMem)); 931 } 932 try { 933 StartupThreading.runWithThrowable(new StartupRunnable(){ 934 935 public void runWithException() throws Throwable { 936 for (int i = 0; i < visibleEditors.size(); i++) { 938 setVisibleEditor((IEditorReference) visibleEditors.get(i), 939 false); 940 } 941 942 if (activeWorkbookID[0] != null) { 944 editorPresentation 945 .setActiveEditorWorkbookFromID(activeWorkbookID[0]); 946 } 947 948 if (activeEditor[0] != null) { 949 IWorkbenchPart editor = activeEditor[0].getPart(true); 950 951 if (editor != null) { 952 page.activate(editor); 953 } 954 } 955 }}); 956 } 957 catch (Throwable t) { 958 result 960 .add(new Status( 961 IStatus.ERROR, 962 PlatformUI.PLUGIN_ID, 963 0, 964 WorkbenchMessages.EditorManager_exceptionRestoringEditor, 965 t)); 966 } 967 968 return result; 969 } 970 971 979 public boolean saveAll(boolean confirm, boolean closing, boolean addNonPartSources) { 980 ISaveablePart[] parts = page.getDirtyParts(); 983 if (parts.length == 0) { 984 return true; 985 } 986 List dirtyParts = new ArrayList (parts.length); 988 for (int i = 0; i < parts.length; i++) { 989 dirtyParts.add(parts[i]); 990 } 991 992 return saveAll(dirtyParts, confirm, closing, addNonPartSources, window); 994 } 995 996 1014 public static boolean saveAll(List dirtyParts, boolean confirm, boolean closing, 1015 boolean addNonPartSources, final IWorkbenchWindow window) { 1016 return saveAll(dirtyParts, confirm, closing, addNonPartSources, window, window); 1017 } 1018 1019 1040 public static boolean saveAll(List dirtyParts, final boolean confirm, final boolean closing, 1041 boolean addNonPartSources, final IRunnableContext runnableContext, final IShellProvider shellProvider) { 1042 dirtyParts = new ArrayList (dirtyParts); 1044 List modelsToSave; 1045 if (confirm) { 1046 boolean saveable2Processed = false; 1047 ListIterator listIterator = dirtyParts.listIterator(); 1055 1056 WorkbenchPage currentPage = null; 1057 Perspective currentPageOriginalPerspective = null; 1058 while (listIterator.hasNext()) { 1059 IWorkbenchPart part = (IWorkbenchPart) listIterator.next(); 1060 if (part instanceof ISaveablePart2) { 1061 WorkbenchPage page = (WorkbenchPage) part.getSite() 1062 .getPage(); 1063 if (!Util.equals(currentPage, page)) { 1064 if (currentPage != null 1065 && currentPageOriginalPerspective != null) { 1066 if (!currentPageOriginalPerspective 1067 .equals(currentPage.getActivePerspective())) { 1068 currentPage 1069 .setPerspective(currentPageOriginalPerspective 1070 .getDesc()); 1071 } 1072 } 1073 currentPage = page; 1074 currentPageOriginalPerspective = page 1075 .getActivePerspective(); 1076 } 1077 if (confirm) { 1078 if (part instanceof IViewPart) { 1079 Perspective perspective = page 1080 .getFirstPerspectiveWithView((IViewPart) part); 1081 if (perspective != null) { 1082 page.setPerspective(perspective.getDesc()); 1083 } 1084 } 1085 IWorkbenchWindow partsWindow = page 1087 .getWorkbenchWindow(); 1088 if (partsWindow != partsWindow.getWorkbench() 1089 .getActiveWorkbenchWindow()) { 1090 Shell shell = partsWindow.getShell(); 1091 if (shell.getMinimized()) { 1092 shell.setMinimized(false); 1093 } 1094 shell.setActive(); 1095 } 1096 page.bringToTop(part); 1097 } 1098 int choice = SaveableHelper.savePart((ISaveablePart2) part, 1100 page.getWorkbenchWindow(), confirm); 1101 if (choice == ISaveablePart2.CANCEL) { 1102 return false; 1106 } else if (choice != ISaveablePart2.DEFAULT) { 1107 saveable2Processed = true; 1108 listIterator.remove(); 1109 } 1110 } 1111 } 1112 1113 if (currentPage != null && currentPageOriginalPerspective != null) { 1115 if (!currentPageOriginalPerspective.equals(currentPage 1116 .getActivePerspective())) { 1117 currentPage.setPerspective(currentPageOriginalPerspective 1118 .getDesc()); 1119 } 1120 } 1121 1122 if (saveable2Processed) { 1125 listIterator = dirtyParts.listIterator(); 1126 while (listIterator.hasNext()) { 1127 ISaveablePart part = (ISaveablePart) listIterator.next(); 1128 if (!part.isDirty()) { 1129 listIterator.remove(); 1130 } 1131 } 1132 } 1133 1134 modelsToSave = convertToSaveables(dirtyParts, closing, addNonPartSources); 1135 1136 if (modelsToSave.isEmpty()) { 1138 return true; 1139 } 1140 boolean canceled = SaveableHelper.waitForBackgroundSaveJobs(modelsToSave); 1141 if (canceled) { 1142 return false; 1143 } 1144 if (modelsToSave.size() == 1) { 1146 Saveable model = (Saveable) modelsToSave.get(0); 1147 String message = NLS.bind(WorkbenchMessages.EditorManager_saveChangesQuestion, model.getName()); 1148 String [] buttons = new String [] { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL, IDialogConstants.CANCEL_LABEL }; 1150 MessageDialog d = new MessageDialog( 1151 shellProvider.getShell(), WorkbenchMessages.Save_Resource, 1152 null, message, MessageDialog.QUESTION, buttons, 0); 1153 1154 int choice = SaveableHelper.testGetAutomatedResponse(); 1155 if (SaveableHelper.testGetAutomatedResponse() == SaveableHelper.USER_RESPONSE) { 1156 choice = d.open(); 1157 } 1158 1159 switch (choice) { 1163 case ISaveablePart2.YES: break; 1165 case ISaveablePart2.NO: return true; 1167 default: 1168 case ISaveablePart2.CANCEL: return false; 1170 } 1171 } 1172 else { 1173 ListSelectionDialog dlg = new ListSelectionDialog( 1174 shellProvider.getShell(), modelsToSave, 1175 new ArrayContentProvider(), 1176 new WorkbenchPartLabelProvider(), RESOURCES_TO_SAVE_MESSAGE); 1177 dlg.setInitialSelections(modelsToSave.toArray()); 1178 dlg.setTitle(SAVE_RESOURCES_TITLE); 1179 1180 if (SaveableHelper.testGetAutomatedResponse()==SaveableHelper.USER_RESPONSE) { 1182 int result = dlg.open(); 1183 if (result == IDialogConstants.CANCEL_ID) { 1185 return false; 1186 } 1187 1188 modelsToSave = Arrays.asList(dlg.getResult()); 1189 } 1190 } 1191 } 1192 else { 1193 modelsToSave = convertToSaveables(dirtyParts, closing, addNonPartSources); 1194 } 1195 1196 if (modelsToSave.isEmpty()) { 1198 return true; 1199 } 1200 1201 final List finalModels = modelsToSave; 1203 IRunnableWithProgress progressOp = new IRunnableWithProgress() { 1204 public void run(IProgressMonitor monitor) { 1205 IProgressMonitor monitorWrap = new EventLoopProgressMonitor( 1206 monitor); 1207 monitorWrap.beginTask("", finalModels.size()); for (Iterator i = finalModels.iterator(); i.hasNext();) { 1209 Saveable model = (Saveable) i.next(); 1210 if (!model.isDirty()) { 1212 monitor.worked(1); 1213 continue; 1214 } 1215 SaveableHelper.doSaveModel(model, new SubProgressMonitor(monitorWrap, 1), shellProvider, closing || confirm); 1216 if (monitorWrap.isCanceled()) { 1217 break; 1218 } 1219 } 1220 monitorWrap.done(); 1221 } 1222 }; 1223 1224 return SaveableHelper.runProgressMonitorOperation( 1226 WorkbenchMessages.Save_All, progressOp, runnableContext, shellProvider); 1227 } 1228 1229 1244 private static List convertToSaveables(List parts, boolean closing, boolean addNonPartSources) { 1245 ArrayList result = new ArrayList (); 1246 HashSet seen = new HashSet (); 1247 for (Iterator i = parts.iterator(); i.hasNext();) { 1248 IWorkbenchPart part = (IWorkbenchPart) i.next(); 1249 Saveable[] saveables = getSaveables(part); 1250 for (int j = 0; j < saveables.length; j++) { 1251 Saveable saveable = saveables[j]; 1252 if (saveable.isDirty() && !seen.contains(saveable)) { 1253 seen.add(saveable); 1254 if (!closing 1255 || closingLastPartShowingModel(saveable, parts, part 1256 .getSite().getPage())) { 1257 result.add(saveable); 1258 } 1259 } 1260 } 1261 } 1262 if (addNonPartSources) { 1263 SaveablesList saveablesList = (SaveablesList) PlatformUI 1264 .getWorkbench().getService( 1265 ISaveablesLifecycleListener.class); 1266 ISaveablesSource[] nonPartSources = saveablesList 1267 .getNonPartSources(); 1268 for (int i = 0; i < nonPartSources.length; i++) { 1269 Saveable[] saveables = nonPartSources[i].getSaveables(); 1270 for (int j = 0; j < saveables.length; j++) { 1271 Saveable saveable = saveables[j]; 1272 if (saveable.isDirty() && !seen.contains(saveable)) { 1273 seen.add(saveable); 1274 result.add(saveable); 1275 } 1276 } 1277 } 1278 } 1279 return result; 1280 } 1281 1282 1290 private static Saveable[] getSaveables(IWorkbenchPart part) { 1291 if (part instanceof ISaveablesSource) { 1292 ISaveablesSource source = (ISaveablesSource) part; 1293 return source.getSaveables(); 1294 } 1295 return new Saveable[] { new DefaultSaveable(part) }; 1296 } 1297 1298 1311 private static boolean closingLastPartShowingModel(Saveable model, 1312 List closingParts, IWorkbenchPage page) { 1313 HashSet closingPartsWithSameModel = new HashSet (); 1314 for (Iterator i = closingParts.iterator(); i.hasNext();) { 1315 IWorkbenchPart part = (IWorkbenchPart) i.next(); 1316 Saveable[] models = getSaveables(part); 1317 if (Arrays.asList(models).contains(model)) { 1318 closingPartsWithSameModel.add(part); 1319 } 1320 } 1321 IWorkbenchPartReference[] pagePartRefs = ((WorkbenchPage) page).getAllParts(); 1322 HashSet pagePartsWithSameModels = new HashSet (); 1323 for (int i = 0; i < pagePartRefs.length; i++) { 1324 IWorkbenchPartReference partRef = pagePartRefs[i]; 1325 IWorkbenchPart part = partRef.getPart(false); 1326 if (part != null) { 1327 Saveable[] models = getSaveables(part); 1328 if (Arrays.asList(models).contains(model)) { 1329 pagePartsWithSameModels.add(part); 1330 } 1331 } 1332 } 1333 for (Iterator i = closingPartsWithSameModel.iterator(); i.hasNext();) { 1334 IWorkbenchPart part = (IWorkbenchPart) i.next(); 1335 pagePartsWithSameModels.remove(part); 1336 } 1337 return pagePartsWithSameModels.isEmpty(); 1338 } 1339 1340 1343 public boolean savePart(final ISaveablePart saveable, IWorkbenchPart part, 1344 boolean confirm) { 1345 return SaveableHelper.savePart(saveable, part, window, confirm); 1346 } 1347 1348 1351 public IStatus saveState(final IMemento memento) { 1352 1353 final MultiStatus result = new MultiStatus(PlatformUI.PLUGIN_ID, 1354 IStatus.OK, 1355 WorkbenchMessages.EditorManager_problemsSavingEditors, null); 1356 1357 IMemento editorAreaMem = memento 1359 .createChild(IWorkbenchConstants.TAG_AREA); 1360 result.add(editorPresentation.saveState(editorAreaMem)); 1361 1362 editorAreaMem.putString(IWorkbenchConstants.TAG_ACTIVE_WORKBOOK, 1364 editorPresentation.getActiveEditorWorkbookID()); 1365 1366 ArrayList workbooks = editorPresentation.getWorkbooks(); 1368 1369 for (Iterator iter = workbooks.iterator(); iter.hasNext();) { 1370 EditorStack workbook = (EditorStack) iter.next(); 1371 1372 EditorPane editorPanes[] = workbook.getEditors(); 1374 1375 for (int i = 0; i < editorPanes.length; i++) { 1376 IEditorReference editorReference = editorPanes[i] 1378 .getEditorReference(); 1379 EditorReference e = (EditorReference) editorReference; 1380 final IEditorPart editor = editorReference.getEditor(false); 1381 if (editor == null) { 1382 if (e.getMemento() != null) { 1383 IMemento editorMem = memento 1384 .createChild(IWorkbenchConstants.TAG_EDITOR); 1385 editorMem.putMemento(e.getMemento()); 1386 } 1387 continue; 1388 } 1389 1390 saveEditorState(memento, e, result); 1393 } 1394 } 1395 return result; 1396 } 1397 1398 1404 public boolean setVisibleEditor(IEditorReference newEd, boolean setFocus) { 1405 return editorPresentation.setVisibleEditor(newEd, setFocus); 1406 } 1407 1408 private IPathEditorInput getPathEditorInput(IEditorInput input) { 1409 if (input instanceof IPathEditorInput) { 1410 return (IPathEditorInput) input; 1411 } 1412 1413 return (IPathEditorInput) Util.getAdapter(input, IPathEditorInput.class); 1414 } 1415 1416 private class InnerEditor extends EditorReference { 1417 1418 private IEditorReference outerEditor; 1419 1420 public InnerEditor(IEditorReference outerEditor, IEditorInput input, 1421 EditorDescriptor desc) { 1422 super(EditorManager.this, input, desc); 1423 this.outerEditor = outerEditor; 1424 } 1425 1426 protected PartPane createPane() { 1427 return new MultiEditorInnerPane( 1428 (EditorPane) ((EditorReference) outerEditor).getPane(), 1429 this, page, editorPresentation.getActiveWorkbook()); 1430 } 1431 1432 } 1433 1434 1438 public void restoreEditorState(IMemento editorMem, 1439 ArrayList visibleEditors, IEditorReference[] activeEditor, 1440 MultiStatus result) { 1441 final EditorReference e = new EditorReference(this, editorMem); 1444 1445 1448 final String workbookID = editorMem 1449 .getString(IWorkbenchConstants.TAG_WORKBOOK); 1450 1451 try { 1452 StartupThreading.runWithPartInitExceptions(new StartupRunnable () { 1453 1454 public void runWithException() throws Throwable { 1455 createEditorTab(e, workbookID); 1456 }}); 1457 1458 } catch (PartInitException ex) { 1459 result.add(ex.getStatus()); 1460 } 1461 1462 String strActivePart = editorMem 1463 .getString(IWorkbenchConstants.TAG_ACTIVE_PART); 1464 if ("true".equals(strActivePart)) { activeEditor[0] = e; 1466 } 1467 1468 String strFocus = editorMem.getString(IWorkbenchConstants.TAG_FOCUS); 1469 boolean visibleEditor = "true".equals(strFocus); if (visibleEditor) { 1471 visibleEditors.add(e); 1472 } 1473 } 1474 1475 protected void saveEditorState(IMemento mem, IEditorReference ed, 1477 MultiStatus res) { 1478 final EditorReference editorRef = (EditorReference) ed; 1479 final IEditorPart editor = ed.getEditor(false); 1480 final IMemento memento = mem; 1481 final MultiStatus result = res; 1482 if (!(editor.getEditorSite() instanceof EditorSite)) { 1483 return; 1484 } 1485 final EditorSite site = (EditorSite) editor.getEditorSite(); 1486 if (site.getPane() instanceof MultiEditorInnerPane) { 1487 return; 1488 } 1489 1490 SafeRunner.run(new SafeRunnable() { 1491 public void run() { 1492 IEditorInput input = editor.getEditorInput(); 1494 IPersistableElement persistable = input.getPersistable(); 1495 if (persistable == null) { 1496 return; 1497 } 1498 1499 IMemento editorMem = memento 1501 .createChild(IWorkbenchConstants.TAG_EDITOR); 1502 editorMem.putString(IWorkbenchConstants.TAG_TITLE, editorRef 1503 .getTitle()); 1504 editorMem.putString(IWorkbenchConstants.TAG_NAME, editorRef 1505 .getName()); 1506 editorMem.putString(IWorkbenchConstants.TAG_ID, editorRef 1507 .getId()); 1508 editorMem.putString(IWorkbenchConstants.TAG_TOOLTIP, editorRef 1509 .getTitleToolTip()); 1510 1511 editorMem.putString(IWorkbenchConstants.TAG_PART_NAME, 1512 editorRef.getPartName()); 1513 1514 if (editor instanceof IWorkbenchPart3) { 1515 Map properties = ((IWorkbenchPart3) editor) 1516 .getPartProperties(); 1517 if (!properties.isEmpty()) { 1518 IMemento propBag = editorMem 1519 .createChild(IWorkbenchConstants.TAG_PROPERTIES); 1520 Iterator i = properties.entrySet().iterator(); 1521 while (i.hasNext()) { 1522 Map.Entry entry = (Map.Entry ) i.next(); 1523 IMemento p = propBag.createChild( 1524 IWorkbenchConstants.TAG_PROPERTY, 1525 (String ) entry.getKey()); 1526 p.putTextData((String ) entry.getValue()); 1527 } 1528 } 1529 } 1530 1531 if (editorRef.isPinned()) { 1532 editorMem.putString(IWorkbenchConstants.TAG_PINNED, "true"); } 1534 1535 EditorPane editorPane = (EditorPane) ((EditorSite) editor 1536 .getEditorSite()).getPane(); 1537 editorMem.putString(IWorkbenchConstants.TAG_WORKBOOK, 1538 editorPane.getWorkbook().getID()); 1539 1540 if (editor == page.getActivePart()) { 1541 editorMem.putString(IWorkbenchConstants.TAG_ACTIVE_PART, 1542 "true"); } 1544 1545 if (editorPane == editorPane.getWorkbook().getSelection()) { 1546 editorMem.putString(IWorkbenchConstants.TAG_FOCUS, "true"); } 1548 1549 if (input instanceof IPathEditorInput) { 1550 IPath path = ((IPathEditorInput) input).getPath(); 1551 if (path != null) { 1552 editorMem.putString(IWorkbenchConstants.TAG_PATH, path 1553 .toString()); 1554 } 1555 } 1556 1557 IMemento inputMem = editorMem 1559 .createChild(IWorkbenchConstants.TAG_INPUT); 1560 inputMem.putString(IWorkbenchConstants.TAG_FACTORY_ID, 1561 persistable.getFactoryId()); 1562 persistable.saveState(inputMem); 1563 1564 if (editor instanceof IPersistableEditor) { 1566 IMemento editorState = editorMem 1567 .createChild(IWorkbenchConstants.TAG_EDITOR_STATE); 1568 ((IPersistableEditor) editor).saveState(editorState); 1569 } 1570 } 1571 1572 public void handleException(Throwable e) { 1573 result 1574 .add(new Status( 1575 IStatus.ERROR, 1576 PlatformUI.PLUGIN_ID, 1577 0, 1578 NLS 1579 .bind( 1580 WorkbenchMessages.EditorManager_unableToSaveEditor, 1581 editorRef.getTitle()), e)); 1582 } 1583 }); 1584 } 1585 1586 public IMemento getMemento(IEditorReference e) { 1588 if (e instanceof EditorReference) { 1589 return ((EditorReference) e).getMemento(); 1590 } 1591 return null; 1592 } 1593 1594 1600 public void removeExtension(IExtension source, Object [] objects) { 1601 for (int i = 0; i < objects.length; i++) { 1602 if (objects[i] instanceof IEditorPart) { 1603 1605 IEditorPart editor = (IEditorPart) objects[i]; 1606 IEditorInput input = editor.getEditorInput(); 1607 page.closeEditor(editor, true); 1608 ((Workbench) window.getWorkbench()).getEditorHistory().remove( 1609 input); 1610 } 1611 } 1612 } 1613 1614 1620 public void addExtension(IExtensionTracker tracker, IExtension extension) { 1621 } 1623 1624 1627 IEditorReference openEmptyTab() { 1628 IEditorInput input = new NullEditorInput(); 1629 EditorDescriptor desc = (EditorDescriptor) ((EditorRegistry) getEditorRegistry()) 1630 .findEditor(EditorRegistry.EMPTY_EDITOR_ID); 1631 EditorReference result = new EditorReference(this, input, desc); 1632 try { 1633 createEditorTab(result, ""); return result; 1635 } catch (PartInitException e) { 1636 StatusManager.getManager().handle( 1637 StatusUtil.newStatus(WorkbenchPlugin.PI_WORKBENCH, e)); 1638 } 1639 return null; 1640 } 1641 1642 public static boolean useIPersistableEditor() { 1643 IPreferenceStore store = WorkbenchPlugin.getDefault() 1644 .getPreferenceStore(); 1645 return store.getBoolean(IPreferenceConstants.USE_IPERSISTABLE_EDITORS); 1646 } 1647} 1648 | Popular Tags |