merge-update from trunk
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / EditorUtil.java
index 135d2a7bfd32d638ab47cfba472d9d03cb05c35f..4ed9313d32cf8063e44cccad67ca856f13df9eb9 100644 (file)
@@ -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,11 +27,24 @@ 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.molecular.AlignmentEditor;
+import eu.etaxonomy.taxeditor.editor.molecular.AlignmentEditorInput;
+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;
@@ -93,6 +107,42 @@ public class EditorUtil extends AbstractUtility {
            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 AlignmentEditor for the given input
+        * @param input
+        * @throws PartInitException
+        */
+       public static void open(AlignmentEditorInput input)
+               throws PartInitException {
+           open(input, AlignmentEditor.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
@@ -298,7 +348,7 @@ public class EditorUtil extends AbstractUtility {
                                return false;
                        }
 
-                       editor.doSave(EditorUtil.getMonitor());
+                       editor.doSave(AbstractUtility.getMonitor());
                }
                return true;
        }
@@ -342,4 +392,57 @@ public class EditorUtil extends AbstractUtility {
                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;
+    }
 }