Fix NPE
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / EditorUtil.java
index 222bf2c60890352ea1fd390835eceeafa3645e9a..e87142c40ea62fbe582d74221d87aa1d5b932a1a 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,14 +27,33 @@ import org.eclipse.ui.IEditorReference;
 import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.handlers.HandlerUtil;
 
+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
- * 
+ *
  * @author n.hoffmann
  * @created 20.01.2009
  * @version 1.0
@@ -44,7 +64,7 @@ public class EditorUtil extends AbstractUtility {
 
        /**
         * Opens a new editor window with the given input
-        * 
+        *
         * @param input
         * @param editorId
         * @return
@@ -58,7 +78,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.
@@ -74,10 +94,51 @@ public class EditorUtil extends AbstractUtility {
                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
@@ -92,7 +153,7 @@ public class EditorUtil extends AbstractUtility {
         * <p>
         * openTaxonBase
         * </p>
-        * 
+        *
         * @param taxonBaseUuid
         *            a {@link java.util.UUID} object.
         * @throws org.eclipse.ui.PartInitException
@@ -100,16 +161,23 @@ 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);
+               if(input!=null){
+                   open(input);
+               }
        }
 
        /**
         * <p>
         * findEditorByTaxonNodeUuid
         * </p>
-        * 
+        *
         * @param taxonNodeUuid
         *            a {@link java.util.UUID} object.
         * @return a {@link org.eclipse.ui.IEditorPart} object.
@@ -126,7 +194,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
@@ -145,7 +213,7 @@ public class EditorUtil extends AbstractUtility {
         * <p>
         * setSaving
         * </p>
-        * 
+        *
         * @param isSaving
         *            a boolean.
         */
@@ -157,7 +225,7 @@ public class EditorUtil extends AbstractUtility {
         * <p>
         * isSaving
         * </p>
-        * 
+        *
         * @return a boolean.
         */
        public static boolean isSaving() {
@@ -166,7 +234,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() {
@@ -187,7 +255,7 @@ public class EditorUtil extends AbstractUtility {
 
        /**
         * Returns the currently active taxon editor
-        * 
+        *
         * @return the taxon editor that has focus
         */
        public static MultiPageTaxonEditor getActiveMultiPageTaxonEditor() {
@@ -204,7 +272,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.
@@ -217,7 +285,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() {
@@ -233,7 +301,7 @@ public class EditorUtil extends AbstractUtility {
         * <p>
         * getUndoContext
         * </p>
-        * 
+        *
         * @param editor
         *            a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor}
         *            object.
@@ -248,7 +316,7 @@ public class EditorUtil extends AbstractUtility {
         * <p>
         * getUndoContext
         * </p>
-        * 
+        *
         * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
         *         object.
         */
@@ -260,7 +328,7 @@ public class EditorUtil extends AbstractUtility {
         * <p>
         * forceUserSave
         * </p>
-        * 
+        *
         * @param editor
         *            a {@link org.eclipse.ui.IEditorPart} object.
         * @param shell
@@ -279,7 +347,7 @@ public class EditorUtil extends AbstractUtility {
                                return false;
                        }
 
-                       editor.doSave(EditorUtil.getMonitor());
+                       editor.doSave(AbstractUtility.getMonitor());
                }
                return true;
        }
@@ -288,7 +356,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.
@@ -304,7 +372,7 @@ public class EditorUtil extends AbstractUtility {
         * <p>
         * getPluginId
         * </p>
-        * 
+        *
         * @return a {@link java.lang.String} object.
         */
        public static String getPluginId() {
@@ -317,4 +385,74 @@ 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;
+    }
+
+    /**
+     * 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());
+        }
+    }
 }