ref #9537: new label for ratio to
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / EditorUtil.java
index a8652bdeffc340c45fd9b498063440df5ac33e70..ee461f17229e314b394c26d52d73f1d0c4ee6575 100644 (file)
@@ -6,10 +6,11 @@
  * 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;
 
 import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
 import java.util.UUID;
 
 import org.eclipse.core.commands.ExecutionEvent;
@@ -27,61 +28,157 @@ import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.jface.viewers.TreeNode;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.ui.IEditorPart;
-import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.handlers.HandlerUtil;
 
+import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
+import eu.etaxonomy.cdm.model.description.DescriptiveDataSet;
 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.ITaxonTreeNode;
 import eu.etaxonomy.cdm.model.taxon.Synonym;
 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
+import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
+import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
+import eu.etaxonomy.taxeditor.bulkeditor.e4.BulkEditorE4;
+import eu.etaxonomy.taxeditor.bulkeditor.input.TaxonEditorInput;
+import eu.etaxonomy.taxeditor.editor.descriptiveDataSet.DescriptiveDataSetEditor;
+import eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix.CharacterMatrixPart;
 import eu.etaxonomy.taxeditor.editor.e4.TaxonEditorInputE4;
+import eu.etaxonomy.taxeditor.editor.group.authority.CdmAuthorityEditorInput;
+import eu.etaxonomy.taxeditor.editor.group.authority.e4.CdmAuthorityEditorE4;
 import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
 import eu.etaxonomy.taxeditor.editor.l10n.Messages;
 import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
+import eu.etaxonomy.taxeditor.editor.view.checklist.e4.DistributionEditorPart;
 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.security.RequiredPermissions;
+import eu.etaxonomy.taxeditor.store.CdmStore;
 import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
