Merge branch 'release/4.6.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / key / polytomous / handler / DeleteNodeHandler.java
index 988564378556ac3167280f5702a803e238e2f71c..01a42bedc7a49ff31b3b5171d846e6f50bd57dff 100644 (file)
@@ -1,20 +1,21 @@
-// $Id$
 /**
  * Copyright (C) 2007 EDIT
- * European Distributed Institute of Taxonomy 
+ * European Distributed Institute of Taxonomy
  * http://www.e-taxonomy.eu
- * 
+ *
  * The contents of this file are subject to the Mozilla Public License Version 1.1
  * See LICENSE.TXT at the top of this package for the full license terms.
  */
 
 package eu.etaxonomy.taxeditor.editor.key.polytomous.handler;
 
-import org.eclipse.core.commands.AbstractHandler;
 import org.eclipse.core.commands.ExecutionEvent;
-import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.core.commands.common.NotDefinedException;
+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;
+import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.ui.IEditorPart;
 import org.eclipse.ui.handlers.HandlerUtil;
@@ -23,7 +24,11 @@ import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
 import eu.etaxonomy.taxeditor.editor.EditorUtil;
 import eu.etaxonomy.taxeditor.editor.key.KeyEditor;
 import eu.etaxonomy.taxeditor.editor.key.polytomous.IPolytomousKeyEditorPage;
+import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyEditorLabels;
 import eu.etaxonomy.taxeditor.editor.key.polytomous.operation.DeleteNodeOperation;
+import eu.etaxonomy.taxeditor.editor.l10n.Messages;
+import eu.etaxonomy.taxeditor.model.AbstractUtility;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
 
 /**
@@ -31,47 +36,137 @@ import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
  * @created Dec 6, 2010
  * @version 1.0
  */
