1 7 package Olj.presentation; 8 9 10 import org.eclipse.emf.common.command.BasicCommandStack; 11 import org.eclipse.emf.common.command.Command; 12 import org.eclipse.emf.common.command.CommandStack; 13 import org.eclipse.emf.common.command.CommandStackListener; 14 15 import org.eclipse.emf.common.notify.AdapterFactory; 16 17 import org.eclipse.emf.common.ui.ViewerPane; 18 19 import org.eclipse.emf.common.ui.viewer.IViewerProvider; 20 21 import org.eclipse.emf.common.util.URI; 22 23 import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain; 24 import org.eclipse.emf.edit.domain.EditingDomain; 25 import org.eclipse.emf.edit.domain.IEditingDomainProvider; 26 27 import org.eclipse.emf.edit.provider.AdapterFactoryItemDelegator; 28 import org.eclipse.emf.edit.provider.ComposedAdapterFactory; 29 import org.eclipse.emf.edit.provider.ReflectiveItemProviderAdapterFactory; 30 31 import org.eclipse.emf.edit.provider.resource.ResourceItemProviderAdapterFactory; 32 33 import org.eclipse.emf.edit.ui.action.EditingDomainActionBarContributor; 34 35 import org.eclipse.emf.edit.ui.celleditor.AdapterFactoryTreeEditor; 36 37 import org.eclipse.emf.edit.ui.dnd.EditingDomainViewerDropAdapter; 38 import org.eclipse.emf.edit.ui.dnd.LocalTransfer; 39 import org.eclipse.emf.edit.ui.dnd.ViewerDragAdapter; 40 41 import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider; 42 import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider; 43 44 import org.eclipse.emf.ecore.EObject; 45 import org.eclipse.emf.ecore.EValidator; 46 47 import org.eclipse.emf.ecore.resource.Resource; 48 import org.eclipse.emf.ecore.resource.ResourceSet; 49 50 import java.io.IOException ; 51 52 import java.util.ArrayList ; 53 import java.util.Collection ; 54 import java.util.Collections ; 55 import java.util.EventObject ; 56 import java.util.Iterator ; 57 import java.util.List ; 58 59 import org.eclipse.core.resources.IFile; 60 import org.eclipse.core.resources.IMarker; 61 import org.eclipse.core.resources.IResource; 62 import org.eclipse.core.resources.IResourceChangeListener; 63 import org.eclipse.core.resources.IResourceChangeEvent; 64 import org.eclipse.core.resources.IResourceDelta; 65 import org.eclipse.core.resources.IResourceDeltaVisitor; 66 import org.eclipse.core.resources.ResourcesPlugin; 67 68 import org.eclipse.core.runtime.CoreException; 69 import org.eclipse.core.runtime.IPath; 70 import org.eclipse.core.runtime.IProgressMonitor; 71 72 import org.eclipse.jface.action.IMenuListener; 73 import org.eclipse.jface.action.IMenuManager; 74 import org.eclipse.jface.action.IStatusLineManager; 75 import org.eclipse.jface.action.IToolBarManager; 76 import org.eclipse.jface.action.MenuManager; 77 import org.eclipse.jface.action.Separator; 78 79 import org.eclipse.jface.dialogs.MessageDialog; 80 import org.eclipse.jface.dialogs.ProgressMonitorDialog; 81 82 import org.eclipse.jface.viewers.ColumnWeightData; 83 import org.eclipse.jface.viewers.ISelection; 84 import org.eclipse.jface.viewers.ISelectionChangedListener; 85 import org.eclipse.jface.viewers.ISelectionProvider; 86 import org.eclipse.jface.viewers.IStructuredSelection; 87 import org.eclipse.jface.viewers.ListViewer; 88 import org.eclipse.jface.viewers.SelectionChangedEvent; 89 import org.eclipse.jface.viewers.StructuredSelection; 90 import org.eclipse.jface.viewers.StructuredViewer; 91 import org.eclipse.jface.viewers.TableLayout; 92 import org.eclipse.jface.viewers.TableTreeViewer; 93 import org.eclipse.jface.viewers.TableViewer; 94 import org.eclipse.jface.viewers.TreeViewer; 95 import org.eclipse.jface.viewers.Viewer; 96 97 import org.eclipse.swt.SWT; 98 99 import org.eclipse.swt.custom.TableTree; 100 import org.eclipse.swt.custom.CTabFolder; 101 102 import org.eclipse.swt.dnd.DND; 103 import org.eclipse.swt.dnd.Transfer; 104 105 import org.eclipse.swt.events.ControlAdapter; 106 import org.eclipse.swt.events.ControlEvent; 107 108 import org.eclipse.swt.graphics.Point; 109 110 import org.eclipse.swt.widgets.Composite; 111 import org.eclipse.swt.widgets.Control; 112 import org.eclipse.swt.widgets.Menu; 113 import org.eclipse.swt.widgets.Table; 114 import org.eclipse.swt.widgets.TableColumn; 115 import org.eclipse.swt.widgets.Tree; 116 117 import org.eclipse.ui.IActionBars; 118 import org.eclipse.ui.IEditorInput; 119 import org.eclipse.ui.IEditorPart; 120 import org.eclipse.ui.IEditorSite; 121 import org.eclipse.ui.IFileEditorInput; 122 import org.eclipse.ui.IPartListener; 123 import org.eclipse.ui.IWorkbenchPart; 124 import org.eclipse.ui.PartInitException; 125 126 import org.eclipse.ui.dialogs.SaveAsDialog; 127 128 import org.eclipse.ui.ide.IGotoMarker; 129 130 import org.eclipse.ui.part.FileEditorInput; 131 import org.eclipse.ui.part.MultiPageEditorPart; 132 133 import org.eclipse.ui.views.contentoutline.ContentOutline; 134 import org.eclipse.ui.views.contentoutline.ContentOutlinePage; 135 import org.eclipse.ui.views.contentoutline.IContentOutlinePage; 136 137 import org.eclipse.ui.views.properties.IPropertySheetPage; 138 import org.eclipse.ui.views.properties.PropertySheet; 139 import org.eclipse.ui.views.properties.PropertySheetPage; 140 141 import Olj.provider.OljItemProviderAdapterFactory; 142 143 144 import java.util.HashMap ; 145 146 import org.eclipse.core.runtime.NullProgressMonitor; 147 148 import org.eclipse.ui.actions.WorkspaceModifyOperation; 149 150 151 157 public class OljEditor 158 extends MultiPageEditorPart 159 implements IEditingDomainProvider, ISelectionProvider, IMenuListener, IViewerProvider, IGotoMarker { 160 166 protected AdapterFactoryEditingDomain editingDomain; 167 168 174 protected ComposedAdapterFactory adapterFactory; 175 176 182 protected IContentOutlinePage contentOutlinePage; 183 184 190 protected IStatusLineManager contentOutlineStatusLineManager; 191 192 198 protected TreeViewer contentOutlineViewer; 199 200 206 protected PropertySheetPage propertySheetPage; 207 208 215 protected TreeViewer selectionViewer; 216 217 223 protected TreeViewer parentViewer; 224 225 231 protected TreeViewer treeViewer; 232 233 240 protected ListViewer listViewer; 241 242 249 protected TableViewer tableViewer; 250 251 258 protected TableTreeViewer tableTreeViewer; 259 260 266 protected ViewerPane currentViewerPane; 267 268 274 protected Viewer currentViewer; 275 276 282 protected ISelectionChangedListener selectionChangedListener; 283 284 290 protected Collection selectionChangedListeners = new ArrayList (); 291 292 298 protected ISelection editorSelection = StructuredSelection.EMPTY; 299 300 306 protected IPartListener partListener = 307 new IPartListener() { 308 public void partActivated(IWorkbenchPart p) { 309 if (p instanceof ContentOutline) { 310 if (((ContentOutline)p).getCurrentPage() == contentOutlinePage) { 311 getActionBarContributor().setActiveEditor(OljEditor.this); 312 313 setCurrentViewer(contentOutlineViewer); 314 } 315 } 316 else if (p instanceof PropertySheet) { 317 if (((PropertySheet)p).getCurrentPage() == propertySheetPage) { 318 getActionBarContributor().setActiveEditor(OljEditor.this); 319 handleActivate(); 320 } 321 } 322 else if (p == OljEditor.this) { 323 handleActivate(); 324 } 325 } 326 public void partBroughtToTop(IWorkbenchPart p) { 327 } 328 public void partClosed(IWorkbenchPart p) { 329 } 330 public void partDeactivated(IWorkbenchPart p) { 331 } 332 public void partOpened(IWorkbenchPart p) { 333 } 334 }; 335 336 340 Collection removedResources = new ArrayList (); 341 342 346 Collection changedResources = new ArrayList (); 347 348 352 Collection savedResources = new ArrayList (); 353 354 360 protected IResourceChangeListener resourceChangeListener = 361 new IResourceChangeListener() { 362 public void resourceChanged(IResourceChangeEvent event) { 363 ResourceSet resourceSet = editingDomain.getResourceSet(); 364 { 367 IResourceDelta delta = event.getDelta(); 368 try { 369 class ResourceDeltaVisitor implements IResourceDeltaVisitor { 370 protected ResourceSet resourceSet = editingDomain.getResourceSet(); 371 protected Collection changedResources = new ArrayList (); 372 protected Collection removedResources = new ArrayList (); 373 374 public boolean visit(IResourceDelta delta) { 375 if (delta.getFlags() != IResourceDelta.MARKERS && 376 delta.getResource().getType() == IResource.FILE) { 377 if ((delta.getKind() & (IResourceDelta.CHANGED | IResourceDelta.REMOVED)) != 0) { 378 Resource resource = resourceSet.getResource(URI.createURI(delta.getFullPath().toString()), false); 379 if (resource != null) { 380 if ((delta.getKind() & IResourceDelta.REMOVED) != 0) { 381 removedResources.add(resource); 382 } 383 else { 384 changedResources.add(resource); 385 } 386 } 387 } 388 } 389 390 return true; 391 } 392 393 public Collection getChangedResources() { 394 return changedResources; 395 } 396 397 public Collection getRemovedResources() { 398 return removedResources; 399 } 400 } 401 402 ResourceDeltaVisitor visitor = new ResourceDeltaVisitor(); 403 delta.accept(visitor); 404 405 removedResources.addAll(visitor.getRemovedResources()); 406 if (!visitor.getRemovedResources().isEmpty() && !isDirty()) { 407 getSite().getShell().getDisplay().asyncExec 408 (new Runnable () { 409 public void run() { 410 getSite().getPage().closeEditor(OljEditor.this, false); 411 OljEditor.this.dispose(); 412 } 413 }); 414 } 415 416 changedResources.addAll(visitor.getChangedResources()); 417 } 418 catch (CoreException exception) { 419 OljEditorPlugin.INSTANCE.log(exception); 420 } 421 } 422 } 423 }; 424 425 429 protected void handleActivate() { 430 if (editingDomain.getResourceToReadOnlyMap() != null) { 433 editingDomain.getResourceToReadOnlyMap().clear(); 434 435 setSelection(getSelection()); 438 } 439 440 if (!removedResources.isEmpty()) { 441 if (handleDirtyConflict()) { 442 getSite().getPage().closeEditor(OljEditor.this, false); 443 OljEditor.this.dispose(); 444 } 445 else { 446 removedResources.clear(); 447 changedResources.clear(); 448 savedResources.clear(); 449 } 450 } 451 else if (!changedResources.isEmpty()) { 452 changedResources.removeAll(savedResources); 453 handleChangedResources(); 454 changedResources.clear(); 455 savedResources.clear(); 456 } 457 } 458 459 460 464 protected void handleChangedResources() { 465 if (!changedResources.isEmpty() && (!isDirty() || handleDirtyConflict())) { 466 editingDomain.getCommandStack().flush(); 467 468 for (Iterator i = changedResources.iterator(); i.hasNext(); ) { 469 Resource resource = (Resource)i.next(); 470 if (resource.isLoaded()) { 471 resource.unload(); 472 try { 473 resource.load(Collections.EMPTY_MAP); 474 } 475 catch (IOException exception) { 476 OljEditorPlugin.INSTANCE.log(exception); 477 } 478 } 479 } 480 } 481 } 482 483 487 protected boolean handleDirtyConflict() { 488 return 489 MessageDialog.openQuestion 490 (getSite().getShell(), 491 getString("_UI_FileConflict_label"), 492 getString("_WARN_FileConflict")); 493 } 494 495 501 public OljEditor() { 502 super(); 503 504 List factories = new ArrayList (); 507 factories.add(new ResourceItemProviderAdapterFactory()); 508 factories.add(new OljItemProviderAdapterFactory()); 509 factories.add(new ReflectiveItemProviderAdapterFactory()); 510 511 adapterFactory = new ComposedAdapterFactory(factories); 512 513 BasicCommandStack commandStack = new BasicCommandStack(); 516 517 commandStack.addCommandStackListener 520 (new CommandStackListener() { 521 public void commandStackChanged(final EventObject event) { 522 getContainer().getDisplay().asyncExec 523 (new Runnable () { 524 public void run() { 525 firePropertyChange(IEditorPart.PROP_DIRTY); 526 527 Command mostRecentCommand = ((CommandStack)event.getSource()).getMostRecentCommand(); 530 if (mostRecentCommand != null) { 531 setSelectionToViewer(mostRecentCommand.getAffectedObjects()); 532 } 533 if (propertySheetPage != null) { 534 propertySheetPage.refresh(); 535 } 536 } 537 }); 538 } 539 }); 540 541 editingDomain = new AdapterFactoryEditingDomain(adapterFactory, commandStack, new HashMap ()); 544 } 545 546 552 protected void firePropertyChange(int action) { 553 super.firePropertyChange(action); 554 } 555 556 562 public void setSelectionToViewer(Collection collection) { 563 final Collection theSelection = collection; 564 if (theSelection != null && !theSelection.isEmpty()) { 567 Runnable runnable = 573 new Runnable () { 574 public void run() { 575 if (currentViewer != null) { 578 currentViewer.setSelection(new StructuredSelection(theSelection.toArray()), true); 579 } 580 } 581 }; 582 runnable.run(); 583 } 584 } 585 586 594 public EditingDomain getEditingDomain() { 595 return editingDomain; 596 } 597 598 603 public class ReverseAdapterFactoryContentProvider extends AdapterFactoryContentProvider { 604 public ReverseAdapterFactoryContentProvider(AdapterFactory adapterFactory) { 605 super(adapterFactory); 606 } 607 608 public Object [] getElements(Object object) { 609 Object parent = super.getParent(object); 610 return (parent == null ? Collections.EMPTY_SET : Collections.singleton(parent)).toArray(); 611 } 612 613 public Object [] getChildren(Object object) { 614 Object parent = super.getParent(object); 615 return (parent == null ? Collections.EMPTY_SET : Collections.singleton(parent)).toArray(); 616 } 617 618 public boolean hasChildren(Object object) { 619 Object parent = super.getParent(object); 620 return parent != null; 621 } 622 623 public Object getParent(Object object) { 624 return null; 625 } 626 } 627 628 633 public void setCurrentViewerPane(ViewerPane viewerPane) { 634 if (currentViewerPane != viewerPane) { 635 if (currentViewerPane != null) { 636 currentViewerPane.showFocus(false); 637 } 638 currentViewerPane = viewerPane; 639 } 640 setCurrentViewer(currentViewerPane.getViewer()); 641 } 642 643 650 public void setCurrentViewer(Viewer viewer) { 651 if (currentViewer != viewer) { 654 if (selectionChangedListener == null) { 655 selectionChangedListener = 658 new ISelectionChangedListener() { 659 public void selectionChanged(SelectionChangedEvent selectionChangedEvent) { 662 setSelection(selectionChangedEvent.getSelection()); 663 } 664 }; 665 } 666 667 if (currentViewer != null) { 670 currentViewer.removeSelectionChangedListener(selectionChangedListener); 671 } 672 673 if (viewer != null) { 676 viewer.addSelectionChangedListener(selectionChangedListener); 677 } 678 679 currentViewer = viewer; 682 683 setSelection(currentViewer == null ? StructuredSelection.EMPTY : currentViewer.getSelection()); 686 } 687 } 688 689 695 public Viewer getViewer() { 696 return currentViewer; 697 } 698 699 705 protected void createContextMenuFor(StructuredViewer viewer) { 706 MenuManager contextMenu = new MenuManager("#PopUp"); 707 contextMenu.add(new Separator("additions")); 708 contextMenu.setRemoveAllWhenShown(true); 709 contextMenu.addMenuListener(this); 710 Menu menu= contextMenu.createContextMenu(viewer.getControl()); 711 viewer.getControl().setMenu(menu); 712 getSite().registerContextMenu(contextMenu, viewer); 713 714 int dndOperations = DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_LINK; 715 Transfer[] transfers = new Transfer[] { LocalTransfer.getInstance() }; 716 viewer.addDragSupport(dndOperations, transfers, new ViewerDragAdapter(viewer)); 717 viewer.addDropSupport(dndOperations, transfers, new EditingDomainViewerDropAdapter(editingDomain, viewer)); 718 } 719 720 726 public void createModel() { 727 IFileEditorInput modelFile = (IFileEditorInput)getEditorInput(); 730 731 try { 732 Resource resource = 735 editingDomain.loadResource 736 (URI.createPlatformResourceURI(modelFile.getFile().getFullPath().toString()).toString()); 737 } 738 catch (Exception exception) { 739 OljEditorPlugin.INSTANCE.log(exception); 740 } 741 } 742 743 749 public void createPages() { 750 createModel(); 753 754 { 757 ViewerPane viewerPane = 758 new ViewerPane(getSite().getPage(), OljEditor.this) { 759 public Viewer createViewer(Composite composite) { 760 Tree tree = new Tree(composite, SWT.MULTI); 761 TreeViewer newTreeViewer = new TreeViewer(tree); 762 return newTreeViewer; 763 } 764 public void requestActivation() { 765 super.requestActivation(); 766 setCurrentViewerPane(this); 767 } 768 }; 769 viewerPane.createControl(getContainer()); 770 771 selectionViewer = (TreeViewer)viewerPane.getViewer(); 772 selectionViewer.setContentProvider(new AdapterFactoryContentProvider(adapterFactory)); 773 774 selectionViewer.setLabelProvider(new AdapterFactoryLabelProvider(adapterFactory)); 775 selectionViewer.setInput(editingDomain.getResourceSet()); 776 viewerPane.setTitle(editingDomain.getResourceSet()); 777 778 new AdapterFactoryTreeEditor(selectionViewer.getTree(), adapterFactory); 779 780 createContextMenuFor(selectionViewer); 781 int pageIndex = addPage(viewerPane.getControl()); 782 setPageText(pageIndex, getString("_UI_SelectionPage_label")); 783 } 784 785 { 788 ViewerPane viewerPane = 789 new ViewerPane(getSite().getPage(), OljEditor.this) { 790 public Viewer createViewer(Composite composite) { 791 Tree tree = new Tree(composite, SWT.MULTI); 792 TreeViewer newTreeViewer = new TreeViewer(tree); 793 return newTreeViewer; 794 } 795 public void requestActivation() { 796 super.requestActivation(); 797 setCurrentViewerPane(this); 798 } 799 }; 800 viewerPane.createControl(getContainer()); 801 802 parentViewer = (TreeViewer)viewerPane.getViewer(); 803 parentViewer.setAutoExpandLevel(30); 804 parentViewer.setContentProvider(new ReverseAdapterFactoryContentProvider(adapterFactory)); 805 parentViewer.setLabelProvider(new AdapterFactoryLabelProvider(adapterFactory)); 806 807 createContextMenuFor(parentViewer); 808 int pageIndex = addPage(viewerPane.getControl()); 809 setPageText(pageIndex, getString("_UI_ParentPage_label")); 810 } 811 812 { 815 ViewerPane viewerPane = 816 new ViewerPane(getSite().getPage(), OljEditor.this) { 817 public Viewer createViewer(Composite composite) { 818 return new ListViewer(composite); 819 } 820 public void requestActivation() { 821 super.requestActivation(); 822 setCurrentViewerPane(this); 823 } 824 }; 825 viewerPane.createControl(getContainer()); 826 listViewer = (ListViewer)viewerPane.getViewer(); 827 listViewer.setContentProvider(new AdapterFactoryContentProvider(adapterFactory)); 828 listViewer.setLabelProvider(new AdapterFactoryLabelProvider(adapterFactory)); 829 830 createContextMenuFor(listViewer); 831 int pageIndex = addPage(viewerPane.getControl()); 832 setPageText(pageIndex, getString("_UI_ListPage_label")); 833 } 834 835 { 838 ViewerPane viewerPane = 839 new ViewerPane(getSite().getPage(), OljEditor.this) { 840 public Viewer createViewer(Composite composite) { 841 return new TreeViewer(composite); 842 } 843 public void requestActivation() { 844 super.requestActivation(); 845 setCurrentViewerPane(this); 846 } 847 }; 848 viewerPane.createControl(getContainer()); 849 treeViewer = (TreeViewer)viewerPane.getViewer(); 850 treeViewer.setContentProvider(new AdapterFactoryContentProvider(adapterFactory)); 851 treeViewer.setLabelProvider(new AdapterFactoryLabelProvider(adapterFactory)); 852 853 new AdapterFactoryTreeEditor(treeViewer.getTree(), adapterFactory); 854 855 createContextMenuFor(treeViewer); 856 int pageIndex = addPage(viewerPane.getControl()); 857 setPageText(pageIndex, getString("_UI_TreePage_label")); 858 } 859 860 { 863 ViewerPane viewerPane = 864 new ViewerPane(getSite().getPage(), OljEditor.this) { 865 public Viewer createViewer(Composite composite) { 866 return new TableViewer(composite); 867 } 868 public void requestActivation() { 869 super.requestActivation(); 870 setCurrentViewerPane(this); 871 } 872 }; 873 viewerPane.createControl(getContainer()); 874 tableViewer = (TableViewer)viewerPane.getViewer(); 875 876 Table table = tableViewer.getTable(); 877 TableLayout layout = new TableLayout(); 878 table.setLayout(layout); 879 table.setHeaderVisible(true); 880 table.setLinesVisible(true); 881 882 TableColumn objectColumn = new TableColumn(table, SWT.NONE); 883 layout.addColumnData(new ColumnWeightData(3, 100, true)); 884 objectColumn.setText(getString("_UI_ObjectColumn_label")); 885 objectColumn.setResizable(true); 886 887 TableColumn selfColumn = new TableColumn(table, SWT.NONE); 888 layout.addColumnData(new ColumnWeightData(2, 100, true)); 889 selfColumn.setText(getString("_UI_SelfColumn_label")); 890 selfColumn.setResizable(true); 891 892 tableViewer.setColumnProperties(new String [] {"a", "b"}); 893 tableViewer.setContentProvider(new AdapterFactoryContentProvider(adapterFactory)); 894 tableViewer.setLabelProvider(new AdapterFactoryLabelProvider(adapterFactory)); 895 896 createContextMenuFor(tableViewer); 897 int pageIndex = addPage(viewerPane.getControl()); 898 setPageText(pageIndex, getString("_UI_TablePage_label")); 899 } 900 901 { 904 ViewerPane viewerPane = 905 new ViewerPane(getSite().getPage(), OljEditor.this) { 906 public Viewer createViewer(Composite composite) { 907 return new TableTreeViewer(composite); 908 } 909 public void requestActivation() { 910 super.requestActivation(); 911 setCurrentViewerPane(this); 912 } 913 }; 914 viewerPane.createControl(getContainer()); 915 916 tableTreeViewer = (TableTreeViewer)viewerPane.getViewer(); 917 918 TableTree tableTree = tableTreeViewer.getTableTree(); 919 TableLayout layout = new TableLayout(); 920 tableTree.getTable().setLayout(layout); 921 tableTree.getTable().setHeaderVisible(true); 922 tableTree.getTable().setLinesVisible(true); 923 924 TableColumn objectColumn = new TableColumn(tableTree.getTable(), SWT.NONE); 925 layout.addColumnData(new ColumnWeightData(3, 100, true)); 926 objectColumn.setText(getString("_UI_ObjectColumn_label")); 927 objectColumn.setResizable(true); 928 929 TableColumn selfColumn = new TableColumn(tableTree.getTable(), SWT.NONE); 930 layout.addColumnData(new ColumnWeightData(2, 100, true)); 931 selfColumn.setText(getString("_UI_SelfColumn_label")); 932 selfColumn.setResizable(true); 933 934 tableTreeViewer.setColumnProperties(new String [] {"a", "b"}); 935 tableTreeViewer.setContentProvider(new AdapterFactoryContentProvider(adapterFactory)); 936 tableTreeViewer.setLabelProvider(new AdapterFactoryLabelProvider(adapterFactory)); 937 938 createContextMenuFor(tableTreeViewer); 939 int pageIndex = addPage(viewerPane.getControl()); 940 setPageText(pageIndex, getString("_UI_TableTreePage_label")); 941 } 942 943 setActivePage(0); 944 945 getContainer().addControlListener 946 (new ControlAdapter() { 947 boolean guard = false; 948 public void controlResized(ControlEvent event) { 949 if (!guard) { 950 guard = true; 951 hideTabs(); 952 guard = false; 953 } 954 } 955 }); 956 } 957 958 965 protected void hideTabs() { 966 if (getPageCount() <= 1) { 967 setPageText(0, ""); 968 if (getContainer() instanceof CTabFolder) { 969 ((CTabFolder)getContainer()).setTabHeight(1); 970 Point point = getContainer().getSize(); 971 getContainer().setSize(point.x, point.y + 6); 972 } 973 } 974 } 975 976 982 protected void pageChange(int pageIndex) { 983 super.pageChange(pageIndex); 984 985 Control control = getControl(pageIndex); 988 if (control != null) { 989 control.setVisible(true); 990 control.setFocus(); 991 } 992 993 if (contentOutlinePage != null) { 994 handleContentOutlineSelection(contentOutlinePage.getSelection()); 995 } 996 } 997 998 1004 public Object getAdapter(Class key) { 1005 if (key.equals(IContentOutlinePage.class)) { 1006 return getContentOutlinePage(); 1007 } 1008 else if (key.equals(IPropertySheetPage.class)) { 1009 return getPropertySheetPage(); 1010 } 1011 else if (key.equals(IGotoMarker.class)) { 1012 return this; 1013 } 1014 else { 1015 return super.getAdapter(key); 1016 } 1017 } 1018 1019 1025 public IContentOutlinePage getContentOutlinePage() { 1026 if (contentOutlinePage == null) { 1027 class MyContentOutlinePage extends ContentOutlinePage { 1030 public void createControl(Composite parent) { 1031 super.createControl(parent); 1032 contentOutlineViewer = getTreeViewer(); 1033 contentOutlineViewer.addSelectionChangedListener(this); 1034 1035 contentOutlineViewer.setContentProvider(new AdapterFactoryContentProvider(adapterFactory)); 1038 contentOutlineViewer.setLabelProvider(new AdapterFactoryLabelProvider(adapterFactory)); 1039 contentOutlineViewer.setInput(editingDomain.getResourceSet()); 1040 1041 createContextMenuFor(contentOutlineViewer); 1044 1045 if (!editingDomain.getResourceSet().getResources().isEmpty()) { 1046 ArrayList selection = new ArrayList (); 1049 selection.add(editingDomain.getResourceSet().getResources().get(0)); 1050 contentOutlineViewer.setSelection(new StructuredSelection(selection), true); 1051 } 1052 } 1053 1054 public void makeContributions(IMenuManager menuManager, IToolBarManager toolBarManager, IStatusLineManager statusLineManager) { 1055 super.makeContributions(menuManager, toolBarManager, statusLineManager); 1056 contentOutlineStatusLineManager = statusLineManager; 1057 } 1058 1059 public void setActionBars(IActionBars actionBars) { 1060 super.setActionBars(actionBars); 1061 getActionBarContributor().shareGlobalActions(this, actionBars); 1062 } 1063 } 1064 1065 contentOutlinePage = new MyContentOutlinePage(); 1066 1067 contentOutlinePage.addSelectionChangedListener 1070 (new ISelectionChangedListener() { 1071 public void selectionChanged(SelectionChangedEvent event) { 1074 handleContentOutlineSelection(event.getSelection()); 1075 } 1076 }); 1077 } 1078 1079 return contentOutlinePage; 1080 } 1081 1082 1088 public IPropertySheetPage getPropertySheetPage() { 1089 if (propertySheetPage == null) { 1090 propertySheetPage = 1091 new PropertySheetPage() { 1092 public void makeContributions(IMenuManager menuManager, IToolBarManager toolBarManager, IStatusLineManager statusLineManager) { 1093 super.makeContributions(menuManager, toolBarManager, statusLineManager); 1094 } 1095 1096 public void setActionBars(IActionBars actionBars) { 1097 super.setActionBars(actionBars); 1098 getActionBarContributor().shareGlobalActions(this, actionBars); 1099 } 1100 }; 1101 propertySheetPage.setPropertySourceProvider(new AdapterFactoryContentProvider(adapterFactory)); 1102 } 1103 1104 return propertySheetPage; 1105 } 1106 1107 1113 public void handleContentOutlineSelection(ISelection selection) { 1114 if (currentViewerPane != null && !selection.isEmpty() && selection instanceof IStructuredSelection) { 1115 Iterator selectedElements = ((IStructuredSelection)selection).iterator(); 1116 if (selectedElements.hasNext()) { 1117 Object selectedElement = selectedElements.next(); 1120 1121 if (currentViewerPane.getViewer() == selectionViewer) { 1124 ArrayList selectionList = new ArrayList (); 1125 selectionList.add(selectedElement); 1126 while (selectedElements.hasNext()) { 1127 selectionList.add(selectedElements.next()); 1128 } 1129 1130 selectionViewer.setSelection(new StructuredSelection(selectionList)); 1133 } 1134 else { 1135 if (currentViewerPane.getViewer().getInput() != selectedElement) { 1138 currentViewerPane.getViewer().setInput(selectedElement); 1139 currentViewerPane.setTitle(selectedElement); 1140 } 1141 } 1142 } 1143 } 1144 } 1145 1146 1152 public boolean isDirty() { 1153 return ((BasicCommandStack)editingDomain.getCommandStack()).isSaveNeeded(); 1154 } 1155 1156 1162 public void doSave(IProgressMonitor progressMonitor) { 1163 WorkspaceModifyOperation operation = 1166 new WorkspaceModifyOperation() { 1167 public void execute(IProgressMonitor monitor) { 1170 try { 1171 Resource savedResource = (Resource)editingDomain.getResourceSet().getResources().get(0); 1174 savedResources.add(savedResource); 1175 savedResource.save(Collections.EMPTY_MAP); 1176 } 1177 catch (Exception exception) { 1178 OljEditorPlugin.INSTANCE.log(exception); 1179 } 1180 } 1181 }; 1182 1183 try { 1184 new ProgressMonitorDialog(getSite().getShell()).run(true, false, operation); 1187 1188 ((BasicCommandStack)editingDomain.getCommandStack()).saveIsDone(); 1191 firePropertyChange(IEditorPart.PROP_DIRTY); 1192 } 1193 catch (Exception exception) { 1194 OljEditorPlugin.INSTANCE.log(exception); 1197 } 1198 } 1199 1200 1206 public boolean isSaveAsAllowed() { 1207 return true; 1208 } 1209 1210 1216 public void doSaveAs() { 1217 SaveAsDialog saveAsDialog= new SaveAsDialog(getSite().getShell()); 1218 saveAsDialog.open(); 1219 IPath path= saveAsDialog.getResult(); 1220 if (path != null) { 1221 IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path); 1222 if (file != null) { 1223 doSaveAs(URI.createPlatformResourceURI(file.getFullPath().toString()), new FileEditorInput(file)); 1224 } 1225 } 1226 } 1227 1228 1233 protected void doSaveAs(URI uri, IEditorInput editorInput) { 1234 ((Resource)editingDomain.getResourceSet().getResources().get(0)).setURI(uri); 1235 setInput(editorInput); 1236 setPartName(editorInput.getName()); 1237 IProgressMonitor progressMonitor = 1238 getActionBars().getStatusLineManager() != null ? 1239 getActionBars().getStatusLineManager().getProgressMonitor() : 1240 new NullProgressMonitor(); 1241 doSave(progressMonitor); 1242 } 1243 1244 1249 public void gotoMarker(IMarker marker) { 1250 try { 1251 if (marker.getType().equals(EValidator.MARKER)) { 1252 String uriAttribute = marker.getAttribute(EValidator.URI_ATTRIBUTE, null); 1253 if (uriAttribute != null) { 1254 URI uri = URI.createURI(uriAttribute); 1255 EObject eObject = editingDomain.getResourceSet().getEObject(uri, true); 1256 if (eObject != null) { 1257 setSelectionToViewer(Collections.singleton(editingDomain.getWrapper(eObject))); 1258 } 1259 } 1260 } 1261 } 1262 catch (CoreException exception) { 1263 OljEditorPlugin.INSTANCE.log(exception); 1264 } 1265 } 1266 1267 1273 public void init(IEditorSite site, IEditorInput editorInput) throws PartInitException { 1274 setSite(site); 1275 setInput(editorInput); 1276 setPartName(editorInput.getName()); 1277 site.setSelectionProvider(this); 1278 site.getPage().addPartListener(partListener); 1279 ResourcesPlugin.getWorkspace().addResourceChangeListener(resourceChangeListener, IResourceChangeEvent.POST_CHANGE); 1280 } 1281 1282 1287 public void setFocus() { 1288 getControl(getActivePage()).setFocus(); 1289 } 1290 1291 1297 public void addSelectionChangedListener(ISelectionChangedListener listener) { 1298 selectionChangedListeners.add(listener); 1299 } 1300 1301 1307 public void removeSelectionChangedListener(ISelectionChangedListener listener) { 1308 selectionChangedListeners.remove(listener); 1309 } 1310 1311 1317 public ISelection getSelection() { 1318 return editorSelection; 1319 } 1320 1321 1328 public void setSelection(ISelection selection) { 1329 editorSelection = selection; 1330 1331 for (Iterator listeners = selectionChangedListeners.iterator(); listeners.hasNext(); ) { 1332 ISelectionChangedListener listener = (ISelectionChangedListener)listeners.next(); 1333 listener.selectionChanged(new SelectionChangedEvent(this, selection)); 1334 } 1335 setStatusLineManager(selection); 1336 } 1337 1338 1343 public void setStatusLineManager(ISelection selection) { 1344 IStatusLineManager statusLineManager = currentViewer != null && currentViewer == contentOutlineViewer ? 1345 contentOutlineStatusLineManager : getActionBars().getStatusLineManager(); 1346 1347 if (statusLineManager != null) { 1348 if (selection instanceof IStructuredSelection) { 1349 Collection collection = ((IStructuredSelection)selection).toList(); 1350 switch (collection.size()) { 1351 case 0: { 1352 statusLineManager.setMessage(getString("_UI_NoObjectSelected")); 1353 break; 1354 } 1355 case 1: { 1356 String text = new AdapterFactoryItemDelegator(adapterFactory).getText(collection.iterator().next()); 1357 statusLineManager.setMessage(getString("_UI_SingleObjectSelected", text)); 1358 break; 1359 } 1360 default: { 1361 statusLineManager.setMessage(getString("_UI_MultiObjectSelected", Integer.toString(collection.size()))); 1362 break; 1363 } 1364 } 1365 } 1366 else { 1367 statusLineManager.setMessage(""); 1368 } 1369 } 1370 } 1371 1372 1378 private static String getString(String key) { 1379 return OljEditorPlugin.INSTANCE.getString(key); 1380 } 1381 1382 1388 private static String getString(String key, Object s1) { 1389 return OljEditorPlugin.INSTANCE.getString(key, new Object [] { s1 }); 1390 } 1391 1392 1398 public void menuAboutToShow(IMenuManager menuManager) { 1399 ((IMenuListener)getEditorSite().getActionBarContributor()).menuAboutToShow(menuManager); 1400 } 1401 1402 1407 public EditingDomainActionBarContributor getActionBarContributor() { 1408 return (EditingDomainActionBarContributor)getEditorSite().getActionBarContributor(); 1409 } 1410 1411 1416 public IActionBars getActionBars() { 1417 return getActionBarContributor().getActionBars(); 1418 } 1419 1420 1425 public AdapterFactory getAdapterFactory() { 1426 return adapterFactory; 1427 } 1428 1429 1434 public void dispose() { 1435 ResourcesPlugin.getWorkspace().removeResourceChangeListener(resourceChangeListener); 1436 1437 getSite().getPage().removePartListener(partListener); 1438 1439 adapterFactory.dispose(); 1440 1441 if (getActionBarContributor().getActiveEditor() == this) { 1442 getActionBarContributor().setActiveEditor(null); 1443 } 1444 1445 if (propertySheetPage != null) { 1446 propertySheetPage.dispose(); 1447 } 1448 1449 if (contentOutlinePage != null) { 1450 contentOutlinePage.dispose(); 1451 } 1452 1453 super.dispose(); 1454 } 1455 1456} 1457 | Popular Tags |