fixing #5341 and colouring for cache relevant fields #4915 and #4944
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / handler / DeleteHandler.java
index ef3965ce6c127625fd34f6839cd151e9d7192422..ea3fb60d95574044088195bfcc1cd162e470636c 100644 (file)
@@ -17,7 +17,6 @@ import java.util.Set;
 import org.eclipse.core.commands.AbstractHandler;
 import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
-import org.eclipse.core.commands.IHandler;
 import org.eclipse.core.commands.common.NotDefinedException;
 import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.viewers.TreeSelection;
@@ -28,15 +27,19 @@ import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.handlers.HandlerUtil;
 
 import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
+import eu.etaxonomy.cdm.api.service.config.TaxonNodeDeletionConfigurator;
+import eu.etaxonomy.cdm.api.service.config.NodeDeletionConfigurator.ChildHandling;
 import eu.etaxonomy.cdm.model.common.ITreeNode;
 import eu.etaxonomy.cdm.model.taxon.Classification;
 import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
 import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
 import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator;
 import eu.etaxonomy.taxeditor.navigation.navigator.operation.DeleteOperation;
 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
+import eu.etaxonomy.taxeditor.ui.dialog.deleteConfigurator.DeleteConfiguratorDialog;
 
 /**
  * <p>DeleteTreeNodeHandler class.</p>
@@ -45,10 +48,10 @@ import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
  * @created 06.04.2009
  * @version 1.0
  */
