1 11 package org.eclipse.ui.part; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.core.commands.util.Tracing; 18 import org.eclipse.core.runtime.Assert; 19 import org.eclipse.core.runtime.ISafeRunnable; 20 import org.eclipse.core.runtime.SafeRunner; 21 import org.eclipse.jface.viewers.ISelectionProvider; 22 import org.eclipse.jface.viewers.SelectionChangedEvent; 23 import org.eclipse.swt.SWT; 24 import org.eclipse.swt.custom.CTabFolder; 25 import org.eclipse.swt.custom.CTabItem; 26 import org.eclipse.swt.events.SelectionAdapter; 27 import org.eclipse.swt.events.SelectionEvent; 28 import org.eclipse.swt.graphics.Image; 29 import org.eclipse.swt.layout.FillLayout; 30 import org.eclipse.swt.widgets.Composite; 31 import org.eclipse.swt.widgets.Control; 32 import org.eclipse.swt.widgets.Display; 33 import org.eclipse.swt.widgets.Item; 34 import org.eclipse.ui.IEditorActionBarContributor; 35 import org.eclipse.ui.IEditorInput; 36 import org.eclipse.ui.IEditorPart; 37 import org.eclipse.ui.IEditorSite; 38 import org.eclipse.ui.IKeyBindingService; 39 import org.eclipse.ui.INestableKeyBindingService; 40 import org.eclipse.ui.IPropertyListener; 41 import org.eclipse.ui.IWorkbenchPart; 42 import org.eclipse.ui.PartInitException; 43 import org.eclipse.ui.internal.WorkbenchPlugin; 44 import org.eclipse.ui.internal.misc.Policy; 45 import org.eclipse.ui.internal.services.INestable; 46 import org.eclipse.ui.internal.util.Util; 47 import org.eclipse.ui.services.IServiceLocator; 48 49 74 public abstract class MultiPageEditorPart extends EditorPart { 75 76 79 private static final String TRACING_COMPONENT = "MPE"; 81 85 private INestable activeServiceLocator; 86 87 90 private CTabFolder container; 91 92 98 private ArrayList nestedEditors = new ArrayList (3); 99 100 103 protected MultiPageEditorPart() { 104 super(); 105 } 106 107 118 public int addPage(Control control) { 119 int index = getPageCount(); 120 addPage(index, control); 121 return index; 122 } 123 124 137 public void addPage(int index, Control control) { 138 createItem(index, control); 139 } 140 141 157 public int addPage(IEditorPart editor, IEditorInput input) 158 throws PartInitException { 159 int index = getPageCount(); 160 addPage(index, editor, input); 161 return index; 162 } 163 164 181 public void addPage(int index, IEditorPart editor, IEditorInput input) 182 throws PartInitException { 183 IEditorSite site = createSite(editor); 184 editor.init(site, input); 187 Composite parent2 = new Composite(getContainer(), 188 getOrientation(editor)); 189 parent2.setLayout(new FillLayout()); 190 editor.createPartControl(parent2); 191 editor.addPropertyListener(new IPropertyListener() { 192 public void propertyChanged(Object source, int propertyId) { 193 MultiPageEditorPart.this.handlePropertyChange(propertyId); 194 } 195 }); 196 Item item = createItem(index, parent2); 198 item.setData(editor); 201 nestedEditors.add(editor); 202 } 203 204 213 private int getOrientation(IEditorPart editor) { 214 if (editor instanceof IWorkbenchPartOrientation) { 215 return ((IWorkbenchPartOrientation) editor).getOrientation(); 216 } 217 return getOrientation(); 218 } 219 220 230 private CTabFolder createContainer(Composite parent) { 231 parent.setLayout(new FillLayout()); 234 final CTabFolder newContainer = new CTabFolder(parent, SWT.BOTTOM 235 | SWT.FLAT); 236 newContainer.addSelectionListener(new SelectionAdapter() { 237 public void widgetSelected(SelectionEvent e) { 238 int newPageIndex = newContainer.indexOf((CTabItem) e.item); 239 pageChange(newPageIndex); 240 } 241 }); 242 return newContainer; 243 } 244 245 255 private CTabItem createItem(int index, Control control) { 256 CTabItem item = new CTabItem(getTabFolder(), SWT.NONE, index); 257 item.setControl(control); 258 return item; 259 } 260 261 267 protected abstract void createPages(); 268 269 280 public final void createPartControl(Composite parent) { 281 Composite pageContainer = createPageContainer(parent); 282 this.container = createContainer(pageContainer); 283 createPages(); 284 if (getActivePage() == -1) { 287 setActivePage(0); 288 IEditorPart part = getEditor(0); 289 if (part!=null) { 290 final IServiceLocator serviceLocator = part.getEditorSite(); 291 if (serviceLocator instanceof INestable) { 292 activeServiceLocator = (INestable) serviceLocator; 293 activeServiceLocator.activate(); 294 } 295 } 296 } 297 } 298 299 314 protected Composite createPageContainer(Composite parent) { 315 return parent; 316 } 317 318 328 protected IEditorSite createSite(IEditorPart editor) { 329 return new MultiPageEditorSite(this, editor); 330 } 331 332 337 public void dispose() { 338 for (int i = 0; i < nestedEditors.size(); ++i) { 339 IEditorPart editor = (IEditorPart) nestedEditors.get(i); 340 disposePart(editor); 341 } 342 nestedEditors.clear(); 343 } 344 345 353 protected IEditorPart getActiveEditor() { 354 int index = getActivePage(); 355 if (index != -1) { 356 return getEditor(index); 357 } 358 return null; 359 } 360 361 370 protected int getActivePage() { 371 CTabFolder tabFolder = getTabFolder(); 372 if (tabFolder != null && !tabFolder.isDisposed()) { 373 return tabFolder.getSelectionIndex(); 374 } 375 return -1; 376 } 377 378 396 protected Composite getContainer() { 397 return container; 398 } 399 400 412 protected Control getControl(int pageIndex) { 413 return getItem(pageIndex).getControl(); 414 } 415 416 426 protected IEditorPart getEditor(int pageIndex) { 427 Item item = getItem(pageIndex); 428 if (item != null) { 429 Object data = item.getData(); 430 if (data instanceof IEditorPart) { 431 return (IEditorPart) data; 432 } 433 } 434 return null; 435 } 436 437 445 private CTabItem getItem(int pageIndex) { 446 return getTabFolder().getItem(pageIndex); 447 } 448 449 454 protected int getPageCount() { 455 CTabFolder folder = getTabFolder(); 456 if (folder != null && !folder.isDisposed()) { 458 return folder.getItemCount(); 459 } 460 return 0; 461 } 462 463 471 protected Image getPageImage(int pageIndex) { 472 return getItem(pageIndex).getImage(); 473 } 474 475 484 protected String getPageText(int pageIndex) { 485 return getItem(pageIndex).getText(); 486 } 487 488 494 private CTabFolder getTabFolder() { 495 return container; 496 } 497 498 514 protected void handlePropertyChange(int propertyId) { 515 firePropertyChange(propertyId); 516 } 517 518 534 public void init(IEditorSite site, IEditorInput input) 535 throws PartInitException { 536 setSite(site); 537 setInput(input); 538 site.setSelectionProvider(new MultiPageSelectionProvider(this)); 539 } 540 541 553 public boolean isDirty() { 554 for (Iterator i = nestedEditors.iterator(); i.hasNext();) { 556 IEditorPart editor = (IEditorPart) i.next(); 557 if (editor.isDirty()) { 558 return true; 559 } 560 } 561 return false; 562 } 563 564 582 protected void pageChange(int newPageIndex) { 583 if (activeServiceLocator != null) { 585 activeServiceLocator.deactivate(); 586 activeServiceLocator = null; 587 } 588 589 setFocus(); 590 IEditorPart activeEditor = getEditor(newPageIndex); 591 IEditorActionBarContributor contributor = getEditorSite() 592 .getActionBarContributor(); 593 if (contributor != null 594 && contributor instanceof MultiPageEditorActionBarContributor) { 595 ((MultiPageEditorActionBarContributor) contributor) 596 .setActivePage(activeEditor); 597 } 598 if (activeEditor != null) { 599 600 final IServiceLocator serviceLocator = activeEditor.getEditorSite(); 602 if (serviceLocator instanceof INestable) { 603 activeServiceLocator = (INestable) serviceLocator; 604 activeServiceLocator.activate(); 605 } 606 607 ISelectionProvider selectionProvider = activeEditor.getSite() 608 .getSelectionProvider(); 609 if (selectionProvider != null) { 610 ISelectionProvider outerProvider = getSite() 611 .getSelectionProvider(); 612 if (outerProvider instanceof MultiPageSelectionProvider) { 613 SelectionChangedEvent event = new SelectionChangedEvent( 614 selectionProvider, selectionProvider.getSelection()); 615 616 MultiPageSelectionProvider provider = (MultiPageSelectionProvider) outerProvider; 617 provider.fireSelectionChanged(event); 618 provider.firePostSelectionChanged(event); 619 } else { 620 if (Policy.DEBUG_MPE) { 621 Tracing.printTrace(TRACING_COMPONENT, 622 "MultiPageEditorPart " + getTitle() + " did not propogate selection for " + activeEditor.getTitle()); 625 } 626 } 627 } 628 } 629 } 630 631 637 private void disposePart(final IWorkbenchPart part) { 638 SafeRunner.run(new ISafeRunnable() { 639 public void run() { 640 if (part.getSite() instanceof MultiPageEditorSite) { 641 MultiPageEditorSite partSite = (MultiPageEditorSite) part 642 .getSite(); 643 partSite.dispose(); 644 } 645 part.dispose(); 646 } 647 648 public void handleException(Throwable e) { 649 } 651 }); 652 } 653 654 664 public void removePage(int pageIndex) { 665 Assert.isTrue(pageIndex >= 0 && pageIndex < getPageCount()); 666 IEditorPart editor = getEditor(pageIndex); 668 669 CTabItem item = getItem(pageIndex); 671 Control pageControl = item.getControl(); 672 673 item.dispose(); 676 677 if (pageControl != null) { 678 pageControl.dispose(); 679 } 680 681 if (editor != null) { 683 nestedEditors.remove(editor); 684 disposePart(editor); 685 } 686 } 687 688 694 protected void setActivePage(int pageIndex) { 695 Assert.isTrue(pageIndex >= 0 && pageIndex < getPageCount()); 696 getTabFolder().setSelection(pageIndex); 697 pageChange(pageIndex); 698 } 699 700 709 protected void setControl(int pageIndex, Control control) { 710 getItem(pageIndex).setControl(control); 711 } 712 713 721 public void setFocus() { 722 setFocus(getActivePage()); 723 } 724 725 733 private void setFocus(int pageIndex) { 734 final IKeyBindingService service = getSite().getKeyBindingService(); 735 if (pageIndex < 0 || pageIndex >= getPageCount()) { 736 if (service instanceof INestableKeyBindingService) { 738 final INestableKeyBindingService nestableService = (INestableKeyBindingService) service; 739 nestableService.activateKeyBindingService(null); 740 } else { 741 WorkbenchPlugin 742 .log("MultiPageEditorPart.setFocus() Parent key binding service was not an instance of INestableKeyBindingService. It was an instance of " + service.getClass().getName() + " instead."); } 744 return; 745 } 746 747 final IEditorPart editor = getEditor(pageIndex); 748 if (editor != null) { 749 editor.setFocus(); 750 if (service instanceof INestableKeyBindingService) { 752 final INestableKeyBindingService nestableService = (INestableKeyBindingService) service; 753 if (editor != null) { 754 nestableService.activateKeyBindingService(editor 755 .getEditorSite()); 756 } else { 757 nestableService.activateKeyBindingService(null); 758 } 759 } else { 760 WorkbenchPlugin 761 .log("MultiPageEditorPart.setFocus() Parent key binding service was not an instance of INestableKeyBindingService. It was an instance of " + service.getClass().getName() + " instead."); } 763 764 } else { 765 if (service instanceof INestableKeyBindingService) { 767 final INestableKeyBindingService nestableService = (INestableKeyBindingService) service; 768 nestableService.activateKeyBindingService(null); 769 } else { 770 WorkbenchPlugin 771 .log("MultiPageEditorPart.setFocus() Parent key binding service was not an instance of INestableKeyBindingService. It was an instance of " + service.getClass().getName() + " instead."); } 773 774 final Control control = getControl(pageIndex); 776 if (control != null) { 777 control.setFocus(); 778 } 779 } 780 } 781 782 791 protected void setPageImage(int pageIndex, Image image) { 792 getItem(pageIndex).setImage(image); 793 } 794 795 804 protected void setPageText(int pageIndex, String text) { 805 getItem(pageIndex).setText(text); 806 } 807 808 815 public Object getAdapter(Class adapter) { 816 Object result = super.getAdapter(adapter); 817 if (result == null && Display.getCurrent()!=null) { 820 IEditorPart innerEditor = getActiveEditor(); 821 if (innerEditor != null && innerEditor != this) { 823 result = Util.getAdapter(innerEditor, adapter); 824 } 825 } 826 return result; 827 } 828 829 837 public final IEditorPart[] findEditors(IEditorInput input) { 838 List result = new ArrayList (); 839 int count = getPageCount(); 840 for (int i = 0; i < count; i++) { 841 IEditorPart editor = getEditor(i); 842 if (editor != null 843 && editor.getEditorInput() != null 844 && editor.getEditorInput().equals(input)) { 845 result.add(editor); 846 } 847 } 848 return (IEditorPart[]) result.toArray(new IEditorPart[result.size()]); 849 } 850 851 859 public final void setActiveEditor(IEditorPart editorPart) { 860 int count = getPageCount(); 861 for (int i = 0; i < count; i++) { 862 IEditorPart editor = getEditor(i); 863 if (editor == editorPart) { 864 setActivePage(i); 865 break; 866 } 867 } 868 } 869 } 870 | Popular Tags |