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
.e4
.handler
;
12 import javax
.inject
.Named
;
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
.e4
.core
.di
.annotations
.CanExecute
;
19 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MPart
;
20 import org
.eclipse
.e4
.ui
.model
.application
.ui
.menu
.MHandledMenuItem
;
21 import org
.eclipse
.e4
.ui
.services
.IServiceConstants
;
22 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
23 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
24 import org
.eclipse
.swt
.widgets
.Shell
;
26 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKeyNode
;
27 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
28 import eu
.etaxonomy
.taxeditor
.editor
.key
.polytomous
.PolytomousKeyEditorLabels
;
29 import eu
.etaxonomy
.taxeditor
.editor
.key
.polytomous
.e4
.PolytomousKeyListEditorE4
;
30 import eu
.etaxonomy
.taxeditor
.editor
.key
.polytomous
.operation
.DeleteNodeOperation
;
31 import eu
.etaxonomy
.taxeditor
.editor
.l10n
.Messages
;
32 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
33 import eu
.etaxonomy
.taxeditor
.operation
.e4
.RemotingCdmHandlerE4
;
37 * @created Dec 6, 2010
40 public class DeleteNodeHandlerE4
extends RemotingCdmHandlerE4
{
43 private static final String DO_YOU_REALLY_WANT_TO_DELETE_THE_NODE_THIS_OPERATION_IS_NOT_REVERSABLE
= Messages
.DeleteNodeHandler_REALLY_DELETE
;
44 private static final String CONFIRM_DELETION_OF_CHILDREN
= Messages
.DeleteNodeHandler_CONFIRM_DELETE
;
45 private static final String NO
= Messages
.DeleteNodeHandler_NO
;
46 private static final String CANCEL
= Messages
.DeleteNodeHandler_CANCEL
;
47 private static final String YES
= Messages
.DeleteNodeHandler_YES
;
48 PolytomousKeyNode nodeToBeDeleted
;
49 boolean deleteChildren
;
51 public DeleteNodeHandlerE4(String label
) {
55 public DeleteNodeHandlerE4() {
56 super(PolytomousKeyEditorLabels
.DELETE_NODE_POLYTOMOUS_KEY_NODE_LABEL
);
60 public IStatus
allowOperations(@Named(IServiceConstants
.ACTIVE_SELECTION
)IStructuredSelection selection
,
61 @Named(IServiceConstants
.ACTIVE_SHELL
)Shell shell
,
62 @Named(IServiceConstants
.ACTIVE_PART
)MPart activePart
,
63 MHandledMenuItem menuItem
) {
65 if (selection
.getFirstElement() instanceof PolytomousKeyNode
) {
66 label
= menuItem
.getLocalizedLabel();
67 PolytomousKeyNode node
= (PolytomousKeyNode
)selection
.getFirstElement();
68 nodeToBeDeleted
= node
;
70 if (node
.getChildren().size()>0){
71 String
[] buttonLables
= {YES
, NO
,CANCEL
};
72 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);
73 int returnCode
= dialog
.open();
76 deleteChildren
= false;
77 } else if (returnCode
== 1){
78 deleteChildren
= true;
80 return new Status(IStatus
.CANCEL
, "unknown", //$NON-NLS-1$
86 String
[] buttonLables
= {YES
, CANCEL
};
87 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);
88 int returnCode
= dialog
.open();
91 deleteChildren
= false;
92 } else if (returnCode
== 1){
93 return new Status(IStatus
.CANCEL
, "unknown", //$NON-NLS-1$
98 MessageDialog
.openInformation(
100 PolytomousKeyEditorLabels
.NO_KEY_NODE_SELECTED
,
101 PolytomousKeyEditorLabels
.NO_KEY_NODE_FOR_INSERT_NODE_SELECTED_MESSAGE
105 return Status
.OK_STATUS
;
109 public AbstractOperation
prepareOperation(@Named(IServiceConstants
.ACTIVE_SELECTION
)IStructuredSelection selection
,
110 @Named(IServiceConstants
.ACTIVE_SHELL
)Shell shell
,
111 @Named(IServiceConstants
.ACTIVE_PART
)MPart activePart
,
112 MHandledMenuItem menuItem
) {
113 IUndoContext undoContext
= EditorUtil
.getUndoContext();
114 PolytomousKeyListEditorE4 editor
= (PolytomousKeyListEditorE4
) activePart
.getObject();
115 if (editor
.isDirty()){
116 boolean proceed
= MessageDialog
.openQuestion(null,
117 Messages
.DeleteNodeHandler_SAVE_CHANGES_TITLE
, Messages
.DeleteNodeHandler_SAVE_CHANGES_MESSAGE
);
121 editor
.save(AbstractUtility
.getMonitor());
125 label
= menuItem
.getLocalizedLabel();
126 DeleteNodeOperation operation
;
128 operation
= new DeleteNodeOperation(label
, undoContext
, nodeToBeDeleted
, editor
, true);
130 operation
= new DeleteNodeOperation(label
, undoContext
, nodeToBeDeleted
, editor
, false);
136 public void onComplete() {
143 protected Object
getTrigger() {
148 public boolean canExecute(@Named(IServiceConstants
.ACTIVE_SELECTION
)IStructuredSelection selection
,
149 MHandledMenuItem menuItem
){
150 boolean canExecute
= false;
151 canExecute
= selection
!=null && selection
.size()==1;
152 menuItem
.setVisible(canExecute
);