ref #7010 Adapt edit polytomous key handler
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / e4 / handler / RemotingChangeAcceptedTaxonToSynonymHandlerE4.java
index c692d4b0e235ef659f078c642fc624153c0aa258..d15d1ce4d57e6cc0b7d224dcfd30512e9a7d2747 100644 (file)
@@ -4,7 +4,10 @@
 package eu.etaxonomy.taxeditor.navigation.navigator.e4.handler;
 
 import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Iterator;
 import java.util.List;
+import java.util.Set;
 import java.util.UUID;
 
 import javax.inject.Named;
@@ -16,16 +19,20 @@ import org.eclipse.e4.core.di.annotations.CanExecute;
 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.jface.viewers.IStructuredSelection;
 import org.eclipse.jface.viewers.TreeSelection;
 import org.eclipse.swt.widgets.Shell;
 
-import eu.etaxonomy.cdm.api.conversation.ConversationHolderMock;
+import eu.etaxonomy.cdm.api.service.IClassificationService;
+import eu.etaxonomy.cdm.model.taxon.Classification;
 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
+import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
 import eu.etaxonomy.taxeditor.editor.EditorUtil;
 import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
 import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigatorLabels;
 import eu.etaxonomy.taxeditor.navigation.navigator.operation.RemotingChangeAcceptedTaxonToSynonymOperation;
 import eu.etaxonomy.taxeditor.operation.e4.RemotingCdmHandlerE4;
