KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > codegen > ecore > genmodel > presentation > GenModelEditor


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: GenModelEditor.java,v 1.15 2005/06/12 13:23:31 emerks Exp $
16  */

17 package org.eclipse.emf.codegen.ecore.genmodel.presentation;
18
19
20 import java.io.IOException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.EventObject JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import org.eclipse.core.resources.IFile;
29 import org.eclipse.core.resources.IMarker;
30 import org.eclipse.core.resources.IResource;
31 import org.eclipse.core.resources.IResourceChangeEvent;
32 import org.eclipse.core.resources.IResourceChangeListener;
33 import org.eclipse.core.resources.IResourceDelta;
34 import org.eclipse.core.resources.IResourceDeltaVisitor;
35 import org.eclipse.core.resources.ResourcesPlugin;
36 import org.eclipse.core.runtime.CoreException;
37 import org.eclipse.core.runtime.IPath;
38 import org.eclipse.core.runtime.IProgressMonitor;
39 import org.eclipse.core.runtime.IStatus;
40 import org.eclipse.jdt.core.JavaCore;
41 import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
42 import org.eclipse.jface.action.IMenuListener;
43 import org.eclipse.jface.action.IMenuManager;
44 import org.eclipse.jface.action.IStatusLineManager;
45 import org.eclipse.jface.action.IToolBarManager;
46 import org.eclipse.jface.action.MenuManager;
47 import org.eclipse.jface.action.Separator;
48 import org.eclipse.jface.dialogs.ErrorDialog;
49 import org.eclipse.jface.dialogs.MessageDialog;
50 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
51 import org.eclipse.jface.viewers.ISelection;
52 import org.eclipse.jface.viewers.ISelectionChangedListener;
53 import org.eclipse.jface.viewers.ISelectionProvider;
54 import org.eclipse.jface.viewers.IStructuredSelection;
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.TreeViewer;
59 import org.eclipse.jface.viewers.Viewer;
60 import org.eclipse.swt.SWT;
61 import org.eclipse.swt.custom.CTabFolder;
62 import org.eclipse.swt.dnd.DND;
63 import org.eclipse.swt.dnd.Transfer;
64 import org.eclipse.swt.events.ControlAdapter;
65 import org.eclipse.swt.events.ControlEvent;
66 import org.eclipse.swt.graphics.Point;
67 import org.eclipse.swt.widgets.Control;
68 import org.eclipse.swt.widgets.Menu;
69 import org.eclipse.swt.widgets.Tree;
70 import org.eclipse.ui.IActionBars;
71 import org.eclipse.ui.IEditorInput;
72 import org.eclipse.ui.IEditorPart;
73 import org.eclipse.ui.IEditorSite;
74 import org.eclipse.ui.IFileEditorInput;
75 import org.eclipse.ui.IPartListener;
76 import org.eclipse.ui.IWorkbenchPart;
77 import org.eclipse.ui.PartInitException;
78 import org.eclipse.ui.actions.WorkspaceModifyOperation;
79 import org.eclipse.ui.dialogs.SaveAsDialog;
80 import org.eclipse.ui.part.FileEditorInput;
81 import org.eclipse.ui.part.MultiPageEditorPart;
82 import org.eclipse.ui.views.properties.IPropertySheetPage;
83 import org.eclipse.ui.views.properties.PropertySheet;
84 import org.eclipse.ui.views.properties.PropertySheetPage;
85
86 import org.eclipse.emf.codegen.ecore.genmodel.GenModel;
87 import org.eclipse.emf.codegen.ecore.genmodel.provider.GenModelEditPlugin;
88 import org.eclipse.emf.common.command.BasicCommandStack;
89 import org.eclipse.emf.common.command.Command;
90 import org.eclipse.emf.common.command.CommandStack;
91 import org.eclipse.emf.common.command.CommandStackListener;
92 import org.eclipse.emf.common.notify.AdapterFactory;
93 import org.eclipse.emf.common.ui.viewer.IViewerProvider;
94 import org.eclipse.emf.common.util.URI;
95 import org.eclipse.emf.ecore.plugin.EcorePlugin;
96 import org.eclipse.emf.ecore.resource.Resource;
97 import org.eclipse.emf.ecore.resource.ResourceSet;
98 import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
99 import org.eclipse.emf.edit.domain.EditingDomain;
100 import org.eclipse.emf.edit.domain.IEditingDomainProvider;
101 import org.eclipse.emf.edit.provider.AdapterFactoryItemDelegator;
102 import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
103 import org.eclipse.emf.edit.ui.action.EditingDomainActionBarContributor;
104 import org.eclipse.emf.edit.ui.celleditor.AdapterFactoryTreeEditor;
105 import org.eclipse.emf.edit.ui.dnd.EditingDomainViewerDropAdapter;
106 import org.eclipse.emf.edit.ui.dnd.LocalTransfer;
107 import org.eclipse.emf.edit.ui.dnd.ViewerDragAdapter;
108 import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider;
109 import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
110
111
112 /**
113  * This is an example of a GenModel model editor.
114  * <!-- begin-user-doc -->
115  * <!-- end-user-doc -->
116  * @generated NOT
117  */

