minor
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / AbstractCdmEditorViewPart.java
index 8d1f9ad9d6bd7cb678f7b18bfbb77491f5f1d8ad..9afbdeadbb145ce32d4eee3aedf4b06e2daaec33 100644 (file)
@@ -3,12 +3,17 @@
  */
 package eu.etaxonomy.taxeditor.view;
 
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Display;
 import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IWorkbenchPart;
 
 import eu.etaxonomy.taxeditor.editor.ISecuredEditor;
 import eu.etaxonomy.taxeditor.model.AbstractUtility;
-import eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider;
+import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
+import eu.etaxonomy.taxeditor.workbench.part.ISelectionElementEditingPart;
 
 /**
  * <p>
@@ -19,52 +24,152 @@ import eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider;
  * @created Sep 21, 2010
  * @version 1.0
  */
-public abstract class AbstractCdmEditorViewPart extends AbstractCdmViewPart {
-
-       /*
-        * (non-Javadoc)
-        *
-        * @see
-        * eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#createPartControl(org
-        * .eclipse.swt.widgets.Composite)
-        */
-       /** {@inheritDoc} */
-       @Override
-       public void createPartControl(Composite parent) {
-               super.createPartControl(parent);
-       }
-
-       /**
-        * <p>
-        * getEditor
-        * </p>
-        *
-        * @return the currently active editor
-        */
-       public IEditorPart getEditor() {
-               return AbstractUtility.getActiveEditor();
-       }
-
-       /** {@inheritDoc} */
-       @Override
-       public void setFocus() {
-               super.setFocus();
-
-               if(getEditor() != null && ISecuredEditor.class.isAssignableFrom(getEditor().getClass())){
-                       boolean doEnable = ((ISecuredEditor)getEditor()).permissionsSatisfied();
-                       setEnabled(doEnable);
-               }
+public abstract class AbstractCdmEditorViewPart extends AbstractCdmViewPart implements ISelectionElementEditingPart {
+
+    private DelaySelection delaySelection = null;
+    /**
+     * This is the monitor for the DelaySelection runnable.
+     * If it is <code>true</code> then it is currently delaying a selection.
+     */
+    private boolean isInDelay;
+
+    protected Object selectionProvidingPart;
+
+
+    /**
+     * This class invokes internal_selectionChanged() in a separate thread.
+     * This allows an asynchronous and/or delayed handling of selection changes
+     */
+    private class DelaySelection implements Runnable{
+        private IWorkbenchPart part;
+        private ISelection selection;
+
+        public DelaySelection(IWorkbenchPart part, ISelection selection) {
+            super();
+            this.part = part;
+            this.selection = selection;
+        }
+
+        @Override
+        public void run() {
+            try{
+                selectionChanged_internal(part, selection);
+            }
+            finally{
+                isInDelay = false;
+            }
+        }
+
+        public synchronized void setSelection(ISelection selection) {
+            this.selection = selection;
+        }
+
+        public synchronized void setPart(IWorkbenchPart part) {
+            this.part = part;
+        }
+
+    }
+
+    /**
+     * Handle selection change events here and <b>not</b> in
+     * {@link AbstractCdmEditorViewPart#selectionChanged(IWorkbenchPart, ISelection)}
+     * if you want asynchronous selection handling.
+     * @param part The workbench part that has issued the selection change
+     * @param selection the new selection
+     */
+    protected abstract void selectionChanged_internal(IWorkbenchPart part, ISelection selection);
+
+    /** {@inheritDoc} */
+    @Override
+    public void selectionChanged(IWorkbenchPart part, ISelection selection) {
+       /*to avoid widget is disposed exceptions
+        if(getViewer().getControl().isDisposed()){
+            return;
+        }*/
+       if(delaySelection==null){
+            delaySelection = new DelaySelection(part, selection);
+        }
+        delaySelection.setPart(part);
+        delaySelection.setSelection(selection);
+        if(!isInDelay){
+            isInDelay = true;
+            Display.getCurrent().asyncExec(delaySelection);
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void showViewer(Object part, IStructuredSelection selection) {
+        super.showViewer(part, selection);
+        selectionProvidingPart = part;
+    }
+
+    @Override
+    public Object getSelectionProvidingPart() {
+        return selectionProvidingPart;
+    }
+
+
+
+    /** {@inheritDoc} */
+    @Override
+    public void createPartControl(Composite parent) {
+        super.createPartControl(parent);
+        isInDelay = false;
+    }
+
+    /**
+     * <p>
+     * getEditor
+     * </p>
+     *
+     * @return the currently active editor
+     */
+    public IEditorPart getEditor() {
+        return AbstractUtility.getActiveEditor();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void showEmptyPage() {
+        selectionProvidingPart = null;
+        super.showEmptyPage();
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void setFocus() {
+        super.setFocus();
+
+        if(getEditor() != null && ISecuredEditor.class.isAssignableFrom(getEditor().getClass())){
+            boolean doEnable = ((ISecuredEditor)getEditor()).permissionsSatisfied();
+            setEnabled(doEnable);
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void dispose() {
+        selectionProvidingPart = null;
+        super.dispose();
     }
 
     /** {@inheritDoc} */
     @Override
     public void changed(Object object) {
-        if (part instanceof AbstractCdmViewPart) {
-            ((AbstractCdmViewPart) part).changed(object);
-        } else {
+        if(part instanceof IDirtyMarkable){
+            ((IDirtyMarkable) part).changed(object);
+        }
+        else {
             IEditorPart editor = getEditor();
-            if (editor != null && editor instanceof IDirtyMarkableSelectionProvider) {
-                ((IDirtyMarkableSelectionProvider) editor).changed(object);
+            if (editor != null && editor instanceof IDirtyMarkable) {
+                ((IDirtyMarkable) editor).changed(object);
             }
         }
     }