reintegrated redlist branch into trunk
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / EditorUtil.java
index a781c7afe926cdaebf0ce6295053184625eacffc..d3f414a0334e9c9f93ee75f1216c7e1ea9d12b9a 100644 (file)
@@ -1,11 +1,11 @@
 /**
-* Copyright (C) 2007 EDIT
-* 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.
-*/
+ * Copyright (C) 2007 EDIT
+ * 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.
+ */
 
 package eu.etaxonomy.taxeditor.editor;
 
@@ -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,13 +27,25 @@ 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.polytomous.PolytomousKeyEditor;
+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;
-import eu.etaxonomy.taxeditor.view.AbstractCdmDataViewer;
-import eu.etaxonomy.taxeditor.view.detail.DetailsViewPart;
-import eu.etaxonomy.taxeditor.view.supplementaldata.SupplementalDataViewPart;
 
 /**
  * Utility for the editor package
@@ -41,232 +54,383 @@ import eu.etaxonomy.taxeditor.view.supplementaldata.SupplementalDataViewPart;
  * @created 20.01.2009
  * @version 1.0
  */
-public class EditorUtil extends AbstractUtility{
+public class EditorUtil extends AbstractUtility {
 
        private static boolean isSaving = false;
 
        /**
         * Opens a new editor window with the given input
-        * 
+        *
         * @param input
         * @param editorId
-        * @return 
-        * @return 
+        * @return
+        * @return
         * @throws PartInitException
         */
-       private static IEditorPart open(final IEditorInput input, final String editorId) throws PartInitException{
+       private static IEditorPart open(final IEditorInput input,
+                       final String editorId) throws PartInitException {
                return getActivePage().openEditor(input, editorId);
        }
-       
+
        /**
         * Opens a new editor window with the given TaxonEditorInput
         *
-        * @param input a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
-        * @throws org.eclipse.ui.PartInitException if any.
+        * @param input
+        *            a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput}
+        *            object.
+        * @throws org.eclipse.ui.PartInitException
+        *             if any.
         */
-       public static void open(TaxonEditorInput input) throws PartInitException{
+       public static void open(TaxonEditorInput input) throws PartInitException {
                open(input, MultiPageTaxonEditor.ID);
        }
-       
-       public static void open(PolytomousKeyEditorInput input) throws PartInitException{
-               open(input, PolytomousKeyEditor.ID);
+
+       public static void open(PolytomousKeyEditorInput input)
+                       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
+        * 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 if any.
+        * @param taxonNodeUuid
+        *            a {@link java.util.UUID} object.
+        * @throws java.lang.Exception
+        *             if any.
         */
        public static void openTaxonNode(UUID taxonNodeUuid) throws Exception {
-               TaxonEditorInput input = TaxonEditorInput.NewInstance(taxonNodeUuid); 
+               TaxonEditorInput input = TaxonEditorInput.NewInstance(taxonNodeUuid);
                open(input);
        }
-       
+
        /**
-        * <p>openTaxonBase</p>
+        * <p>
+        * openTaxonBase
+        * </p>
         *
-        * @param taxonBaseUuid a {@link java.util.UUID} object.
-        * @throws org.eclipse.ui.PartInitException if any.
+        * @param taxonBaseUuid
+        *            a {@link java.util.UUID} object.
+        * @throws org.eclipse.ui.PartInitException
+        *             if any.
         */
-       public static void openTaxonBase(UUID taxonBaseUuid) throws PartInitException {
-               TaxonEditorInput input = TaxonEditorInput.NewInstanceFromTaxonBase(taxonBaseUuid); 
+       public static void openTaxonBase(UUID taxonBaseUuid)
+                       throws PartInitException {
+               TaxonEditorInput input = TaxonEditorInput
+                               .NewInstanceFromTaxonBase(taxonBaseUuid);
                open(input);
        }
-       
 
        /**
-        * <p>findEditorByTaxonNodeUuid</p>
+        * <p>
+        * findEditorByTaxonNodeUuid
+        * </p>
         *
-        * @param taxonNodeUuid a {@link java.util.UUID} object.
+        * @param taxonNodeUuid
+        *            a {@link java.util.UUID} object.
         * @return a {@link org.eclipse.ui.IEditorPart} object.
-        * @throws java.lang.Exception if any.
+        * @throws java.lang.Exception
+        *             if any.
         */
-       public static IEditorPart findEditorByTaxonNodeUuid(UUID taxonNodeUuid) throws Exception{
-               IEditorInput input = TaxonEditorInput.NewInstance(taxonNodeUuid); 
+       public static IEditorPart findEditorByTaxonNodeUuid(UUID taxonNodeUuid)
+                       throws Exception {
+               IEditorInput input = TaxonEditorInput.NewInstance(taxonNodeUuid);
                return getActivePage().findEditor(input);
        }
-       
+
        /**
-        * 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.
+        * 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 a {@link java.util.UUID} object.
+        * @throws org.eclipse.ui.PartInitException
+        *             if any.
+        * @param parentNodeUuid
+        *            a {@link java.util.UUID} object.
         */
-       public static void openEmpty(UUID parentNodeUuid) throws PartInitException{
-               TaxonEditorInput input = TaxonEditorInput.NewEmptyInstance(parentNodeUuid);
+       public static void openEmpty(UUID parentNodeUuid) throws PartInitException {
+               TaxonEditorInput input = TaxonEditorInput
+                               .NewEmptyInstance(parentNodeUuid);
                open(input, MultiPageTaxonEditor.ID);
-               
+
                getActiveMultiPageTaxonEditor().changed(null);
-               
-               
+
        }
 
        /**
-        * <p>setSaving</p>
+        * <p>
+        * setSaving
+        * </p>
         *
-        * @param isSaving a boolean.
+        * @param isSaving
+        *            a boolean.
         */
        public static void setSaving(boolean isSaving) {
                EditorUtil.isSaving = isSaving;
        }
 
        /**
-        * <p>isSaving</p>
+        * <p>
+        * isSaving
+        * </p>
         *
         * @return a boolean.
         */
        public static boolean isSaving() {
                return isSaving;
        }
-       
+
        /**
-        * Returns a set of all currently open
-        * <code>MultiPageTaxonEditor</code>s.
+        * Returns a set of all currently open <code>MultiPageTaxonEditor</code>s.
         *
         * @return a {@link java.util.Set} object.
         */
        public static Set<IEditorPart> getOpenEditors() {
                Set<IEditorPart> taxonEditors = new HashSet<IEditorPart>();
-               
-               if(getActivePage() != null){
-                       for (IEditorReference reference : getActivePage().getEditorReferences()) {
+
+               if (getActivePage() != null) {
+                       for (IEditorReference reference : getActivePage()
+                                       .getEditorReferences()) {
                                IEditorPart editor = reference.getEditor(false);
                                if (editor instanceof MultiPageTaxonEditor) {
                                        taxonEditors.add(editor);
                                }
                        }
                }
-               
+
                return taxonEditors;
        }
-       
+
        /**
         * Returns the currently active taxon editor
         *
         * @return the taxon editor that has focus
         */
-       public static MultiPageTaxonEditor getActiveMultiPageTaxonEditor(){
+       public static MultiPageTaxonEditor getActiveMultiPageTaxonEditor() {
                IEditorPart editorPart = getActiveEditor();
-               if(editorPart != null && editorPart instanceof MultiPageTaxonEditor){
+               if (editorPart != null && editorPart instanceof MultiPageTaxonEditor) {
                        MultiPageTaxonEditor editor = (MultiPageTaxonEditor) editorPart;
                        editor.getConversationHolder().bind();
                        return editor;
                }
                return null;
        }
-       
+
        /**
-        * <p>getActiveEditorPage</p>
+        * <p>
+        * getActiveEditorPage
+        * </p>
         *
-        * @param page a {@link eu.etaxonomy.taxeditor.editor.Page} object.
+        * @param page
+        *            a {@link eu.etaxonomy.taxeditor.editor.Page} object.
         * @return a {@link org.eclipse.ui.IEditorPart} object.
         */
-       public static IEditorPart getActiveEditorPage(Page page){
+       public static IEditorPart getActiveEditorPage(Page page) {
                MultiPageTaxonEditor editor = getActiveMultiPageTaxonEditor();
-               
+
                return editor != null ? editor.getPage(page) : null;
        }
-       
+
        /**
         * Returns the selection of the currently active taxon editor
         *
         * @return a {@link org.eclipse.jface.viewers.ISelection} object.
         */
-       public static ISelection getCurrentSelection(){
-               if(getActiveMultiPageTaxonEditor() == null){
+       public static ISelection getCurrentSelection() {
+               if (getActiveMultiPageTaxonEditor() == null) {
                        return null;
-               }else{
-                       return getActiveMultiPageTaxonEditor().getSite().getSelectionProvider().getSelection();
+               } else {
+                       return getActiveMultiPageTaxonEditor().getSite()
+                                       .getSelectionProvider().getSelection();
                }
        }
 
        /**
-        * <p>getUndoContext</p>
+        * <p>
+        * getUndoContext
+        * </p>
         *
-        * @param editor a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor} object.
-        * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
+        * @param editor
+        *            a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor}
+        *            object.
+        * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
+        *         object.
         */
-       public static IUndoContext getUndoContext(MultiPageTaxonEditor editor){
+       public static IUndoContext getUndoContext(MultiPageTaxonEditor editor) {
                return editor.getUndoContext();
        }
-       
+
        /**
-        * <p>getUndoContext</p>
+        * <p>
+        * getUndoContext
+        * </p>
         *
-        * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
+        * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
+        *         object.
         */
        public static IUndoContext getUndoContext() {
                return IOperationHistory.GLOBAL_UNDO_CONTEXT;
        }
 
-
        /**
-        * <p>forceUserSave</p>
+        * <p>
+        * forceUserSave
+        * </p>
         *
-        * @param editor a {@link org.eclipse.ui.IEditorPart} object.
-        * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
+        * @param editor
+        *            a {@link org.eclipse.ui.IEditorPart} object.
+        * @param shell
+        *            a {@link org.eclipse.swt.widgets.Shell} object.
         * @return a boolean.
         */
        public static boolean forceUserSave(IEditorPart editor, Shell shell) {
                if (editor.isDirty()) {
-                       
-                       boolean doSave = MessageDialog.openConfirm(shell, "Confirm save", "The current editor must be saved before this action can be executed.");
-                       
+
+                       boolean doSave = MessageDialog
+                                       .openConfirm(shell, "Confirm save",
+                                                       "Warning - this operation will save the editor. This will also save all other unsaved changes " +
+                                                       "in your working editor to the database. Please 'Cancel' if you are not ready to do this.");
+
                        if (!doSave) {
                                return false;
                        }
-                       
-                       editor.doSave(EditorUtil.getMonitor());
+
+                       editor.doSave(AbstractUtility.getMonitor());
                }
                return true;
-       }       
-       
+       }
+
        /**
-        * <p>getSelection</p>
+        * <p>
+        * getSelection
+        * </p>
         *
-        * @param event a {@link org.eclipse.core.commands.ExecutionEvent} object.
+        * @param event
+        *            a {@link org.eclipse.core.commands.ExecutionEvent} object.
         * @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
         */
-       public static IStructuredSelection getSelection(ExecutionEvent event){
+       public static IStructuredSelection getSelection(ExecutionEvent event) {
                IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
-                               
-               return (IStructuredSelection) activeEditor.getSite().getSelectionProvider().getSelection();
+
+               return (IStructuredSelection) activeEditor.getSite()
+                               .getSelectionProvider().getSelection();
        }
-       
+
        /**
-        * <p>getPluginId</p>
+        * <p>
+        * getPluginId
+        * </p>
         *
         * @return a {@link java.lang.String} object.
         */
-       protected static String getPluginId(){
+       public static String getPluginId() {
                return TaxeditorEditorPlugin.PLUGIN_ID;
        }
 
-       public static void openPolytomousKey(UUID polytomousKeyUuid) throws Exception{
-               PolytomousKeyEditorInput input = PolytomousKeyEditorInput.NewInstance(polytomousKeyUuid);
+       public static void openPolytomousKey(UUID polytomousKeyUuid)
+                       throws Exception {
+               PolytomousKeyEditorInput input = PolytomousKeyEditorInput
+                               .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;
+    }
 }