118 public class GenModelEditor
119   extends MultiPageEditorPart
120   implements IEditingDomainProvider, ISelectionProvider, IMenuListener, IViewerProvider
121 {
122   /**
123    * This keeps track of the editing domain that is used to track all changes to the model.
124    * <!-- begin-user-doc -->
125    * <!-- end-user-doc -->
126    * @generated
127    */

128   protected AdapterFactoryEditingDomain editingDomain;
129
130   /**
131    * This is the one adapter factory used for providing views of the model.
132    * <!-- begin-user-doc -->
133    * <!-- end-user-doc -->
134    * @generated
135    */

136   protected ComposedAdapterFactory adapterFactory;
137
138   /**
139    * This is the property sheet page.
140    * <!-- begin-user-doc -->
141    * <!-- end-user-doc -->
142    * @generated
143    */

144   protected PropertySheetPage propertySheetPage;
145
146   /**
147    * This keeps track of the active content viewer, which may be either one of the viewers in the pages or the content outline viewer.
148    * <!-- begin-user-doc -->
149    * <!-- end-user-doc -->
150    * @generated
151    */

152   protected Viewer currentViewer;
153
154   /**
155    * This listens to which ever viewer is active.
156    * <!-- begin-user-doc -->
157    * <!-- end-user-doc -->
158    * @generated
159    */

160   protected ISelectionChangedListener selectionChangedListener;
161
162   /**
163    * This keeps track of all the {@link org.eclipse.jface.viewers.ISelectionChangedListener}s that are listening to this editor.
164    * <!-- begin-user-doc -->
165    * <!-- end-user-doc -->
166    * @generated
167    */

168   protected Collection JavaDoc selectionChangedListeners = new ArrayList JavaDoc();
169
170   /**
171    * This keeps track of the selection of the editor as a whole.
172    * <!-- begin-user-doc -->
173    * <!-- end-user-doc -->
174    * @generated
175    */

176   protected ISelection editorSelection = StructuredSelection.EMPTY;
177
178   /**
179    * This listens for when the outline becomes active
180    * <!-- begin-user-doc -->
181    * <!-- end-user-doc -->
182    * @generated NOT
183    */

184   protected IPartListener partListener =
185     new IPartListener()
186     {
187       public void partActivated(IWorkbenchPart p)
188       {
189         if (p instanceof PropertySheet)
190         {
191           if (((PropertySheet)p).getCurrentPage() == propertySheetPage)
192           {
193             getActionBarContributor().setActiveEditor(GenModelEditor.this);
194             handleActivate();
195           }
196         }
197         else if (p == GenModelEditor.this)
198         {
199           handleActivate();
200         }
201       }
202       public void partBroughtToTop(IWorkbenchPart p)
203       {
204       }
205       public void partClosed(IWorkbenchPart p)
206       {
207       }
208       public void partDeactivated(IWorkbenchPart p)
209       {
210       }
211       public void partOpened(IWorkbenchPart p)
212       {
213       }
214     };
215
216   /**
217    * Resources that have been removed since last activation.
218    * @generated
219    */

220   Collection JavaDoc removedResources = new ArrayList JavaDoc();
221
222   /**
223    * Resources that have been changed since last activation.
224    * @generated
225    */

226   Collection JavaDoc changedResources = new ArrayList JavaDoc();
227
228   /**
229    * Resources that have been saved.
230    * @generated
231    */

232   Collection JavaDoc savedResources = new ArrayList JavaDoc();
233
234   /**
235    * This listens for workspace changes.
236    * <!-- begin-user-doc -->
237    * <!-- end-user-doc -->
238    * @generated
239    */

240   protected IResourceChangeListener resourceChangeListener =
241     new IResourceChangeListener()
242     {
243       public void resourceChanged(IResourceChangeEvent event)
244       {
245         // Only listening to these.
246
// if (event.getType() == IResourceDelta.POST_CHANGE)
247
{
248           IResourceDelta delta = event.getDelta();
249           try
250           {
251             class ResourceDeltaVisitor implements IResourceDeltaVisitor
252             {
253               protected ResourceSet resourceSet = editingDomain.getResourceSet();
254               protected Collection JavaDoc changedResources = new ArrayList JavaDoc();
255               protected Collection JavaDoc removedResources = new ArrayList JavaDoc();
256
257               public boolean visit(IResourceDelta delta)
258               {
259                 if (delta.getResource().getType() == IResource.FILE)
260                 {
261                   if ((delta.getKind() & (IResourceDelta.CHANGED | IResourceDelta.REMOVED)) != 0)
262                   {
263                     Resource resource = resourceSet.getResource(URI.createURI(delta.getFullPath().toString()), false);
264                     if (resource != null)
265                     {
266                       if ((delta.getKind() & IResourceDelta.REMOVED) != 0)
267                       {
268                         removedResources.add(resource);
269                       }
270                       else
271                       {
272                         changedResources.add(resource);
273                       }
274                     }
275                   }
276                 }
277
278                 return true;
279               }
280
281               public Collection JavaDoc getChangedResources()
282               {
283                 return changedResources;
284               }
285
286               public Collection JavaDoc getRemovedResources()
287               {
288                 return removedResources;
289               }
290             }
291
292             ResourceDeltaVisitor visitor = new ResourceDeltaVisitor();
293             delta.accept(visitor);
294
295             if (!visitor.getRemovedResources().isEmpty())
296             {
297               removedResources.addAll(visitor.getRemovedResources());
298               if (!isDirty())
299               {
300                 getSite().getShell().getDisplay().asyncExec
301                   (new Runnable JavaDoc()
302                    {
303                      public void run()
304                      {
305                        getSite().getPage().closeEditor(GenModelEditor.this, false);
306                        GenModelEditor.this.dispose();
307                      }
308                    });
309               }
310             }
311
312             if (!visitor.getChangedResources().isEmpty())
313             {
314               changedResources.addAll(visitor.getChangedResources());
315             }
316           }
317           catch (CoreException exception)
318           {
319             GenModelEditPlugin.INSTANCE.log(exception);
320           }
321         }
322       }
323     };
324
325
326   /**
327    * Handles activation of the editor or it's associated views.
328    * @generated
329    */

330   public void handleActivate()
331   {
332     if (!removedResources.isEmpty())
333     {
334       if (handleDirtyConflict())
335       {
336         getSite().getPage().closeEditor(GenModelEditor.this, false);
337         GenModelEditor.this.dispose();
338       }
339       else
340       {
341         removedResources.clear();
342         changedResources.clear();
343         savedResources.clear();
344       }
345     }
346     else if (!changedResources.isEmpty())
347     {
348       changedResources.removeAll(savedResources);
349       handleChangedResources();
350       changedResources.clear();
351       savedResources.clear();
352     }
353   }
354
355
356   /**
357    * Handles what to do with changed resources on activation.
358    * @generated NOT
359    */

360   protected void handleChangedResources()
361   {
362     if (!changedResources.isEmpty() && (!isDirty() || handleDirtyConflict()))
363     {
364       editingDomain.getCommandStack().flush();
365       Resource mainResource = (Resource)editingDomain.getResourceSet().getResources().get(0);
366       boolean reload = false;
367       for (Iterator JavaDoc i = changedResources.iterator(); i.hasNext(); )
368       {
369         Resource resource = (Resource)i.next();
370         if (resource == mainResource)
371         {
372           reload = true;
373         }
374         if (resource.isLoaded())
375         {
376           resource.unload();
377           try
378           {
379             resource.load(Collections.EMPTY_MAP);
380           }
381           catch (IOException JavaDoc exception)
382           {
383             GenModelEditPlugin.INSTANCE.log(exception);
384           }
385         }
386       }
387
388       if (reload)
389       {
390         GenModel genModel = (GenModel)mainResource.getContents().get(0);
391         initialize(genModel);
392         currentViewer.setInput(mainResource);
393       }
394     }
395   }
396
397   protected void initialize(GenModel genModel)
398   {
399     genModel.reconcile();
400     genModel.setCanGenerate(true);
401     validate();
402
403     Map JavaDoc options = JavaCore.getOptions();
404     String JavaDoc tabSize = (String JavaDoc)options.get(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE);
405     String JavaDoc braceStyle = (String JavaDoc)options.get(DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_TYPE_DECLARATION);
406     String JavaDoc tabCharacter = (String JavaDoc)options.get(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR);
407     if (JavaCore.TAB.equals(tabCharacter))
408     {
409        genModel.getJControlModel().setLeadingTabReplacement("\t");
410     }
411     else
412     {
413       String JavaDoc spaces = "";
414       for (int i = Integer.parseInt(tabSize); i > 0; --i)
415       {
416         spaces += " ";
417       }
418       genModel.getJControlModel().setLeadingTabReplacement(spaces);
419     }
420     genModel.getJControlModel().setConvertToStandardBraceStyle(DefaultCodeFormatterConstants.END_OF_LINE.equals(braceStyle));
421   }
422
423   /**
424    * Shows a dialog that asks if conflicting changes should be discarded.
425    * @generated
426    */

427   protected boolean handleDirtyConflict()
428   {
429     return
430       MessageDialog.openQuestion
431         (getSite().getShell(),
432          getString("_UI_FileConflict_label"),
433          getString("_WARN_FileConflict"));
434   }
435
436   /**
437    * This creates a model editor.
438    * <!-- begin-user-doc -->
439    * <!-- end-user-doc -->
440    * @generated NOT
441    */

442   public GenModelEditor()
443   {
444     super();
445
446     adapterFactory = new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE);
447
448     // Create the command stack that will notify this editor as commands are executed.
449
//
450
BasicCommandStack commandStack = new BasicCommandStack();
451
452     // Add a listener to set the most recent command's affected objects to be the selection of the viewer with focus.
453
//
454
commandStack.addCommandStackListener
455       (new CommandStackListener()
456        {
457          public void commandStackChanged(final EventObject JavaDoc event)
458          {
459            getContainer().getDisplay().asyncExec
460              (new Runnable JavaDoc()
461               {
462                 public void run()
463                 {
464                   firePropertyChange(IEditorPart.PROP_DIRTY);
465
466                   // Try to select the affected objects.
467
//
468
Command mostRecentCommand = ((CommandStack)event.getSource()).getMostRecentCommand();
469                   if (mostRecentCommand != null)
470                   {
471                     setSelectionToViewer(mostRecentCommand.getAffectedObjects());
472                   }
473
474                   if (propertySheetPage != null)
475                   {
476                     propertySheetPage.refresh();
477                   }
478                 }
479               });
480          }
481        });
482
483     // Create the editing domain with a special command stack.
484
//
485
editingDomain = new AdapterFactoryEditingDomain(adapterFactory, commandStack);
486   }
487
488   /**
489    * This is here for the listener to be able to call it.
490    * <!-- begin-user-doc -->
491    * <!-- end-user-doc -->
492    * @generated
493    */

