ref #7575 Merge aggregated and exact value cell editor
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / EditorUtil.java
index b97f2ab04d359a20557779af2121566de4374954..894a8b9e9d97251d45a0fbca09f0b6f093bdeb62 100644 (file)
@@ -27,15 +27,18 @@ 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.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.cdm.persistence.dto.TaxonNodeDto;
+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;
@@ -60,30 +63,52 @@ 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);
-//         }
-//     }
+    public static void openDescriptiveDataSetEditor(UUID descriptiveDataSetUuid, EModelService modelService, EPartService partService, MApplication application){
+        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){
+        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 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 void openTaxonNodeE4(UUID taxonNodeUuid, EModelService modelService, EPartService partService, MApplication application) {
@@ -127,31 +152,17 @@ 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 = partService.createPart(NAME_EDITOR_ID);
-
-        MPartStack editorAreaPartStack = WorkbenchUtility.getEditorAreaPartStack(application, modelService);
-        if(editorAreaPartStack!=null){
-            editorAreaPartStack.getChildren().add(part);
-        }
-        part = partService.showPart(part, PartState.ACTIVATE);
-
+        MPart part = showPart(NAME_EDITOR_ID, modelService, partService, application);
         TaxonNameEditorE4 editor = (TaxonNameEditorE4) part.getObject();
         editor.init(input);
     }
-    public static void openRightsEditor(CdmAuthorityEditorInput input, EModelService modelService, EPartService partService, MApplication application){
-        MPart part = partService.createPart("eu.etaxonomy.taxeditor.editor.group.authority.e4.CdmAuthorityEditorE4");
-        MPartStack editorAreaPartStack = WorkbenchUtility.getEditorAreaPartStack(application, modelService);
-        if(editorAreaPartStack!=null){
-            editorAreaPartStack.getChildren().add(part);
-        }
-        part = partService.showPart(part, PartState.ACTIVATE);
-        CdmAuthorityEditorE4 authorityView = (CdmAuthorityEditorE4) part.getObject();
-        authorityView.init(input);
-    }
 
        /**
         * An uninitialized taxon is one that hasn't been saved yet. As such, it
@@ -283,21 +294,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;
        }
 
     /**
@@ -315,8 +327,8 @@ 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();
@@ -325,7 +337,10 @@ public class EditorUtil extends AbstractUtility {
                 TaxonNode node = taxonEditor.getEditorInput().getTaxonNode();
                 if (node.treeIndex()!= null){
                        if(node.treeIndex().startsWith(treeIndex)){
-                           partService.hidePart(part, true);
+                           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");;
@@ -333,20 +348,15 @@ public class EditorUtil extends AbstractUtility {
             }
         }
     }
-    
-    public static void updateEditor(TaxonNode taxonNode, EPartService partService, MApplication application){
+
+    public static void updateEditor(TaxonNode taxonNode, TaxonNameEditorE4 editor){
         String treeIndex = taxonNode.treeIndex();
-        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)){
-                       TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstanceFromTaxonBase(node.getTaxon().getUuid());
-                       taxonEditor.init(input);
-                }
-            }
+        TaxonNode node = editor.getEditorInput().getTaxonNode();
+        if(node.treeIndex().equals(treeIndex)){
+               TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstance(node.getUuid());
+               editor.init(input);
+
+
         }
     }
 }