roneditor
Class AbstractTreeEditor

java.lang.Object
  extended by org.eclipse.core.commands.common.EventManager
      extended by org.eclipse.ui.part.WorkbenchPart
          extended by org.eclipse.ui.part.EditorPart
              extended by roneditor.AbstractTreeEditor
All Implemented Interfaces:
org.eclipse.core.runtime.IAdaptable, org.eclipse.core.runtime.IExecutableExtension, org.eclipse.gef.commands.CommandStackListener, org.eclipse.jface.viewers.ISelectionChangedListener, org.eclipse.ui.IEditorPart, org.eclipse.ui.ISaveablePart, org.eclipse.ui.IWorkbenchPart, org.eclipse.ui.IWorkbenchPart2, org.eclipse.ui.part.IWorkbenchPartOrientation
Direct Known Subclasses:
RONEditor

public abstract class AbstractTreeEditor
extends org.eclipse.ui.part.EditorPart
implements org.eclipse.gef.commands.CommandStackListener, org.eclipse.jface.viewers.ISelectionChangedListener

This is a basic abstract implementation of an EditorPart with a AbstractTreeEditor.EditorTreeViewer. It is designed to be used in an Eclipse plugin as it demands a AbstractUIPlugin to be able to access some of its Bundle information.

Prerequisites:

When instantiated (and in createPartControl(Composite)) this class does the following:

  1. It creates a DefaultEditDomain and sets itself as the EditorPart.
  2. Looks up the perspective defined for the plugin and opens it.
  3. A AbstractTreeEditor.EditorTreeViewer is created.
  4. Sets the TreeViewer in the EditDomain and listen to selection changes on it.
  5. Installs a SelectionSynchronizer on the AbstractTreeEditor.EditorTreeViewer.
  6. AbstractTreeEditor.EditorTreeViewer is set as ISelectionProvider for this IEditorSite.
  7. Sets the edit part factory, contents, and context menu for the TreeViewer determined by abstract methods.
  8. Sets a standard KeyHandler for the AbstractTreeEditor.EditorTreeViewer.

