ref #9340, #9734, #9668: further improvements for configurable sec handling during...
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / name / e4 / handler / MoveSynonymToAnotherAcceptedTaxonHandlerE4.java
index cc3d70b4a7a97e60fdaea0300d9f40916fa2a0da..30eff79eba48d4263fdb3198edbbc2f8169e4c67 100644 (file)
@@ -1,24 +1,28 @@
 package eu.etaxonomy.taxeditor.editor.name.e4.handler;
 
-import java.util.ArrayList;
-import java.util.List;
+import java.util.HashSet;
+import java.util.Set;
 import java.util.UUID;
 
+import javax.inject.Inject;
 import javax.inject.Named;
 
 import org.apache.log4j.Logger;
 import org.eclipse.e4.core.di.annotations.CanExecute;
 import org.eclipse.e4.core.di.annotations.Execute;
+import org.eclipse.e4.ui.di.UISynchronize;
+import org.eclipse.e4.ui.model.application.MApplication;
 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
 import org.eclipse.e4.ui.services.IServiceConstants;
+import org.eclipse.e4.ui.workbench.modeling.EModelService;
+import org.eclipse.e4.ui.workbench.modeling.EPartService;
 import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.swt.widgets.Shell;
-import org.eclipse.ui.IEditorInput;
 
-import eu.etaxonomy.cdm.model.common.CdmBase;
+import eu.etaxonomy.cdm.model.metadata.SecReferenceHandlingEnum;
+import eu.etaxonomy.cdm.model.reference.Reference;
 import eu.etaxonomy.cdm.model.taxon.Synonym;
-import eu.etaxonomy.cdm.model.taxon.Taxon;
 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
 import eu.etaxonomy.taxeditor.editor.EditorUtil;
 import eu.etaxonomy.taxeditor.editor.e4.TaxonEditorInputE4;
@@ -29,27 +33,43 @@ import eu.etaxonomy.taxeditor.editor.name.handler.NameEditorMenuPropertyTester;
 import eu.etaxonomy.taxeditor.model.AbstractUtility;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
