Merge branch 'hotfix/5.7.1'
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / EditorUtil.java
index 044ad895b71b97d8465a9dd5d25b2dc03c9edda3..63228443bde124056407a3c0ee53cf601e5e0690 100644 (file)
@@ -10,6 +10,7 @@
 package eu.etaxonomy.taxeditor.editor;
 
 import java.util.Collection;
+import java.util.HashSet;
 import java.util.UUID;
 
 import org.eclipse.core.commands.ExecutionEvent;
@@ -37,19 +38,23 @@ 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.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.editor.workingSet.WorkingSetEditor;
-import eu.etaxonomy.taxeditor.editor.workingSet.matrix.CharacterMatrixPart;
 import eu.etaxonomy.taxeditor.model.AbstractUtility;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
+import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
 
 /**
  * Utility for the editor package
@@ -61,22 +66,50 @@ import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
 public class EditorUtil extends AbstractUtility {
 
     private static final String NAME_EDITOR_ID = "eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4";
-    private static boolean isSaving = false;
 
-    public static void openWorkingSetEditor(UUID workingSetUuid, EModelService modelService, EPartService partService, MApplication application){
-        String partId = AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_WORKINGSET_WORKINGSETEDITOR;
+    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);
-        WorkingSetEditor editor = (WorkingSetEditor) part.getObject();
-        editor.init(workingSetUuid);
+        DescriptiveDataSetEditor editor = (DescriptiveDataSetEditor) part.getObject();
+        editor.init(descriptiveDataSetUuid);
     }
 
-    public static void openCharacterMatrix(UUID workingSetUuid, EModelService modelService, EPartService partService, MApplication application){
-        String partId = AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_WORKINGSET_MATRIX_CHARACTERMATRIXPART;
+    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(workingSetUuid, true);
+        editor.init(descriptiveDataSetUuid, true);
     }
 
+    public static void openDistributionEditor(UUID parentTaxonUuid, EModelService modelService, EPartService partService, MApplication application){
+        Collection<MPart> parts = partService.getParts();
+        String partId = AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_CHECKLIST_E4_DISTRIBUTIONEDITORPART;
+        String partIdFactualData = AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_DESCRIPTIVE_E4_FACTUALDATAPARTE4;
+        String partIdMedia = AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_MEDIA_E4_MEDIAVIEWPARTE4;
+        closePart(partIdMedia, partService);
+        closePart(partIdFactualData, partService);
+        MPart part = showPart(partId, modelService, partService, application);
+        DistributionEditorPart editor = (DistributionEditorPart) part.getObject();
+        editor.init(parentTaxonUuid);
+    }
+
+
     public static void openSpecimenEditor(DerivateViewEditorInput input, EModelService modelService, EPartService partService, MApplication application){
         String partId = AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_DERIVATE_DERIVATEVIEW;
         MPart part = showPart(partId, modelService, partService, application);
@@ -85,13 +118,21 @@ public class EditorUtil extends AbstractUtility {
     }
 
     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);
     }
 
-    private static MPart showPart(String partId, EModelService modelService, EPartService partService, MApplication application){
+    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);
@@ -103,6 +144,18 @@ public class EditorUtil extends AbstractUtility {
         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) {
            TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstance(taxonNodeUuid);
            openNameEditor_internal(input, modelService, partService, application);
@@ -144,13 +197,67 @@ public class EditorUtil extends AbstractUtility {
                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;
             }
         }
         MPart part = showPart(NAME_EDITOR_ID, modelService, partService, application);
+
         TaxonNameEditorE4 editor = (TaxonNameEditorE4) part.getObject();
         editor.init(input);
+        String partIdFactualData = AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_DESCRIPTIVE_E4_FACTUALDATAPARTE4;
+        String partIdMedia = AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_MEDIA_E4_MEDIAVIEWPARTE4;
+        showPart(partIdMedia, modelService, partService);
+        showPart(partIdFactualData, modelService, partService);
+        editor.setFocus();
+    }
+
+    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);
+                }
+            }
+        }
+        return dirtyParts;
     }
 
        /**
@@ -171,29 +278,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
@@ -326,6 +410,9 @@ public class EditorUtil extends AbstractUtility {
                 TaxonNode node = taxonEditor.getEditorInput().getTaxonNode();
                 if (node.treeIndex()!= null){
                        if(node.treeIndex().startsWith(treeIndex)){
+                           if (part.isDirty()){
+                               forceUserSaveE4Editor(taxonEditor, getShell());
+                           }
                            partService.hidePart(part);
                        }
                 }else{
@@ -335,6 +422,18 @@ public class EditorUtil extends AbstractUtility {
         }
     }
 
+
+    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();
@@ -345,4 +444,5 @@ public class EditorUtil extends AbstractUtility {
 
         }
     }
+
 }