494   protected void firePropertyChange(int action)
495   {
496     super.firePropertyChange(action);
497   }
498
499   /**
500    * This sets the selection into whichever viewer is active.
501    * <!-- begin-user-doc -->
502    * <!-- end-user-doc -->
503    * @generated
504    */

505   public void setSelectionToViewer(Collection JavaDoc collection)
506   {
507     final Collection JavaDoc theSelection = collection;
508     // Make sure it's okay.
509
//
510
if (theSelection != null && !theSelection.isEmpty())
511     {
512       // I don't know if this should be run this deferred
513
// because we might have to give the editor a chance to process the viewer update events
514
// and hence to update the views first.
515
//
516
//
517
Runnable JavaDoc runnable =
518         new Runnable JavaDoc()
519         {
520           public void run()
521           {
522             // Try to select the items in the current content viewer of the editor.
523
//
524
if (currentViewer != null)
525             {
526               currentViewer.setSelection(new StructuredSelection(theSelection.toArray()), true);
527             }
528           }
529         };
530       runnable.run();
531     }
532   }
533
534   /**
535    * This returns the editing domain as required by the {@link IEditingDomainProvider} interface.
536    * This is important for implementing the static methods of {@link AdapterFactoryEditingDomain}
537    * and for supporting {@link org.eclipse.emf.edit.ui.action.CommandAction}.
538    * <!-- begin-user-doc -->
539    * <!-- end-user-doc -->
540    * @generated
541    */