Additionally it provides the following features:

  • When being disposed the editor stores information (IDs) about the opened views to the plugin's preferences to reopen them when the editor is started again.

    These methods have to be implemented by subclasses:

    Author:
    Tony Modica

    Nested Class Summary
     class AbstractTreeEditor.EditorTreeViewer
              This special TreeViewer is needed to allow the EditParts to access the editor e.g. to generate IDs for models.
     
    Field Summary
    private  org.eclipse.gef.ui.actions.ActionRegistry actionRegistry
              The ActionRegistry containing the actions created by this editor.
    private  org.eclipse.gef.DefaultEditDomain editDomain
              The EditDomain of this editor.
    private  java.util.Map<java.lang.String,org.eclipse.emf.ecore.EObject> id2ModelMap
              A map to resolve unique ids to ecore models.
    private  org.eclipse.gef.KeyHandler keyHandler
              The key handler of this editor.
    private  java.util.Map<org.eclipse.emf.ecore.EObject,java.lang.String> model2IdMap
              A map reversing id2ModelMap to get possible previously generated ids for models rather that create new ones.
    private  java.util.Map<org.eclipse.emf.ecore.EObject,org.eclipse.emf.common.notify.Adapter> model2ListenerMap
              A map only used to be able to remove deletion listeners from the models when the editor is being closed.
    private  boolean previousDirtyState
              A helper flag to decide if the save action should be enabled.
    private  java.util.List<org.eclipse.jface.action.IAction> propertyActions
              The actions of this editor depending on property change notifications.
    private  java.util.List<org.eclipse.jface.action.IAction> selectionActions
              The actions of this editor depending on the actual selection.
    private  java.util.List<org.eclipse.jface.action.IAction> stackActions
              The actions of this editor depending on the actual command stack.
    private  org.eclipse.gef.ui.parts.SelectionSynchronizer synchronizer
              The SelectionSynchronizer managing the selections for this editor and the views it can open.
    private  org.eclipse.gef.ui.parts.TreeViewer treeViewer
              The AbstractTreeEditor.EditorTreeViewer in this editor.
     
    Fields inherited from interface org.eclipse.ui.IEditorPart
    PROP_DIRTY, PROP_INPUT
     
    Fields inherited from interface org.eclipse.ui.IWorkbenchPart
    PROP_TITLE
     
    Constructor Summary
    protected AbstractTreeEditor()
              The standard constructor creates a DefaultEditDomain and registers itself as a CommandStackListener on the domains CommandStack.
     
    Method Summary
    (package private)  java.lang.String closeViewsShowing(org.eclipse.emf.ecore.EObject model)
              Closes alls views in the site's page whose secondary ID correspond to the id the passed model has been registered with.
     void commandStackChanged(java.util.EventObject event)
              When the command stack changes, the actions interested in the command stack are updated.
    protected  void createActions()
              Creates some standard GEF actions for this editor.
    protected abstract  org.eclipse.gef.ContextMenuProvider createContextMenuProvider(org.eclipse.gef.ui.parts.TreeViewer viewer)
               
     java.lang.String createIDForModel(org.eclipse.emf.ecore.EObject model)
              Generates an unique ID for this model and registers them with each other in two maps.
    private  java.lang.String createIDForModel(org.eclipse.emf.ecore.EObject model, java.lang.String forceID)
              Generates an unique ID for this model and registers them with each other in two maps.
     void createPartControl(org.eclipse.swt.widgets.Composite parent)
              Realizes the editor by creating its Control.
    protected abstract  org.eclipse.gef.EditPartFactory createTreeEditPartFactory()
               
    protected  void createTreeViewer(org.eclipse.swt.widgets.Composite parent)
              Creates the GraphicalViewer on the specified Composite.
     void dispose()
               
     void doSave(org.eclipse.core.runtime.IProgressMonitor monitor)
               
     void doSaveAs()
               
    protected  void firePropertyChange(int property)
               
    protected  org.eclipse.gef.ui.actions.ActionRegistry getActionRegistry()
              Lazily creates, initializes and returns the action registry.
    protected abstract  org.eclipse.ui.plugin.AbstractUIPlugin getActivator()
              To be implemented by concrete subclasses!
     java.lang.Object getAdapter(java.lang.Class type)
               IMPORTANT certain requests, such as the property sheet, may be made before or after createPartControl(Composite) is called.
    protected  org.eclipse.gef.commands.CommandStack getCommandStack()
               
    protected  org.eclipse.gef.DefaultEditDomain getEditDomain()
               
    protected  java.util.Vector<java.lang.String> getExtensionAttributeValues(java.lang.String exPointID, java.lang.String attribID)
               
    protected  org.eclipse.gef.KeyHandler getKeyHandler()
               
     org.eclipse.emf.ecore.EObject getModelForID(java.lang.String id)
              Method to resolve an ID to a ECore model element.
    protected abstract  org.eclipse.emf.ecore.EObject getModelRoot()
               
    protected  java.util.List<org.eclipse.jface.action.IAction> getPropertyActions()
              Returns the list of IActions dependent on property changes in the Editor.
    protected  java.util.List<org.eclipse.jface.action.IAction> getSelectionActions()
              Returns the list of IAction that depend on changes in the workbench's ISelectionService.
    protected  org.eclipse.gef.ui.parts.SelectionSynchronizer getSelectionSynchronizer()
              Returns the selection synchronizer object.
    protected  java.util.List<org.eclipse.jface.action.IAction> getStackActions()
              Returns the list of IActions dependent on the CommmandStack's state.
    protected  org.eclipse.gef.ui.parts.TreeViewer getTreeViewer()
               
    protected  java.lang.String getUniqueExtensionAttributeValue(java.lang.String exPointID, java.lang.String attribID)
               
     void init(org.eclipse.ui.IEditorSite site, org.eclipse.ui.IEditorInput input)
              Sets the site and input for this editor and opens a unique perspective defined in plugin.xml.
    protected  void initializeActionRegistry()
              Initializes the action registry.
    protected  boolean isAnotherEditorActive()
              A helper method that looks for other active instances of this editor in the site's page.
     boolean isDirty()
              Returns true if the command stack is dirty
     boolean isSaveAsAllowed()
               
     void logError(java.lang.String message, java.lang.Exception e)
               
    private  void restoreModelRegistry()
              Restores the model-id-mappings from the plugin's preferences if the editor is still working on the same file.
    protected abstract  void save(org.eclipse.core.resources.IFile file, org.eclipse.core.runtime.IProgressMonitor progressMonitor)
              This method saves the model to a file using some facility like EMF Resources.
     void selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent event)
               
     void setFocus()
               
     void showView(java.lang.String viewId, org.eclipse.emf.ecore.EObject model)
              Opens a view on this side's page displaying a model.
    private  void storeAndCleanModelRegistry()
              Unregisters each registered model and closes the views that show them.
    protected  void updateActions(java.util.List<org.eclipse.jface.action.IAction> actions)
              A convenience method for updating a set of actions defined by the given List of actions.
     
    Methods inherited from class org.eclipse.ui.part.EditorPart
    checkSite, getEditorInput, getEditorSite, getTitleToolTip, isSaveOnCloseNeeded, setContentDescription, setInitializationData, setInput, setInputWithNotify, setPartName
     
    Methods inherited from class org.eclipse.ui.part.WorkbenchPart
    addPropertyListener, getConfigurationElement, getContentDescription, getDefaultImage, getOrientation, getPartName, getSite, getTitle, getTitleImage, removePropertyListener, setSite, setTitle, setTitleImage, setTitleToolTip, showBusy
     
    Methods inherited from class org.eclipse.core.commands.common.EventManager
    addListenerObject, clearListeners, getListeners, isListenerAttached, removeListenerObject
     
    Methods inherited from class java.lang.Object
    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
     
    Methods inherited from interface org.eclipse.ui.IWorkbenchPart
    addPropertyListener, getSite, getTitle, getTitleImage, removePropertyListener
     

    Field Detail

    editDomain

    private final org.eclipse.gef.DefaultEditDomain editDomain
    The EditDomain of this editor.


    treeViewer

    private org.eclipse.gef.ui.parts.TreeViewer treeViewer
    The AbstractTreeEditor.EditorTreeViewer in this editor.


    actionRegistry

    private org.eclipse.gef.ui.actions.ActionRegistry actionRegistry
    The ActionRegistry containing the actions created by this editor.


    synchronizer

    private org.eclipse.gef.ui.parts.SelectionSynchronizer synchronizer
    The SelectionSynchronizer managing the selections for this editor and the views it can open.


    selectionActions

    private final java.util.List<org.eclipse.jface.action.IAction> selectionActions
    The actions of this editor depending on the actual selection.


    stackActions

    private final java.util.List<org.eclipse.jface.action.IAction> stackActions
    The actions of this editor depending on the actual command stack.


    propertyActions

    private final java.util.List<org.eclipse.jface.action.IAction> propertyActions
    The actions of this editor depending on property change notifications.


    keyHandler

    private org.eclipse.gef.KeyHandler keyHandler
    The key handler of this editor.


    previousDirtyState

    private boolean previousDirtyState
    A helper flag to decide if the save action should be enabled.


    id2ModelMap

    private final java.util.Map<java.lang.String,org.eclipse.emf.ecore.EObject> id2ModelMap
    A map to resolve unique ids to ecore models. This is used to allow views opened to show a specific model to resolve their secondary id to the model to be shown.


    model2IdMap

    private final java.util.Map<org.eclipse.emf.ecore.EObject,java.lang.String> model2IdMap
    A map reversing id2ModelMap to get possible previously generated ids for models rather that create new ones.


    model2ListenerMap

    private final java.util.Map<org.eclipse.emf.ecore.EObject,org.eclipse.emf.common.notify.Adapter> model2ListenerMap
    A map only used to be able to remove deletion listeners from the models when the editor is being closed.

    Constructor Detail

    AbstractTreeEditor

    protected AbstractTreeEditor()
    The standard constructor creates a DefaultEditDomain and registers itself as a CommandStackListener on the domains CommandStack.

    Method Detail

    getKeyHandler

    protected org.eclipse.gef.KeyHandler getKeyHandler()
    Returns:
    A lazily created KeyHandler for this editor.

    commandStackChanged

    public void commandStackChanged(java.util.EventObject event)
    When the command stack changes, the actions interested in the command stack are updated. Additionally the editor checks if itself has changed its dirty state by comparing its previous dirty state with the actual command stack state.

    Specified by:
    commandStackChanged in interface org.eclipse.gef.commands.CommandStackListener
    Parameters:
    event - the change event

    createActions

    protected void createActions()
    Creates some standard GEF actions for this editor. Subclasses should override this method to create and register custom actions with the ActionRegistry.


    createTreeViewer

    protected void createTreeViewer(org.eclipse.swt.widgets.Composite parent)
    Creates the GraphicalViewer on the specified Composite.

    Parameters:
    parent - the parent composite

    createPartControl

    public void createPartControl(org.eclipse.swt.widgets.Composite parent)
    Realizes the editor by creating its Control.

    WARNING: This method may or may not be called by the workbench prior to dispose().

    Specified by:
    createPartControl in interface org.eclipse.ui.IWorkbenchPart
    Specified by:
    createPartControl in class org.eclipse.ui.part.WorkbenchPart
    Parameters:
    parent - the parent composite

    dispose

    public void dispose()
    Specified by:
    dispose in interface org.eclipse.ui.IWorkbenchPart
    Overrides:
    dispose in class org.eclipse.ui.part.WorkbenchPart

    isAnotherEditorActive

    protected boolean isAnotherEditorActive()
    A helper method that looks for other active instances of this editor in the site's page.

    Returns:
    true if another instance of this editor is active

    firePropertyChange

    protected void firePropertyChange(int property)
    Overrides:
    firePropertyChange in class org.eclipse.ui.part.WorkbenchPart

    getActionRegistry

    protected org.eclipse.gef.ui.actions.ActionRegistry getActionRegistry()
    Lazily creates, initializes and returns the action registry.

    Returns:
    the action registry

    getAdapter

    public java.lang.Object getAdapter(java.lang.Class type)

    IMPORTANT certain requests, such as the property sheet, may be made before or after createPartControl(Composite) is called. The order is unspecified by the Workbench. (non-Javadoc)

    Specified by:
    getAdapter in interface org.eclipse.core.runtime.IAdaptable
    Overrides:
    getAdapter in class org.eclipse.ui.part.WorkbenchPart
    See Also:
    IAdaptable.getAdapter(java.lang.Class)

    getCommandStack

    protected org.eclipse.gef.commands.CommandStack getCommandStack()

    getEditDomain

    protected org.eclipse.gef.DefaultEditDomain getEditDomain()

    getTreeViewer

    protected org.eclipse.gef.ui.parts.TreeViewer getTreeViewer()
    Returns:
    A lazily created AbstractTreeEditor.EditorTreeViewer that allows to access the editor.

    getPropertyActions

    protected java.util.List<org.eclipse.jface.action.IAction> getPropertyActions()
    Returns the list of IActions dependent on property changes in the Editor. These actions should implement the UpdateAction interface so that they can be updated in response to property changes. An example is the "Save" action.

    Returns:
    the list of property-dependent actions

    getSelectionActions

    protected java.util.List<org.eclipse.jface.action.IAction> getSelectionActions()
    Returns the list of IAction that depend on changes in the workbench's ISelectionService. These actions can be updated in response to selection changes. An example is the Delete action.

    Returns:
    the list of selection-dependent actions

    getSelectionSynchronizer

    protected org.eclipse.gef.ui.parts.SelectionSynchronizer getSelectionSynchronizer()
    Returns the selection synchronizer object. The synchronizer can be used to sync the selection of 2 or more EditPartViewers.

    Returns:
    A lazily created SelectionSynchronizer

    getStackActions

    protected java.util.List<org.eclipse.jface.action.IAction> getStackActions()
    Returns the list of IActions dependent on the CommmandStack's state. These actions should implement the UpdateAction interface so that they can be updated in response to command stack changes. An example is the "undo" action.

    Returns:
    the list of stack-dependent actions

    init

    public void init(org.eclipse.ui.IEditorSite site,
                     org.eclipse.ui.IEditorInput input)
    Sets the site and input for this editor and opens a unique perspective defined in plugin.xml. Subclasses may extend this method, but should always call super.init(site, input).

    Specified by:
    init in interface org.eclipse.ui.IEditorPart
    Specified by:
    init in class org.eclipse.ui.part.EditorPart
    See Also:
    IEditorPart.init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)

    initializeActionRegistry

    protected void initializeActionRegistry()
    Initializes the action registry. This registry may be used by ActionBarContributors and/or ContextMenuProviders.

    This method is being called the first time getActionRegistry() gets called.


    getActivator

    protected abstract org.eclipse.ui.plugin.AbstractUIPlugin getActivator()
    To be implemented by concrete subclasses!
    To access the active page PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage() should be used.

    Returns:
    The Activator instance of this plug-in. Needed to access the plugin's Bundle and IExtensions.

    getUniqueExtensionAttributeValue

    protected java.lang.String getUniqueExtensionAttributeValue(java.lang.String exPointID,
                                                                java.lang.String attribID)
    Parameters:
    exPointID - The extension to be looked at.
    attribID - The attribute whose value is requested.
    Returns:
    The value of the specified attribute.
    Throws:
    org.eclipse.core.runtime.CoreException - This is thrown if no (unique) value can be determined.

    getExtensionAttributeValues

    protected java.util.Vector<java.lang.String> getExtensionAttributeValues(java.lang.String exPointID,
                                                                             java.lang.String attribID)
    Parameters:
    exPointID - The extension to be looked at.
    attribID - The attribute whose value is requested.
    Returns:
    The values of the specified attribute in a Vector.
    Throws:
    org.eclipse.core.runtime.CoreException - This is thrown if no (unique) value can be determined.

    logError

    public void logError(java.lang.String message,
                         java.lang.Exception e)
    Parameters:
    message - The message for the IStatus to be logged for the plugin determined by getActivator().
    e - An optional Exception to be reported.

    createContextMenuProvider

    protected abstract org.eclipse.gef.ContextMenuProvider createContextMenuProvider(org.eclipse.gef.ui.parts.TreeViewer viewer)
    Parameters:
    viewer - The AbstractTreeEditor.EditorTreeViewer for that this ContextMenuProvider constructs a menu.

    getModelRoot

    protected abstract org.eclipse.emf.ecore.EObject getModelRoot()
    Returns:
    The model element object that is the root of the model.

    createTreeEditPartFactory

    protected abstract org.eclipse.gef.EditPartFactory createTreeEditPartFactory()
    Returns:
    The EditPartFactory for this GEF AbstractTreeEditor.EditorTreeViewer.

    isDirty

    public boolean isDirty()
    Returns true if the command stack is dirty

    Specified by:
    isDirty in interface org.eclipse.ui.ISaveablePart
    Specified by:
    isDirty in class org.eclipse.ui.part.EditorPart
    See Also:
    ISaveablePart.isDirty()

    setFocus

    public void setFocus()
    Specified by:
    setFocus in interface org.eclipse.ui.IWorkbenchPart
    Specified by:
    setFocus in class org.eclipse.ui.part.WorkbenchPart

    updateActions

    protected void updateActions(java.util.List<org.eclipse.jface.action.IAction> actions)
    A convenience method for updating a set of actions defined by the given List of actions.

    Parameters:
    action - The list of actions to update.

    isSaveAsAllowed

    public boolean isSaveAsAllowed()
    Specified by:
    isSaveAsAllowed in interface org.eclipse.ui.ISaveablePart
    Specified by:
    isSaveAsAllowed in class org.eclipse.ui.part.EditorPart

    doSave

    public void doSave(org.eclipse.core.runtime.IProgressMonitor monitor)
    Specified by:
    doSave in interface org.eclipse.ui.ISaveablePart
    Specified by:
    doSave in class org.eclipse.ui.part.EditorPart

    doSaveAs

    public void doSaveAs()
    Specified by:
    doSaveAs in interface org.eclipse.ui.ISaveablePart
    Specified by:
    doSaveAs in class org.eclipse.ui.part.EditorPart

    selectionChanged

    public void selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent event)
    Specified by:
    selectionChanged in interface org.eclipse.jface.viewers.ISelectionChangedListener

    save

    protected abstract void save(org.eclipse.core.resources.IFile file,
                                 org.eclipse.core.runtime.IProgressMonitor progressMonitor)
                          throws org.eclipse.core.runtime.CoreException
    This method saves the model to a file using some facility like EMF Resources.

    Parameters:
    file - The IFile to save the model to.
    progressMonitor - A progress monitor that could be used to show the saving status.
    Throws:
    org.eclipse.core.runtime.CoreException - This exception indicates that something went wrong during saving.

    showView

    public void showView(java.lang.String viewId,
                         org.eclipse.emf.ecore.EObject model)
    Opens a view on this side's page displaying a model. An unique ID for the passed model is being created and set as secondary ID of the new view. The model for this ID can be resolved via getModelForID(String).

    Parameters:
    viewId - The primary ID of the view to be opened. Should be correspondent to an org.eclipse.ui.views entry in plugin.xml
    model - The model to be shown in the view

    getModelForID

    public org.eclipse.emf.ecore.EObject getModelForID(java.lang.String id)
    Method to resolve an ID to a ECore model element. This ID has to be created explicitly before.

    Parameters:
    id - The id that identifies the wanted model
    Returns:
    The model that has been registered with the id
    See Also:
    createIDForModel(EObject)

    createIDForModel

    public java.lang.String createIDForModel(org.eclipse.emf.ecore.EObject model)
    Generates an unique ID for this model and registers them with each other in two maps. The ID is to be used as secondary ID to tell views what model they shall display. If the model has already been registered before, the previous generated ID is returned rather than a newly created one.
    Additionally an Adapter is added to the model. The adapter will close the views that have been opened with this ID as secondary ID automatically when deletion of the model is being notified.

    Parameters:
    model - The model to get an ID for
    Returns:
    A unique ID for the model

    createIDForModel

    private java.lang.String createIDForModel(org.eclipse.emf.ecore.EObject model,
                                              java.lang.String forceID)
    Generates an unique ID for this model and registers them with each other in two maps. The ID is to be used as secondary ID to tell views what model they shall display. If the model has already been registered before, the previous generated ID is returned rather than a newly created one.
    Additionally an Adapter is added to the model. The adapter will close the views that have been opened with this ID as secondary ID automatically when deletion of the model is being notified. *

    Parameters:
    model - The model to get an ID for
    forceID - May be used to force a particular ID for a model. If null, a new ID will be generated.
    Returns:
    A unique ID for the model

    closeViewsShowing

    java.lang.String closeViewsShowing(org.eclipse.emf.ecore.EObject model)
    Closes alls views in the site's page whose secondary ID correspond to the id the passed model has been registered with.

    Parameters:
    model - The model
    Returns:
    The (primary) id of the last closed view showing the model if any, else null

    storeAndCleanModelRegistry

    private void storeAndCleanModelRegistry()
    Unregisters each registered model and closes the views that show them. The IDs and their correspondent model URIs for the currently open views are stored to the plugin's preferences.
    To be called, when the editor is being closed.

    See Also:
    restoreModelRegistry()

    restoreModelRegistry

    private void restoreModelRegistry()
    Restores the model-id-mappings from the plugin's preferences if the editor is still working on the same file. Tries to reopen the views that were opened when the editor was closed. The preferences are cleaned up afterwards.

    See Also:
    storeAndCleanModelRegistry()