Update operation for move taxon nodes
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / TreeNodeDropAdapterAssistant.java
index d78128c6e3d8c3d0d3b692c3ddd20abfc648694b..842f9a8c6fb0b43ff0cf68a2cc98fac3f1957b10 100644 (file)
@@ -14,8 +14,10 @@ import java.util.EnumSet;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Set;
+import java.util.UUID;
 
 import org.apache.log4j.Logger;
+import org.eclipse.core.commands.operations.AbstractOperation;
 import org.eclipse.core.commands.operations.IUndoContext;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
@@ -28,14 +30,18 @@ import org.eclipse.swt.dnd.TransferData;
 import org.eclipse.ui.navigator.CommonDropAdapter;
 import org.eclipse.ui.navigator.CommonDropAdapterAssistant;
 
+import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
 import eu.etaxonomy.cdm.model.common.CdmBase;
+import eu.etaxonomy.cdm.model.taxon.Classification;
 import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
 import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
 import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
 import eu.etaxonomy.taxeditor.navigation.navigator.operation.MoveTaxonOperation;
+import eu.etaxonomy.taxeditor.navigation.navigator.operation.RemotingMoveTaxonOperation;
 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
+import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
 import eu.etaxonomy.taxeditor.store.CdmStore;
 
 /**
@@ -62,11 +68,22 @@ public class TreeNodeDropAdapterAssistant extends CommonDropAdapterAssistant imp
        public IStatus handleDrop(CommonDropAdapter dropAdapter,
                        DropTargetEvent dropTargetEvent, Object target) {
 
+
                if (target instanceof ITaxonTreeNode) {
                        Set<TaxonNode> taxonNodes = getSelectedTaxa();
                        ITaxonTreeNode targetTreeNode = (ITaxonTreeNode) target;
-                       if(taxonNodes != null) {
-                return moveTaxon(taxonNodes, targetTreeNode);
+                       if (targetTreeNode instanceof Classification){
+                               targetTreeNode = ((Classification)targetTreeNode).getRootNode();
+                               targetTreeNode = HibernateProxyHelper.deproxy(targetTreeNode, TaxonNode.class);
+                       }
+                       //if(taxonNodes != null) {
+                               if (taxonNodes.size() >= 1){
+                                       return moveTaxon(taxonNodes, targetTreeNode);
+                               /*} else{
+                                       if( MessageDialog.openConfirm(null, "Moving taxon", "The operation move accepted taxon to other parent is available only for a single taxon.")){
+                                               return null;
+                                       }
+                               }*/
             }
                }
 
