2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.editor
.key
.polytomous
.handler
;
12 import org
.eclipse
.core
.commands
.ExecutionEvent
;
13 import org
.eclipse
.core
.commands
.ExecutionException
;
14 import org
.eclipse
.core
.commands
.common
.NotDefinedException
;
15 import org
.eclipse
.core
.commands
.operations
.AbstractOperation
;
16 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
17 import org
.eclipse
.core
.runtime
.IStatus
;
18 import org
.eclipse
.core
.runtime
.Status
;
19 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
20 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
21 import org
.eclipse
.ui
.IEditorPart
;
22 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
24 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
25 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKeyNode
;
26 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
27 import eu
.etaxonomy
.taxeditor
.editor
.key
.KeyEditor
;
28 import eu
.etaxonomy
.taxeditor
.editor
.key
.polytomous
.IPolytomousKeyEditorPage
;
29 import eu
.etaxonomy
.taxeditor
.editor
.key
.polytomous
.PolytomousKeyEditorLabels
;
30 import eu
.etaxonomy
.taxeditor
.editor
.key
.polytomous
.PolytomousKeyListEditor
;
31 import eu
.etaxonomy
.taxeditor
.editor
.key
.polytomous
.operation
.DeleteNodeOperation
;
32 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
33 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostOperation
;
37 * @created Dec 6, 2010
40 public class DeleteNodeHandler
extends AbstractPolytomousKeyNodeHandler
{
43 PolytomousKeyNode nodeToBeDeleted
;
47 public DeleteNodeHandler(String label
) {
54 public DeleteNodeHandler() {
55 super(PolytomousKeyEditorLabels
.DELETE_NODE_POLYTOMOUS_KEY_NODE_LABEL
);
62 * org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.
66 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
67 IEditorPart editor
= HandlerUtil
.getActiveEditor(event
);
69 if (editor
.isDirty()){
70 boolean proceed
= MessageDialog
.openQuestion(null,
71 "Save changes", "You have made changes that must be saved before you can delete the node. Would you like to proceed?");
75 editor
.doSave(EditorUtil
.getMonitor());
78 if (editor
instanceof KeyEditor
) {
79 IPolytomousKeyEditorPage editorPage
= (IPolytomousKeyEditorPage
) ((KeyEditor
) editor
)
82 IStructuredSelection selection
= (IStructuredSelection
) HandlerUtil
83 .getCurrentSelection(event
);
85 if (selection
.getFirstElement() instanceof PolytomousKeyNode
) {
87 String label
= event
.getCommand().getName();
88 IUndoContext undoContext
= EditorUtil
.getUndoContext();
90 for (Object element
: selection
.toArray()) {
91 PolytomousKeyNode keyNode
= HibernateProxyHelper
.deproxy(element
, PolytomousKeyNode
.class);
93 AbstractPostOperation operation
= new DeleteNodeOperation(
94 label
, undoContext
, keyNode
, editorPage
);
95 EditorUtil
.executeOperation(operation
);
98 } catch (NotDefinedException e
) {
99 MessagingUtils
.warn(getClass(), "Command name not set.");
102 MessageDialog
.openInformation(
103 EditorUtil
.getShell(),
104 "No Key Node Selected",
105 "Please right-click on a specific key node to delete a key node.");
116 public IStatus
allowOperations(ExecutionEvent event
) {
117 IEditorPart editor
= HandlerUtil
.getActiveEditor(event
);
119 if (editor
instanceof KeyEditor
) {
120 editorPage
= (IPolytomousKeyEditorPage
) ((KeyEditor
) editor
)
123 if (editorPage
instanceof PolytomousKeyListEditor
) {
124 PolytomousKeyListEditor klEditor
= (PolytomousKeyListEditor
) editorPage
;
125 IStructuredSelection selection
= (IStructuredSelection
) HandlerUtil
.getCurrentSelection(event
);
126 if (selection
.getFirstElement() instanceof PolytomousKeyNode
) {
127 nodeToBeDeleted
= (PolytomousKeyNode
) selection
.getFirstElement();
129 return new Status(IStatus
.ERROR
,
131 PolytomousKeyEditorLabels
.NO_KEY_NODE_FOR_CHILD_SELECTED_MESSAGE
);
136 return Status
.OK_STATUS
;
143 public AbstractOperation
prepareOperation(ExecutionEvent event
) {
144 IUndoContext undoContext
= EditorUtil
.getUndoContext();
147 label
= event
.getCommand().getName();
148 } catch (NotDefinedException e
) {
149 MessagingUtils
.warn(getClass(), "Command name not set.");
151 return new DeleteNodeOperation(label
, undoContext
, nodeToBeDeleted
, editorPage
);
158 public void onComplete() {
159 // TODO Auto-generated method stub