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
.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
;
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
;
36 * @created Dec 6, 2010
39 public class DeleteNodeHandler
extends AbstractPolytomousKeyNodeHandler
{
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
;
52 public DeleteNodeHandler(String label
) {
59 public DeleteNodeHandler() {
60 super(PolytomousKeyEditorLabels
.DELETE_NODE_POLYTOMOUS_KEY_NODE_LABEL
);
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
)
78 if (selection
.getFirstElement() instanceof PolytomousKeyNode
) {
80 String label
= event
.getCommand().getName();
81 IUndoContext undoContext
= EditorUtil
.getUndoContext();
82 PolytomousKeyNode node
= (PolytomousKeyNode
)selection
.getFirstElement();
83 nodeToBeDeleted
= node
;
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();
91 deleteChildren
= false;
92 } else if (returnCode
== 1){
93 deleteChildren
= true;
95 return new Status(IStatus
.CANCEL
, "unknown", //$NON-NLS-1$
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();
105 if (returnCode
== 0){
106 deleteChildren
= false;
107 } else if (returnCode
== 1){
108 return new Status(IStatus
.CANCEL
, "unknown", //$NON-NLS-1$
116 } catch (NotDefinedException e
) {
117 MessagingUtils
.warn(getClass(), "Command name not set."); //$NON-NLS-1$
120 MessageDialog
.openInformation(
121 AbstractUtility
.getShell(),
122 PolytomousKeyEditorLabels
.NO_KEY_NODE_SELECTED
,
123 PolytomousKeyEditorLabels
.NO_KEY_NODE_FOR_INSERT_NODE_SELECTED_MESSAGE
127 return Status
.OK_STATUS
;
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
);
144 editor
.doSave(EditorUtil
.getMonitor());
149 label
= event
.getCommand().getName();
150 } catch (NotDefinedException e
) {
151 MessagingUtils
.warn(getClass(), "Command name not set."); //$NON-NLS-1$
153 DeleteNodeOperation operation
;
155 operation
= new DeleteNodeOperation(label
, undoContext
, nodeToBeDeleted
, editorPage
, true);
157 operation
= new DeleteNodeOperation(label
, undoContext
, nodeToBeDeleted
, editorPage
, false);
167 public void onComplete() {
168 // TODO Auto-generated method stub