+import eu.etaxonomy.taxeditor.store.CdmStore;
 import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
 
 /**
@@ -37,23 +44,24 @@ import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
 public class RemotingChangeAcceptedTaxonToSynonymHandlerE4 extends RemotingCdmHandlerE4 {
 
 
-    private TaxonNode oldTaxonNode;
+    private Set<TaxonNodeDto> oldTaxonNodes = new HashSet();
+    private Classification classification;
 
     public RemotingChangeAcceptedTaxonToSynonymHandlerE4() {
         super(TaxonNavigatorLabels.CHANGE_ACCEPTED_TAXON_TO_SYNONYM_LABEL);
     }
 
     @Override
-    public IStatus allowOperations(TreeSelection selection,
+    public IStatus allowOperations(IStructuredSelection selection,
             Shell shell,
             MPart activePart,
             MHandledMenuItem menuItem) {
         // check that only a single taxon tree node has been selected
-        if(selection.size() > 1) {
-            return new Status(IStatus.ERROR,
-                    "unknown", //$NON-NLS-1$
-                    TaxonNavigatorLabels.SINGLE_TAXON_SELECTION_MESSAGE);
-        }
+//        if(selection.size() > 1) {
+//            return new Status(IStatus.ERROR,
+//                    "unknown", //$NON-NLS-1$
+//                    TaxonNavigatorLabels.SINGLE_TAXON_SELECTION_MESSAGE);
+//        }
 
         // check for no taxon tree node selected
         if(selection.size() == 0) {
@@ -63,56 +71,73 @@ public class RemotingChangeAcceptedTaxonToSynonymHandlerE4 extends RemotingCdmHa
         }
 
         // check that selected object is a taxon node
-        Object obj = selection.iterator().next();
-        if(obj instanceof TaxonNode && ((TaxonNode)obj).hasTaxon()) {
-            oldTaxonNode = (TaxonNode)obj;
-        } else {
-               if (obj instanceof TaxonNode && !((TaxonNode)obj).hasTaxon()){
-                       return new Status(IStatus.ERROR,
-                        "Operation not available for Classifications", //$NON-NLS-1$
-                        TaxonNavigatorLabels.SELECTED_OBJECT_NOT_TREE_NODE_MESSAGE);
-               }
-            return new Status(IStatus.ERROR,
-                    "unknown", //$NON-NLS-1$
-                    TaxonNavigatorLabels.SELECTED_OBJECT_NOT_TREE_NODE_MESSAGE);
-        }
-        // check that the source taxon node does not have children
-        if(oldTaxonNode.getCountChildren() > 0) {
-            return new Status(IStatus.ERROR,
-                    "unknown", //$NON-NLS-1$
-                    TaxonNavigatorLabels.SOURCE_TAXON_HAS_CHILDREN_MESSAGE);
-
+        Iterator it = selection.iterator();
+        Classification nextClassification;
+        oldTaxonNodes = new HashSet<>();
+        while(it.hasNext()){
+               Object obj = it.next();
+               if(obj instanceof TaxonNodeDto) {
+                   oldTaxonNodes.add((TaxonNodeDto)obj);
+                   nextClassification = CdmStore.getService(IClassificationService.class).find(((TaxonNodeDto)obj).getClassificationUUID());
+                   if (classification == null){
+                       classification = nextClassification;
+                   }else if (!classification.equals(nextClassification)){
+                       return new Status(IStatus.ERROR,
+                               "unknown", //$NON-NLS-1$
+                               TaxonNavigatorLabels.ACCEPTED_TAXA_NEED_TO_BE_FROM_SAME_CLASSIFICATION);
+                   }
+               } else {
+                       if (obj instanceof TaxonNode && !((TaxonNode)obj).hasTaxon()){
+                               return new Status(IStatus.ERROR,
+                               "Operation not available for Classifications", //$NON-NLS-1$
+                               TaxonNavigatorLabels.SELECTED_OBJECT_NOT_TREE_NODE_MESSAGE);
+                       }
+                   return new Status(IStatus.ERROR,
+                           "unknown", //$NON-NLS-1$
+                           TaxonNavigatorLabels.SELECTED_OBJECT_NOT_TREE_NODE_MESSAGE);
+               }
+               // check that the source taxon node does not have children
+               if(((TaxonNodeDto)obj).getTaxonomicChildrenCount() > 0) {
+                   return new Status(IStatus.ERROR,
+                           "unknown", //$NON-NLS-1$
+                           TaxonNavigatorLabels.SOURCE_TAXON_HAS_CHILDREN_MESSAGE);
+
+               }
+
+               // check if corresponding name editor is closed
+               EditorUtil.closeObsoleteEditor((TaxonNodeDto)obj, partService);
         }
-
-        // check if corresponding name editor is closed
-        EditorUtil.closeObsoleteEditor(oldTaxonNode, partService);
-
         return Status.OK_STATUS;
     }
 
     @Override
-    public AbstractOperation prepareOperation(TreeSelection selection,
+    public AbstractOperation prepareOperation(IStructuredSelection selection,
             Shell shell,
             MPart activePart,
             MHandledMenuItem menuItem) {
         List<UUID> excludeTaxa = new ArrayList<UUID>();
-        excludeTaxa.add(oldTaxonNode.getTaxon().getUuid());
+        for (TaxonNodeDto oldNode:oldTaxonNodes){
+               excludeTaxa.add(oldNode.getTaxonUuid());
+        }
         TaxonNode newAcceptedTaxonNode = TaxonNodeSelectionDialog.select(shell,
-                new ConversationHolderMock(),
+//                new ConversationHolderMock(),
                 Messages.RemotingChangeAcceptedTaxonToSynonymHandler_CHOOSE_TAXON,
                 excludeTaxa,
-                oldTaxonNode,
-                oldTaxonNode.getClassification());
+                null,
+                classification.getUuid());
 
         if (newAcceptedTaxonNode == null) {
             return null;
         }
-
+        Set<UUID> nodeUuids = new HashSet();
+        for (TaxonNodeDto node: oldTaxonNodes){
+               nodeUuids.add(node.getUuid());
+        }
         RemotingChangeAcceptedTaxonToSynonymOperation rcattso =
                 new RemotingChangeAcceptedTaxonToSynonymOperation(getTrigger(),
                         false,
-                        oldTaxonNode.getUuid(),
-                        newAcceptedTaxonNode.getUuid());
+                        nodeUuids,
+                        newAcceptedTaxonNode.getUuid(), partService, activePart, application);
 
         return rcattso;
     }
@@ -120,8 +145,7 @@ public class RemotingChangeAcceptedTaxonToSynonymHandlerE4 extends RemotingCdmHa
     @CanExecute
     private boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection, MHandledMenuItem menuItem){
         boolean canExecute = false;
-        canExecute = selection.getFirstElement() instanceof TaxonNode
-                && ((TaxonNode) selection.getFirstElement()).getTaxon()!=null;
+        canExecute = (selection.getFirstElement() instanceof TaxonNodeDto) && ((TaxonNodeDto)selection.getFirstElement()).getTaxonUuid() != null ;
         menuItem.setVisible(canExecute);
         return canExecute;
     }