+import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
 
 /**
  * Utility for the editor package
  *
  * @author n.hoffmann
  * @created 20.01.2009
- * @version 1.0
  */
 public class EditorUtil extends AbstractUtility {
 
     private static final String NAME_EDITOR_ID = "eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4";
-    private static boolean isSaving = false;
 
-       /**
-        * 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);
-//         }
-//     }
+    private static boolean factsVisible = true;
+
+    private static boolean mediaVisible = true;
+
+    public static void openDescriptiveDataSetEditor(UUID descriptiveDataSetUuid, EModelService modelService, EPartService partService, MApplication application){
+        Collection<MPart> parts = partService.getParts();
+        for (MPart part : parts) {
+            if(part.getObject() instanceof DescriptiveDataSetEditor
+                && ((DescriptiveDataSetEditor) part.getObject()).getDescriptiveDataSet().getUuid().equals(descriptiveDataSetUuid)){
+                partService.showPart(part, PartState.ACTIVATE);
+                return;
+            }
+        }
+        String partId = AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_DESCRIPTIVEDATASET_DESCRIPTIVEDATASETEDITOR;
+        MPart part = showPart(partId, modelService, partService, application);
+        DescriptiveDataSetEditor editor = (DescriptiveDataSetEditor) part.getObject();
+        editor.init(descriptiveDataSetUuid);
+    }
+
+    public static void openCharacterMatrix(UUID descriptiveDataSetUuid, EModelService modelService, EPartService partService, MApplication application){
+        Collection<MPart> parts = partService.getParts();
+        for (MPart part : parts) {
+            if(part.getObject() instanceof CharacterMatrixPart
+                && ((CharacterMatrixPart) part.getObject()).getDescriptiveDataSet().getUuid().equals(descriptiveDataSetUuid)){
+                partService.showPart(part, PartState.ACTIVATE);
+                return;
+            }
+        }
+        String partId = AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_DESCRIPTIVEDATASET_MATRIX_CHARACTERMATRIXPART;
+        MPart part = showPart(partId, modelService, partService, application);
+        CharacterMatrixPart editor = (CharacterMatrixPart) part.getObject();
+        editor.init(descriptiveDataSetUuid, true);
+    }
+
+    public static void openDistributionEditor(List<UuidAndTitleCache<ITaxonTreeNode>> parentTaxonUuidList, EModelService modelService, EPartService partService, MApplication application){
+        String partId = AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_CHECKLIST_E4_DISTRIBUTIONEDITORPART;
+        checkAndCloseFactsAndMediaParts(partService);
+        MPart part = showPart(partId, modelService, partService, application);
+        DistributionEditorPart editor = (DistributionEditorPart) part.getObject();
+        editor.init(parentTaxonUuidList);
+    }
+
+    public static void checkAndCloseFactsAndMediaParts(EPartService partService) {
+        String partIdFactualData = AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_DESCRIPTIVE_E4_FACTUALDATAPARTE4;
+        String partIdMedia = AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_MEDIA_E4_MEDIAVIEWPARTE4;
+        MPart part1 = partService.findPart(AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_DESCRIPTIVE_E4_FACTUALDATAPARTE4);
+        if (factsVisible || (part1!=null && part1.getWidget() != null && partService.isPartVisible(part1))){
+            factsVisible = true;
+        }else{
+            factsVisible = false;
+        }
+        part1 = partService.findPart(AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_MEDIA_E4_MEDIAVIEWPARTE4);
+        if (mediaVisible || (part1!=null && part1.getWidget() != null && partService.isPartVisible(part1))){
+            mediaVisible = true;
+        }else{
+            mediaVisible = false;
+        }
+        closePart(partIdMedia, partService);
+        closePart(partIdFactualData, partService);
+    }
+
 
     public static void openSpecimenEditor(DerivateViewEditorInput input, EModelService modelService, EPartService partService, MApplication application){
-        MPart part = partService.createPart(AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_DERIVATE_DERIVATEVIEW);
+        String partId = AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_DERIVATE_DERIVATEVIEW;
+        MPart part = showPart(partId, modelService, partService, application);
+        DerivateView derivateView = (DerivateView) part.getObject();
+        derivateView.init(input);
+    }
+
+    public static void openRightsEditor(CdmAuthorityEditorInput input, EModelService modelService, EPartService partService, MApplication application){
+        Collection<MPart> parts = partService.getParts();
+        for (MPart part : parts) {
+            if(part.getObject() instanceof CdmAuthorityEditorE4
+                && ((CdmAuthorityEditorE4) part.getObject()).getInput().getGroup().equals(input.getGroup())){
+                partService.showPart(part, PartState.ACTIVATE);
+                return;
+            }
+        }
+        String partId = AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_GROUP_AUTHORITY_E4_CDMAUTHORITYEDITORE4;
+        MPart part = showPart(partId, modelService, partService, application);
+        CdmAuthorityEditorE4 authorityView = (CdmAuthorityEditorE4) part.getObject();
+        authorityView.init(input);
+    }
+
+    public static MPart showPart(String partId, EModelService modelService, EPartService partService, MApplication application){
+        MPart part = partService.findPart(partId);
+        if(part==null || modelService.getPartDescriptor(partId).isAllowMultiple()){
+            part = partService.createPart(partId);
+        }
         MPartStack editorAreaPartStack = WorkbenchUtility.getEditorAreaPartStack(application, modelService);
         if(editorAreaPartStack!=null){
             editorAreaPartStack.getChildren().add(part);
         }
-        part = partService.showPart(part, PartState.ACTIVATE);
-        DerivateView derivateView = (DerivateView) part.getObject();
-        derivateView.init(input);
+
+        return partService.showPart(part, PartState.ACTIVATE);
+
+    }
+
+    public static MPart showPart(String partId, EModelService modelService, EPartService partService){
+        MPart part = partService.findPart(partId);
+        if(part==null || modelService.getPartDescriptor(partId).isAllowMultiple()){
+            part = partService.createPart(partId);
+            partService.activate(part);
+        }else{
+            partService.activate(part);
+        }
+
+       return part;
     }
 
        public static void openTaxonNodeE4(UUID taxonNodeUuid, EModelService modelService, EPartService partService, MApplication application) {
@@ -104,10 +201,19 @@ public class EditorUtil extends AbstractUtility {
     }
 
     private static void openNameEditor_internal(TaxonEditorInputE4 input, EModelService modelService, EPartService partService, MApplication application) {
-        TaxonBase taxonBase = input.getTaxon();
+        TaxonBase<?> taxonBase = input.getTaxon();
+        TaxonNode node = input.getTaxonNode();
         if(taxonBase==null){
             return;
         }
+        boolean hasPermission = false;
+//        if (node != null){
+            hasPermission = CdmStore.currentAuthentiationHasPermission(node, RequiredPermissions.TAXON_EDIT);
+//        }
+//        if (!hasPermission){
+//            MessagingUtils.warningDialog(Messages.EditorUtil_MISSING_PERMISSION, TaxonEditorInputE4.class, Messages.EditorUtil_MISSING_PERMISSION_MESSAGE);
+//            return;
+//        }
         if (taxonBase.isOrphaned()) {
             if(taxonBase.isInstanceOf(Synonym.class)){
                 MessagingUtils.warningDialog(Messages.EditorUtil_ORPHAN_ACCEPTED_TAXON, TaxonEditorInputE4.class, Messages.EditorUtil_ORPHAN_ACCEPTED_TAXON_MESSAGE);
@@ -120,31 +226,91 @@ public class EditorUtil extends AbstractUtility {
         }
 
         Collection<MPart> parts = partService.getParts();
-        MPart part = null;
         //check if part is already opened
-        for (MPart mPart : parts) {
-            if(mPart.getObject() instanceof TaxonNameEditorE4
-                    && ((TaxonNameEditorE4) mPart.getObject()).getTaxon()!=null
-                    && ((TaxonNameEditorE4) mPart.getObject()).getTaxon().equals(input.getTaxon())){
-                part = mPart;
+        for (MPart part : parts) {
+               if(part.getObject() instanceof TaxonNameEditorE4
+                    && ((TaxonNameEditorE4) part.getObject()).getTaxon()!=null
+                    && ((TaxonNameEditorE4) part.getObject()).getTaxon().getUuid().equals(input.getTaxon().getUuid())){
+                   if (part.isDirty()){
+                       forceUserSaveE4Editor(((TaxonNameEditorE4) part.getObject()), getShell());
+                   }
+                partService.hidePart(part);
                 break;
             }
         }
-        if(part==null){
-            part = partService.createPart(NAME_EDITOR_ID);
+        MPart part = showPart(NAME_EDITOR_ID, modelService, partService, application);
 
-            MPartStack editorAreaPartStack = WorkbenchUtility.getEditorAreaPartStack(application, modelService);
-            if(editorAreaPartStack!=null){
-                editorAreaPartStack.getChildren().add(part);
-            }
-            part = partService.showPart(part, PartState.ACTIVATE);
+        TaxonNameEditorE4 editor = (TaxonNameEditorE4) part.getObject();
+//        editor.setDisabled();
+        editor.init(input);
+//        editor.setEnabled();
+        editor.setFocus();
+        if (factsVisible){
+            showFacts(modelService, partService);
+            factsVisible = false;
+        }
 
-            TaxonNameEditorE4 editor = (TaxonNameEditorE4) part.getObject();
-            editor.init(input);
+        if (mediaVisible){
+            showMedia(modelService, partService);
+            mediaVisible = false;
         }
-        else{
-            part = partService.showPart(part, PartState.ACTIVATE);
+    }
+
+    public static void showMedia(EModelService modelService, EPartService partService) {
+        String partIdFactualData = AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_MEDIA_E4_MEDIAVIEWPARTE4;
+        showPart(partIdFactualData, modelService, partService);
+
+    }
+
+    public static void showFacts(EModelService modelService, EPartService partService) {
+        String partIdFactualData = AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_DESCRIPTIVE_E4_FACTUALDATAPARTE4;
+        showPart(partIdFactualData, modelService, partService);
+
+    }
+
+    public static Collection<MPart> checkForChanges(UUID taxonUUID, EPartService partService ){
+        Collection<MPart> parts = partService.getParts();
+        Collection<MPart> dirtyParts = new HashSet();
+        //check if part is already opened
+        boolean isDirty = false;
+        for (MPart part : parts) {
+            if(part.getObject() instanceof TaxonNameEditorE4
+                    && ((TaxonNameEditorE4) part.getObject()).getTaxon()!=null
+                    && ((TaxonNameEditorE4) part.getObject()).getTaxon().getUuid().equals(taxonUUID)){
+                if (part.isDirty()){
+                    dirtyParts.add(part);
+                }
+
+                break;
+            }else if (taxonUUID == null){
+                if (part.isDirty()){
+                    dirtyParts.add(part);
+                }
+            }
+        }
+        return dirtyParts;
+    }
+
+    public static Collection<IE4SavablePart> checkForTaxonChanges(UUID taxonUUID, EPartService partService ){
+        Collection<MPart> parts = partService.getParts();
+        Collection<IE4SavablePart> dirtyParts = new HashSet<>();
+        //check if part is already opened
+        for (MPart part : parts) {
+            if(part.getObject() instanceof TaxonNameEditorE4
+                    && ((TaxonNameEditorE4) part.getObject()).getTaxon()!=null
+                    && ((TaxonNameEditorE4) part.getObject()).getTaxon().getUuid().equals(taxonUUID)){
+                if (part.isDirty()){
+                    dirtyParts.add((IE4SavablePart) part);
+                }
+
+                break;
+            }else if (taxonUUID == null){
+                if (part.isDirty() && (part.getObject() instanceof TaxonNameEditorE4 || (part.getObject() instanceof BulkEditorE4 && ((BulkEditorE4)part.getObject()).getEditorInput() instanceof TaxonEditorInput))){
+                    dirtyParts.add((IE4SavablePart) part.getObject());
+                }
+            }
         }
+        return dirtyParts;
     }
 
        /**
@@ -165,29 +331,6 @@ public class EditorUtil extends AbstractUtility {
         editor.init(input);
        }
 
-       /**
-        * <p>
-        * setSaving
-        * </p>
-        *
-        * @param isSaving
-        *            a boolean.
-        */
-       public static void setSaving(boolean isSaving) {
-               EditorUtil.isSaving = isSaving;
-       }
-
-       /**
-        * <p>
-        * isSaving
-        * </p>
-        *
-        * @return a boolean.
-        */
-       public static boolean isSaving() {
-               return isSaving;
-       }
-
        /**
         * <p>
         * getUndoContext
@@ -200,7 +343,23 @@ public class EditorUtil extends AbstractUtility {
                return IOperationHistory.GLOBAL_UNDO_CONTEXT;
        }
 
-       /**
+       public static boolean isFactsVisible() {
+        return factsVisible;
+    }
+
+    public static void setFactsVisible(boolean factsVisible) {
+        EditorUtil.factsVisible = factsVisible;
+    }
+
+    public static boolean isMediaVisible() {
+        return mediaVisible;
+    }
+
+    public static void setMediaVisible(boolean mediaVisible) {
+        EditorUtil.mediaVisible = mediaVisible;
+    }
+
+    /**
         * <p>
         * forceUserSave
         * </p>
@@ -227,7 +386,7 @@ public class EditorUtil extends AbstractUtility {
                return true;
        }
 
-       public static boolean forceUserSaveE4Editor(TaxonNameEditorE4 editor, Shell shell) {
+       public static boolean forceUserSaveE4Editor(IE4SavablePart editor, Shell shell) {
            if (editor.isDirty()) {
 
                boolean doSave = MessageDialog
@@ -277,21 +436,22 @@ public class EditorUtil extends AbstractUtility {
         * @return either a FieldUnit or a the topmost DerivedUnit (which can be itself)
         */
        public static SpecimenOrObservationBase<?> getTopMostDerivate(SpecimenOrObservationBase<?> specimen){
+           if(specimen==null){
+               return null;
+           }
            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);
+           else if(specimen.isInstanceOf(DerivedUnit.class)){
+               DerivedUnit derivedUnit = HibernateProxyHelper.deproxy(specimen, DerivedUnit.class);
+               if(derivedUnit.getOriginals()!=null
+                       && !(derivedUnit.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;
            }
+           return specimen;
        }
 
     /**
@@ -309,18 +469,86 @@ public class EditorUtil extends AbstractUtility {
         return null;
     }
 
-    public static void closeObsoleteEditor(TaxonNode taxonNode, EPartService partService){
-        String treeIndex = taxonNode.treeIndex();
+    public static void closeObsoleteEditor(TaxonNodeDto taxonNode, EPartService partService){
+        String treeIndex = taxonNode.getTreeIndex();
+        Collection<MPart> parts = partService.getParts();
+        for (MPart part : parts) {
+            Object object = part.getObject();
+            if(object instanceof TaxonNameEditorE4){
+                TaxonNameEditorE4 taxonEditor = (TaxonNameEditorE4)object;
+                TaxonNode node = taxonEditor.getEditorInput().getTaxonNode();
+                if (node.treeIndex()!= null){
+                       if(node.treeIndex().startsWith(treeIndex)){
+                           if (part.isDirty()){
+                               forceUserSaveE4Editor(taxonEditor, getShell());
+                           }
+                           partService.hidePart(part);
+                       }
+                }else{
+                       logger.debug("The taxonnode of taxon " + node.getTaxon().getTitleCache() + " uuid: " + node.getUuid() + " has no treeindex");;
+                }
+            }
+        }
+    }
+
+    public static void closeObsoleteEditorWithChildren(TaxonNodeDto taxonNode, EPartService partService){
+        String treeIndex = taxonNode.getTreeIndex();
         Collection<MPart> parts = partService.getParts();
         for (MPart part : parts) {
             Object object = part.getObject();
             if(object instanceof TaxonNameEditorE4){
                 TaxonNameEditorE4 taxonEditor = (TaxonNameEditorE4)object;
                 TaxonNode node = taxonEditor.getEditorInput().getTaxonNode();
-                if(node.treeIndex().startsWith(treeIndex)){
-                    partService.hidePart(part, true);
+                if (node.treeIndex()!= null){
+                    if(node.treeIndex().startsWith(treeIndex)){
+                        if (part.isDirty()){
+                            forceUserSaveE4Editor(taxonEditor, getShell());
+                        }
+                        partService.hidePart(part);
+                    }
+                }else{
+                    logger.debug("The taxonnode of taxon " + node.getTaxon().getTitleCache() + " uuid: " + node.getUuid() + " has no treeindex");;
                 }
             }
         }
     }
-}
+
+    public static void closeObsoleteDescriptiveDatasetEditor(UUID datasetUuid, EPartService partService){
+
+        Collection<MPart> parts = partService.getParts();
+        for (MPart part : parts) {
+            Object object = part.getObject();
+            if(object instanceof DescriptiveDataSetEditor){
+                DescriptiveDataSetEditor editor = (DescriptiveDataSetEditor)object;
+                DescriptiveDataSet descDataSet = editor.getDescriptiveDataSet();
+                if(descDataSet.getUuid().equals(datasetUuid)){
+                    if (part.isDirty()){
+                        forceUserSaveE4Editor(editor, getShell());
+                    }
+                    partService.hidePart(part);
+                }
+
+            }
+        }
+    }
+
+    public static void closePart(String partID, EPartService partService){
+
+        Collection<MPart> parts = partService.getParts();
+        for (MPart part : parts) {
+            String elementId = part.getElementId();
+            if (elementId.equals(partID)){
+                partService.hidePart(part);
+            }
+        }
+    }
+
+    public static void updateEditor(TaxonNode taxonNode, TaxonNameEditorE4 editor){
+        String treeIndex = taxonNode.treeIndex();
+        TaxonNode node = editor.getEditorInput().getTaxonNode();
+        if(node.treeIndex().equals(treeIndex)){
+               TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstance(node.getUuid());
+               editor.init(input);
+        }
+    }
+}
\ No newline at end of file