542   public EditingDomain getEditingDomain()
543   {
544     return editingDomain;
545   }
546
547   /**
548    * <!-- begin-user-doc -->
549    * <!-- end-user-doc -->
550    * @generated
551    */

552   public class ReverseAdapterFactoryContentProvider extends AdapterFactoryContentProvider
553   {
554     public ReverseAdapterFactoryContentProvider(AdapterFactory adapterFactory)
555     {
556       super(adapterFactory);
557     }
558
559     public Object JavaDoc [] getElements(Object JavaDoc object)
560     {
561       Object JavaDoc parent = super.getParent(object);
562       return (parent == null ? Collections.EMPTY_SET : Collections.singleton(parent)).toArray();
563     }
564
565     public Object JavaDoc [] getChildren(Object JavaDoc object)
566     {
567       Object JavaDoc parent = super.getParent(object);
568       return (parent == null ? Collections.EMPTY_SET : Collections.singleton(parent)).toArray();
569     }
570
571     public boolean hasChildren(Object JavaDoc object)
572     {
573       Object JavaDoc parent = super.getParent(object);
574       return parent != null;
575     }
576
577     public Object JavaDoc getParent(Object JavaDoc object)
578     {
579       return null;
580     }
581   }
582
583
584   /**
585    * This makes sure that one content viewer, either for the current page or the outline view, if it has focus,
586    * is the current one.
587    * <!-- begin-user-doc -->
588    * <!-- end-user-doc -->
589    * @generated
590    */

