all polytomouskey related handler are now extended from RemotingCdmHandler
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / key / polytomous / handler / DeleteNodeHandler.java
index 4fc42da010535d14387ff4b3c0c0812b3b5c9ac7..2e5e72fff281c56b2e96991487b3ce64d18389ba 100644 (file)
@@ -1,47 +1,64 @@
 // $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;
 
-import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
-import eu.etaxonomy.cdm.api.service.IPolytomousKeyNodeService;
 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
 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.PolytomousKeyListEditor;
 import eu.etaxonomy.taxeditor.editor.key.polytomous.operation.DeleteNodeOperation;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
-import eu.etaxonomy.taxeditor.store.CdmStore;
 
 /**
  * @author n.hoffmann
  * @created Dec 6, 2010
  * @version 1.0
  */
-public class DeleteNodeHandler extends AbstractHandler {
+public class DeleteNodeHandler extends AbstractPolytomousKeyNodeHandler {
+
+
+    PolytomousKeyNode nodeToBeDeleted;
+    /**
+     * @param label
+     */
+    public DeleteNodeHandler(String label) {
+        super(label);
+
+    }
+
+
+
+    public DeleteNodeHandler() {
+        super(PolytomousKeyEditorLabels.DELETE_NODE_POLYTOMOUS_KEY_NODE_LABEL);
+    }
 
        /*
         * (non-Javadoc)
-        * 
+        *
         * @see
         * org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.
         * ExecutionEvent)
@@ -73,7 +90,7 @@ public class DeleteNodeHandler extends AbstractHandler {
 
                                        for (Object element : selection.toArray()) {
                                                PolytomousKeyNode keyNode = HibernateProxyHelper.deproxy(element, PolytomousKeyNode.class);
-                                               
+
                                                AbstractPostOperation operation = new DeleteNodeOperation(
                                                                label, undoContext, keyNode, editorPage);
                                                EditorUtil.executeOperation(operation);
@@ -86,11 +103,62 @@ public class DeleteNodeHandler extends AbstractHandler {
                        MessageDialog.openInformation(
                                EditorUtil.getShell(),
                                "No Key Node Selected",
-                               "Please right-click on a specific key node to delete a key node.");                 
-                       }                       
+                               "Please right-click on a specific key node to delete a key node.");
+                       }
                }
 
                return null;
        }
 
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public IStatus allowOperations(ExecutionEvent event) {
+        IEditorPart editor = HandlerUtil.getActiveEditor(event);
+
+        if (editor instanceof KeyEditor) {
+            editorPage = (IPolytomousKeyEditorPage) ((KeyEditor) editor)
+                    .getActiveEditor();
+
+            if (editorPage instanceof PolytomousKeyListEditor) {
+                PolytomousKeyListEditor klEditor = (PolytomousKeyListEditor) editorPage;
+                IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelection(event);
+                    if (selection.getFirstElement() instanceof PolytomousKeyNode) {
+                        nodeToBeDeleted = (PolytomousKeyNode) selection.getFirstElement();
+                    } else {
+                        return new Status(IStatus.ERROR,
+                                "unknown",
+                                PolytomousKeyEditorLabels.NO_KEY_NODE_FOR_CHILD_SELECTED_MESSAGE);
+                    }
+                }
+
+        }
+        return Status.OK_STATUS;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public AbstractOperation prepareOperation(ExecutionEvent event) {
+        IUndoContext undoContext = EditorUtil.getUndoContext();
+        String label = "";
+        try {
+            label = event.getCommand().getName();
+        } catch (NotDefinedException e) {
+            MessagingUtils.warn(getClass(), "Command name not set.");
+        }
+        return new DeleteNodeOperation(label, undoContext, nodeToBeDeleted, editorPage);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void onComplete() {
+        // TODO Auto-generated method stub
+
+    }
+
 }