1 7 package org.enhydra.dods.editor.Doml.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 org.enhydra.dods.editor.Doml.provider.DomlItemProviderAdapterFactory; 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 import org.enhydra.dods.editor.Doml.provider.DomlEditPlugin; 151 152 153 159 public class DomlEditor 160 extends MultiPageEditorPart 161 implements IEditingDomainProvider, ISelectionProvider, IMenuListener, IViewerProvider, IGotoMarker { 162 168 protected AdapterFactoryEditingDomain editingDomain; 169 170 176 protected ComposedAdapterFactory adapterFactory; 177 178 184 protected IContentOutlinePage contentOutlinePage; 185 186 192 protected IStatusLineManager contentOutlineStatusLineManager; 193 194 200 protected TreeViewer contentOutlineViewer; 201 202 208 protected PropertySheetPage propertySheetPage; 209 210 217 protected TreeViewer selectionViewer; 218 219 225 protected TreeViewer parentViewer; 226 227 233 protected TreeViewer treeViewer; 234 235 242 protected ListViewer listViewer; 243 244 251 protected TableViewer tableViewer; 252 253 260 protected TableTreeViewer tableTreeViewer; 261 262 268 protected ViewerPane currentViewerPane; 269 270 276 protected Viewer currentViewer; 277 278 284 protected ISelectionChangedListener selectionChangedListener; 285 286 292 protected Collection selectionChangedListeners = new ArrayList (); 293 294 300 protected ISelection editorSelection = StructuredSelection.EMPTY; 301 302 308 protected IPartListener partListener = 309 new IPartListener() { 310 public void partActivated(IWorkbenchPart p) { 311 if (p instanceof ContentOutline) { 312 if (((ContentOutline)p).getCurrentPage() == contentOutlinePage) { 313 getActionBarContributor().setActiveEditor(DomlEditor.this); 314 315 setCurrentViewer(contentOutlineViewer); 316 } 317 } 318 else if (p instanceof PropertySheet) { 319 if (((PropertySheet)p).getCurrentPage() == propertySheetPage) { 320 getActionBarContributor().setActiveEditor(DomlEditor.this); 321 handleActivate(); 322 } 323 } 324 else if (p == DomlEditor.this) { 325 handleActivate(); 326 } 327 } 328 public void partBroughtToTop(IWorkbenchPart p) { 329 } 330 public void partClosed(IWorkbenchPart p) { 331 } 332 public void partDeactivated(IWorkbenchPart p) { 333 } 334 public void partOpened(IWorkbenchPart p) { 335 } 336 }; 337 338 342 Collection removedResources = new ArrayList (); 343 344 348 Collection changedResources = new ArrayList (); 349 350 354 Collection savedResources = new ArrayList (); 355 356 362 protected IResourceChangeListener resourceChangeListener = 363 new IResourceChangeListener() { 364 public void resourceChanged(IResourceChangeEvent event) { 365 ResourceSet resourceSet = editingDomain.getResourceSet(); 366 { 369 IResourceDelta delta = event.getDelta(); 370 try { 371 class ResourceDeltaVisitor implements IResourceDeltaVisitor { 372 protected ResourceSet resourceSet = editingDomain.getResourceSet(); 373 protected Collection changedResources = new ArrayList (); 374 protected Collection removedResources = new ArrayList (); 375 376 public boolean visit(IResourceDelta delta) { 377 if (delta.getFlags() != IResourceDelta.MARKERS && 378 delta.getResource().getType() == IResource.FILE) { 379 if ((delta.getKind() & (IResourceDelta.CHANGED | IResourceDelta.REMOVED)) != 0) { 380 Resource resource = resourceSet.getResource(URI.createURI(delta.getFullPath().toString()), false); 381 if (resource != null) { 382 if ((delta.getKind() & IResourceDelta.REMOVED) != 0) { 383 removedResources.add(resource); 384 } 385 else { 386 changedResources.add(resource); 387 } 388 } 389 } 390 } 391 392 return true; 393 } 394 395 public Collection getChangedResources() { 396 return changedResources; 397 } 398 399 public Collection getRemovedResources() { 400 return removedResources; 401 } 402 } 403 404 ResourceDeltaVisitor visitor = new ResourceDeltaVisitor(); 405 delta.accept(visitor); 406 407 removedResources.addAll(visitor.getRemovedResources()); 408 if (!visitor.getRemovedResources().isEmpty() && !isDirty()) { 409 getSite().getShell().getDisplay().asyncExec 410 (new Runnable () { 411 public void run() { 412 getSite().getPage().closeEditor(DomlEditor.this, false); 413 DomlEditor.this.dispose(); 414 } 415 }); 416 } 417 418 changedResources.addAll(visitor.getChangedResources()); 419 } 420 catch (CoreException exception) { 421 DomlEditPlugin.INSTANCE.log(exception); 422 } 423 } 424 } 425 }; 426 427 431 protected void handleActivate() { 432 if (editingDomain.getResourceToReadOnlyMap() != null) { 435 editingDomain.getResourceToReadOnlyMap().clear(); 436 437 setSelection(getSelection()); 440 } 441 442 if (!removedResources.isEmpty()) { 443 if (handleDirtyConflict()) { 444 getSite().getPage().closeEditor(DomlEditor.this, false); 445 DomlEditor.this.dispose(); 446 } 447 else { 448 removedResources.clear(); 449 changedResources.clear(); 450 savedResources.clear(); 451 } 452 } 453 else if (!changedResources.isEmpty()) { 454 changedResources.removeAll(savedResources); 455 handleChangedResources(); 456 changedResources.clear(); 457 savedResources.clear(); 458 } 459 } 460 461 462 466 protected void handleChangedResources() { 467 if (!changedResources.isEmpty() && (!isDirty() || handleDirtyConflict())) { 468 editingDomain.getCommandStack().flush(); 469 470 for (Iterator i = changedResources.iterator(); i.hasNext(); ) { 471 Resource resource = (Resource)i.next(); 472 if (resource.isLoaded()) { 473 resource.unload(); 474 try { 475 resource.load(Collections.EMPTY_MAP); 476 } 477 catch (IOException exception) { 478 DomlEditPlugin.INSTANCE.log(exception); 479 } 480 } 481 } 482 } 483 } 484 485 489 protected boolean handleDirtyConflict() { 490 return 491 MessageDialog.openQuestion 492 (getSite().getShell(), 493 getString("_UI_FileConflict_label"), 494 getString("_WARN_FileConflict")); 495 } 496 497 503 public DomlEditor() { 504 super(); 505 506 List factories = new ArrayList (); 509 factories.add(new ResourceItemProviderAdapterFactory()); 510 factories.add(new DomlItemProviderAdapterFactory()); 511 factories.add(new ReflectiveItemProviderAdapterFactory()); 512 513 adapterFactory = new ComposedAdapterFactory(factories); 514 515 BasicCommandStack commandStack = new BasicCommandStack(); 518 519 commandStack.addCommandStackListener 522 (new CommandStackListener() { 523 public void commandStackChanged(final EventObject event) { 524 getContainer().getDisplay().asyncExec 525 (new Runnable () { 526 public void run() { 527 firePropertyChange(IEditorPart.PROP_DIRTY); 528 529 Command mostRecentCommand = ((CommandStack)event.getSource()).getMostRecentCommand(); 532 if (mostRecentCommand != null) { 533 setSelectionToViewer(mostRecentCommand.getAffectedObjects()); 534 } 535 if (propertySheetPage != null) { 536 propertySheetPage.refresh(); 537 } 538 } 539 }); 540 } 541 }); 542 543 editingDomain = new AdapterFactoryEditingDomain(adapterFactory, commandStack, new HashMap ()); 546 } 547 548 554 protected void firePropertyChange(int action) { 555 super.firePropertyChange(action); 556 } 557 558 564 public void setSelectionToViewer(Collection collection) { 565 final Collection theSelection = collection; 566 if (theSelection != null && !theSelection.isEmpty()) { 569 Runnable runnable = 575 new Runnable () { 576 public void run() { 577 if (currentViewer != null) { 580 currentViewer.setSelection(new StructuredSelection(theSelection.toArray()), true); 581 } 582 } 583 }; 584 runnable.run(); 585 } 586 } 587 588 596 public EditingDomain getEditingDomain() { 597 return editingDomain; 598 } 599 600 605 public class ReverseAdapterFactoryContentProvider extends AdapterFactoryContentProvider { 606 public ReverseAdapterFactoryContentProvider(AdapterFactory adapterFactory) { 607 super(adapterFactory); 608 } 609 610 public Object [] getElements(Object object) { 611 Object parent = super.getParent(object); 612 return (parent == null ? Collections.EMPTY_SET : Collections.singleton(parent)).toArray(); 613 } 614 615 public Object [] getChildren(Object object) { 616 Object parent = super.getParent(object); 617 return (parent == null ? Collections.EMPTY_SET : Collections.singleton(parent)).toArray(); 618 } 619 620 public boolean hasChildren(Object object) { 621 Object parent = super.getParent(object); 622 return parent != null; 623 } 624 625 public Object getParent(Object object) { 626 return null; 627 } 628 } 629 630 635 public void setCurrentViewerPane(ViewerPane viewerPane) { 636 if (currentViewerPane != viewerPane) { 637 if (currentViewerPane != null) { 638 currentViewerPane.showFocus(false); 639 } 640 currentViewerPane = viewerPane; 641 } 642 setCurrentViewer(currentViewerPane.getViewer()); 643 } 644 645 652 public void setCurrentViewer(Viewer viewer) { 653 if (currentViewer != viewer) { 656 if (selectionChangedListener == null) { 657 selectionChangedListener = 660 new ISelectionChangedListener() { 661 public void selectionChanged(SelectionChangedEvent selectionChangedEvent) { 664 setSelection(selectionChangedEvent.getSelection()); 665 } 666 }; 667 } 668 669 if (currentViewer != null) { 672 currentViewer.removeSelectionChangedListener(selectionChangedListener); 673 } 674 675 if (viewer != null) { 678 viewer.addSelectionChangedListener(selectionChangedListener); 679 } 680 681 currentViewer = viewer; 684 685 setSelection(currentViewer == null ? StructuredSelection.EMPTY : currentViewer.getSelection()); 688 } 689 } 690 691 697 public Viewer getViewer() { 698 return currentViewer; 699 } 700 701 707 protected void createContextMenuFor(StructuredViewer viewer) { 708 MenuManager contextMenu = new MenuManager("#PopUp"); 709 contextMenu.add(new Separator("additions")); 710 contextMenu.setRemoveAllWhenShown(true); 711 contextMenu.addMenuListener(this); 712 Menu menu= contextMenu.createContextMenu(viewer.getControl()); 713 viewer.getControl().setMenu(menu); 714 getSite().registerContextMenu(contextMenu, viewer); 715 716 int dndOperations = DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_LINK; 717 Transfer[] transfers = new Transfer[] { LocalTransfer.getInstance() }; 718 viewer.addDragSupport(dndOperations, transfers, new ViewerDragAdapter(viewer)); 719 viewer.addDropSupport(dndOperations, transfers, new EditingDomainViewerDropAdapter(editingDomain, viewer)); 720 } 721 722 728 public void createModel() { 729 IFileEditorInput modelFile = (IFileEditorInput)getEditorInput(); 732 733 try { 734 Resource resource = 737 editingDomain.loadResource 738 (URI.createPlatformResourceURI(modelFile.getFile().getFullPath().toString()).toString()); 739 } 740 catch (Exception exception) { 741 DomlEditPlugin.INSTANCE.log(exception); 742 } 743 } 744 745 751 public void createPages() { 752 createModel(); 755 756 { 759 ViewerPane viewerPane = 760 new ViewerPane(getSite().getPage(), DomlEditor.this) { 761 public Viewer createViewer(Composite composite) { 762 Tree tree = new Tree(composite, SWT.MULTI); 763 TreeViewer newTreeViewer = new TreeViewer(tree); 764 return newTreeViewer; 765 } 766 public void requestActivation() { 767 super.requestActivation(); 768 setCurrentViewerPane(this); 769 } 770 }; 771 viewerPane.createControl(getContainer()); 772 773 selectionViewer = (TreeViewer)viewerPane.getViewer(); 774 selectionViewer.setContentProvider(new AdapterFactoryContentProvider(adapterFactory)); 775 776 selectionViewer.setLabelProvider(new AdapterFactoryLabelProvider(adapterFactory)); 777 selectionViewer.setInput(editingDomain.getResourceSet()); 778 viewerPane.setTitle(editingDomain.getResourceSet()); 779 780 new AdapterFactoryTreeEditor(selectionViewer.getTree(), adapterFactory); 781 782 createContextMenuFor(selectionViewer); 783 int pageIndex = addPage(viewerPane.getControl()); 784 setPageText(pageIndex, getString("_UI_SelectionPage_label")); 785 } 786 787 { 790 ViewerPane viewerPane = 791 new ViewerPane(getSite().getPage(), DomlEditor.this) { 792 public Viewer createViewer(Composite composite) { 793 Tree tree = new Tree(composite, SWT.MULTI); 794 TreeViewer newTreeViewer = new TreeViewer(tree); 795 return newTreeViewer; 796 } 797 public void requestActivation() { 798 super.requestActivation(); 799 setCurrentViewerPane(this); 800 } 801 }; 802 viewerPane.createControl(getContainer()); 803 804 parentViewer = (TreeViewer)viewerPane.getViewer(); 805 parentViewer.setAutoExpandLevel(30); 806 parentViewer.setContentProvider(new ReverseAdapterFactoryContentProvider(adapterFactory)); 807 parentViewer.setLabelProvider(new AdapterFactoryLabelProvider(adapterFactory)); 808 809 createContextMenuFor(parentViewer); 810 int pageIndex = addPage(viewerPane.getControl()); 811 setPageText(pageIndex, getString("_UI_ParentPage_label")); 812 } 813 814 { 817 ViewerPane viewerPane = 818 new ViewerPane(getSite().getPage(), DomlEditor.this) { 819 public Viewer createViewer(Composite composite) { 820 return new ListViewer(composite); 821 } 822 public void requestActivation() { 823 super.requestActivation(); 824 setCurrentViewerPane(this); 825 } 826 }; 827 viewerPane.createControl(getContainer()); 828 listViewer = (ListViewer)viewerPane.getViewer(); 829 listViewer.setContentProvider(new AdapterFactoryContentProvider(adapterFactory)); 830 listViewer.setLabelProvider(new AdapterFactoryLabelProvider(adapterFactory)); 831 832 createContextMenuFor(listViewer); 833 int pageIndex = addPage(viewerPane.getControl()); 834 setPageText(pageIndex, getString("_UI_ListPage_label")); 835 } 836 837 { 840 ViewerPane viewerPane = 841 new ViewerPane(getSite().getPage(), DomlEditor.this) { 842 public Viewer createViewer(Composite composite) { 843 return new TreeViewer(composite); 844 } 845 public void requestActivation() { 846 super.requestActivation(); 847 setCurrentViewerPane(this); 848 } 849 }; 850 viewerPane.createControl(getContainer()); 851 treeViewer = (TreeViewer)viewerPane.getViewer(); 852 treeViewer.setContentProvider(new AdapterFactoryContentProvider(adapterFactory)); 853 treeViewer.setLabelProvider(new AdapterFactoryLabelProvider(adapterFactory)); 854 855 new AdapterFactoryTreeEditor(treeViewer.getTree(), adapterFactory); 856 857 createContextMenuFor(treeViewer); 858 int pageIndex = addPage(viewerPane.getControl()); 859 setPageText(pageIndex, getString("_UI_TreePage_label")); 860 } 861 862 { 865 ViewerPane viewerPane = 866 new ViewerPane(getSite().getPage(), DomlEditor.this) { 867 public Viewer createViewer(Composite composite) { 868 return new TableViewer(composite); 869 } 870 public void requestActivation() { 871 super.requestActivation(); 872 setCurrentViewerPane(this); 873 } 874 }; 875 viewerPane.createControl(getContainer()); 876 tableViewer = (TableViewer)viewerPane.getViewer(); 877 878 Table table = tableViewer.getTable(); 879 TableLayout layout = new TableLayout(); 880 table.setLayout(layout); 881 table.setHeaderVisible(true); 882 table.setLinesVisible(true); 883 884 TableColumn objectColumn = new TableColumn(table, SWT.NONE); 885 layout.addColumnData(new ColumnWeightData(3, 100, true)); 886 objectColumn.setText(getString("_UI_ObjectColumn_label")); 887 objectColumn.setResizable(true); 888 889 TableColumn selfColumn = new TableColumn(table, SWT.NONE); 890 layout.addColumnData(new ColumnWeightData(2, 100, true)); 891 selfColumn.setText(getString("_UI_SelfColumn_label")); 892 selfColumn.setResizable(true); 893 894 tableViewer.setColumnProperties(new String [] {"a", "b"}); 895 tableViewer.setContentProvider(new AdapterFactoryContentProvider(adapterFactory)); 896 tableViewer.setLabelProvider(new AdapterFactoryLabelProvider(adapterFactory)); 897 898 createContextMenuFor(tableViewer); 899 int pageIndex = addPage(viewerPane.getControl()); 900 setPageText(pageIndex, getString("_UI_TablePage_label")); 901 } 902 903 { 906 ViewerPane viewerPane = 907 new ViewerPane(getSite().getPage(), DomlEditor.this) { 908 public Viewer createViewer(Composite composite) { 909 return new TableTreeViewer(composite); 910 } 911 public void requestActivation() { 912 super.requestActivation(); 913 setCurrentViewerPane(this); 914 } 915 }; 916 viewerPane.createControl(getContainer()); 917 918 tableTreeViewer = (TableTreeViewer)viewerPane.getViewer(); 919 920 TableTree tableTree = tableTreeViewer.getTableTree(); 921 TableLayout layout = new TableLayout(); 922 tableTree.getTable().setLayout(layout); 923 tableTree.getTable().setHeaderVisible(true); 924 tableTree.getTable().setLinesVisible(true); 925 926 TableColumn objectColumn = new TableColumn(tableTree.getTable(), SWT.NONE); 927 layout.addColumnData(new ColumnWeightData(3, 100, true)); 928 objectColumn.setText(getString("_UI_ObjectColumn_label")); 929 objectColumn.setResizable(true); 930 931 TableColumn selfColumn = new TableColumn(tableTree.getTable(), SWT.NONE); 932 layout.addColumnData(new ColumnWeightData(2, 100, true)); 933 selfColumn.setText(getString("_UI_SelfColumn_label")); 934 selfColumn.setResizable(true); 935 936 tableTreeViewer.setColumnProperties(new String [] {"a", "b"}); 937 tableTreeViewer.setContentProvider(new AdapterFactoryContentProvider(adapterFactory)); 938 tableTreeViewer.setLabelProvider(new AdapterFactoryLabelProvider(adapterFactory)); 939 940 createContextMenuFor(tableTreeViewer); 941 int pageIndex = addPage(viewerPane.getControl()); 942 setPageText(pageIndex, getString("_UI_TableTreePage_label")); 943 } 944 945 setActivePage(0); 946 947 getContainer().addControlListener 948 (new ControlAdapter() { 949 boolean guard = false; 950 public void controlResized(ControlEvent event) { 951 if (!guard) { 952 guard = true; 953 hideTabs(); 954 guard = false; 955 } 956 } 957 }); 958 } 959 960 967 protected void hideTabs() { 968 if (getPageCount() <= 1) { 969 setPageText(0, ""); 970 if (getContainer() instanceof CTabFolder) { 971 ((CTabFolder)getContainer()).setTabHeight(1); 972 Point point = getContainer().getSize(); 973 getContainer().setSize(point.x, point.y + 6); 974 } 975 } 976 } 977 978 984 protected void pageChange(int pageIndex) { 985 super.pageChange(pageIndex); 986 987 Control control = getControl(pageIndex); 990 if (control != null) { 991 control.setVisible(true); 992 control.setFocus(); 993 } 994 995 if (contentOutlinePage != null) { 996 handleContentOutlineSelection(contentOutlinePage.getSelection()); 997 } 998 } 999 1000 1006 public Object getAdapter(Class key) { 1007 if (key.equals(IContentOutlinePage.class)) { 1008 return getContentOutlinePage(); 1009 } 1010 else if (key.equals(IPropertySheetPage.class)) { 1011 return getPropertySheetPage(); 1012 } 1013 else if (key.equals(IGotoMarker.class)) { 1014 return this; 1015 } 1016 else { 1017 return super.getAdapter(key); 1018 } 1019 } 1020 1021 1027 public IContentOutlinePage getContentOutlinePage() { 1028 if (contentOutlinePage == null) { 1029 class MyContentOutlinePage extends ContentOutlinePage { 1032 public void createControl(Composite parent) { 1033 super.createControl(parent); 1034 contentOutlineViewer = getTreeViewer(); 1035 contentOutlineViewer.addSelectionChangedListener(this); 1036 1037 contentOutlineViewer.setContentProvider(new AdapterFactoryContentProvider(adapterFactory)); 1040 contentOutlineViewer.setLabelProvider(new AdapterFactoryLabelProvider(adapterFactory)); 1041 contentOutlineViewer.setInput(editingDomain.getResourceSet()); 1042 1043 createContextMenuFor(contentOutlineViewer); 1046 1047 if (!editingDomain.getResourceSet().getResources().isEmpty()) { 1048 ArrayList selection = new ArrayList (); 1051 selection.add(editingDomain.getResourceSet().getResources().get(0)); 1052 contentOutlineViewer.setSelection(new StructuredSelection(selection), true); 1053 } 1054 } 1055 1056 public void makeContributions(IMenuManager menuManager, IToolBarManager toolBarManager, IStatusLineManager statusLineManager) { 1057 super.makeContributions(menuManager, toolBarManager, statusLineManager); 1058 contentOutlineStatusLineManager = statusLineManager; 1059 } 1060 1061 public void setActionBars(IActionBars actionBars) { 1062 super.setActionBars(actionBars); 1063 getActionBarContributor().shareGlobalActions(this, actionBars); 1064 } 1065 } 1066 1067 contentOutlinePage = new MyContentOutlinePage(); 1068 1069 contentOutlinePage.addSelectionChangedListener 1072 (new ISelectionChangedListener() { 1073 public void selectionChanged(SelectionChangedEvent event) { 1076 handleContentOutlineSelection(event.getSelection()); 1077 } 1078 }); 1079 } 1080 1081 return contentOutlinePage; 1082 } 1083 1084 1090 public IPropertySheetPage getPropertySheetPage() { 1091 if (propertySheetPage == null) { 1092 propertySheetPage = 1093 new PropertySheetPage() { 1094 public void makeContributions(IMenuManager menuManager, IToolBarManager toolBarManager, IStatusLineManager statusLineManager) { 1095 super.makeContributions(menuManager, toolBarManager, statusLineManager); 1096 } 1097 1098 public void setActionBars(IActionBars actionBars) { 1099 super.setActionBars(actionBars); 1100 getActionBarContributor().shareGlobalActions(this, actionBars); 1101 } 1102 }; 1103 propertySheetPage.setPropertySourceProvider(new AdapterFactoryContentProvider(adapterFactory)); 1104 } 1105 1106 return propertySheetPage; 1107 } 1108 1109 1115 public void handleContentOutlineSelection(ISelection selection) { 1116 if (currentViewerPane != null && !selection.isEmpty() && selection instanceof IStructuredSelection) { 1117 Iterator selectedElements = ((IStructuredSelection)selection).iterator(); 1118 if (selectedElements.hasNext()) { 1119 Object selectedElement = selectedElements.next(); 1122 1123 if (currentViewerPane.getViewer() == selectionViewer) { 1126 ArrayList selectionList = new ArrayList (); 1127 selectionList.add(selectedElement); 1128 while (selectedElements.hasNext()) { 1129 selectionList.add(selectedElements.next()); 1130 } 1131 1132 selectionViewer.setSelection(new StructuredSelection(selectionList)); 1135 } 1136 else { 1137 if (currentViewerPane.getViewer().getInput() != selectedElement) { 1140 currentViewerPane.getViewer().setInput(selectedElement); 1141 currentViewerPane.setTitle(selectedElement); 1142 } 1143 } 1144 } 1145 } 1146 } 1147 1148 1154 public boolean isDirty() { 1155 return ((BasicCommandStack)editingDomain.getCommandStack()).isSaveNeeded(); 1156 } 1157 1158 1164 public void doSave(IProgressMonitor progressMonitor) { 1165 WorkspaceModifyOperation operation = 1168 new WorkspaceModifyOperation() { 1169 public void execute(IProgressMonitor monitor) { 1172 try { 1173 Resource savedResource = (Resource)editingDomain.getResourceSet().getResources().get(0); 1176 savedResources.add(savedResource); 1177 savedResource.save(Collections.EMPTY_MAP); 1178 } 1179 catch (Exception exception) { 1180 DomlEditPlugin.INSTANCE.log(exception); 1181 } 1182 } 1183 }; 1184 1185 try { 1186 new ProgressMonitorDialog(getSite().getShell()).run(true, false, operation); 1189 1190 ((BasicCommandStack)editingDomain.getCommandStack()).saveIsDone(); 1193 firePropertyChange(IEditorPart.PROP_DIRTY); 1194 } 1195 catch (Exception exception) { 1196 DomlEditPlugin.INSTANCE.log(exception); 1199 } 1200 } 1201 1202 1208 public boolean isSaveAsAllowed() { 1209 return true; 1210 } 1211 1212 1218 public void doSaveAs() { 1219 SaveAsDialog saveAsDialog= new SaveAsDialog(getSite().getShell()); 1220 saveAsDialog.open(); 1221 IPath path= saveAsDialog.getResult(); 1222 if (path != null) { 1223 IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path); 1224 if (file != null) { 1225 doSaveAs(URI.createPlatformResourceURI(file.getFullPath().toString()), new FileEditorInput(file)); 1226 } 1227 } 1228 } 1229 1230 1235 protected void doSaveAs(URI uri, IEditorInput editorInput) { 1236 ((Resource)editingDomain.getResourceSet().getResources().get(0)).setURI(uri); 1237 setInput(editorInput); 1238 setPartName(editorInput.getName()); 1239 IProgressMonitor progressMonitor = 1240 getActionBars().getStatusLineManager() != null ? 1241 getActionBars().getStatusLineManager().getProgressMonitor() : 1242 new NullProgressMonitor(); 1243 doSave(progressMonitor); 1244 } 1245 1246 1251 public void gotoMarker(IMarker marker) { 1252 try { 1253 if (marker.getType().equals(EValidator.MARKER)) { 1254 String uriAttribute = marker.getAttribute(EValidator.URI_ATTRIBUTE, null); 1255 if (uriAttribute != null) { 1256 URI uri = URI.createURI(uriAttribute); 1257 EObject eObject = editingDomain.getResourceSet().getEObject(uri, true); 1258 if (eObject != null) { 1259 setSelectionToViewer(Collections.singleton(editingDomain.getWrapper(eObject))); 1260 } 1261 } 1262 } 1263 } 1264 catch (CoreException exception) { 1265 DomlEditPlugin.INSTANCE.log(exception); 1266 } 1267 } 1268 1269 1275 public void init(IEditorSite site, IEditorInput editorInput) throws PartInitException { 1276 setSite(site); 1277 setInput(editorInput); 1278 setPartName(editorInput.getName()); 1279 site.setSelectionProvider(this); 1280 site.getPage().addPartListener(partListener); 1281 ResourcesPlugin.getWorkspace().addResourceChangeListener(resourceChangeListener, IResourceChangeEvent.POST_CHANGE); 1282 } 1283 1284 1289 public void setFocus() { 1290 getControl(getActivePage()).setFocus(); 1291 } 1292 1293 1299 public void addSelectionChangedListener(ISelectionChangedListener listener) { 1300 selectionChangedListeners.add(listener); 1301 } 1302 1303 1309 public void removeSelectionChangedListener(ISelectionChangedListener listener) { 1310 selectionChangedListeners.remove(listener); 1311 } 1312 1313 1319 public ISelection getSelection() { 1320 return editorSelection; 1321 } 1322 1323 1330 public void setSelection(ISelection selection) { 1331 editorSelection = selection; 1332 1333 for (Iterator listeners = selectionChangedListeners.iterator(); listeners.hasNext(); ) { 1334 ISelectionChangedListener listener = (ISelectionChangedListener)listeners.next(); 1335 listener.selectionChanged(new SelectionChangedEvent(this, selection)); 1336 } 1337 setStatusLineManager(selection); 1338 } 1339 1340 1345 public void setStatusLineManager(ISelection selection) { 1346 IStatusLineManager statusLineManager = currentViewer != null && currentViewer == contentOutlineViewer ? 1347 contentOutlineStatusLineManager : getActionBars().getStatusLineManager(); 1348 1349 if (statusLineManager != null) { 1350 if (selection instanceof IStructuredSelection) { 1351 Collection collection = ((IStructuredSelection)selection).toList(); 1352 switch (collection.size()) { 1353 case 0: { 1354 statusLineManager.setMessage(getString("_UI_NoObjectSelected")); 1355 break; 1356 } 1357 case 1: { 1358 String text = new AdapterFactoryItemDelegator(adapterFactory).getText(collection.iterator().next()); 1359 statusLineManager.setMessage(getString("_UI_SingleObjectSelected", text)); 1360 break; 1361 } 1362 default: { 1363 statusLineManager.setMessage(getString("_UI_MultiObjectSelected", Integer.toString(collection.size()))); 1364 break; 1365 } 1366 } 1367 } 1368 else { 1369 statusLineManager.setMessage(""); 1370 } 1371 } 1372 } 1373 1374 1380 private static String getString(String key) { 1381 return DomlEditPlugin.INSTANCE.getString(key); 1382 } 1383 1384 1390 private static String getString(String key, Object s1) { 1391 return DomlEditPlugin.INSTANCE.getString(key, new Object [] { s1 }); 1392 } 1393 1394 1400 public void menuAboutToShow(IMenuManager menuManager) { 1401 ((IMenuListener)getEditorSite().getActionBarContributor()).menuAboutToShow(menuManager); 1402 } 1403 1404 1409 public EditingDomainActionBarContributor getActionBarContributor() { 1410 return (EditingDomainActionBarContributor)getEditorSite().getActionBarContributor(); 1411 } 1412 1413 1418 public IActionBars getActionBars() { 1419 return getActionBarContributor().getActionBars(); 1420 } 1421 1422 1427 public AdapterFactory getAdapterFactory() { 1428 return adapterFactory; 1429 } 1430 1431 1436 public void dispose() { 1437 ResourcesPlugin.getWorkspace().removeResourceChangeListener(resourceChangeListener); 1438 1439 getSite().getPage().removePartListener(partListener); 1440 1441 adapterFactory.dispose(); 1442 1443 if (getActionBarContributor().getActiveEditor() == this) { 1444 getActionBarContributor().setActiveEditor(null); 1445 } 1446 1447 if (propertySheetPage != null) { 1448 propertySheetPage.dispose(); 1449 } 1450 1451 if (contentOutlinePage != null) { 1452 contentOutlinePage.dispose(); 1453 } 1454 1455 super.dispose(); 1456 } 1457 1458} 1459 | Popular Tags |