591   public void setCurrentViewer(Viewer viewer)
592   {
593     // If it is changing...
594
//
595
if (currentViewer != viewer)
596     {
597       if (selectionChangedListener == null)
598       {
599         // Create the listener on demand.
600
//
601
selectionChangedListener =
602           new ISelectionChangedListener()
603           {
604             // This just notifies those things that are affected by the section.
605
//
606
public void selectionChanged(SelectionChangedEvent selectionChangedEvent)
607             {
608               setSelection(selectionChangedEvent.getSelection());
609             }
610           };
611       }
612
613       // Stop listening to the old one.
614
//
615
if (currentViewer != null)
616       {
617         currentViewer.removeSelectionChangedListener(selectionChangedListener);
618       }
619
620       // Start listening to the new one.
621
//
622
if (viewer != null)
623       {
624         viewer.addSelectionChangedListener(selectionChangedListener);
625       }
626
627       // Remember it.
628
//
629
currentViewer = viewer;
630
631       // Set the editors selection based on the current viewer's selection.
632
//
633
setSelection(currentViewer == null ? StructuredSelection.EMPTY : currentViewer.getSelection());
634     }
635   }
636
637   /**
638    * This returns the viewer as required by the {@link IViewerProvider} interface.
639    * <!-- begin-user-doc -->
640    * <!-- end-user-doc -->
641    * @generated
642    */

643   public Viewer getViewer()
644   {
645     return currentViewer;
646   }
647
648   /**
649    * This creates a context menu for the viewer and adds a listener as well registering the menu for extension.
650    * <!-- begin-user-doc -->
651    * <!-- end-user-doc -->
652    * @generated
653    */

654   protected void createContextMenuFor(StructuredViewer viewer)
655   {
656     MenuManager contextMenu = new MenuManager("#PopUp");
657     contextMenu.add(new Separator("additions"));
658     contextMenu.setRemoveAllWhenShown(true);
659     contextMenu.addMenuListener(this);
660     Menu menu= contextMenu.createContextMenu(viewer.getControl());
661     viewer.getControl().setMenu(menu);
662     getSite().registerContextMenu(contextMenu, viewer);
663
664     int dndOperations = DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_LINK;
665     Transfer[] transfers = new Transfer[] { LocalTransfer.getInstance() };
666     viewer.addDragSupport(dndOperations, transfers, new ViewerDragAdapter(viewer));
667     viewer.addDropSupport(dndOperations, transfers, new EditingDomainViewerDropAdapter(editingDomain, viewer));
668   }
669
670   /**
671    * This is the method used by the framework to install your own controls.
672    * <!-- begin-user-doc -->
673    * <!-- end-user-doc -->
674    * @generated NOT
675    */

