1 17 package org.eclipse.emf.ecore.sdo.presentation; 18 19 20 import java.io.IOException ; 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 import java.util.Collections ; 24 import java.util.EventObject ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 29 import org.eclipse.core.resources.IFile; 30 import org.eclipse.core.resources.IMarker; 31 import org.eclipse.core.resources.IResource; 32 import org.eclipse.core.resources.IResourceChangeEvent; 33 import org.eclipse.core.resources.IResourceChangeListener; 34 import org.eclipse.core.resources.IResourceDelta; 35 import org.eclipse.core.resources.IResourceDeltaVisitor; 36 import org.eclipse.core.resources.ResourcesPlugin; 37 import org.eclipse.core.runtime.CoreException; 38 import org.eclipse.core.runtime.IPath; 39 import org.eclipse.core.runtime.IProgressMonitor; 40 import org.eclipse.core.runtime.NullProgressMonitor; 41 import org.eclipse.jface.action.IMenuListener; 42 import org.eclipse.jface.action.IMenuManager; 43 import org.eclipse.jface.action.IStatusLineManager; 44 import org.eclipse.jface.action.IToolBarManager; 45 import org.eclipse.jface.action.MenuManager; 46 import org.eclipse.jface.action.Separator; 47 import org.eclipse.jface.dialogs.MessageDialog; 48 import org.eclipse.jface.dialogs.ProgressMonitorDialog; 49 import org.eclipse.jface.viewers.ColumnWeightData; 50 import org.eclipse.jface.viewers.ISelection; 51 import org.eclipse.jface.viewers.ISelectionChangedListener; 52 import org.eclipse.jface.viewers.ISelectionProvider; 53 import org.eclipse.jface.viewers.IStructuredSelection; 54 import org.eclipse.jface.viewers.ListViewer; 55 import org.eclipse.jface.viewers.SelectionChangedEvent; 56 import org.eclipse.jface.viewers.StructuredSelection; 57 import org.eclipse.jface.viewers.StructuredViewer; 58 import org.eclipse.jface.viewers.TableLayout; 59 import org.eclipse.jface.viewers.TableViewer; 60 import org.eclipse.jface.viewers.TreeViewer; 61 import org.eclipse.jface.viewers.Viewer; 62 import org.eclipse.swt.SWT; 63 import org.eclipse.swt.custom.CTabFolder; 64 import org.eclipse.swt.dnd.DND; 65 import org.eclipse.swt.dnd.Transfer; 66 import org.eclipse.swt.events.ControlAdapter; 67 import org.eclipse.swt.events.ControlEvent; 68 import org.eclipse.swt.graphics.Point; 69 import org.eclipse.swt.layout.FillLayout; 70 import org.eclipse.swt.widgets.Composite; 71 import org.eclipse.swt.widgets.Control; 72 import org.eclipse.swt.widgets.Menu; 73 import org.eclipse.swt.widgets.Table; 74 import org.eclipse.swt.widgets.TableColumn; 75 import org.eclipse.swt.widgets.Tree; 76 import org.eclipse.swt.widgets.TreeColumn; 77 import org.eclipse.ui.IActionBars; 78 import org.eclipse.ui.IEditorInput; 79 import org.eclipse.ui.IEditorPart; 80 import org.eclipse.ui.IEditorSite; 81 import org.eclipse.ui.IFileEditorInput; 82 import org.eclipse.ui.IPartListener; 83 import org.eclipse.ui.IWorkbenchPart; 84 import org.eclipse.ui.PartInitException; 85 import org.eclipse.ui.actions.WorkspaceModifyOperation; 86 import org.eclipse.ui.dialogs.SaveAsDialog; 87 import org.eclipse.ui.ide.IGotoMarker; 88 import org.eclipse.ui.part.FileEditorInput; 89 import org.eclipse.ui.part.MultiPageEditorPart; 90 import org.eclipse.ui.views.contentoutline.ContentOutline; 91 import org.eclipse.ui.views.contentoutline.ContentOutlinePage; 92 import org.eclipse.ui.views.contentoutline.IContentOutlinePage; 93 import org.eclipse.ui.views.properties.IPropertySheetPage; 94 import org.eclipse.ui.views.properties.PropertySheet; 95 import org.eclipse.ui.views.properties.PropertySheetPage; 96 97 import org.eclipse.emf.common.command.BasicCommandStack; 98 import org.eclipse.emf.common.command.Command; 99 import org.eclipse.emf.common.command.CommandStack; 100 import org.eclipse.emf.common.command.CommandStackListener; 101 import org.eclipse.emf.common.notify.Adapter; 102 import org.eclipse.emf.common.notify.AdapterFactory; 103 import org.eclipse.emf.common.ui.ViewerPane; 104 import org.eclipse.emf.common.ui.viewer.IViewerProvider; 105 import org.eclipse.emf.common.util.URI; 106 import org.eclipse.emf.ecore.EObject; 107 import org.eclipse.emf.ecore.EValidator; 108 import org.eclipse.emf.ecore.change.provider.ChangeItemProviderAdapterFactory; 109 import org.eclipse.emf.ecore.provider.EcoreItemProviderAdapterFactory; 110 import org.eclipse.emf.ecore.resource.Resource; 111 import org.eclipse.emf.ecore.resource.ResourceSet; 112 import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain; 113 import org.eclipse.emf.edit.domain.EditingDomain; 114 import org.eclipse.emf.edit.domain.IEditingDomainProvider; 115 import org.eclipse.emf.edit.provider.AdapterFactoryItemDelegator; 116 import org.eclipse.emf.edit.provider.ComposedAdapterFactory; 117 import org.eclipse.emf.edit.provider.resource.ResourceItemProviderAdapterFactory; 118 import org.eclipse.emf.edit.provider.resource.ResourceSetItemProvider; 119 import org.eclipse.emf.edit.ui.action.EditingDomainActionBarContributor; 120 import org.eclipse.emf.edit.ui.celleditor.AdapterFactoryTreeEditor; 121 import org.eclipse.emf.edit.ui.dnd.EditingDomainViewerDropAdapter; 122 import org.eclipse.emf.edit.ui.dnd.LocalTransfer; 123 import org.eclipse.emf.edit.ui.dnd.ViewerDragAdapter; 124 import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider; 125 import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider; 126 127 import org.eclipse.emf.ecore.sdo.EChangeSummary; 128 import org.eclipse.emf.ecore.sdo.EDataGraph; 129 import org.eclipse.emf.ecore.sdo.EDataObject; 130 import org.eclipse.emf.ecore.sdo.provider.SDOItemProviderAdapterFactory; 131 132 134 135 141 public class SDOEditor 142 extends MultiPageEditorPart 143 implements IEditingDomainProvider, ISelectionProvider, IMenuListener, IViewerProvider, IGotoMarker 144 { 145 151 protected AdapterFactoryEditingDomain editingDomain; 152 153 159 protected ComposedAdapterFactory adapterFactory; 160 161 167 protected IContentOutlinePage contentOutlinePage; 168 169 175 protected IStatusLineManager contentOutlineStatusLineManager; 176 177 183 protected TreeViewer contentOutlineViewer; 184 185 191 protected PropertySheetPage propertySheetPage; 192 193 200 protected TreeViewer selectionViewer; 201 202 208 protected TreeViewer parentViewer; 209 210 216 protected TreeViewer treeViewer; 217 218 225 protected ListViewer listViewer; 226 227 234 protected TableViewer tableViewer; 235 236 242 protected TreeViewer treeViewerWithColumns; 243 244 250 protected ViewerPane currentViewerPane; 251 252 258 protected Viewer currentViewer; 259 260 266 protected ISelectionChangedListener selectionChangedListener; 267 268 274 protected Collection selectionChangedListeners = new ArrayList (); 275 276 282 protected ISelection editorSelection= StructuredSelection.EMPTY; 283 284 290 protected IPartListener partListener = 291 new IPartListener() 292 { 293 public void partActivated(IWorkbenchPart p) 294 { 295 if (p instanceof ContentOutline) 296 { 297 if (((ContentOutline)p).getCurrentPage() == contentOutlinePage) 298 { 299 getActionBarContributor().setActiveEditor(SDOEditor.this); 300 301 setCurrentViewer(contentOutlineViewer); 302 } 303 } 304 else if (p instanceof PropertySheet) 305 { 306 if (((PropertySheet)p).getCurrentPage() == propertySheetPage) 307 { 308 getActionBarContributor().setActiveEditor(SDOEditor.this); 309 handleActivate(); 310 } 311 } 312 else if (p == SDOEditor.this) 313 { 314 handleActivate(); 315 } 316 } 317 public void partBroughtToTop(IWorkbenchPart p) 318 { 319 } 320 public void partClosed(IWorkbenchPart p) 321 { 322 } 323 public void partDeactivated(IWorkbenchPart p) 324 { 325 } 326 public void partOpened(IWorkbenchPart p) 327 { 328 } 329 }; 330 331 335 Collection removedResources = new ArrayList (); 336 337 341 Collection changedResources = new ArrayList (); 342 343 347 Collection savedResources = new ArrayList (); 348 349 355 protected IResourceChangeListener resourceChangeListener = 356 new IResourceChangeListener() 357 { 358 public void resourceChanged(IResourceChangeEvent event) 359 { 360 { 363 IResourceDelta delta = event.getDelta(); 364 try 365 { 366 class ResourceDeltaVisitor implements IResourceDeltaVisitor 367 { 368 protected ResourceSet resourceSet = editingDomain.getResourceSet(); 369 protected Collection changedResources = new ArrayList (); 370 protected Collection removedResources = new ArrayList (); 371 372 public boolean visit(IResourceDelta delta) 373 { 374 if (delta.getFlags() != IResourceDelta.MARKERS && 375 delta.getResource().getType() == IResource.FILE) 376 { 377 if ((delta.getKind() & (IResourceDelta.CHANGED | IResourceDelta.REMOVED)) != 0) 378 { 379 Resource resource = resourceSet.getResource(URI.createURI(delta.getFullPath().toString()), false); 380 if (resource != null) 381 { 382 if ((delta.getKind() & IResourceDelta.REMOVED) != 0) 383 { 384 removedResources.add(resource); 385 } 386 else 387 { 388 changedResources.add(resource); 389 } 390 } 391 } 392 } 393 394 return true; 395 } 396 397 public Collection getChangedResources() 398 { 399 return changedResources; 400 } 401 402 public Collection getRemovedResources() 403 { 404 return removedResources; 405 } 406 } 407 408 ResourceDeltaVisitor visitor = new ResourceDeltaVisitor(); 409 delta.accept(visitor); 410 411 if (!visitor.getRemovedResources().isEmpty()) 412 { 413 removedResources.addAll(visitor.getRemovedResources()); 414 if (!isDirty()) 415 { 416 getSite().getShell().getDisplay().asyncExec 417 (new Runnable () 418 { 419 public void run() 420 { 421 getSite().getPage().closeEditor(SDOEditor.this, false); 422 SDOEditor.this.dispose(); 423 } 424 }); 425 } 426 } 427 428 if (!visitor.getChangedResources().isEmpty()) 429 { 430 changedResources.addAll(visitor.getChangedResources()); 431 } 432 } 433 catch (CoreException exception) 434 { 435 SDOEditorPlugin.INSTANCE.log(exception); 436 } 437 } 438 } 439 }; 440 441 442 446 protected void handleActivate() 447 { 448 if (editingDomain.getResourceToReadOnlyMap() != null) 451 { 452 editingDomain.getResourceToReadOnlyMap().clear(); 453 454 setSelection(getSelection()); 457 } 458 459 if (!removedResources.isEmpty()) 460 { 461 if (handleDirtyConflict()) 462 { 463 getSite().getPage().closeEditor(SDOEditor.this, false); 464 SDOEditor.this.dispose(); 465 } 466 else 467 { 468 removedResources.clear(); 469 changedResources.clear(); 470 savedResources.clear(); 471 } 472 } 473 else if (!changedResources.isEmpty()) 474 { 475 changedResources.removeAll(savedResources); 476 handleChangedResources(); 477 changedResources.clear(); 478 savedResources.clear(); 479 } 480 } 481 482 483 487 protected void handleChangedResources() 488 { 489 if (!changedResources.isEmpty() && (!isDirty() || handleDirtyConflict())) 490 { 491 editingDomain.getCommandStack().flush(); 492 493 for (Iterator i = changedResources.iterator(); i.hasNext(); ) 494 { 495 Resource resource = (Resource)i.next(); 496 if (resource.isLoaded()) 497 { 498 resource.unload(); 499 try 500 { 501 resource.load(Collections.EMPTY_MAP); 502 } 503 catch (IOException exception) 504 { 505 SDOEditorPlugin.INSTANCE.log(exception); 506 } 507 } 508 } 509 } 510 } 511 512 516 protected boolean handleDirtyConflict() 517 { 518 return 519 MessageDialog.openQuestion 520 (getSite().getShell(), 521 getString("_UI_FileConflict_label"), 522 getString("_WARN_FileConflict")); 523 } 524 525 531 public SDOEditor() 532 { 533 super(); 534 535 List factories = new ArrayList (); 538 factories.add 539 (new ResourceItemProviderAdapterFactory() 540 { 541 public Adapter createResourceSetAdapter() 542 { 543 return 544 new ResourceSetItemProvider(this) 545 { 546 public Collection getChildren(Object object) 547 { 548 Collection result = new ArrayList (super.getChildren(object)); 549 for (Iterator i = result.iterator(); i.hasNext(); ) 550 { 551 Resource resource = (Resource)i.next(); 552 if (resource.getContents().size() == 1) 553 { 554 Object content = resource.getContents().get(0); 555 if (content instanceof EChangeSummary || content instanceof EDataObject) 556 { 557 i.remove(); 558 } 559 } 560 } 561 return result; 562 } 563 }; 564 } 565 }); 566 factories.add(new SDOItemProviderAdapterFactory()); 567 factories.add(new ChangeItemProviderAdapterFactory()); 568 factories.add(new EcoreItemProviderAdapterFactory()); 569 571 adapterFactory = new ComposedAdapterFactory(factories); 572 573 BasicCommandStack commandStack = new BasicCommandStack(); 576 577 commandStack.addCommandStackListener 580 (new CommandStackListener() 581 { 582 public void commandStackChanged(final EventObject event) 583 { 584 getContainer().getDisplay().asyncExec 585 (new Runnable () 586 { 587 public void run() 588 { 589 firePropertyChange(IEditorPart.PROP_DIRTY); 590 591 Command mostRecentCommand = ((CommandStack)event.getSource()).getMostRecentCommand(); 594 if (mostRecentCommand != null) 595 { 596 setSelectionToViewer(mostRecentCommand.getAffectedObjects()); 597 } 598 if (propertySheetPage != null) 599 { 600 propertySheetPage.refresh(); 601 } 602 } 603 }); 604 } 605 }); 606 607 editingDomain = new AdapterFactoryEditingDomain(adapterFactory, commandStack, new HashMap ()); 610 } 611 612 618 protected void firePropertyChange(int action) 619 { 620 super.firePropertyChange(action); 621 } 622 623 629 public void setSelectionToViewer(Collection collection) 630 { 631 final Collection theSelection = collection; 632 if (theSelection != null && !theSelection.isEmpty()) 635 { 636 Runnable runnable = 642 new Runnable () 643 { 644 public void run() 645 { 646 if (currentViewer != null) 649 { 650 currentViewer.setSelection(new StructuredSelection(theSelection.toArray()), true); 651 } 652 } 653 }; 654 runnable.run(); 655 } 656 } 657 658 666 public EditingDomain getEditingDomain() 667 { 668 return editingDomain; 669 } 670 671 676 public class ReverseAdapterFactoryContentProvider extends AdapterFactoryContentProvider 677 { 678 public ReverseAdapterFactoryContentProvider(AdapterFactory adapterFactory) 679 { 680 super(adapterFactory); 681 } 682 683 public Object [] getElements(Object object) 684 { 685 Object parent = super.getParent(object); 686 return (parent == null ? Collections.EMPTY_SET : Collections.singleton(parent)).toArray(); 687 } 688 689 public Object [] getChildren(Object object) 690 { 691 Object parent = super.getParent(object); 692 return (parent == null ? Collections.EMPTY_SET : Collections.singleton(parent)).toArray(); 693 } 694 695 public boolean hasChildren(Object object) 696 { 697 Object parent = super.getParent(object); 698 return parent != null; 699 } 700 701 public Object getParent(Object object) 702 { 703 return null; 704 } 705 } 706 707 712 public void setCurrentViewerPane(ViewerPane viewerPane) 713 { 714 if (currentViewerPane != viewerPane) 715 { 716 if (currentViewerPane != null) 717 { 718 currentViewerPane.showFocus(false); 719 } 720 currentViewerPane = viewerPane; 721 } 722 setCurrentViewer(currentViewerPane.getViewer()); 723 } 724 725 732 public void setCurrentViewer(Viewer viewer) 733 { 734 if (currentViewer != viewer) 737 { 738 if (selectionChangedListener == null) 739 { 740 selectionChangedListener = 743 new ISelectionChangedListener() 744 { 745 public void selectionChanged(SelectionChangedEvent selectionChangedEvent) 748 { 749 setSelection(selectionChangedEvent.getSelection()); 750 } 751 }; 752 } 753 754 if (currentViewer != null) 757 { 758 currentViewer.removeSelectionChangedListener(selectionChangedListener); 759 } 760 761 if (viewer != null) 764 { 765 viewer.addSelectionChangedListener(selectionChangedListener); 766 } 767 768 currentViewer = viewer; 771 772 setSelection(currentViewer == null ? StructuredSelection.EMPTY : currentViewer.getSelection()); 775 } 776 } 777 778 784 public Viewer getViewer() 785 { 786 return currentViewer; 787 } 788 789 795 protected void createContextMenuFor(StructuredViewer viewer) 796 { 797 MenuManager contextMenu = new MenuManager("#PopUp"); 798 contextMenu.add(new Separator("additions")); 799 contextMenu.setRemoveAllWhenShown(true); 800 contextMenu.addMenuListener(this); 801 Menu menu= contextMenu.createContextMenu(viewer.getControl()); 802 viewer.getControl().setMenu(menu); 803 getSite().registerContextMenu(contextMenu, viewer); 804 805 int dndOperations = DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_LINK; 806 Transfer[] transfers = new Transfer[] { LocalTransfer.getInstance() }; 807 viewer.addDragSupport(dndOperations, transfers, new ViewerDragAdapter(viewer)); 808 viewer.addDropSupport(dndOperations, transfers, new EditingDomainViewerDropAdapter(editingDomain, viewer)); 809 } 810 811 817 public void createModel() 818 { 819 IFileEditorInput modelFile = (IFileEditorInput)getEditorInput(); 822 823 try 824 { 825 Resource resource = 828 editingDomain.loadResource 829 (URI.createPlatformResourceURI(modelFile.getFile().getFullPath().toString()).toString()); 830 831 EDataGraph eDataGraph = (EDataGraph)resource.getContents().get(0); 832 eDataGraph.setResourceSet(editingDomain.getResourceSet()); 833 eDataGraph.getChangeSummary(); } 835 catch (Exception exception) 836 { 837 SDOEditorPlugin.INSTANCE.log(exception); 838 } 839 } 840 841 847 public void createPages() 848 { 849 createModel(); 852 853 { 856 ViewerPane viewerPane = 857 new ViewerPane(getSite().getPage(), SDOEditor.this) 858 { 859 public Viewer createViewer(Composite composite) 860 { 861 Tree tree = new Tree(composite, SWT.MULTI); 862 TreeViewer newTreeViewer = new TreeViewer(tree); 863 return newTreeViewer; 864 } 865 public void requestActivation() 866 { 867 super.requestActivation(); 868 setCurrentViewerPane(this); 869 } 870 }; 871 viewerPane.createControl(getContainer()); 872 873 selectionViewer = (TreeViewer)viewerPane.getViewer(); 874 selectionViewer.setContentProvider(new AdapterFactoryContentProvider(adapterFactory)); 875 876 selectionViewer.setLabelProvider(new AdapterFactoryLabelProvider(adapterFactory)); 877 selectionViewer.setInput(editingDomain.getResourceSet()); 878 viewerPane.setTitle(editingDomain.getResourceSet()); 879 880 new AdapterFactoryTreeEditor(selectionViewer.getTree(), adapterFactory); 881 882 createContextMenuFor(selectionViewer); 883 int pageIndex = addPage(viewerPane.getControl()); 884 setPageText(pageIndex, getString("_UI_SelectionPage_label")); 885 } 886 887 { 890 ViewerPane viewerPane = 891 new ViewerPane(getSite().getPage(), SDOEditor.this) 892 { 893 public Viewer createViewer(Composite composite) 894 { 895 Tree tree = new Tree(composite, SWT.MULTI); 896 TreeViewer newTreeViewer = new TreeViewer(tree); 897 return newTreeViewer; 898 } 899 public void requestActivation() 900 { 901 super.requestActivation(); 902 setCurrentViewerPane(this); 903 } 904 }; 905 viewerPane.createControl(getContainer()); 906 907 parentViewer = (TreeViewer)viewerPane.getViewer(); 908 parentViewer.setAutoExpandLevel(30); 909 parentViewer.setContentProvider(new ReverseAdapterFactoryContentProvider(adapterFactory)); 910 parentViewer.setLabelProvider(new AdapterFactoryLabelProvider(adapterFactory)); 911 912 createContextMenuFor(parentViewer); 913 int pageIndex = addPage(viewerPane.getControl()); 914 setPageText(pageIndex, getString("_UI_ParentPage_label")); 915 } 916 917 { 920 ViewerPane viewerPane = 921 new ViewerPane(getSite().getPage(), SDOEditor.this) 922 { 923 public Viewer createViewer(Composite composite) 924 { 925 return new ListViewer(composite); 926 } 927 public void requestActivation() 928 { 929 super.requestActivation(); 930 setCurrentViewerPane(this); 931 } 932 }; 933 viewerPane.createControl(getContainer()); 934 listViewer = (ListViewer)viewerPane.getViewer(); 935 listViewer.setContentProvider(new AdapterFactoryContentProvider(adapterFactory)); 936 listViewer.setLabelProvider(new AdapterFactoryLabelProvider(adapterFactory)); 937 938 createContextMenuFor(listViewer); 939 int pageIndex = addPage(viewerPane.getControl()); 940 setPageText(pageIndex, getString("_UI_ListPage_label")); 941 } 942 943 { 946 ViewerPane viewerPane = 947 new ViewerPane(getSite().getPage(), SDOEditor.this) 948 { 949 public Viewer createViewer(Composite composite) 950 { 951 return new TreeViewer(composite); 952 } 953 public void requestActivation() 954 { 955 super.requestActivation(); 956 setCurrentViewerPane(this); 957 } 958 }; 959 viewerPane.createControl(getContainer()); 960 treeViewer = (TreeViewer)viewerPane.getViewer(); 961 treeViewer.setContentProvider(new AdapterFactoryContentProvider(adapterFactory)); 962 treeViewer.setLabelProvider(new AdapterFactoryLabelProvider(adapterFactory)); 963 964 new AdapterFactoryTreeEditor(treeViewer.getTree(), adapterFactory); 965 966 createContextMenuFor(treeViewer); 967 int pageIndex = addPage(viewerPane.getControl()); 968 setPageText(pageIndex, getString("_UI_TreePage_label")); 969 } 970 971 { 974 ViewerPane viewerPane = 975 new ViewerPane(getSite().getPage(), SDOEditor.this) 976 { 977 public Viewer createViewer(Composite composite) 978 { 979 return new TableViewer(composite); 980 } 981 public void requestActivation() 982 { 983 super.requestActivation(); 984 setCurrentViewerPane(this); 985 } 986 }; 987 viewerPane.createControl(getContainer()); 988 tableViewer = (TableViewer)viewerPane.getViewer(); 989 990 Table table = tableViewer.getTable(); 991 TableLayout layout = new TableLayout(); 992 table.setLayout(layout); 993 table.setHeaderVisible(true); 994 table.setLinesVisible(true); 995 996 TableColumn objectColumn = new TableColumn(table, SWT.NONE); 997 layout.addColumnData(new ColumnWeightData(3, 100, true)); 998 objectColumn.setText(getString("_UI_ObjectColumn_label")); 999 objectColumn.setResizable(true); 1000 1001 TableColumn selfColumn = new TableColumn(table, SWT.NONE); 1002 layout.addColumnData(new ColumnWeightData(2, 100, true)); 1003 selfColumn.setText(getString("_UI_SelfColumn_label")); 1004 selfColumn.setResizable(true); 1005 1006 tableViewer.setColumnProperties(new String [] {"a", "b"}); 1007 tableViewer.setContentProvider(new AdapterFactoryContentProvider(adapterFactory)); 1008 tableViewer.setLabelProvider(new AdapterFactoryLabelProvider(adapterFactory)); 1009 1010 createContextMenuFor(tableViewer); 1011 int pageIndex = addPage(viewerPane.getControl()); 1012 setPageText(pageIndex, getString("_UI_TablePage_label")); 1013 } 1014 1015 { 1018 ViewerPane viewerPane = 1019 new ViewerPane(getSite().getPage(), SDOEditor.this) 1020 { 1021 public Viewer createViewer(Composite composite) 1022 { 1023 return new TreeViewer(composite); 1024 } 1025 public void requestActivation() 1026 { 1027 super.requestActivation(); 1028 setCurrentViewerPane(this); 1029 } 1030 }; 1031 viewerPane.createControl(getContainer()); 1032 1033 treeViewerWithColumns = (TreeViewer)viewerPane.getViewer(); 1034 1035 Tree tree = treeViewerWithColumns.getTree(); 1036 tree.setLayoutData(new FillLayout()); 1037 tree.setHeaderVisible(true); 1038 tree.setLinesVisible(true); 1039 1040 TreeColumn objectColumn = new TreeColumn(tree, SWT.NONE); 1041 objectColumn.setText(getString("_UI_ObjectColumn_label")); 1042 objectColumn.setResizable(true); 1043 objectColumn.setWidth(250); 1044 1045 TreeColumn selfColumn = new TreeColumn(tree, SWT.NONE); 1046 selfColumn.setText(getString("_UI_SelfColumn_label")); 1047 selfColumn.setResizable(true); 1048 selfColumn.setWidth(200); 1049 1050 treeViewerWithColumns.setColumnProperties(new String [] {"a", "b"}); 1051 treeViewerWithColumns.setContentProvider(new AdapterFactoryContentProvider(adapterFactory)); 1052 treeViewerWithColumns.setLabelProvider(new AdapterFactoryLabelProvider(adapterFactory)); 1053 1054 createContextMenuFor(treeViewerWithColumns); 1055 int pageIndex = addPage(viewerPane.getControl()); 1056 setPageText(pageIndex, getString("_UI_TreeWithColumnsPage_label")); 1057 } 1058 1059 setActivePage(0); 1060 1061 getContainer().addControlListener 1062 (new ControlAdapter() 1063 { 1064 boolean guard = false; 1065 public void controlResized(ControlEvent event) 1066 { 1067 if (!guard) 1068 { 1069 guard = true; 1070 hideTabs(); 1071 guard = false; 1072 } 1073 } 1074 }); 1075 } 1076 1077 1084 protected void hideTabs() 1085 { 1086 if (getPageCount() <= 1) 1087 { 1088 setPageText(0, ""); 1089 if (getContainer() instanceof CTabFolder) 1090 { 1091 ((CTabFolder)getContainer()).setTabHeight(1); 1092 Point point = getContainer().getSize(); 1093 getContainer().setSize(point.x, point.y + 6); 1094 } 1095 } 1096 } 1097 1098 1104 protected void pageChange(int pageIndex) 1105 { 1106 super.pageChange(pageIndex); 1107 1108 Control control = getControl(pageIndex); 1111 if (control != null) 1112 { 1113 control.setVisible(true); 1114 control.setFocus(); 1115 } 1116 1117 if (contentOutlinePage != null) 1118 { 1119 handleContentOutlineSelection(contentOutlinePage.getSelection()); 1120 } 1121 } 1122 1123 1129 public Object getAdapter(Class key) 1130 { 1131 if (key.equals(IContentOutlinePage.class)) 1132 { 1133 return getContentOutlinePage(); 1134 } 1135 else if (key.equals(IPropertySheetPage.class)) 1136 { 1137 return getPropertySheetPage(); 1138 } 1139 else if (key.equals(IGotoMarker.class)) 1140 { 1141 return this; 1142 } 1143 else 1144 { 1145 return super.getAdapter(key); 1146 } 1147 } 1148 1149 1155 public IContentOutlinePage getContentOutlinePage() 1156 { 1157 if (contentOutlinePage == null) 1158 { 1159 class MyContentOutlinePage extends ContentOutlinePage 1162 { 1163 public void createControl(Composite parent) 1164 { 1165 super.createControl(parent); 1166 contentOutlineViewer = getTreeViewer(); 1167 contentOutlineViewer.addSelectionChangedListener(this); 1168 1169 contentOutlineViewer.setContentProvider(new AdapterFactoryContentProvider(adapterFactory)); 1172 contentOutlineViewer.setLabelProvider(new AdapterFactoryLabelProvider(adapterFactory)); 1173 contentOutlineViewer.setInput(editingDomain.getResourceSet()); 1174 1175 createContextMenuFor(contentOutlineViewer); 1178 1179 if (!editingDomain.getResourceSet().getResources().isEmpty()) 1180 { 1181 ArrayList selection = new ArrayList (); 1184 selection.add(editingDomain.getResourceSet().getResources().get(0)); 1185 contentOutlineViewer.setSelection(new StructuredSelection(selection), true); 1186 } 1187 } 1188 1189 public void makeContributions(IMenuManager menuManager, IToolBarManager toolBarManager, IStatusLineManager statusLineManager) 1190 { 1191 super.makeContributions(menuManager, toolBarManager, statusLineManager); 1192 contentOutlineStatusLineManager = statusLineManager; 1193 } 1194 1195 public void setActionBars(IActionBars actionBars) 1196 { 1197 super.setActionBars(actionBars); 1198 getActionBarContributor().shareGlobalActions(this, actionBars); 1199 } 1200 } 1201 1202 contentOutlinePage = new MyContentOutlinePage(); 1203 1204 contentOutlinePage.addSelectionChangedListener 1207 (new ISelectionChangedListener() 1208 { 1209 public void selectionChanged(SelectionChangedEvent event) 1212 { 1213 handleContentOutlineSelection(event.getSelection()); 1214 } 1215 }); 1216 } 1217 1218 return contentOutlinePage; 1219 } 1220 1221 1227 public IPropertySheetPage getPropertySheetPage() 1228 { 1229 if (propertySheetPage == null) 1230 { 1231 propertySheetPage = 1232 new PropertySheetPage() 1233 { 1234 public void makeContributions(IMenuManager menuManager, IToolBarManager toolBarManager, IStatusLineManager statusLineManager) 1235 { 1236 super.makeContributions(menuManager, toolBarManager, statusLineManager); 1237 } 1238 1239 public void setActionBars(IActionBars actionBars) 1240 { 1241 super.setActionBars(actionBars); 1242 getActionBarContributor().shareGlobalActions(this, actionBars); 1243 } 1244 }; 1245 propertySheetPage.setPropertySourceProvider(new AdapterFactoryContentProvider(adapterFactory)); 1246 } 1247 1248 return propertySheetPage; 1249 } 1250 1251 1257 public void handleContentOutlineSelection(ISelection selection) 1258 { 1259 if (currentViewerPane != null && !selection.isEmpty() && selection instanceof IStructuredSelection) 1260 { 1261 Iterator selectedElements = ((IStructuredSelection)selection).iterator(); 1262 if (selectedElements.hasNext()) 1263 { 1264 Object selectedElement = selectedElements.next(); 1267 1268 if (currentViewerPane.getViewer() == selectionViewer) 1271 { 1272 ArrayList selectionList = new ArrayList (); 1273 selectionList.add(selectedElement); 1274 while (selectedElements.hasNext()) 1275 { 1276 selectionList.add(selectedElements.next()); 1277 } 1278 1279 selectionViewer.setSelection(new StructuredSelection(selectionList)); 1282 } 1283 else 1284 { 1285 if (currentViewerPane.getViewer().getInput() != selectedElement) 1288 { 1289 currentViewerPane.getViewer().setInput(selectedElement); 1290 currentViewerPane.setTitle(selectedElement); 1291 } 1292 } 1293 } 1294 } 1295 } 1296 1297 1303 public boolean isDirty() 1304 { 1305 return ((BasicCommandStack)editingDomain.getCommandStack()).isSaveNeeded(); 1306 } 1307 1308 1314 public void doSave(IProgressMonitor progressMonitor) 1315 { 1316 WorkspaceModifyOperation operation = 1319 new WorkspaceModifyOperation() 1320 { 1321 public void execute(IProgressMonitor monitor) 1324 { 1325 try 1326 { 1327 Resource savedResource = (Resource)editingDomain.getResourceSet().getResources().get(0); 1330 savedResources.add(savedResource); 1331 savedResource.save(Collections.EMPTY_MAP); 1332 } 1333 catch (Exception exception) 1334 { 1335 SDOEditorPlugin.INSTANCE.log(exception); 1336 } 1337 } 1338 }; 1339 1340 try 1341 { 1342 new ProgressMonitorDialog(getSite().getShell()).run(true, false, operation); 1345 1346 ((BasicCommandStack)editingDomain.getCommandStack()).saveIsDone(); 1349 firePropertyChange(IEditorPart.PROP_DIRTY); 1350 } 1351 catch (Exception exception) 1352 { 1353 SDOEditorPlugin.INSTANCE.log(exception); 1356 } 1357 } 1358 1359 1365 public boolean isSaveAsAllowed() 1366 { 1367 return true; 1368 } 1369 1370 1376 public void doSaveAs() 1377 { 1378 SaveAsDialog saveAsDialog= new SaveAsDialog(getSite().getShell()); 1379 saveAsDialog.open(); 1380 IPath path= saveAsDialog.getResult(); 1381 if (path != null) 1382 { 1383 IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path); 1384 if (file != null) 1385 { 1386 doSaveAs(URI.createPlatformResourceURI(file.getFullPath().toString()), new FileEditorInput(file)); 1387 } 1388 } 1389 } 1390 1391 1396 protected void doSaveAs(URI uri, IEditorInput editorInput) 1397 { 1398 ((Resource)editingDomain.getResourceSet().getResources().get(0)).setURI(uri); 1399 setInput(editorInput); 1400 setPartName(editorInput.getName()); 1401 IProgressMonitor progressMonitor = 1402 getActionBars().getStatusLineManager() != null ? 1403 getActionBars().getStatusLineManager().getProgressMonitor() : 1404 new NullProgressMonitor(); 1405 doSave(progressMonitor); 1406 } 1407 1408 1413 public void gotoMarker(IMarker marker) 1414 { 1415 try 1416 { 1417 if (marker.getType().equals(EValidator.MARKER)) 1418 { 1419 String uriAttribute = marker.getAttribute(EValidator.URI_ATTRIBUTE, null); 1420 if (uriAttribute != null) 1421 { 1422 URI uri = URI.createURI(uriAttribute); 1423 EObject eObject = editingDomain.getResourceSet().getEObject(uri, true); 1424 if (eObject != null) 1425 { 1426 setSelectionToViewer(Collections.singleton(editingDomain.getWrapper(eObject))); 1427 } 1428 } 1429 } 1430 } 1431 catch (CoreException exception) 1432 { 1433 SDOEditorPlugin.INSTANCE.log(exception); 1434 } 1435 } 1436 1437 1443 public void init(IEditorSite site, IEditorInput editorInput) throws PartInitException 1444 { 1445 setSite(site); 1446 setInput(editorInput); 1447 setPartName(editorInput.getName()); 1448 site.setSelectionProvider(this); 1449 site.getPage().addPartListener(partListener); 1450 ResourcesPlugin.getWorkspace().addResourceChangeListener(resourceChangeListener, IResourceChangeEvent.POST_CHANGE); 1451 } 1452 1453 1458 public void setFocus() 1459 { 1460 getControl(getActivePage()).setFocus(); 1461 } 1462 1463 1469 public void addSelectionChangedListener(ISelectionChangedListener listener) 1470 { 1471 selectionChangedListeners.add(listener); 1472 } 1473 1474 1480 public void removeSelectionChangedListener(ISelectionChangedListener listener) 1481 { 1482 selectionChangedListeners.remove(listener); 1483 } 1484 1485 1491 public ISelection getSelection() 1492 { 1493 return editorSelection; 1494 } 1495 1496 1503 public void setSelection(ISelection selection) 1504 { 1505 editorSelection = selection; 1506 1507 for (Iterator listeners = selectionChangedListeners.iterator(); listeners.hasNext(); ) 1508 { 1509 ISelectionChangedListener listener = (ISelectionChangedListener)listeners.next(); 1510 listener.selectionChanged(new SelectionChangedEvent(this, selection)); 1511 } 1512 setStatusLineManager(selection); 1513 } 1514 1515 1520 public void setStatusLineManager(ISelection selection) 1521 { 1522 IStatusLineManager statusLineManager = currentViewer != null && currentViewer == contentOutlineViewer ? 1523 contentOutlineStatusLineManager : getActionBars().getStatusLineManager(); 1524 1525 if (statusLineManager != null) 1526 { 1527 if (selection instanceof IStructuredSelection) 1528 { 1529 Collection collection = ((IStructuredSelection)selection).toList(); 1530 switch (collection.size()) 1531 { 1532 case 0: 1533 { 1534 statusLineManager.setMessage(getString("_UI_NoObjectSelected")); 1535 break; 1536 } 1537 case 1: 1538 { 1539 String text = new AdapterFactoryItemDelegator(adapterFactory).getText(collection.iterator().next()); 1540 statusLineManager.setMessage(getString("_UI_SingleObjectSelected", text)); 1541 break; 1542 } 1543 default: 1544 { 1545 statusLineManager.setMessage(getString("_UI_MultiObjectSelected", Integer.toString(collection.size()))); 1546 break; 1547 } 1548 } 1549 } 1550 else 1551 { 1552 statusLineManager.setMessage(""); 1553 } 1554 } 1555 } 1556 1557 1563 private static String getString(String key) 1564 { 1565 return SDOEditorPlugin.INSTANCE.getString(key); 1566 } 1567 1568 1574 private static String getString(String key, Object s1) 1575 { 1576 return SDOEditorPlugin.INSTANCE.getString(key, new Object [] { s1 }); 1577 } 1578 1579 1585 public void menuAboutToShow(IMenuManager menuManager) 1586 { 1587 ((IMenuListener)getEditorSite().getActionBarContributor()).menuAboutToShow(menuManager); 1588 } 1589 1590 1595 public EditingDomainActionBarContributor getActionBarContributor() 1596 { 1597 return (EditingDomainActionBarContributor)getEditorSite().getActionBarContributor(); 1598 } 1599 1600 1605 public IActionBars getActionBars() 1606 { 1607 return getActionBarContributor().getActionBars(); 1608 } 1609 1610 1615 public AdapterFactory getAdapterFactory() 1616 { 1617 return adapterFactory; 1618 } 1619 1620 1625 public void dispose() 1626 { 1627 ResourcesPlugin.getWorkspace().removeResourceChangeListener(resourceChangeListener); 1628 1629 getSite().getPage().removePartListener(partListener); 1630 1631 adapterFactory.dispose(); 1632 1633 if (getActionBarContributor().getActiveEditor() == this) 1634 { 1635 getActionBarContributor().setActiveEditor(null); 1636 } 1637 1638 if (propertySheetPage != null) 1639 { 1640 propertySheetPage.dispose(); 1641 } 1642 1643 if (contentOutlinePage != null) 1644 { 1645 contentOutlinePage.dispose(); 1646 } 1647 1648 super.dispose(); 1649 } 1650 1651} 1652 | Popular Tags |