+import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
+import eu.etaxonomy.taxeditor.ui.dialog.selection.ReferenceSelectionDialog;
 import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
 
 public class MoveSynonymToAnotherAcceptedTaxonHandlerE4 implements IPostOperationEnabled{
        private static final Logger logger = Logger
        .getLogger(MoveSynonymToAnotherAcceptedTaxonHandlerE4.class);
 
+       private TaxonNameEditorE4 editor;
+
+    private TaxonNode newParentNode;
+
+    private EPartService partService;
+
+    @Inject
+    private EModelService modelService;
+
+    @Inject
+    private MApplication application;
+
+    @Inject
+    private MPart activePart;
+
        @Execute
     public void execute(@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
             @Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
-            @Named(IServiceConstants.ACTIVE_SHELL)Shell shell) {
+            @Named(IServiceConstants.ACTIVE_SHELL)Shell shell, EPartService partService,
+            UISynchronize sync) {
 
-        TaxonNameEditorE4 editor = (TaxonNameEditorE4) activePart.getObject();
+           this.activePart = activePart;
+           this.partService = partService;
 
-               IEditorInput input = editor.getEditorInput();
+        editor = (TaxonNameEditorE4) activePart.getObject();
 
-               if (!EditorUtil.forceUserSaveE4Editor(editor, shell)) {
-                       return ;
-               }
+        TaxonEditorInputE4 input = editor.getEditorInput();
 
-               if (!(input instanceof TaxonEditorInputE4)) {
-                       logger.error("Editor input is not TaxonEditorInput"); //$NON-NLS-1$
+               if (!EditorUtil.forceUserSaveE4Editor(editor, shell)) {
                        return ;
                }
 
@@ -60,18 +80,46 @@ public class MoveSynonymToAnotherAcceptedTaxonHandlerE4 implements IPostOperatio
                }
 
                Synonym synonym = (Synonym) selection.getFirstElement();
-               Taxon oldParent = ((TaxonEditorInputE4)input).getTaxon();
-               List<UUID> excludeTaxa = new ArrayList<UUID>();
+               TaxonNode oldParent = input.getTaxonNode();
+               Set<UUID> excludeTaxa = new HashSet<>();
                excludeTaxa.add(oldParent.getUuid());
 
 
-               TaxonNode newParentNode = TaxonNodeSelectionDialog.select(shell, editor.getConversationHolder(), Messages.MoveSynonymToAnotherAcceptedTaxonHandler_SELECT_ACC_TAXON, excludeTaxa,  ((TaxonEditorInputE4)input).getTaxonNode(), ((TaxonEditorInputE4)input).getTaxonNode().getClassification());
-
+               newParentNode = TaxonNodeSelectionDialog.select(shell, Messages.MoveSynonymToAnotherAcceptedTaxonHandler_SELECT_ACC_TAXON, excludeTaxa,  input.getTaxonNode(), input.getTaxonNode().getClassification().getUuid());
+               SecReferenceHandlingEnum secHandling = PreferencesUtil.getSecReferenceHandlingPreference();
+        UUID newSecUuid = null;
+
+        Reference synSecRef = synonym.getSec();
+        Reference parentSecRef = newParentNode.getTaxon() != null? newParentNode.getTaxon().getSec():null;
+        if ((synSecRef != parentSecRef && secHandling.equals(SecReferenceHandlingEnum.KeepOrWarn) )|| secHandling.equals(SecReferenceHandlingEnum.KeepOrSelect)){
+
+            if ((parentSecRef != synSecRef && secHandling.equals(SecReferenceHandlingEnum.KeepOrWarn) )|| secHandling.equals(SecReferenceHandlingEnum.KeepOrSelect)){
+                int result = MessagingUtils.confirmDialog(Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Handling_title, Messages.MoveSynonymToAcceptedHandler_Select_Sec_Reference_Handling_message,
+                        new String[]{Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Keep, Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Parent, Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Select});
+                if (result == 2){
+                    Reference sec = ReferenceSelectionDialog.select(shell, null);
+                    newSecUuid = sec != null? sec.getUuid(): null;
+                }else if (result == 1){
+                    newSecUuid = parentSecRef != null? parentSecRef.getUuid(): null;
+                }else if (result == 0){
+                    newSecUuid = synSecRef != null? synSecRef.getUuid(): null;
+                }else{
+                    return ;
+                }
+
+            }
+
+        }
+        boolean homotypicGroupPublish = true;
+//        for (synonym.getName().getHomotypicalGroup().getTypifiedNames())
+               if (synonym.isPublish() != newParentNode.getTaxon().isPublish()){
+            MessagingUtils.warningDialog("Publish flag", this,Messages.ChangeSynonymToAcceptedHandler_Different_Publish_Flag);
+        }
                if(newParentNode!=null){
                   MoveSynonymToAnotherAcceptedTaxonOperationE4 operation = new MoveSynonymToAnotherAcceptedTaxonOperationE4(Messages.MoveSynonymToAnotherAcceptedTaxonHandler_CHANGE_ACC_TAXON, EditorUtil.getUndoContext(),
-                           synonym.getUuid(), newParentNode.getTaxon(), editor, editor);
+                           synonym.getUuid(), newParentNode.getTaxon(),newSecUuid, this, editor);
 
-                   AbstractUtility.executeOperation(operation);
+                   AbstractUtility.executeOperation(operation, sync);
                }
        }
 
@@ -79,23 +127,27 @@ public class MoveSynonymToAnotherAcceptedTaxonHandlerE4 implements IPostOperatio
     public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
             MHandledMenuItem menuItem){
         boolean canExecute = false;
-        Object selectedElement = selection.getFirstElement();
-        canExecute =
-                NameEditorMenuPropertyTester.isSynonym(selectedElement)
-                        && NameEditorMenuPropertyTester.isNotHomotypicSynonymOfAcceptedTaxon(selectedElement);
-
+        if(selection.size()==1){
+            Object selectedElement = selection.getFirstElement();
+            canExecute =
+                    NameEditorMenuPropertyTester.isSynonym(selectedElement)
+                    && NameEditorMenuPropertyTester.isNotHomotypicSynonymOfAcceptedTaxon(selectedElement);
+        }
         menuItem.setVisible(canExecute);
         return canExecute;
     }
 
        @Override
-       public boolean postOperation(CdmBase objectAffectedByOperation) {
+       public boolean postOperation(Object objectAffectedByOperation) {
+           editor.redraw();
                return false;
        }
 
        @Override
        public boolean onComplete() {
-               return false;
+           partService.hidePart(activePart);
+           EditorUtil.openTaxonNodeE4(newParentNode.getUuid(), modelService, partService, application);
+               return true;
        }
 
 }