Merge branch 'release/4.6.0'
[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.common.NotDefinedException;
14 import org.eclipse.core.commands.operations.AbstractOperation;
15 import org.eclipse.core.commands.operations.IUndoContext;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.jface.dialogs.MessageDialog;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.ui.IEditorPart;
21 import org.eclipse.ui.handlers.HandlerUtil;
22
23 import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
24 import eu.etaxonomy.taxeditor.editor.EditorUtil;
25 import eu.etaxonomy.taxeditor.editor.key.KeyEditor;
26 import eu.etaxonomy.taxeditor.editor.key.polytomous.IPolytomousKeyEditorPage;
27 import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyEditorLabels;
28 import eu.etaxonomy.taxeditor.editor.key.polytomous.operation.DeleteNodeOperation;
29 import eu.etaxonomy.taxeditor.editor.l10n.Messages;
30 import eu.etaxonomy.taxeditor.model.AbstractUtility;
31 import eu.etaxonomy.taxeditor.model.MessagingUtils;
32 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
33
34 /**
35 * @author n.hoffmann
36 * @created Dec 6, 2010
37 * @version 1.0
38 */
39 public class DeleteNodeHandler extends AbstractPolytomousKeyNodeHandler {
40
41
42 private static final String DO_YOU_REALLY_WANT_TO_DELETE_THE_NODE_THIS_OPERATION_IS_NOT_REVERSABLE = Messages.DeleteNodeHandler_REALLY_DELETE;
43 private static final String CONFIRM_DELETION_OF_CHILDREN = Messages.DeleteNodeHandler_CONFIRM_DELETE;
44 private static final String NO = Messages.DeleteNodeHandler_NO;
45 private static final String CANCEL = Messages.DeleteNodeHandler_CANCEL;
46 private static final String YES = Messages.DeleteNodeHandler_YES;
47 PolytomousKeyNode nodeToBeDeleted;
48 boolean deleteChildren;
49 /**
50 * @param label
51 */
52 public DeleteNodeHandler(String label) {
53 super(label);
54
55 }
56
57
58
59 public DeleteNodeHandler() {
60 super(PolytomousKeyEditorLabels.DELETE_NODE_POLYTOMOUS_KEY_NODE_LABEL);
61 }
62
63 /**
64 * {@inheritDoc}
65 */
66 @Override
67 public IStatus allowOperations(ExecutionEvent event) {
68 IEditorPart editor = HandlerUtil.getActiveEditor(event);
69 IStructuredSelection selection = (IStructuredSelection) HandlerUtil
70 .getCurrentSelection(event);
71 AbstractPostOperation operation;
72 if (editor instanceof KeyEditor) {
73 editorPage = (IPolytomousKeyEditorPage) ((KeyEditor) editor)
74 .getActiveEditor();
75 }
76
77
78 if (selection.getFirstElement() instanceof PolytomousKeyNode) {
79 try {
80 String label = event.getCommand().getName();
81 IUndoContext undoContext = EditorUtil.getUndoContext();
82 PolytomousKeyNode node = (PolytomousKeyNode)selection.getFirstElement();
83 nodeToBeDeleted = node;
84 MessageDialog dialog;
85 if (node.getChildren().size()>0){
86 String[] buttonLables = {YES, NO,CANCEL};
87 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);
88 int returnCode = dialog.open();
89
90 if (returnCode == 0){
91 deleteChildren = false;
92 } else if (returnCode == 1){
93 deleteChildren = true;
94 } else{
95 return new Status(IStatus.CANCEL, "unknown", //$NON-NLS-1$
96 null);
97 }
98
99
100 }else{
101 String[] buttonLables = {YES, CANCEL};
102 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);
103 int returnCode = dialog.open();
104
105 if (returnCode == 0){
106 deleteChildren = false;
107 } else if (returnCode == 1){
108 return new Status(IStatus.CANCEL, "unknown", //$NON-NLS-1$
109 null);
110 }
111 }
112
113
114
115
116 } catch (NotDefinedException e) {
117 MessagingUtils.warn(getClass(), "Command name not set."); //$NON-NLS-1$
118 }
119 } else {
120 MessageDialog.openInformation(
121 AbstractUtility.getShell(),
122 PolytomousKeyEditorLabels.NO_KEY_NODE_SELECTED,
123 PolytomousKeyEditorLabels.NO_KEY_NODE_FOR_INSERT_NODE_SELECTED_MESSAGE
124 );
125 }
126
127 return Status.OK_STATUS;
128 }
129
130 /**
131 * {@inheritDoc}
132 */
133 @Override
134 public AbstractOperation prepareOperation(ExecutionEvent event) {
135 IUndoContext undoContext = EditorUtil.getUndoContext();
136 String label = ""; //$NON-NLS-1$
137 IEditorPart editor = HandlerUtil.getActiveEditor(event);
138 if (editor.isDirty()){
139 boolean proceed = MessageDialog.openQuestion(null,
140 Messages.DeleteNodeHandler_SAVE_CHANGES_TITLE, Messages.DeleteNodeHandler_SAVE_CHANGES_MESSAGE);
141 if (!proceed) {
142 return null;
143 }else{
144 editor.doSave(EditorUtil.getMonitor());
145 }
146 }
147
148 try {
149 label = event.getCommand().getName();
150 } catch (NotDefinedException e) {
151 MessagingUtils.warn(getClass(), "Command name not set."); //$NON-NLS-1$
152 }
153 DeleteNodeOperation operation ;
154 if (deleteChildren){
155 operation = new DeleteNodeOperation(label, undoContext, nodeToBeDeleted, editorPage, true);
156 }else{
157 operation = new DeleteNodeOperation(label, undoContext, nodeToBeDeleted, editorPage, false);
158 }
159
160 return operation;
161 }
162
163 /**
164 * {@inheritDoc}
165 */
166 @Override
167 public void onComplete() {
168 // TODO Auto-generated method stub
169
170 }
171
172 }