ref #6315: implement set secundum reference for subtree in editor
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / EditorUtil.java
index 4ed9313d32cf8063e44cccad67ca856f13df9eb9..522bd59e370f8a163ee79fe5495353e9d1c4fa67 100644 (file)
@@ -24,30 +24,36 @@ import org.eclipse.swt.widgets.Shell;
 import org.eclipse.ui.IEditorInput;
 import org.eclipse.ui.IEditorPart;
 import org.eclipse.ui.IEditorReference;
+import org.eclipse.ui.IWorkbenchPage;
 import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.handlers.HandlerUtil;
 
+import eu.etaxonomy.cdm.api.service.ITaxonService;
+import eu.etaxonomy.cdm.model.common.ITreeNode;
 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.Synonym;
+import eu.etaxonomy.cdm.model.taxon.TaxonBase;
+import eu.etaxonomy.cdm.model.taxon.TaxonNode;
 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.checklist.ChecklistEditorInput;
+//import eu.etaxonomy.taxeditor.store.view.dataimport.BioCaseEditorInput;
+//import eu.etaxonomy.taxeditor.view.dataimport.DataImportEditor;
+//import eu.etaxonomy.taxeditor.view.dataimport.DataImportEditorInput;
+//import eu.etaxonomy.taxeditor.view.dataimport.GbifImportEditor;
+//import eu.etaxonomy.taxeditor.view.dataimport.GbifImportEditorInput;
+//import eu.etaxonomy.taxeditor.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
@@ -71,7 +77,14 @@ public class EditorUtil extends AbstractUtility {
         */
        private static IEditorPart open(final IEditorInput input,
                        final String editorId) throws PartInitException {
-               return getActivePage().openEditor(input, editorId);
+               IEditorPart editor = getActivePage().openEditor(input, editorId);
+               if(input != null &&
+                       editor.getEditorInput() != null &&
+                       input != editor.getEditorInput() &&
+                       input instanceof CdmEntitySessionInput) {
+                   ((CdmEntitySessionInput)input).dispose();
+               }
+               return editor;
        }
 
        /**
@@ -118,30 +131,20 @@ public class EditorUtil extends AbstractUtility {
            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);
-           }
-       }
+//     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
@@ -169,9 +172,27 @@ 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()) {
+            if(taxonBase.isInstanceOf(Synonym.class)){
+                MessagingUtils.warningDialog("Orphaned accepted taxon", TaxonEditorInput.class, "The accepted "
+                        + "taxon of this synonym is not part of any classification. Editing with the "
+                        + "name editor is currently not implemented. Try to edit the taxon with the bulk editor.");
+                return;
+            }
+            else{
+                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 taxa in the name editor is currently not supported. "
+                        + "Try editing with the bulk editor");
+                return;
+            }
+        }
                TaxonEditorInput input = TaxonEditorInput
                                .NewInstanceFromTaxonBase(taxonBaseUuid);
-               open(input);
+               if(input!=null && input.getTaxonNode()!=null){
+                   open(input);
+               }
        }
 
        /**
@@ -387,6 +408,12 @@ public class EditorUtil extends AbstractUtility {
                open(input);
        }
 
+//     public static void openPolytomousKeyEditor(UUID polytomousKeyUuid, String name)
+//             throws Exception {
+//         PolytomousKeyEditorInput input = new PolytomousKeyEditorInput(polytomousKeyUuid, name);
+//         open(input);
+//     }
+
        public static void openCdmAuthorities(UUID groupUuid)
                        throws Exception {
                CdmAuthorityEditorInput input = CdmAuthorityEditorInput.NewInstance(groupUuid);
@@ -417,20 +444,6 @@ public class EditorUtil extends AbstractUtility {
            }
        }
 
-       /**
-        * 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
@@ -445,4 +458,39 @@ public class EditorUtil extends AbstractUtility {
         }
         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());
+        }
+    }
+
+    public static boolean closeObsoleteEditor(TaxonNode taxonNode, IWorkbenchPage activePage){
+        boolean result = true;
+        for (IEditorReference ref : activePage.getEditorReferences()) {
+            try {
+                String treeIndex = ((ITreeNode)taxonNode).treeIndex();
+
+
+                IEditorInput input = ref.getEditorInput();
+                if (input instanceof TaxonEditorInput) {
+                    TaxonNode node = ((TaxonEditorInput) input).getTaxonNode();
+                    //if node is a child of taxonNode then close the editor
+                    if( ((ITreeNode) node).treeIndex().startsWith(treeIndex)){
+                    //if (taxonNode.equals(node)) {
+                        result &= activePage.closeEditor(ref.getEditor(false), true);
+
+                    }
+                }
+            } catch (PartInitException e) {
+                continue;
+            }
+        }
+        return result;
+    }
 }