@@ -158,47 +175,86 @@ public class TreeNodeDropAdapterAssistant extends CommonDropAdapterAssistant imp
                if(targetITaxonTreeNode instanceof TaxonNode){
 
                        TaxonNode targetTaxonNode = (TaxonNode) targetITaxonTreeNode;
-
-//                     for(TaxonNode taxonNode : taxonNodes){
-//                             if (taxonNode.equals(targetTaxonNode)) {
-//                                     return Status.CANCEL_STATUS;
-//                             }
-//                     }
-
-                       // Make sure parent taxon does not have unsaved changes
+               // Make sure parent taxon does not have unsaved changes
                        if (NavigationUtil.isDirty(targetTaxonNode)){
                                MessageDialog.openWarning(NavigationUtil.getShell(), "Unsaved Parent Taxon", "There are unsaved " +
                                "changes in the parent taxon. Pleas save first.");
                                return Status.CANCEL_STATUS;
                        }
 
+               }
+               Iterator<TaxonNode> taxIterator = taxonNodes.iterator();
+        Set<UUID> uuids = new HashSet<UUID>();
+        TaxonNode node = null;
+        while(taxIterator.hasNext()){
+            node = taxIterator.next();
+            uuids.add(node.getUuid());
+        }
+               if (!PreferencesUtil.getSortNodesNaturally()){
+                       IUndoContext workspaceUndoContext = NavigationUtil.getWorkbenchUndoContext();
+                       if (workspaceUndoContext == null) {
+                               logger.error("Workspace undo context is null. DND operation cancelled");
+                               return Status.CANCEL_STATUS;
+                       }
 
-                       // Make sure parentTaxon is not the drop target
-//                     if (!childTaxonNode.isTopmostNode() && childTaxonNode.getParent().equals(targetTaxonNode)){
-//                             return Status.CANCEL_STATUS;
-//                     }
+                       if(CdmStore.getCurrentSessionManager().isRemoting()) {
+                           AbstractOperation operation = new RemotingMoveTaxonOperation(taxonNavigator, false, uuids, (TaxonNode)targetITaxonTreeNode, true);
+                           NavigationUtil.executeOperation(operation, null);
+                       } else {
+                           AbstractPostOperation operation = new MoveTaxonOperation
+                                   ("Move Taxon", workspaceUndoContext, uuids, targetITaxonTreeNode, this, taxonNavigator, true);
+                           NavigationUtil.executeOperation(operation);
+            }
 
-                       // Make sure taxon is not being dropped onto itself
-//                     if (childTaxonNode.equals(targetTaxonNode)) {
-//                             return Status.CANCEL_STATUS;
-//                     }
+                       logger.info("Moved taxa to new parent " + targetITaxonTreeNode);
+                       return Status.OK_STATUS;
+               }else{
+                       String[] buttonLables = {"Parent", "Predecessor", "Cancel"};
+                       MessageDialog dialog = new MessageDialog(null, "Target node", null, "Do you want to use the target node as parent or do you want to move the taxon below the target.", MessageDialog.QUESTION_WITH_CANCEL, buttonLables, 0);
+                       dialog.open();
+                       int returnCode = dialog.getReturnCode();
+                       if (returnCode == 0){
+                               IUndoContext workspaceUndoContext = NavigationUtil.getWorkbenchUndoContext();
+                               if (workspaceUndoContext == null) {
+                                       logger.error("Workspace undo context is null. DND operation cancelled");
+                                       return Status.CANCEL_STATUS;
+                               }
 
+                               if(CdmStore.getCurrentSessionManager().isRemoting()) {
+                                   AbstractOperation operation = new RemotingMoveTaxonOperation(taxonNavigator, false, uuids, (TaxonNode)targetITaxonTreeNode, true);
+                                   NavigationUtil.executeOperation(operation, null);
+                               } else {
+                                   AbstractPostOperation operation = new MoveTaxonOperation
+                                           ("Move Taxon", workspaceUndoContext, uuids, targetITaxonTreeNode, this, taxonNavigator, true);
+                                   NavigationUtil.executeOperation(operation);
+                               }
 
+                               logger.info("Moved taxa to new parent " + targetITaxonTreeNode);
+                               return Status.OK_STATUS;
+                       }else if (returnCode == 1){
+                               IUndoContext workspaceUndoContext = NavigationUtil.getWorkbenchUndoContext();
+                               if (workspaceUndoContext == null) {
+                                       logger.error("Workspace undo context is null. DND operation cancelled");
+                                       return Status.CANCEL_STATUS;
+                               }
+                               TaxonNode targetNode = (TaxonNode) targetITaxonTreeNode;
+
+                               if(CdmStore.getCurrentSessionManager().isRemoting()) {
+                                   AbstractOperation operation = new RemotingMoveTaxonOperation(taxonNavigator, false, uuids, targetNode.getParent(), true);
+                                   NavigationUtil.executeOperation(operation, null);
+                               } else {
+                                   AbstractPostOperation operation = new MoveTaxonOperation
+                                           ("Move Taxon", workspaceUndoContext, uuids, targetNode.getParent(), this, taxonNavigator, false);
+                                   NavigationUtil.executeOperation(operation);
+                               }
+                               logger.info("Moved taxa to new parent " + targetITaxonTreeNode);
+                               return Status.OK_STATUS;
+                       } else{
+                               return Status.CANCEL_STATUS;
+                       }
 
-               }
 
-               IUndoContext workspaceUndoContext = NavigationUtil.getWorkbenchUndoContext();
-               if (workspaceUndoContext == null) {
-                       logger.error("Workspace undo context is null. DND operation cancelled");
-                       return Status.CANCEL_STATUS;
                }
-
-               AbstractPostOperation operation = new MoveTaxonOperation
-                               ("Move Taxon", workspaceUndoContext, taxonNodes, targetITaxonTreeNode, this, taxonNavigator);
-               NavigationUtil.executeOperation(operation);
-
-               logger.info("Moved taxa to new parent " + targetITaxonTreeNode);
-               return Status.OK_STATUS;
        }
 
        /* (non-Javadoc)