ref #6190 removing svn property place holder in first line of code - java files
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / key / polytomous / handler / DeleteNodeHandler.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.taxeditor.editor.key.polytomous.handler;
11
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;
23
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;
34
35 /**
36 * @author n.hoffmann
37 * @created Dec 6, 2010
38 * @version 1.0
39 */
40 public class DeleteNodeHandler extends AbstractPolytomousKeyNodeHandler {
41
42
43 PolytomousKeyNode nodeToBeDeleted;
44 /**
45 * @param label
46 */
47 public DeleteNodeHandler(String label) {
48 super(label);
49
50 }
51
52
53
54 public DeleteNodeHandler() {
55 super(PolytomousKeyEditorLabels.DELETE_NODE_POLYTOMOUS_KEY_NODE_LABEL);
56 }
57
58 /*
59 * (non-Javadoc)
60 *
61 * @see
62 * org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.
63 * ExecutionEvent)
64 */
65 @Override
66 public Object execute(ExecutionEvent event) throws ExecutionException {
67 IEditorPart editor = HandlerUtil.getActiveEditor(event);
68
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?");
72 if (!proceed) {
73 return null;
74 }else{
75 editor.doSave(EditorUtil.getMonitor());
76 }
77 }
78 if (editor instanceof KeyEditor) {
79 IPolytomousKeyEditorPage editorPage = (IPolytomousKeyEditorPage) ((KeyEditor) editor)
80 .getActiveEditor();
81
82 IStructuredSelection selection = (IStructuredSelection) HandlerUtil
83 .getCurrentSelection(event);
84
85 if (selection.getFirstElement() instanceof PolytomousKeyNode) {
86 try {
87 String label = event.getCommand().getName();
88 IUndoContext undoContext = EditorUtil.getUndoContext();
89
90 for (Object element : selection.toArray()) {
91 PolytomousKeyNode keyNode = HibernateProxyHelper.deproxy(element, PolytomousKeyNode.class);
92
93 AbstractPostOperation operation = new DeleteNodeOperation(
94 label, undoContext, keyNode, editorPage);
95 EditorUtil.executeOperation(operation);
96 }
97
98 } catch (NotDefinedException e) {
99 MessagingUtils.warn(getClass(), "Command name not set.");
100 }
101 } else {
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.");
106 }
107 }
108
109 return null;
110 }
111
112 /**
113 * {@inheritDoc}
114 */
115 @Override
116 public IStatus allowOperations(ExecutionEvent event) {
117 IEditorPart editor = HandlerUtil.getActiveEditor(event);
118
119 if (editor instanceof KeyEditor) {
120 editorPage = (IPolytomousKeyEditorPage) ((KeyEditor) editor)
121 .getActiveEditor();
122
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();
128 } else {
129 return new Status(IStatus.ERROR,
130 "unknown",
131 PolytomousKeyEditorLabels.NO_KEY_NODE_FOR_CHILD_SELECTED_MESSAGE);
132 }
133 }
134
135 }
136 return Status.OK_STATUS;
137 }
138
139 /**
140 * {@inheritDoc}
141 */
142 @Override
143 public AbstractOperation prepareOperation(ExecutionEvent event) {
144 IUndoContext undoContext = EditorUtil.getUndoContext();
145 String label = "";
146 try {
147 label = event.getCommand().getName();
148 } catch (NotDefinedException e) {
149 MessagingUtils.warn(getClass(), "Command name not set.");
150 }
151 return new DeleteNodeOperation(label, undoContext, nodeToBeDeleted, editorPage);
152 }
153
154 /**
155 * {@inheritDoc}
156 */
157 @Override
158 public void onComplete() {
159 // TODO Auto-generated method stub
160
161 }
162
163 }