- removed dependencies to LibrAlign
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / EditorUtil.java
index 22faa208a06733faa99f7be025b10996e7bbbfe6..c9350145261000221c842ae1127f8c5d96779a32 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,18 +27,29 @@ import org.eclipse.ui.IEditorReference;
 import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.handlers.HandlerUtil;
 
-import eu.etaxonomy.cdm.model.common.CdmBase;
+import eu.etaxonomy.cdm.api.service.ITaxonService;
 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
 import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
+import eu.etaxonomy.cdm.model.taxon.TaxonBase;
 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.ChecklistEditor;
+import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditorInput;
+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;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
+import eu.etaxonomy.taxeditor.store.CdmStore;
 
 /**
  * Utility for the editor package
@@ -97,6 +109,32 @@ 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 {@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
@@ -123,6 +161,11 @@ public class EditorUtil extends AbstractUtility {
         */
        public static void openTaxonBase(UUID taxonBaseUuid)
                        throws PartInitException {
+           TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).find(taxonBaseUuid);
+        if (taxonBase != null && taxonBase.isOrphaned()) {
+                MessagingUtils.warningDialog("Orphaned Taxon", TaxonEditorInput.class, "This is an orphaned taxon i.e. a taxon that is not connected to a classification and not having any taxonomic relationships. Editing of orphaned taxon is currently not supported.");
+                return;
+        }
                TaxonEditorInput input = TaxonEditorInput
                                .NewInstanceFromTaxonBase(taxonBaseUuid);
                open(input);
@@ -347,23 +390,67 @@ public class EditorUtil extends AbstractUtility {
                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){
-        if(specimen==null) {
-            return null;
-        }
-        if(specimen.isInstanceOf(FieldUnit.class)){
-            return (FieldUnit) specimen;
+        SpecimenOrObservationBase<?> topMostDerivate = getTopMostDerivate(specimen);
+        if(topMostDerivate instanceof FieldUnit) {
+            return (FieldUnit) topMostDerivate;
         }
-        else if(specimen instanceof DerivedUnit && ((DerivedUnit) specimen).getOriginals()!=null){
-            for(SpecimenOrObservationBase original:((DerivedUnit) specimen).getOriginals()){
-                if(original.isInstanceOf(FieldUnit.class)){
-                    return (FieldUnit) original;
-                }
-                else if(original.isInstanceOf(DerivedUnit.class)){
-                    return getFieldUnit(original);
-                }
-            }
+        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;
     }
+
+    /**
+     * Opens a taxon editor for the given object if the given object is a valid input.
+     * @param object the object for which the editor will be opened
+     * @throws PartInitException
+     */
+    public static void openTaxonEditor(Object object) throws PartInitException {
+        if(object instanceof TaxonBase<?>){
+            openTaxonBase(((TaxonBase<?>) object).getUuid());
+        }
+    }
 }