ref #6909 migrate taxon navigator context menu
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / model / AbstractUtility.java
index a490390a637d2b46e6ef073a324e6a39f07ec447..fa3e8232c00385aaee0f4be1cf4fd7e5518bc79f 100644 (file)
@@ -34,11 +34,13 @@ import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.SubProgressMonitor;
 import org.eclipse.core.runtime.jobs.ISchedulingRule;
 import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.e4.ui.workbench.modeling.EPartService;
 import org.eclipse.jface.action.IStatusLineManager;
 import org.eclipse.jface.operation.IRunnableWithProgress;
 import org.eclipse.jface.resource.ColorRegistry;
 import org.eclipse.jface.resource.FontRegistry;
-import org.eclipse.jface.window.ApplicationWindow;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.viewers.SelectionChangedEvent;
 import org.eclipse.swt.graphics.Color;
 import org.eclipse.swt.graphics.Font;
 import org.eclipse.swt.widgets.Display;
@@ -70,17 +72,16 @@ import eu.etaxonomy.taxeditor.operation.IFeedbackGenerator;
 import eu.etaxonomy.taxeditor.operation.IPostMoniteredOperationEnabled;
 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
 import eu.etaxonomy.taxeditor.operation.RemotingCdmHandler;
+import eu.etaxonomy.taxeditor.operation.e4.RemotingCdmHandlerE4;
 import eu.etaxonomy.taxeditor.store.CdmStore;
 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
 import eu.etaxonomy.taxeditor.ui.dialog.ReportTextDialog;
 import eu.etaxonomy.taxeditor.view.AbstractCdmDataViewer;
 import eu.etaxonomy.taxeditor.view.detail.DetailsViewPart;
 import eu.etaxonomy.taxeditor.view.supplementaldata.SupplementalDataViewPart;
