Merge branch 'hotfix/5.18.2'
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / key / polytomous / e4 / handler / DeleteNodeHandlerE4.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.e4.handler;
11
12 import javax.inject.Named;
13
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;
25
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;
34
35 /**
36 * @author n.hoffmann
37 * @created Dec 6, 2010
38 * @version 1.0
39 */
40 public class DeleteNodeHandlerE4 extends RemotingCdmHandlerE4 {
41
42
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;
50
51 public DeleteNodeHandlerE4(String label) {
52 super(label);
53 }
54
55 public DeleteNodeHandlerE4() {
56 super(PolytomousKeyEditorLabels.DELETE_NODE_POLYTOMOUS_KEY_NODE_LABEL);
57 }
58
59 @Override
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) {
64
65 if (selection.getFirstElement() instanceof PolytomousKeyNode) {
66 label = menuItem.getLocalizedLabel();
67 PolytomousKeyNode node = (PolytomousKeyNode)selection.getFirstElement();
68 nodeToBeDeleted = node;
69 MessageDialog dialog;
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();
74
75 if (returnCode == 0){
76 deleteChildren = false;
77 } else if (returnCode == 1){
78 deleteChildren = true;
79 } else{
80 return new Status(IStatus.CANCEL, "unknown", //$NON-NLS-1$
81 null);
82 }
83
84
85 }else{
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();
89
90 if (returnCode == 0){
91 deleteChildren = false;
92 } else if (returnCode == 1){
93 return new Status(IStatus.CANCEL, "unknown", //$NON-NLS-1$
94 null);
95 }
96 }
97 } else {
98 MessageDialog.openInformation(
99 shell,
100 PolytomousKeyEditorLabels.NO_KEY_NODE_SELECTED,
101 PolytomousKeyEditorLabels.NO_KEY_NODE_FOR_INSERT_NODE_SELECTED_MESSAGE
102 );
103 }
104
105 return Status.OK_STATUS;
106 }
107
108 @Override
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);
118 if (!proceed) {
119 return null;
120 }else{
121 editor.save(AbstractUtility.getMonitor());
122 }
123 }
124
125 label = menuItem.getLocalizedLabel();
126 DeleteNodeOperation operation ;
127 if (deleteChildren){
128 operation = new DeleteNodeOperation(label, undoContext, nodeToBeDeleted, editor, true);
129 }else{
130 operation = new DeleteNodeOperation(label, undoContext, nodeToBeDeleted, editor, false);
131 }
132 return operation;
133 }
134
135 @Override
136 public void onComplete() {
137 }
138
139 /**
140 * {@inheritDoc}
141 */
142 @Override
143 protected Object getTrigger() {
144 return this;
145 }
146
147 @CanExecute
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);
153 return canExecute;
154 }
155
156 }