676   public void createPages()
677   {
678     // I assume that the input is a file object.
679
//
680
IFileEditorInput modelFile = (IFileEditorInput)getEditorInput();
681
682     editingDomain.getResourceSet().getURIConverter().getURIMap().putAll(EcorePlugin.computePlatformURIMap());
683     
684     try
685     {
686       // Load the resource through the editing domain.
687
//
688
Resource resource =
689         editingDomain.loadResource
690           (URI.createPlatformResourceURI(modelFile.getFile().getFullPath().toString()).toString());
691
692       // Initialize the formatting styles based on the Eclipse preferences.
693
//
694
GenModel genModel = (GenModel)resource.getContents().get(0);
695       initialize(genModel);
696     }
697     catch (Exception JavaDoc exception)
698     {
699       GenModelEditPlugin.INSTANCE.log(exception);
700     }
701
702     // Create a page for the selection tree view.
703
//
704
{
705       Tree tree = new Tree(getContainer(), SWT.MULTI);
706       TreeViewer treeViewer = new TreeViewer(tree);
707       setCurrentViewer(treeViewer);
708
709       treeViewer.setContentProvider(new AdapterFactoryContentProvider(adapterFactory));
710       treeViewer.setLabelProvider(new AdapterFactoryLabelProvider(adapterFactory));
711       treeViewer.setInput(editingDomain.getResourceSet().getResources().get(0));
712
713       new AdapterFactoryTreeEditor(treeViewer.getTree(), adapterFactory);
714
715       createContextMenuFor(treeViewer);
716       addPage(tree);
717     }
718     setActivePage(0);
719
720     getContainer().addControlListener
721       (new ControlAdapter()
722        {
723          boolean guard = false;
724          public void controlResized(ControlEvent event)
725          {
726            if (!guard)
727            {
728              guard = true;
729              hideTabs();
730              guard = false;
731            }
732          }
733        });
734   }
735
736   protected void hideTabs()
737   {
738     if (getPageCount() <= 1)
739     {
740       setPageText(0, "");
741       if (getContainer() instanceof CTabFolder)
742       {
743         ((CTabFolder)getContainer()).setTabHeight(1);
744         Point point = getContainer().getSize();
745         getContainer().setSize(point.x, point.y + 6);
746       }
747     }
748   }
749
750   /**
751    * This is used to track the active viewer.
752    * <!-- begin-user-doc -->
753    * <!-- end-user-doc -->
754    * @generated
755    */

756   protected void pageChange(int pageIndex)
757   {
758     super.pageChange(pageIndex);
759
760     // This is a temporary workaround... EATM
761
//
762
Control control = getControl(pageIndex);
763     if (control != null)
764     {
765       control.setVisible(true);
766       control.setFocus();
767     }
768   }
769
770   /**
771    * This is how the framework determines which interfaces we implement.
772    * <!-- begin-user-doc -->
773    * <!-- end-user-doc -->
774    * @generated NOT
775    */

776   public Object JavaDoc getAdapter(Class JavaDoc key)
777   {
778     if (key.equals(IPropertySheetPage.class)) return getPropertySheetPage();
779     return super.getAdapter(key);
780   }
781
782
783   /**
784    * This accesses a cached version of the property sheet.
785    * <!-- begin-user-doc -->
786    * <!-- end-user-doc -->
787    * @generated
788    */

789   public IPropertySheetPage getPropertySheetPage()
790   {
791     if (propertySheetPage == null)
792     {
793       propertySheetPage =
794         new PropertySheetPage()
795         {
796           public void makeContributions(IMenuManager menuManager, IToolBarManager toolBarManager, IStatusLineManager statusLineManager)
797           {
798             super.makeContributions(menuManager, toolBarManager, statusLineManager);
799           }
800
801           public void setActionBars(IActionBars actionBars)
802           {
803             super.setActionBars(actionBars);
804             getActionBarContributor().shareGlobalActions(this, actionBars);
805           }
806         };
807       propertySheetPage.setPropertySourceProvider(new AdapterFactoryContentProvider(adapterFactory));
808     }
809
810     return propertySheetPage;
811   }
812
813   /**
814    * This is for implementing {@link IEditorPart} and simply tests the command stack.
815    * <!-- begin-user-doc -->
816    * <!-- end-user-doc -->
817    * @generated
818    */

819   public boolean isDirty()
820   {
821     return ((BasicCommandStack)editingDomain.getCommandStack()).isSaveNeeded();
822   }
823
824   /**
825    * This is for implementing {@link IEditorPart} and simply saves the model file.
826    * <!-- begin-user-doc -->
827    * <!-- end-user-doc -->
828    * @generated
829    */

830   public void doSave(IProgressMonitor progressMonitor)
831   {
832     // Do the work within an operation because this is a long running activity that modifies the workbench.
833
//
834
WorkspaceModifyOperation operation =
835       new WorkspaceModifyOperation()
836       {
837         // This is the method that gets invoked when the operation runs.
838
//
839
protected void execute(IProgressMonitor monitor) throws CoreException
840         {
841           try
842           {
843             // Save the resource to the file system.
844
//
845
Resource savedResource = (Resource)editingDomain.getResourceSet().getResources().get(0);
846             savedResources.add(savedResource);
847             savedResource.save(Collections.EMPTY_MAP);
848           }
849           catch (Exception JavaDoc exception)
850           {
851             GenModelEditPlugin.INSTANCE.log(exception);
852           }
853         }
854       };
855
856     try
857     {
858       // This runs the options, and shows progress.
859
// (It appears to be a bad thing to fork this onto another thread.)
860
//
861
new ProgressMonitorDialog(getSite().getShell()).run(true, false, operation);
862
863       // Refresh the necessary state.
864
//
865
((BasicCommandStack)editingDomain.getCommandStack()).saveIsDone();
866       firePropertyChange(IEditorPart.PROP_DIRTY);
867     }
868     catch (Exception JavaDoc exception)
869     {
870       // Something went wrong that shouldn't.
871
//
872
GenModelEditPlugin.INSTANCE.log(exception);
873     }
874   }
875
876   /**
877    * This always returns false because it is not current supported.
878    * <!-- begin-user-doc -->
879    * <!-- end-user-doc -->
880    * @generated
881    */

