3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.taxeditor
.editor
.key
.polytomous
.handler
;
13 import org
.eclipse
.core
.commands
.ExecutionEvent
;
14 import org
.eclipse
.core
.commands
.ExecutionException
;
15 import org
.eclipse
.core
.commands
.common
.NotDefinedException
;
16 import org
.eclipse
.core
.commands
.operations
.AbstractOperation
;
17 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
18 import org
.eclipse
.core
.runtime
.IStatus
;
19 import org
.eclipse
.core
.runtime
.Status
;
20 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
21 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
22 import org
.eclipse
.ui
.IEditorPart
;
23 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
25 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
26 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKeyNode
;
27 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
28 import eu
.etaxonomy
.taxeditor
.editor
.key
.KeyEditor
;
29 import eu
.etaxonomy
.taxeditor
.editor
.key
.polytomous
.IPolytomousKeyEditorPage
;
30 import eu
.etaxonomy
.taxeditor
.editor
.key
.polytomous
.PolytomousKeyEditorLabels
;
31 import eu
.etaxonomy
.taxeditor
.editor
.key
.polytomous
.PolytomousKeyListEditor
;
32 import eu
.etaxonomy
.taxeditor
.editor
.key
.polytomous
.operation
.DeleteNodeOperation
;
33 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
34 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostOperation
;
38 * @created Dec 6, 2010
41 public class DeleteNodeHandler
extends AbstractPolytomousKeyNodeHandler
{
44 PolytomousKeyNode nodeToBeDeleted
;
48 public DeleteNodeHandler(String label
) {
55 public DeleteNodeHandler() {
56 super(PolytomousKeyEditorLabels
.DELETE_NODE_POLYTOMOUS_KEY_NODE_LABEL
);
63 * org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.
67 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
68 IEditorPart editor
= HandlerUtil
.getActiveEditor(event
);
70 if (editor
.isDirty()){
71 boolean proceed
= MessageDialog
.openQuestion(null,
72 "Save changes", "You have made changes that must be saved before you can delete the node. Would you like to proceed?");
76 editor
.doSave(EditorUtil
.getMonitor());
79 if (editor
instanceof KeyEditor
) {
80 IPolytomousKeyEditorPage editorPage
= (IPolytomousKeyEditorPage
) ((KeyEditor
) editor
)
83 IStructuredSelection selection
= (IStructuredSelection
) HandlerUtil
84 .getCurrentSelection(event
);
86 if (selection
.getFirstElement() instanceof PolytomousKeyNode
) {
88 String label
= event
.getCommand().getName();
89 IUndoContext undoContext
= EditorUtil
.getUndoContext();
91 for (Object element
: selection
.toArray()) {
92 PolytomousKeyNode keyNode
= HibernateProxyHelper
.deproxy(element
, PolytomousKeyNode
.class);
94 AbstractPostOperation operation
= new DeleteNodeOperation(
95 label
, undoContext
, keyNode
, editorPage
);
96 EditorUtil
.executeOperation(operation
);
99 } catch (NotDefinedException e
) {
100 MessagingUtils
.warn(getClass(), "Command name not set.");
103 MessageDialog
.openInformation(
104 EditorUtil
.getShell(),
105 "No Key Node Selected",
106 "Please right-click on a specific key node to delete a key node.");
117 public IStatus
allowOperations(ExecutionEvent event
) {
118 IEditorPart editor
= HandlerUtil
.getActiveEditor(event
);
120 if (editor
instanceof KeyEditor
) {
121 editorPage
= (IPolytomousKeyEditorPage
) ((KeyEditor
) editor
)
124 if (editorPage
instanceof PolytomousKeyListEditor
) {
125 PolytomousKeyListEditor klEditor
= (PolytomousKeyListEditor
) editorPage
;
126 IStructuredSelection selection
= (IStructuredSelection
) HandlerUtil
.getCurrentSelection(event
);
127 if (selection
.getFirstElement() instanceof PolytomousKeyNode
) {
128 nodeToBeDeleted
= (PolytomousKeyNode
) selection
.getFirstElement();
130 return new Status(IStatus
.ERROR
,
132 PolytomousKeyEditorLabels
.NO_KEY_NODE_FOR_CHILD_SELECTED_MESSAGE
);
137 return Status
.OK_STATUS
;
144 public AbstractOperation
prepareOperation(ExecutionEvent event
) {
145 IUndoContext undoContext
= EditorUtil
.getUndoContext();
148 label
= event
.getCommand().getName();
149 } catch (NotDefinedException e
) {
150 MessagingUtils
.warn(getClass(), "Command name not set.");
152 return new DeleteNodeOperation(label
, undoContext
, nodeToBeDeleted
, editorPage
);
159 public void onComplete() {
160 // TODO Auto-generated method stub