make the order of the taxon nodes editable
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / TreeNodeDropAdapterAssistant.java
index fbf20ea45cfa2e92d287b62520ef53496af2493f..86eb9506f926fd1267fbb4f1a23972d0ff3d4663 100644 (file)
@@ -19,12 +19,15 @@ import org.apache.log4j.Logger;
 import org.eclipse.core.commands.operations.IUndoContext;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
+import org.eclipse.jface.dialogs.IconAndMessageDialog;
 import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.util.LocalSelectionTransfer;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.TreeSelection;
 import org.eclipse.swt.dnd.DropTargetEvent;
 import org.eclipse.swt.dnd.TransferData;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.widgets.Shell;
 import org.eclipse.ui.handlers.HandlerUtil;
 import org.eclipse.ui.navigator.CommonDropAdapter;
 import org.eclipse.ui.navigator.CommonDropAdapterAssistant;
@@ -39,6 +42,7 @@ import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
 import eu.etaxonomy.taxeditor.navigation.navigator.operation.MoveTaxonOperation;
 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
+import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
 import eu.etaxonomy.taxeditor.store.CdmStore;
 
 /**
@@ -65,6 +69,7 @@ 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;
@@ -82,7 +87,7 @@ public class TreeNodeDropAdapterAssistant extends CommonDropAdapterAssistant imp
                                }
             }
                }
-
+               
                return Status.CANCEL_STATUS;
        }
 
@@ -171,51 +176,64 @@ 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;
                        }
 
-                       
-                       /*if (((TaxonNode) targetITaxonTreeNode).isTopmostNode()) {
-                               MessageDialog.openConfirm(null, "Taxonnode can not be moved", "A taxonnode can not be moved to the classification");
-                       }*/
-
-                       // Make sure parentTaxon is not the drop target
-//                     if (!childTaxonNode.isTopmostNode() && childTaxonNode.getParent().equals(targetTaxonNode)){
-//                             return Status.CANCEL_STATUS;
-//                     }
-
-                       // Make sure taxon is not being dropped onto itself
-//                     if (childTaxonNode.equals(targetTaxonNode)) {
-//                             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, taxonNode, targetITaxonTreeNode, this, taxonNavigator);
-               NavigationUtil.executeOperation(operation);
+               if (!PreferencesUtil.getSortNodesNaturally()){
+                       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, taxonNode, targetITaxonTreeNode, this, taxonNavigator, true);
+                       NavigationUtil.executeOperation(operation);
+                       
+                       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;
+                               }
+               
+                               AbstractPostOperation operation = new MoveTaxonOperation
+                                               ("Move Taxon", workspaceUndoContext, taxonNode, 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;
+                               }
                
-               logger.info("Moved taxa to new parent " + targetITaxonTreeNode);
-               return Status.OK_STATUS;
+                               AbstractPostOperation operation = new MoveTaxonOperation
+                                               ("Move Taxon", workspaceUndoContext, taxonNode, targetITaxonTreeNode, this, taxonNavigator, false);
+                               NavigationUtil.executeOperation(operation);
+                               
+                               logger.info("Moved taxa to new parent " + targetITaxonTreeNode);
+                               return Status.OK_STATUS;
+                       } else{
+                               return Status.CANCEL_STATUS;
+                       }
+                       
+                       
+               }
        }
 
        /* (non-Javadoc)