882   public boolean isSaveAsAllowed()
883   {
884     return true;
885   }
886
887   /**
888    * This also changes the editor's input.
889    * <!-- begin-user-doc -->
890    * <!-- end-user-doc -->
891    * @generated
892    */

893   public void doSaveAs()
894   {
895     SaveAsDialog saveAsDialog= new SaveAsDialog(getSite().getShell());
896     saveAsDialog.open();
897     IPath path= saveAsDialog.getResult();
898     if (path != null)
899     {
900       IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
901       if (file != null)
902       {
903         ((Resource)editingDomain.getResourceSet().getResources().get(0)).setURI
904           (URI.createPlatformResourceURI(file.getFullPath().toString()));
905         IFileEditorInput modelFile = new FileEditorInput(file);
906         setInput(modelFile);
907         setPartName(file.getName());
908         doSave(getActionBars().getStatusLineManager().getProgressMonitor());
909       }
910     }
911   }
912
913   /**
914    * <!-- begin-user-doc -->
915    * <!-- end-user-doc -->
916    * @generated
917    */

918   public void gotoMarker(IMarker marker)
919   {
920   }
921
922   /**
923    * This is called during startup.
924    * <!-- begin-user-doc -->
925    * <!-- end-user-doc -->
926    * @generated
927    */

928   public void init(IEditorSite site, IEditorInput editorInput) throws PartInitException, PartInitException, PartInitException, PartInitException, PartInitException, PartInitException, PartInitException, PartInitException, PartInitException, PartInitException
929   {
930     if (editorInput instanceof IFileEditorInput)
931     {
932       setSite(site);
933       setInput(editorInput);
934       setPartName(((IFileEditorInput)editorInput).getFile().getName());
935       site.setSelectionProvider(this);
936       site.getPage().addPartListener(partListener);
937       ResourcesPlugin.getWorkspace().addResourceChangeListener(resourceChangeListener, IResourceChangeEvent.POST_CHANGE);
938     }
939     else
940     {
941       throw new PartInitException("Invalid Input: Must be IFileEditorInput.");
942     }
943   }
944
945   /**
946    * <!-- begin-user-doc -->
947    * <!-- end-user-doc -->
948    * @generated
949    */

950   public void setFocus()
951   {
952     getControl(getActivePage()).setFocus();
953   }
954
955   /**
956    * This implements {@link org.eclipse.jface.viewers.ISelectionProvider}.
957    * <!-- begin-user-doc -->
958    * <!-- end-user-doc -->
959    * @generated
960    */

961   public void addSelectionChangedListener(ISelectionChangedListener listener)
962   {
963     selectionChangedListeners.add(listener);
964   }
965
966   /**
967    * This implements {@link org.eclipse.jface.viewers.ISelectionProvider}.
968    * <!-- begin-user-doc -->
969    * <!-- end-user-doc -->
970    * @generated
971    */

972   public void removeSelectionChangedListener(ISelectionChangedListener listener)
973   {
974     selectionChangedListeners.remove(listener);
975   }
976
977   /**
978    * This implements {@link org.eclipse.jface.viewers.ISelectionProvider} to return this editor's overall selection.
979    * <!-- begin-user-doc -->
980    * <!-- end-user-doc -->
981    * @generated
982    */

983   public ISelection getSelection()
984   {
985     return editorSelection;
986   }
987
988   /**
989    * This implements {@link org.eclipse.jface.viewers.ISelectionProvider} to set this editor's overall selection.
990    * Calling this result will notify the listeners.
991    * <!-- begin-user-doc -->
992    * <!-- end-user-doc -->
993    * @generated
994    */

995   public void setSelection(ISelection selection)
996   {
997     editorSelection = selection;
998     for (Iterator JavaDoc listeners = selectionChangedListeners.iterator(); listeners.hasNext(); )
999     {
1000      ISelectionChangedListener listener = (ISelectionChangedListener)listeners.next();
1001      listener.selectionChanged(new SelectionChangedEvent(this, selection));
1002    }
1003    setStatusLineManager(selection);
1004  }
1005
1006  /**
1007   * <!-- begin-user-doc -->
1008   * <!-- end-user-doc -->
1009   * @generated NOT
1010   */

