1 11 package org.eclipse.ui.internal; 12 13 import org.eclipse.core.runtime.IAdaptable; 14 import org.eclipse.core.runtime.IPath; 15 import org.eclipse.core.runtime.IStatus; 16 import org.eclipse.core.runtime.Path; 17 import org.eclipse.jface.preference.IPreferenceStore; 18 import org.eclipse.jface.resource.ImageDescriptor; 19 import org.eclipse.osgi.util.NLS; 20 import org.eclipse.swt.SWT; 21 import org.eclipse.swt.graphics.Point; 22 import org.eclipse.swt.layout.FillLayout; 23 import org.eclipse.swt.widgets.Composite; 24 import org.eclipse.ui.IEditorInput; 25 import org.eclipse.ui.IEditorPart; 26 import org.eclipse.ui.IPersistableEditor; 27 import org.eclipse.ui.IEditorReference; 28 import org.eclipse.ui.IEditorRegistry; 29 import org.eclipse.ui.IElementFactory; 30 import org.eclipse.ui.IMemento; 31 import org.eclipse.ui.IPersistableElement; 32 import org.eclipse.ui.IReusableEditor; 33 import org.eclipse.ui.IWorkbenchPage; 34 import org.eclipse.ui.IWorkbenchPart; 35 import org.eclipse.ui.IWorkbenchPart2; 36 import org.eclipse.ui.IWorkbenchPart3; 37 import org.eclipse.ui.IWorkbenchPartConstants; 38 import org.eclipse.ui.PartInitException; 39 import org.eclipse.ui.PlatformUI; 40 import org.eclipse.ui.internal.editorsupport.ComponentSupport; 41 import org.eclipse.ui.internal.misc.StatusUtil; 42 import org.eclipse.ui.internal.misc.UIStats; 43 import org.eclipse.ui.internal.part.NullEditorInput; 44 import org.eclipse.ui.internal.registry.EditorDescriptor; 45 import org.eclipse.ui.internal.registry.EditorRegistry; 46 import org.eclipse.ui.internal.tweaklets.TabBehaviour; 47 import org.eclipse.ui.internal.tweaklets.Tweaklets; 48 import org.eclipse.ui.internal.util.Util; 49 import org.eclipse.ui.part.IWorkbenchPartOrientation; 50 import org.eclipse.ui.part.MultiEditor; 51 import org.eclipse.ui.part.MultiEditorInput; 52 import org.eclipse.ui.statushandlers.StatusManager; 53 54 public class EditorReference extends WorkbenchPartReference implements 55 IEditorReference { 56 57 60 private final EditorManager manager; 61 62 private IMemento editorMemento; 63 64 private IMemento editorState = null; 65 66 70 private boolean expectingInputChange = false; 71 72 79 private boolean reportedMalfunctioningEditor = false; 80 81 84 String name; 85 86 String factoryId; 87 88 IEditorInput restoredInput; 89 90 94 private IEditorReference[] multiEditorChildren = null; 95 96 97 105 public EditorReference(EditorManager manager, IEditorInput input, 106 EditorDescriptor desc) { 107 this(manager, input, desc, null); 108 } 109 110 120 public EditorReference(EditorManager manager, IEditorInput input, 121 EditorDescriptor desc, IMemento editorState) { 122 this.manager = manager; 123 initListenersAndHandlers(); 124 restoredInput = input; 125 this.editorState = editorState; 126 init(desc.getId(), desc.getLabel(), 127 "", desc.getImageDescriptor(), desc.getLabel(), ""); } 129 130 134 EditorReference(EditorManager manager, IMemento memento) { 135 this.manager = manager; 136 initListenersAndHandlers(); 137 this.editorMemento = memento; 138 if (EditorManager.useIPersistableEditor()) { 139 editorState = editorMemento.getChild(IWorkbenchConstants.TAG_EDITOR_STATE); 140 } else { 141 editorState = null; 142 } 143 String id = memento.getString(IWorkbenchConstants.TAG_ID); 144 String title = memento.getString(IWorkbenchConstants.TAG_TITLE); 145 String tooltip = Util.safeString(memento 146 .getString(IWorkbenchConstants.TAG_TOOLTIP)); 147 String partName = memento 148 .getString(IWorkbenchConstants.TAG_PART_NAME); 149 150 IMemento propBag = memento.getChild(IWorkbenchConstants.TAG_PROPERTIES); 151 if (propBag != null) { 152 IMemento[] props = propBag 153 .getChildren(IWorkbenchConstants.TAG_PROPERTY); 154 for (int i = 0; i < props.length; i++) { 155 propertyCache.put(props[i].getID(), props[i].getTextData()); 156 } 157 } 158 159 if (partName == null) { 161 partName = title; 162 } 163 164 EditorDescriptor desc = null; 166 if (id != null) { 167 desc = getDescriptor(id); 168 } 169 String location = memento.getString(IWorkbenchConstants.TAG_PATH); 171 IPath path = location == null ? null : new Path(location); 172 ImageDescriptor iDesc = this.manager.findImage(desc, path); 173 174 this.name = memento.getString(IWorkbenchConstants.TAG_NAME); 175 if (this.name == null) { 176 this.name = title; 177 } 178 setPinned("true".equals(memento.getString(IWorkbenchConstants.TAG_PINNED))); 180 IMemento inputMem = memento.getChild(IWorkbenchConstants.TAG_INPUT); 181 if (inputMem != null) { 182 this.factoryId = inputMem 183 .getString(IWorkbenchConstants.TAG_FACTORY_ID); 184 } 185 186 init(id, title, tooltip, iDesc, partName, ""); } 188 189 public EditorDescriptor getDescriptor() { 190 return getDescriptor(getId()); 191 } 192 193 199 private EditorDescriptor getDescriptor(String id) { 200 EditorDescriptor desc; 201 IEditorRegistry reg = WorkbenchPlugin.getDefault() 202 .getEditorRegistry(); 203 desc = (EditorDescriptor) reg.findEditor(id); 204 return desc; 205 } 206 207 210 private void initListenersAndHandlers() { 211 this.manager.checkCreateEditorPropListener(); 215 this.manager.checkCreatePinEditorShortcutKeyHandler(); 218 } 219 220 protected PartPane createPane() { 221 return new EditorPane(this, this.manager.page, this.manager.editorPresentation.getActiveWorkbook()); 222 } 223 224 230 public void pinStatusUpdated() { 231 firePropertyChange(IWorkbenchPart.PROP_TITLE); 232 } 233 234 public String getFactoryId() { 235 IEditorPart editor = getEditor(false); 236 if (editor != null) { 237 IPersistableElement persistable = editor.getEditorInput() 238 .getPersistable(); 239 if (persistable != null) { 240 return persistable.getFactoryId(); 241 } 242 return null; 243 } 244 return factoryId; 245 } 246 247 protected String computePartName() { 248 if (part instanceof IWorkbenchPart2) { 249 return super.computePartName(); 250 } else { 251 return getRawTitle(); 252 } 253 } 254 255 public String getName() { 256 if (part != null) { 257 return getEditor(false).getEditorInput().getName(); 258 } 259 return name; 260 } 261 262 public IEditorPart getEditor(boolean restore) { 263 return (IEditorPart)getPart(restore); 264 } 265 266 protected void releaseReferences() { 267 super.releaseReferences(); 268 editorMemento = null; 269 editorState = null; 270 name = null; 271 factoryId = null; 272 restoredInput = null; 273 } 274 275 void setName(String name) { 276 this.name = name; 277 } 278 279 public IMemento getMemento() { 280 return editorMemento; 281 } 282 283 public IWorkbenchPage getPage() { 284 return this.manager.page; 285 } 286 287 protected void doDisposePart() { 288 if (multiEditorChildren!=null) { 289 for (int i=0; i<multiEditorChildren.length; ++i) { 290 EditorReference ref = (EditorReference)multiEditorChildren[i]; 291 if (ref!=null) { 292 ref.dispose(); 293 } 294 } 295 multiEditorChildren = null; 296 } 297 298 if (part != null) { 299 EditorSite site = (EditorSite) ((IEditorPart)part).getEditorSite(); 300 manager.disposeEditorActionBars((EditorActionBars) site.getActionBars()); 301 site.dispose(); 302 } 303 304 this.manager.checkDeleteEditorResources(); 305 306 super.doDisposePart(); 307 editorMemento = null; 308 editorState = null; 309 restoredInput = new NullEditorInput(); 310 } 311 312 public IEditorInput getEditorInput() throws PartInitException { 313 if (isDisposed()) { 314 if (!(restoredInput instanceof NullEditorInput)) { 315 restoredInput = new NullEditorInput(); 316 } 317 return restoredInput; 318 } 319 320 IEditorPart part = getEditor(false); 321 if (part != null) { 322 return part.getEditorInput(); 323 } 324 return getRestoredInput(); 325 } 326 327 private IEditorInput getRestoredInput() throws PartInitException { 328 if (restoredInput != null) { 329 return restoredInput; 330 } 331 332 IMemento editorMem = getMemento(); 334 if (editorMem == null) { 335 throw new PartInitException(NLS.bind(WorkbenchMessages.EditorManager_no_persisted_state, getId(), getName())); 336 } 337 IMemento inputMem = editorMem 338 .getChild(IWorkbenchConstants.TAG_INPUT); 339 String factoryID = null; 340 if (inputMem != null) { 341 factoryID = inputMem 342 .getString(IWorkbenchConstants.TAG_FACTORY_ID); 343 } 344 if (factoryID == null) { 345 throw new PartInitException(NLS.bind(WorkbenchMessages.EditorManager_no_input_factory_ID, getId(), getName())); 346 } 347 IAdaptable input = null; 348 String label = null; if (UIStats.isDebugging(UIStats.CREATE_PART_INPUT)) { 350 label = getName() != null ? getName() : factoryID; 351 } 352 try { 353 UIStats.start(UIStats.CREATE_PART_INPUT, label); 354 IElementFactory factory = PlatformUI.getWorkbench() 355 .getElementFactory(factoryID); 356 if (factory == null) { 357 throw new PartInitException(NLS.bind(WorkbenchMessages.EditorManager_bad_element_factory, new Object [] { factoryID, getId(), getName() })); 358 } 359 360 input = factory.createElement(inputMem); 362 if (input == null) { 363 throw new PartInitException(NLS.bind(WorkbenchMessages.EditorManager_create_element_returned_null, new Object [] { factoryID, getId(), getName() })); 364 } 365 } finally { 366 UIStats.end(UIStats.CREATE_PART_INPUT, input, label); 367 } 368 if (!(input instanceof IEditorInput)) { 369 throw new PartInitException(NLS.bind(WorkbenchMessages.EditorManager_wrong_createElement_result, new Object [] { factoryID, getId(), getName() })); 370 } 371 restoredInput = (IEditorInput) input; 372 return restoredInput; 373 } 374 375 381 public ImageDescriptor computeImageDescriptor() { 382 ImageDescriptor descriptor = super.computeImageDescriptor(); 383 if (!isPinned()) { 384 return descriptor; 385 } 386 387 IPreferenceStore prefStore = WorkbenchPlugin.getDefault() 389 .getPreferenceStore(); 390 boolean bUsePin = prefStore 391 .getBoolean(IPreferenceConstants.REUSE_EDITORS_BOOLEAN) 392 || ((TabBehaviour)Tweaklets.get(TabBehaviour.KEY)).alwaysShowPinAction(); 393 394 if (!bUsePin) { 395 return descriptor; 396 } 397 398 ImageDescriptor pinDesc = this.manager.getEditorPinImageDesc(); 399 if (pinDesc == null) { 400 return descriptor; 401 } 402 403 return new OverlayIcon(descriptor, pinDesc, new Point(16, 16)); 404 } 405 406 415 protected IWorkbenchPart createPart() { 416 if (EditorRegistry.EMPTY_EDITOR_ID.equals(getId())) { 417 return getEmptyEditor(getDescriptor()); 418 } 419 PartInitException exception = null; 420 421 IWorkbenchPart result = null; 422 423 try { 426 result = createPartHelper(); 427 } catch (PartInitException e) { 428 exception = e; 429 } 430 431 432 if (exception != null) { 435 436 IStatus originalStatus = exception.getStatus(); 437 IStatus logStatus = StatusUtil.newStatus(originalStatus, 438 NLS.bind("Unable to create editor ID {0}: {1}", getId(), originalStatus.getMessage())); 440 IStatus displayStatus = StatusUtil.newStatus(originalStatus, 441 WorkbenchMessages.EditorManager_unableToCreateEditor); 442 443 StatusManager.getManager().handle(logStatus); 445 StatusManager.getManager().handle(displayStatus, 446 StatusManager.SHOW); 447 448 ErrorEditorPart part = new ErrorEditorPart(displayStatus); 449 450 IEditorInput input; 451 try { 452 input = getEditorInput(); 453 } catch (PartInitException e1) { 454 input = new NullEditorInput(); 455 } 456 457 EditorPane pane = (EditorPane)getPane(); 458 459 pane.createControl((Composite) manager.page.getEditorPresentation().getLayoutPart().getControl()); 460 461 EditorDescriptor descr = getDescriptor(); 462 463 EditorSite site = new EditorSite(this, part, manager.page, descr); 464 465 site.setActionBars(new EditorActionBars(manager.page, site.getWorkbenchWindow(), getId())); 466 467 part.init(site, input); 468 469 Composite parent = (Composite)pane.getControl(); 470 Composite content = new Composite(parent, SWT.NONE); 471 content.setLayout(new FillLayout()); 472 473 try { 474 part.createPartControl(content); 475 } catch (Exception e) { 476 content.dispose(); 477 StatusUtil.handleStatus(e, StatusManager.SHOW 478 | StatusManager.LOG); 479 return null; 480 } 481 482 result = part; 483 } 484 485 return result; 486 } 487 488 protected void partPropertyChanged(Object source, int propId) { 489 490 if (propId == IWorkbenchPartConstants.PROP_INPUT) { 494 expectingInputChange = false; 495 } 496 497 super.partPropertyChanged(source, propId); 498 } 499 500 510 public boolean setInput(IEditorInput input) { 511 512 if (part != null) { 513 if (part instanceof IReusableEditor) { 514 IReusableEditor editor = (IReusableEditor) part; 515 516 expectingInputChange = true; 517 518 editor.setInput(input); 519 520 if (expectingInputChange) { 527 528 reportMalfunction("Editor is not firing a PROP_INPUT event in response to IReusableEditor.setInput(...)"); 531 firePropertyChange(IWorkbenchPartConstants.PROP_INPUT); 534 } 535 536 return editor.getEditorInput() == input; 537 538 } else { 539 return false; 541 } 542 } else { 543 if (input != restoredInput) { 545 restoredInput = input; 546 547 firePropertyChange(IWorkbenchPartConstants.PROP_INPUT); 548 } 549 } 550 551 return true; 552 } 553 554 563 private void reportMalfunction(String string) { 564 if (!reportedMalfunctioningEditor) { 565 reportedMalfunctioningEditor = true; 566 567 String errorMessage = "Problem detected with part " + getId(); if (part != null) { 569 errorMessage += " (class = " + part.getClass().getName() + ")"; } 571 572 errorMessage += ": " + string; 574 StatusManager.getManager().handle( 575 StatusUtil.newStatus(getDescriptor().getPluginId(), 576 errorMessage, null)); 577 } 578 } 579 580 private IEditorPart createPartHelper() throws PartInitException { 581 582 Composite content = null; 586 IEditorPart part = null; 587 EditorActionBars actionBars = null; 588 EditorSite site = null; 589 590 try { 591 IEditorInput editorInput = getEditorInput(); 592 593 String editorID = getId(); 595 EditorDescriptor desc = getDescriptor(); 596 597 if (desc == null) { 598 throw new PartInitException(NLS.bind(WorkbenchMessages.EditorManager_missing_editor_descriptor, editorID)); 599 } 600 601 602 if (desc.isInternal()) { 603 try { 605 UIStats.start(UIStats.CREATE_PART, editorID); 606 part = manager.createPart(desc); 607 608 if (part != null && part instanceof MultiEditor) { 609 multiEditorChildren = manager.openMultiEditor(this, 610 (MultiEditor) part, (MultiEditorInput) editorInput); 611 } 612 if (part instanceof IWorkbenchPart3) { 613 createPartProperties((IWorkbenchPart3)part); 614 } 615 } finally { 616 UIStats.end(UIStats.CREATE_PART, this, editorID); 617 } 618 619 } else if (desc.getId().equals( 620 IEditorRegistry.SYSTEM_INPLACE_EDITOR_ID)) { 621 622 part = ComponentSupport.getSystemInPlaceEditor(); 623 624 if (part == null) { 625 throw new PartInitException(WorkbenchMessages.EditorManager_no_in_place_support); 626 } 627 } else { 628 throw new PartInitException(NLS.bind(WorkbenchMessages.EditorManager_invalid_editor_descriptor, editorID)); 629 } 630 PartPane pane = getPane(); 632 633 pane.createControl((Composite) manager.page.getEditorPresentation().getLayoutPart().getControl()); 634 635 int style = SWT.NONE; 637 if(part instanceof IWorkbenchPartOrientation){ 638 style = ((IWorkbenchPartOrientation) part).getOrientation(); 639 } 640 641 site = manager.createSite(this, part, desc, editorInput); 644 645 if (part instanceof IPersistableEditor && editorState != null) { 647 ((IPersistableEditor) part).restoreState(editorState); 648 } 649 650 actionBars = (EditorActionBars) site.getActionBars(); 653 654 Composite parent = (Composite)pane.getControl(); 655 content = new Composite(parent, style); 656 657 content.setLayout(new FillLayout()); 658 659 try { 660 UIStats.start(UIStats.CREATE_PART_CONTROL, editorID); 661 part.createPartControl(content); 662 663 parent.layout(true); 664 } finally { 665 UIStats.end(UIStats.CREATE_PART_CONTROL, part, editorID); 666 } 667 668 PartTester.testEditor(part); 672 673 return part; 674 675 } catch (Exception e) { 676 if (content != null) { 678 try { 679 content.dispose(); 680 } catch (RuntimeException re) { 681 StatusManager.getManager().handle( 682 StatusUtil.newStatus(WorkbenchPlugin.PI_WORKBENCH, 683 re)); 684 } 685 } 686 687 if (part != null) { 688 try { 689 part.dispose(); 690 } catch (RuntimeException re) { 691 StatusManager.getManager().handle( 692 StatusUtil.newStatus(WorkbenchPlugin.PI_WORKBENCH, 693 re)); 694 } 695 } 696 697 if (actionBars != null) { 698 try { 699 manager.disposeEditorActionBars(actionBars); 700 } catch (RuntimeException re) { 701 StatusManager.getManager().handle( 702 StatusUtil.newStatus(WorkbenchPlugin.PI_WORKBENCH, 703 re)); 704 } 705 } 706 707 if (site != null) { 708 try { 709 site.dispose(); 710 } catch (RuntimeException re) { 711 StatusManager.getManager().handle( 712 StatusUtil.newStatus(WorkbenchPlugin.PI_WORKBENCH, 713 re)); 714 } 715 } 716 717 throw new PartInitException(StatusUtil.getLocalizedMessage(e), StatusUtil.getCause(e)); 718 } 719 720 } 721 722 730 public boolean isMultiReference() { 731 return multiEditorChildren!=null || restoredInput instanceof MultiEditorInput; 732 } 733 734 738 public IEditorPart getEmptyEditor(EditorDescriptor descr) { 739 ErrorEditorPart part = new ErrorEditorPart(); 740 741 IEditorInput input; 742 try { 743 input = getEditorInput(); 744 } catch (PartInitException e1) { 745 input = new NullEditorInput(); 746 } 747 748 EditorPane pane = (EditorPane)getPane(); 749 750 pane.createControl((Composite) manager.page.getEditorPresentation().getLayoutPart().getControl()); 751 752 EditorSite site = new EditorSite(this, part, manager.page, descr); 753 754 site.setActionBars(new EditorActionBars(manager.page, site.getWorkbenchWindow(), getId())); 755 756 part.init(site, input); 757 758 Composite parent = (Composite)pane.getControl(); 759 Composite content = new Composite(parent, SWT.NONE); 760 content.setLayout(new FillLayout()); 761 762 try { 763 part.createPartControl(content); 764 } catch (Exception e) { 765 content.dispose(); 766 StatusManager.getManager().handle( 767 StatusUtil.newStatus(WorkbenchPlugin.PI_WORKBENCH, e)); 768 return null; 769 } 770 771 this.part = part; 772 776 part.setPartName("(Empty)"); refreshFromPart(); 778 releaseReferences(); 779 780 if (((WorkbenchPage)getPage()).getActiveEditorReference()!=this) { 781 fireInternalPropertyChange(INTERNAL_PROPERTY_OPENED); 782 } 783 784 return part; 785 } 786 } 787 788 | Popular Tags |