-public class DeleteNodeHandler extends AbstractHandler {
-
-       /*
-        * (non-Javadoc)
-        * 
-        * @see
-        * org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.
-        * ExecutionEvent)
-        */
-       @Override
-       public Object execute(ExecutionEvent event) throws ExecutionException {
-               IEditorPart editor = HandlerUtil.getActiveEditor(event);
-
-               if (editor instanceof KeyEditor) {
-                       IPolytomousKeyEditorPage editorPage = (IPolytomousKeyEditorPage) ((KeyEditor) editor)
-                                       .getActiveEditor();
-
-                       IStructuredSelection selection = (IStructuredSelection) HandlerUtil
-                                       .getCurrentSelection(event);
-
-                       if (selection.getFirstElement() instanceof PolytomousKeyNode) {
-                               try {
-                                       String label = event.getCommand().getName();
-                                       IUndoContext undoContext = EditorUtil.getUndoContext();
-
-                                       for (Object element : selection.toArray()) {
-                                               PolytomousKeyNode keyNode = (PolytomousKeyNode) element;
-
-                                               AbstractPostOperation operation = new DeleteNodeOperation(
-                                                               label, undoContext, keyNode, editorPage);
-                                               EditorUtil.executeOperation(operation);
-                                       }
-
-                               } catch (NotDefinedException e) {
-                                       EditorUtil.warn(getClass(), "Command name not set.");
-                               }
-
-                       }
-               }
-
-               return null;
-       }
+public class DeleteNodeHandler extends AbstractPolytomousKeyNodeHandler {
+
+
+    private static final String DO_YOU_REALLY_WANT_TO_DELETE_THE_NODE_THIS_OPERATION_IS_NOT_REVERSABLE = Messages.DeleteNodeHandler_REALLY_DELETE;
+    private static final String CONFIRM_DELETION_OF_CHILDREN = Messages.DeleteNodeHandler_CONFIRM_DELETE;
+    private static final String NO = Messages.DeleteNodeHandler_NO;
+    private static final String CANCEL = Messages.DeleteNodeHandler_CANCEL;
+    private static final String YES = Messages.DeleteNodeHandler_YES;
+    PolytomousKeyNode nodeToBeDeleted;
+    boolean deleteChildren;
+    /**
+     * @param label
+     */
+    public DeleteNodeHandler(String label) {
+        super(label);
+
+    }
+
+
+
+    public DeleteNodeHandler() {
+        super(PolytomousKeyEditorLabels.DELETE_NODE_POLYTOMOUS_KEY_NODE_LABEL);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public IStatus allowOperations(ExecutionEvent event) {
+        IEditorPart editor = HandlerUtil.getActiveEditor(event);
+        IStructuredSelection selection = (IStructuredSelection) HandlerUtil
+                .getCurrentSelection(event);
+        AbstractPostOperation operation;
+        if (editor instanceof KeyEditor) {
+            editorPage = (IPolytomousKeyEditorPage) ((KeyEditor) editor)
+                    .getActiveEditor();
+        }
+
+
+        if (selection.getFirstElement() instanceof PolytomousKeyNode) {
+            try {
+                String label = event.getCommand().getName();
+                IUndoContext undoContext = EditorUtil.getUndoContext();
+                PolytomousKeyNode node = (PolytomousKeyNode)selection.getFirstElement();
+                nodeToBeDeleted = node;
+                MessageDialog dialog;
+                if (node.getChildren().size()>0){
+                    String[] buttonLables = {YES, NO,CANCEL};
+                    dialog = new MessageDialog(null, CONFIRM_DELETION_OF_CHILDREN, null, DO_YOU_REALLY_WANT_TO_DELETE_THE_NODE_THIS_OPERATION_IS_NOT_REVERSABLE+Messages.DeleteNodeHandler_NODE_HAS_CHILDREN, MessageDialog.QUESTION_WITH_CANCEL, buttonLables, 0);
+                    int returnCode = dialog.open();
+
+                    if (returnCode == 0){
+                        deleteChildren = false;
+                    } else if (returnCode == 1){
+                        deleteChildren = true;
+                    } else{
+                        return new Status(IStatus.CANCEL, "unknown", //$NON-NLS-1$
+                                null);
+                    }
+
+
+                }else{
+                    String[] buttonLables = {YES, CANCEL};
+                    dialog = new MessageDialog(null, CONFIRM_DELETION_OF_CHILDREN, null, DO_YOU_REALLY_WANT_TO_DELETE_THE_NODE_THIS_OPERATION_IS_NOT_REVERSABLE, MessageDialog.QUESTION_WITH_CANCEL, buttonLables, 1);
+                    int returnCode = dialog.open();
+
+                    if (returnCode == 0){
+                        deleteChildren = false;
+                    } else if (returnCode == 1){
+                       return new Status(IStatus.CANCEL, "unknown", //$NON-NLS-1$
+                                null);
+                    }
+                }
+
+
+
+
+            } catch (NotDefinedException e) {
+                MessagingUtils.warn(getClass(), "Command name not set."); //$NON-NLS-1$
+            }
+        } else {
+                MessageDialog.openInformation(
+                        AbstractUtility.getShell(),
+                        PolytomousKeyEditorLabels.NO_KEY_NODE_SELECTED,
+                        PolytomousKeyEditorLabels.NO_KEY_NODE_FOR_INSERT_NODE_SELECTED_MESSAGE
+                        );
+        }
+
+        return Status.OK_STATUS;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public AbstractOperation prepareOperation(ExecutionEvent event) {
+        IUndoContext undoContext = EditorUtil.getUndoContext();
+        String label = ""; //$NON-NLS-1$
+        IEditorPart editor = HandlerUtil.getActiveEditor(event);
+        if (editor.isDirty()){
+            boolean proceed = MessageDialog.openQuestion(null,
+                    Messages.DeleteNodeHandler_SAVE_CHANGES_TITLE, Messages.DeleteNodeHandler_SAVE_CHANGES_MESSAGE);
+            if (!proceed) {
+                return null;
+            }else{
+                editor.doSave(EditorUtil.getMonitor());
+            }
+        }
+
+        try {
+            label = event.getCommand().getName();
+        } catch (NotDefinedException e) {
+            MessagingUtils.warn(getClass(), "Command name not set."); //$NON-NLS-1$
+        }
+        DeleteNodeOperation operation ;
+        if (deleteChildren){
+            operation = new DeleteNodeOperation(label, undoContext, nodeToBeDeleted, editorPage, true);
+        }else{
+            operation =  new DeleteNodeOperation(label, undoContext, nodeToBeDeleted, editorPage, false);
+        }
+
+        return operation;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void onComplete() {
+        // TODO Auto-generated method stub
+
+    }
 
 }