1011  public void setStatusLineManager(ISelection selection)
1012  {
1013    IStatusLineManager statusLineManager = getActionBars().getStatusLineManager();
1014    if(statusLineManager != null)
1015    {
1016      if (selection instanceof IStructuredSelection)
1017      {
1018        Collection JavaDoc collection = ((IStructuredSelection)selection).toList();
1019        switch (collection.size())
1020        {
1021          case 0:
1022          {
1023            statusLineManager.setMessage(getString("_UI_NoObjectSelected"));
1024            break;
1025          }
1026          case 1:
1027          {
1028            String JavaDoc text = new AdapterFactoryItemDelegator(adapterFactory).getText(collection.iterator().next());
1029            statusLineManager.setMessage(getString("_UI_SingleObjectSelected", text));
1030            break;
1031          }
1032          default:
1033          {
1034            statusLineManager.setMessage(getString("_UI_MultiObjectSelected", Integer.toString(collection.size())));
1035            break;
1036          }
1037        }
1038      }
1039      else
1040      {
1041        statusLineManager.setMessage("");
1042      }
1043    }
1044  }
1045
1046  /**
1047   * This looks up a string in the plugin's plugin.properties file.
1048   * <!-- begin-user-doc -->
1049   * <!-- end-user-doc -->
1050   * @generated
1051   */

1052  private static String JavaDoc getString(String JavaDoc key)
1053  {
1054    return GenModelEditPlugin.INSTANCE.getString(key);
1055  }
1056
1057  /**
1058   * This looks up a string in plugin.properties, making a substitution.
1059   * <!-- begin-user-doc -->
1060   * <!-- end-user-doc -->
1061   * @generated
1062   */

1063  private static String JavaDoc getString(String JavaDoc key, Object JavaDoc s1)
1064  {
1065    return GenModelEditPlugin.INSTANCE.getString(key, new Object JavaDoc [] { s1 });
1066  }
1067
1068  /**
1069   * This implements {@link org.eclipse.jface.action.IMenuListener} to help fill the context menus with contributions from the Edit menu.
1070   * <!-- begin-user-doc -->
1071   * <!-- end-user-doc -->
1072   * @generated
1073   */

1074  public void menuAboutToShow(IMenuManager menuManager)
1075  {
1076    ((IMenuListener)getEditorSite().getActionBarContributor()).menuAboutToShow(menuManager);
1077  }
1078
1079  /**
1080   * <!-- begin-user-doc -->
1081   * <!-- end-user-doc -->
1082   * @generated
1083   */

1084  public EditingDomainActionBarContributor getActionBarContributor()
1085  {
1086    return (EditingDomainActionBarContributor)getEditorSite().getActionBarContributor();
1087  }
1088
1089  /**
1090   * <!-- begin-user-doc -->
1091   * <!-- end-user-doc -->
1092   * @generated
1093   */

1094  public IActionBars getActionBars()
1095  {
1096    return getActionBarContributor().getActionBars();
1097  }
1098
1099  /**
1100   * <!-- begin-user-doc -->
1101   * <!-- end-user-doc -->
1102   * @generated
1103   */

1104  public AdapterFactory getAdapterFactory()
1105  {
1106    return adapterFactory;
1107  }
1108
1109  /**
1110   * <!-- begin-user-doc -->
1111   * <!-- end-user-doc -->
1112   * @generated
1113   */

1114  public void dispose()
1115  {
1116    ResourcesPlugin.getWorkspace().removeResourceChangeListener(resourceChangeListener);
1117
1118    getSite().getPage().removePartListener(partListener);
1119
1120    adapterFactory.dispose();
1121
1122    getActionBarContributor().setActiveEditor(null);
1123
1124    if (propertySheetPage != null)
1125    {
1126      propertySheetPage.dispose();
1127    }
1128
1129    super.dispose();
1130  }
1131
1132  public void validate()
1133  {
1134    Resource mainResource = (Resource)editingDomain.getResourceSet().getResources().get(0);
1135    GenModel genModel = (GenModel)mainResource.getContents().get(0);
1136    IStatus status = genModel.validate();
1137    if (!status.isOK())
1138    {
1139      ErrorDialog.openError
1140        (getSite().getShell(),
1141         GenModelEditPlugin.INSTANCE.getString("_UI_ModelProblems_title"),
1142         "",
1143         status);
1144    }
1145  }
1146}
1147
Popular Tags