reintegrated redlist branch into trunk
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / EditorUtil.java
index a951ef9030fb6dfa797c384340a67361a46f2350..d3f414a0334e9c9f93ee75f1216c7e1ea9d12b9a 100644 (file)
@@ -1,8 +1,8 @@
 /**
  * Copyright (C) 2007 EDIT
- * European Distributed Institute of Taxonomy 
+ * European Distributed Institute of Taxonomy
  * http://www.e-taxonomy.eu
- * 
+ *
  * The contents of this file are subject to the Mozilla Public License Version 1.1
  * See LICENSE.TXT at the top of this package for the full license terms.
  */
@@ -19,6 +19,7 @@ import org.eclipse.core.commands.operations.IUndoContext;
 import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.viewers.TreeNode;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.ui.IEditorInput;
 import org.eclipse.ui.IEditorPart;
@@ -26,16 +27,29 @@ import org.eclipse.ui.IEditorReference;
 import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.handlers.HandlerUtil;
 
+import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
+import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
+import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
 import eu.etaxonomy.taxeditor.editor.group.authority.CdmAuthorityEditor;
 import eu.etaxonomy.taxeditor.editor.group.authority.CdmAuthorityEditorInput;
 import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
 import eu.etaxonomy.taxeditor.editor.key.KeyEditor;
 import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyEditorInput;
+import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditorInput;
+import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditor;
+import eu.etaxonomy.taxeditor.editor.view.dataimport.BioCaseEditorInput;
+import eu.etaxonomy.taxeditor.editor.view.dataimport.DataImportEditor;
+import eu.etaxonomy.taxeditor.editor.view.dataimport.DataImportEditorInput;
+import eu.etaxonomy.taxeditor.editor.view.dataimport.GbifImportEditor;
+import eu.etaxonomy.taxeditor.editor.view.dataimport.GbifImportEditorInput;
+import eu.etaxonomy.taxeditor.editor.view.dataimport.SpecimenImportEditor;
+import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
+import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateViewEditorInput;
 import eu.etaxonomy.taxeditor.model.AbstractUtility;
 
 /**
  * Utility for the editor package
- * 
+ *
  * @author n.hoffmann
  * @created 20.01.2009
  * @version 1.0
@@ -46,7 +60,7 @@ public class EditorUtil extends AbstractUtility {
 
        /**
         * Opens a new editor window with the given input
-        * 
+        *
         * @param input
         * @param editorId
         * @return
@@ -60,7 +74,7 @@ public class EditorUtil extends AbstractUtility {
 
        /**
         * Opens a new editor window with the given TaxonEditorInput
-        * 
+        *
         * @param input
         *            a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput}
         *            object.
@@ -75,16 +89,52 @@ public class EditorUtil extends AbstractUtility {
                        throws PartInitException {
                open(input, KeyEditor.ID);
        }
-       
+
        public static void open(CdmAuthorityEditorInput input)
                        throws PartInitException {
                open(input, CdmAuthorityEditor.ID);
        }
 
+       /**
+        * Opens a new DerivateView for the given input
+        * @param input a {@link DerivateViewEditorInput} representing the selected derivate
+        * @throws PartInitException
+        */
+       public static void open(DerivateViewEditorInput input)
+               throws PartInitException {
+           open(input, DerivateView.ID);
+       }
+
+
+       /**
+        * Opens a new ChecklistView for the given input
+        * @param input a {@link ChecklistEditorInput} representing the selected checklist
+        * @throws PartInitException
+        */
+       public static void open(ChecklistEditorInput input)
+               throws PartInitException {
+           open(input, ChecklistEditor.ID);
+       }
+
+       /**
+        * Opens a new {@link DataImportEditor} for the given input
+        * @param input a {@link DataImportEditorInput}
+        * @throws PartInitException
+        */
+       public static void open(DataImportEditorInput<?> input)
+               throws PartInitException {
+           if(input instanceof BioCaseEditorInput){
+               open(input, SpecimenImportEditor.ID);
+           }
+           else if(input instanceof GbifImportEditorInput){
+               open(input, GbifImportEditor.ID);
+           }
+       }
+
        /**
         * Taxon Editors may be opened by supplying a taxon node uuid. Session gets
         * initialised here and is passed to the editor
-        * 
+        *
         * @param taxonNodeUuid
         *            a {@link java.util.UUID} object.
         * @throws java.lang.Exception
@@ -99,7 +149,7 @@ public class EditorUtil extends AbstractUtility {
         * <p>
         * openTaxonBase
         * </p>
-        * 
+        *
         * @param taxonBaseUuid
         *            a {@link java.util.UUID} object.
         * @throws org.eclipse.ui.PartInitException
@@ -116,7 +166,7 @@ public class EditorUtil extends AbstractUtility {
         * <p>
         * findEditorByTaxonNodeUuid
         * </p>
-        * 
+        *
         * @param taxonNodeUuid
         *            a {@link java.util.UUID} object.
         * @return a {@link org.eclipse.ui.IEditorPart} object.
@@ -133,7 +183,7 @@ public class EditorUtil extends AbstractUtility {
         * An uninitialized taxon is one that hasn't been saved yet. As such, it
         * should appear in neither the list of recent names nor in the taxonomic
         * tree when opened.
-        * 
+        *
         * @throws org.eclipse.ui.PartInitException
         *             if any.
         * @param parentNodeUuid
@@ -152,7 +202,7 @@ public class EditorUtil extends AbstractUtility {
         * <p>
         * setSaving
         * </p>
-        * 
+        *
         * @param isSaving
         *            a boolean.
         */