-public class DeleteHandler extends AbstractHandler implements IHandler{
+public class DeleteHandler extends AbstractHandler{
 
-       private IWorkbenchPage activePage;
-       private TaxonNavigator taxonNavigator;
+       protected IWorkbenchPage activePage;
+       protected TaxonNavigator taxonNavigator;
 
        /** {@inheritDoc} */
        @Override
@@ -60,11 +63,11 @@ public class DeleteHandler extends AbstractHandler implements IHandler{
 
                TreeSelection selection = (TreeSelection) HandlerUtil.getCurrentSelection(event);
 
+
                String plural = selection.size() > 1 ? "s" : "";
                // Prompt user for confirmation
-               if(! MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Confirm Deletion", "Are you sure you want to delete the selected node" + plural +"?")){
-                       return null;
-               }
+
+
 
                Iterator selectionIterator = selection.iterator();
                Set<ITaxonTreeNode> treeNodes = new HashSet<ITaxonTreeNode>();
@@ -82,56 +85,118 @@ public class DeleteHandler extends AbstractHandler implements IHandler{
                        }
                }
                AbstractPostOperation operation = null;
+               TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
+               config.setDeleteInAllClassifications(false);
+
                if (treeNodes.size() == 1 ){
                        try {
-                               
-       
+
+                               ITaxonTreeNode treeNode = treeNodes.iterator().next();
+                               ITaxonTreeNode taxonNode =treeNode;
+                               TaxonNodeDeletionConfigurator configNodes = new TaxonNodeDeletionConfigurator();
+
+                               //configNodes.setDeleteTaxon(false);
+                               if (taxonNode instanceof Classification && taxonNode.hasChildNodes()){
+                                       if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(config, HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the classification? The tree has children, they will be deleted, too.")){
+                                               return null;
+                                       }
+                               } else if (taxonNode instanceof Classification && !taxonNode.hasChildNodes()){
+                                       if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(config, HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the classification?")){
+                                               return null;
+                                       }
+                               } else {
+
+                                       if (taxonNode.hasChildNodes()){
+                        DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(
+                                config,
+                                HandlerUtil.getActiveShell(event),
+                                "Confirm Deletion",
+                                null,
+                                "Do you really want to delete the selected node? It has childnodes, they will be deleted, too.",
+                                MessageDialog.WARNING, new String[] { "Delete all children",
+                                        "Move children to parent node", "Skip" }, 0);
+                                               int dialog_result = dialog.open();
+
+                                               if (dialog_result == 0){
+                                                       //delete all children
+                                                       configNodes.setChildHandling(ChildHandling.DELETE);
+                                                       config.setTaxonNodeConfig(configNodes);
+                                               } else if (dialog_result == 1){
+                                                       //move children
+                                                       configNodes.setChildHandling(ChildHandling.MOVE_TO_PARENT);
+                                                       config.setTaxonNodeConfig(configNodes);
+                                               } else if (dialog_result == 2){
+                                                       //skip
+                                                       return null;
+
+                                               }
+                                       }else{
+                                               if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(configNodes, HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the selected node?")){
+                                                       return null;
+                                               }
+                                               config.setTaxonNodeConfig(configNodes);
+                                       }
+                               }
+
                                if (allEditorsClosed){
-                                       ITaxonTreeNode treeNode = treeNodes.iterator().next();
-                                       if (treeNode instanceof Classification){
-                                               operation = new DeleteOperation(
-                                                               event.getCommand().getName(), NavigationUtil.getUndoContext(),
-                                                               treeNodes, new TaxonDeletionConfigurator(), taxonNavigator, taxonNavigator);
-                       
-                                               NavigationUtil.executeOperation(operation);
-                                       }else {
-                                               TaxonNode taxonNode = (TaxonNode)treeNode;
-                                               operation = new DeleteOperation(
-                                                               event.getCommand().getName(), NavigationUtil.getUndoContext(),
-                                                               taxonNode, new TaxonDeletionConfigurator(), taxonNavigator, taxonNavigator);
-                       
+                                       /*if(! MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the selected nodes?")){
+                                               return null;
+                                       }*/
+
+                                               operation = new DeleteOperation(event.getCommand().getName(),
+                                                       NavigationUtil.getUndoContext(),
+                                                               taxonNode,
+                                                               config,
+                                                               taxonNavigator,
+                                                               taxonNavigator,
+                                                               taxonNavigator);
+
                                                NavigationUtil.executeOperation(operation);
-                                               }
+
+                                               //}
                                }
-       
-               
-       
+
+
+
                        } catch (NotDefinedException e) {
-                               NavigationUtil.warn(getClass(), "Command name not set");
+                               MessagingUtils.warn(getClass(), "Command name not set");
+                       } catch (Exception e){
+                           MessagingUtils.error(getClass(), e);
                        }
                } else{
                        try{
+                               if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(config, HandlerUtil.getActiveShell(event), "Confirm Deletion", "Do you really want to delete the selected nodes?")){
+                                       return null;
+                               }
                                if (allEditorsClosed){
-                                       operation = new DeleteOperation(
-                                                       event.getCommand().getName(), NavigationUtil.getUndoContext(),
-                                                       treeNodes, new TaxonDeletionConfigurator(), taxonNavigator, taxonNavigator);
-               
+
+                                       operation = new DeleteOperation(event.getCommand().getName(),
+                                               NavigationUtil.getUndoContext(),
+                                                       treeNodes,
+                                                       new TaxonDeletionConfigurator(),
+                                                       taxonNavigator,
+                                                       taxonNavigator,
+                                                       taxonNavigator);
+
                                        NavigationUtil.executeOperation(operation);
+
                                }
                        }catch (NotDefinedException e) {
-                               NavigationUtil.warn(getClass(), "Command name not set");
-                       }
+                               MessagingUtils.warn(getClass(), "Command name not set");
+                       } catch (Exception e){
+                MessagingUtils.error(getClass(), e);
+            }
                }
                return null;
        }
 
-       private boolean closeObsoleteEditor(TaxonNode taxonNode){
+       protected boolean closeObsoleteEditor(TaxonNode taxonNode){
                boolean result = true;
                for (IEditorReference ref : activePage.getEditorReferences()) {
                        try {
                                String treeIndex = ((ITreeNode)taxonNode).treeIndex();
-                               
-                               
+
+
                                IEditorInput input = ref.getEditorInput();
                                if (input instanceof TaxonEditorInput) {
                                        TaxonNode node = ((TaxonEditorInput) input).getTaxonNode();
@@ -139,7 +204,7 @@ public class DeleteHandler extends AbstractHandler implements IHandler{
                                        if( ((ITreeNode) node).treeIndex().startsWith(treeIndex)){
                                        //if (taxonNode.equals(node)) {
                                                result &= activePage.closeEditor(ref.getEditor(false), true);
-                                               
+
                                        }
                                }
                        } catch (PartInitException e) {