+import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
 
 /**
- * <p>
- * Abstract AbstractUtility class.
- * </p>
  *
  * @author n.hoffmann
  * @created 11.05.2009
@@ -94,13 +95,6 @@ public abstract class AbstractUtility {
     protected static IStatusLineManager statusLineManager;
 
 
-    /**
-     * <p>
-     * closeAll
-     * </p>
-     *
-     * @return a boolean.
-     */
     public static boolean closeAll() {
         if(getActivePage()!=null){
             return getActivePage().closeAllEditors(true);
@@ -116,73 +110,56 @@ public abstract class AbstractUtility {
      * @return <tt>true</tt> on success
      */
     public static boolean close(EditorPart editor) {
-        return getActivePage().closeEditor(editor, true);
+        return getActivePage() != null ? getActivePage().closeEditor(editor, true):false;
     }
 
-    /**
-     * <p>
-     * getShell
-     * </p>
-     *
-     * @return a {@link org.eclipse.swt.widgets.Shell} object.
-     */
     public static Shell getShell() {
 
         return TaxeditorStorePlugin.getDefault().getWorkbench()
                 .getActiveWorkbenchWindow().getShell();
     }
 
-    /**
-     * <p>
-     * getActivePage
-     * </p>
-     *
-     * @return a {@link org.eclipse.ui.IWorkbenchPage} object.
-     */
     public static IWorkbenchPage getActivePage() {
+        try{
+            return TaxeditorStorePlugin.getDefault().getWorkbench()
+                    .getActiveWorkbenchWindow().getActivePage();
+        } catch(NullPointerException npe){
+            return null;
+        }
+    }
 
-        return TaxeditorStorePlugin.getDefault().getWorkbench()
-                .getActiveWorkbenchWindow().getActivePage();
+    public static IWorkbenchPart getActiveWorkbenchPart() {
+        IWorkbenchPage activePage = getActivePage();
+        if(activePage!=null){
+            IWorkbenchPart activePart = activePage.getActivePart();
+            if(activePart!=null){
+                return activePart;
+            }
+        }
+        return null;
     }
 
-    /**
-     * <p>
-     * getActivePart
-     * </p>
-     *
-     * @return a {@link org.eclipse.ui.IWorkbenchPart} object.
-     */
-    public static IWorkbenchPart getActivePart() {
-        return getActivePage() != null ? getActivePage().getActivePart() : null;
+    public static Object getActiveE4Part() {
+        IWorkbenchPage activePage = getActivePage();
+        if(activePage!=null){
+            IWorkbenchPart activePart = activePage.getActivePart();
+            Object e4WrappedPart = WorkbenchUtility.getE4WrappedPart(activePart);
+            return e4WrappedPart!=null?e4WrappedPart:activePart;
+        }
+        return null;
     }
 
     public static IWorkbench getWorkbench() {
         return TaxeditorStorePlugin.getDefault().getWorkbench();
     }
 
-    /**
-     * <p>
-     * getWorkbenchWindow
-     * </p>
-     *
-     * @return a {@link org.eclipse.jface.window.ApplicationWindow} object.
-     */
-    public static ApplicationWindow getWorkbenchWindow() {
+    public static IWorkbenchWindow getWorkbenchWindow() {
         if (getWorkbench().getWorkbenchWindowCount() > 1) {
             throw new IllegalStateException("More than one workbench window");
         }
-        return (ApplicationWindow) getWorkbench().getWorkbenchWindows()[0];
+        return getWorkbench().getWorkbenchWindows()[0];
     }
 
-    /**
-     * <p>
-     * showView
-     * </p>
-     *
-     * @param id
-     *            a {@link java.lang.String} object.
-     * @return a {@link org.eclipse.ui.IViewPart} object.
-     */
     public static IViewPart showView(String id) {
         try {
             return PlatformUI.getWorkbench().getActiveWorkbenchWindow()
@@ -194,30 +171,11 @@ public abstract class AbstractUtility {
         }
     }
 
-    /**
-     * <p>
-     * hideView
-     * </p>
-     *
-     * @param view
-     *            a {@link org.eclipse.ui.IViewPart} object.
-     */
     public static void hideView(IViewPart view) {
         PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
         .hideView(view);
     }
 
-    /**
-     * <p>
-     * getView
-     * </p>
-     *
-     * @param id
-     *            a {@link java.lang.String} object.
-     * @param restore
-     *            a boolean.
-     * @return a {@link org.eclipse.ui.IViewPart} object.
-     */
     public static IViewPart getView(String id, boolean restore) {
        IWorkbench workbench = PlatformUI.getWorkbench();
         IWorkbenchWindow activeWorkbenchWindow = workbench.getActiveWorkbenchWindow();
@@ -238,26 +196,10 @@ public abstract class AbstractUtility {
         return null;
     }
 
-    /**
-     * <p>
-     * getService
-     * </p>
-     *
-     * @param api
-     *            a {@link java.lang.Class} object.
-     * @return a {@link java.lang.Object} object.
-     */
     public static Object getService(Class api) {
         return TaxeditorStorePlugin.getDefault().getWorkbench().getService(api);
     }
 
-    /**
-     * <p>
-     * getCurrentTheme
-     * </p>
-     *
-     * @return a {@link org.eclipse.ui.themes.ITheme} object.
-     */
     public static ITheme getCurrentTheme() {
         IThemeManager themeManager = TaxeditorStorePlugin.getDefault()
                 .getWorkbench().getThemeManager();
@@ -275,15 +217,6 @@ public abstract class AbstractUtility {
         return getCurrentTheme().getFontRegistry();
     }
 
-    /**
-     * <p>
-     * getFont
-     * </p>
-     *
-     * @param symbolicName
-     *            a {@link java.lang.String} object.
-     * @return a {@link org.eclipse.swt.graphics.Font} object.
-     */
     public static Font getFont(String symbolicName) {
         return getFontRegistry().get(symbolicName);
     }
@@ -299,30 +232,10 @@ public abstract class AbstractUtility {
         return getCurrentTheme().getColorRegistry();
     }
 
-    /**
-     * <p>
-     * getColor
-     * </p>
-     *
-     * @param symbolicName
-     *            a {@link java.lang.String} object.
-     * @return a {@link org.eclipse.swt.graphics.Color} object.
-     */
     public static Color getColor(String symbolicName) {
         return getColorRegistry().get(symbolicName);
     }
 
-    /**
-     * <p>
-     * executeOperation
-     * </p>
-     *
-     * @param operation
-     *            a
-     *            {@link eu.etaxonomy.taxeditor.operation.AbstractPostTaxonOperation}
-     *            object.
-     * @return a {@link org.eclipse.core.runtime.IStatus} object.
-     */
     public static IStatus executeOperation(final AbstractPostOperation operation) {
         if (getOperationHistory() == null) {
             throw new IllegalArgumentException(
@@ -373,7 +286,15 @@ public abstract class AbstractUtility {
         return Status.OK_STATUS;
     }
 
+    public static IStatus executeOperation(final AbstractOperation operation, final RemotingCdmHandlerE4 handler) {
+        return executeOperation(operation, handler);
+    }
+
     public static IStatus executeOperation(final AbstractOperation operation, final RemotingCdmHandler handler) {
+        return executeOperation(operation, handler);
+    }
+
+    public static IStatus executeOperation(final AbstractOperation operation, final Object handler) {
         if (getOperationHistory() == null) {
             throw new IllegalArgumentException(
                     "There is no operation history for this context");
@@ -394,8 +315,11 @@ public abstract class AbstractUtility {
                     operation.addContext(IOperationHistory.GLOBAL_UNDO_CONTEXT);
                     status = getOperationHistory().execute(operation, monitor,
                             uiInfoAdapter);
-                    if(handler != null) {
-                        handler.postOperation(status);
+                    if(handler instanceof RemotingCdmHandler) {
+                        ((RemotingCdmHandler) handler).postOperation(status);
+                    }
+                    else if(handler instanceof RemotingCdmHandlerE4) {
+                        ((RemotingCdmHandlerE4) handler).postOperation(status);
                     }
                 } catch (ExecutionException e) {
                     MessagingUtils.operationDialog(this, e, TaxeditorStorePlugin.PLUGIN_ID, operationlabel, null);
@@ -506,27 +430,10 @@ public abstract class AbstractUtility {
         return Status.OK_STATUS;
     }
 
-
-    /**
-     * <p>
-     * getOperationHistory
-     * </p>
-     *
-     * @return a {@link org.eclipse.core.commands.operations.IOperationHistory}
-     *         object.
-     */
     public static IOperationHistory getOperationHistory() {
         return getWorkbench().getOperationSupport().getOperationHistory();
     }
 
-    /**
-     * <p>
-     * setStatusLine
-     * </p>
-     *
-     * @param message
-     *            a {@link java.lang.String} object.
-     */
     public static void setStatusLine(final String message) {
         Display.getDefault().asyncExec(new Runnable() {
 
@@ -539,13 +446,6 @@ public abstract class AbstractUtility {
 
     }
 
-    /**
-     * <p>
-     * getMonitor
-     * </p>
-     *
-     * @return a {@link org.eclipse.core.runtime.IProgressMonitor} object.
-     */
     public static IProgressMonitor getMonitor() {
         statusLineManager.setCancelEnabled(false);
         return statusLineManager.getProgressMonitor();
@@ -634,127 +534,60 @@ public abstract class AbstractUtility {
         getProgressService().busyCursorWhile(runnable);
     }
 
-    /**
-     * <p>
-     * runInUI
-     * </p>
-     *
-     * @see {@link IProgressService#runInUI(org.eclipse.jface.operation.IRunnableContext, IRunnableWithProgress, ISchedulingRule)}
-     * @param runnable
-     *            a {@link org.eclipse.jface.operation.IRunnableWithProgress}
-     *            object.
-     * @param rule
-     *            a {@link org.eclipse.core.runtime.jobs.ISchedulingRule}
-     *            object.
-     * @throws java.lang.reflect.InvocationTargetException
-     *             if any.
-     * @throws java.lang.InterruptedException
-     *             if any.
-     */
     public static void runInUI(IRunnableWithProgress runnable,
             ISchedulingRule rule) throws InvocationTargetException,
             InterruptedException {
         getProgressService().runInUI(getWorkbenchWindow(), runnable, rule);
     }
 
-    /**
-     * <p>
-     * run
-     * </p>
-     *
-     * @param fork
-     *            a boolean.
-     * @param cancelable
-     *            a boolean.
-     * @param runnable
-     *            a {@link org.eclipse.jface.operation.IRunnableWithProgress}
-     *            object.
-     * @throws java.lang.reflect.InvocationTargetException
-     *             if any.
-     * @throws java.lang.InterruptedException
-     *             if any.
-     */
     public static void run(boolean fork, boolean cancelable,
             IRunnableWithProgress runnable) throws InvocationTargetException,
             InterruptedException {
         getProgressService().run(fork, cancelable, runnable);
     }
 
-    /**
-     * <p>
-     * getProgressService
-     * </p>
-     *
-     * @return a {@link org.eclipse.ui.progress.IProgressService} object.
-     */
     public static IProgressService getProgressService() {
         IWorkbench workbench = PlatformUI.getWorkbench();
         return workbench.getProgressService();
     }
 
-    /**
-     * <p>
-     * getProgressService2
-     * </p>
-     *
-     * @return a {@link org.eclipse.ui.progress.IWorkbenchSiteProgressService}
-     *         object.
-     */
     public static IWorkbenchSiteProgressService getProgressService2() {
         return (IWorkbenchSiteProgressService) getService(IWorkbenchSiteProgressService.class);
     }
 
-    /**
-     * <p>
-     * getPluginId
-     * </p>
-     *
-     * @return a {@link java.lang.String} object.
-     */
     public static String getPluginId() {
         return "eu.taxeditor";
     }
 
-    /**
-     * <p>
-     * getActiveEditor
-     * </p>
-     *
-     * @return a {@link org.eclipse.ui.IEditorPart} object.
-     */
     public static IEditorPart getActiveEditor() {
         return getActivePage() != null ? getActivePage().getActiveEditor()
                 : null;
     }
 
-    /**
-     * <p>
-     * getDetailsView
-     * </p>
-     *
-     * @return a {@link eu.etaxonomy.taxeditor.view.detail.DetailsViewPart}
-     *         object.
-     */
+    public static Object getActiveE4Editor() {
+        //FIXME E4 this can be simplified/removed when fully migrated
+        if(getActivePage()!=null){
+            IEditorPart activeEditor = getActivePage().getActiveEditor();
+            Object activePart = getActivePage().getActivePart();
+            if(activeEditor==null){
+                activePart = TaxeditorStorePlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EPartService.class).getActivePart();
+            }
+            Object wrappedPart = WorkbenchUtility.getE4WrappedPart(activePart);
+            return wrappedPart!=null?wrappedPart:activeEditor;
+        }
+        return null;
+    }
+
     public static DetailsViewPart getDetailsView() {
         return (DetailsViewPart) getView(DetailsViewPart.ID, false);
     }
 
-    /**
-     * <p>
-     * refreshDetailsViewer
-     * </p>
-     */
     public static void refreshDetailsViewer() {
         if (getDetailsView() != null) {
             ((AbstractCdmDataViewer) getDetailsView().getViewer()).refresh();
         }
     }
 
-    /**
-     * <p>
-     * reflowDetailsViewer
-     * </p>
-     */
     public static void reflowDetailsViewer() {
         if (getDetailsView() != null) {
             ((AbstractCdmDataViewer) getDetailsView().getViewer()).reflow();
@@ -868,10 +701,6 @@ public abstract class AbstractUtility {
         private final T term;
         private final TreeSet<TermNode<T>> children;
 
-        /**
-         * @param term
-         * @param children
-         */
         public TermNode(T term) {
             super();
             this.term = term;
@@ -882,23 +711,14 @@ public abstract class AbstractUtility {
             this.children.add(child);
         }
 
-        /**
-         * @return the children
-         */
         public TreeSet<TermNode<T>> getChildren() {
             return children;
         }
 
-        /**
-         * @return the term
-         */
         public T getTerm() {
             return term;
         }
 
-        /* (non-Javadoc)
-         * @see java.lang.Object#hashCode()
-         */
         @Override
         public int hashCode() {
             final int prime = 31;
@@ -907,9 +727,6 @@ public abstract class AbstractUtility {
             return result;
         }
 
-        /* (non-Javadoc)
-         * @see java.lang.Object#equals(java.lang.Object)
-         */
         @Override
         public boolean equals(Object obj) {
             if (this == obj) {
@@ -932,9 +749,6 @@ public abstract class AbstractUtility {
             return true;
         }
 
-        /* (non-Javadoc)
-         * @see java.lang.Comparable#compareTo(java.lang.Object)
-         */
         @Override
         public int compareTo(TermNode<T> that) {
             return this.term.getMessage().compareTo(that.term.getMessage());
@@ -966,4 +780,16 @@ public abstract class AbstractUtility {
             }
         }
     }
+
+    public static Object getElementsFromSelectionChangedEvent(SelectionChangedEvent event) {
+        IStructuredSelection selection = (IStructuredSelection) event.getSelection();
+        Object selectionToSet = selection;
+        if(selection.size() == 1){
+            selectionToSet = selection.getFirstElement();
+        }
+        else if(!selection.isEmpty()){
+            selectionToSet = selection.toArray();
+        }
+        return selectionToSet;
+    }
 }