@@ -164,7 +214,7 @@ public class EditorUtil extends AbstractUtility {
         * <p>
         * isSaving
         * </p>
-        * 
+        *
         * @return a boolean.
         */
        public static boolean isSaving() {
@@ -173,7 +223,7 @@ public class EditorUtil extends AbstractUtility {
 
        /**
         * Returns a set of all currently open <code>MultiPageTaxonEditor</code>s.
-        * 
+        *
         * @return a {@link java.util.Set} object.
         */
        public static Set<IEditorPart> getOpenEditors() {
@@ -194,7 +244,7 @@ public class EditorUtil extends AbstractUtility {
 
        /**
         * Returns the currently active taxon editor
-        * 
+        *
         * @return the taxon editor that has focus
         */
        public static MultiPageTaxonEditor getActiveMultiPageTaxonEditor() {
@@ -211,7 +261,7 @@ public class EditorUtil extends AbstractUtility {
         * <p>
         * getActiveEditorPage
         * </p>
-        * 
+        *
         * @param page
         *            a {@link eu.etaxonomy.taxeditor.editor.Page} object.
         * @return a {@link org.eclipse.ui.IEditorPart} object.
@@ -224,7 +274,7 @@ public class EditorUtil extends AbstractUtility {
 
        /**
         * Returns the selection of the currently active taxon editor
-        * 
+        *
         * @return a {@link org.eclipse.jface.viewers.ISelection} object.
         */
        public static ISelection getCurrentSelection() {
@@ -240,7 +290,7 @@ public class EditorUtil extends AbstractUtility {
         * <p>
         * getUndoContext
         * </p>
-        * 
+        *
         * @param editor
         *            a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor}
         *            object.
@@ -255,7 +305,7 @@ public class EditorUtil extends AbstractUtility {
         * <p>
         * getUndoContext
         * </p>
-        * 
+        *
         * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
         *         object.
         */
@@ -267,7 +317,7 @@ public class EditorUtil extends AbstractUtility {
         * <p>
         * forceUserSave
         * </p>
-        * 
+        *
         * @param editor
         *            a {@link org.eclipse.ui.IEditorPart} object.
         * @param shell
@@ -286,7 +336,7 @@ public class EditorUtil extends AbstractUtility {
                                return false;
                        }
 
-                       editor.doSave(EditorUtil.getMonitor());
+                       editor.doSave(AbstractUtility.getMonitor());
                }
                return true;
        }
@@ -295,7 +345,7 @@ public class EditorUtil extends AbstractUtility {
         * <p>
         * getSelection
         * </p>
-        * 
+        *
         * @param event
         *            a {@link org.eclipse.core.commands.ExecutionEvent} object.
         * @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
@@ -311,7 +361,7 @@ public class EditorUtil extends AbstractUtility {
         * <p>
         * getPluginId
         * </p>
-        * 
+        *
         * @return a {@link java.lang.String} object.
         */
        public static String getPluginId() {
@@ -324,10 +374,63 @@ public class EditorUtil extends AbstractUtility {
                                .NewInstance(polytomousKeyUuid);
                open(input);
        }
-       
+
        public static void openCdmAuthorities(UUID groupUuid)
                        throws Exception {
                CdmAuthorityEditorInput input = CdmAuthorityEditorInput.NewInstance(groupUuid);
                open(input);
        }
+
+       /**
+        * Iterates recursively over all originals having the given specimen as a derivate.
+        * The first {@link DerivedUnit} with no more originals or the first {@link FieldUnit} is returned
+        * @param specimen the start element for which the originals are iterated recursively
+        * @return either a FieldUnit or a the topmost DerivedUnit (which can be itself)
+        */
+       public static SpecimenOrObservationBase<?> getTopMostDerivate(SpecimenOrObservationBase<?> specimen){
+           if(specimen.isInstanceOf(FieldUnit.class)){
+               return specimen;
+           }
+           else if(specimen instanceof DerivedUnit
+                   && ((DerivedUnit) specimen).getOriginals()!=null
+                   && !((DerivedUnit) specimen).getOriginals().isEmpty()){
+               for(SpecimenOrObservationBase<?> original:((DerivedUnit) specimen).getOriginals()){
+                   return getTopMostDerivate(original);
+               }
+               //needed to add this for compilation although this is unreachable
+               return specimen;
+           }
+           else{
+               return specimen;
+           }
+       }
+
+       /**
+        * Iterates recursively over all originals having the given specimen as a derivate.
+        * If a {@link FieldUnit} is found it is returned
+        * @param specimen the start element for which the originals are iterated recursively
+        * @return the FieldUnit if found, <code>null</code> otherwise
+        */
+    public static FieldUnit getFieldUnit(SpecimenOrObservationBase<?> specimen){
+        SpecimenOrObservationBase<?> topMostDerivate = getTopMostDerivate(specimen);
+        if(topMostDerivate instanceof FieldUnit) {
+            return (FieldUnit) topMostDerivate;
+        }
+        return null;
+    }
+
+    /**
+     * If the current selection is a single {@link TreeNode} it will be returned.
+     * @param selection the selection to check
+     * @return the selected TreeNode or <code>null</code> if no TreeNode selected
+     */
+    public static TreeNode getTreeNodeOfSelection(ISelection selection){
+        if(selection instanceof IStructuredSelection
+                && ((IStructuredSelection) selection).size()==1
+                && ((IStructuredSelection) selection).getFirstElement() instanceof TreeNode){
+            return (TreeNode) ((IStructuredSelection) selection).getFirstElement();
+
+        }
+        return null;
+    }
 }