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.CdmHandlerE4;
|
34
|
|
35
|
/**
|
36
|
* @author n.hoffmann
|
37
|
* @created Dec 6, 2010
|
38
|
*/
|
39
|
public class DeleteNodeHandlerE4 extends CdmHandlerE4 {
|
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
|
public DeleteNodeHandlerE4(String label) {
|
51
|
super(label);
|
52
|
}
|
53
|
|
54
|
public DeleteNodeHandlerE4() {
|
55
|
super(PolytomousKeyEditorLabels.DELETE_NODE_POLYTOMOUS_KEY_NODE_LABEL);
|
56
|
}
|
57
|
|
58
|
@Override
|
59
|
public IStatus allowOperations(@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
|
60
|
@Named(IServiceConstants.ACTIVE_SHELL)Shell shell,
|
61
|
@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
|
62
|
MHandledMenuItem menuItem) {
|
63
|
|
64
|
if (selection.getFirstElement() instanceof PolytomousKeyNode) {
|
65
|
label = menuItem.getLocalizedLabel();
|
66
|
PolytomousKeyNode node = (PolytomousKeyNode)selection.getFirstElement();
|
67
|
nodeToBeDeleted = node;
|
68
|
MessageDialog dialog;
|
69
|
if (node.getChildren().size()>0){
|
70
|
String[] buttonLables = {YES, NO,CANCEL};
|
71
|
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);
|
72
|
int returnCode = dialog.open();
|
73
|
|
74
|
if (returnCode == 0){
|
75
|
deleteChildren = false;
|
76
|
} else if (returnCode == 1){
|
77
|
deleteChildren = true;
|
78
|
} else{
|
79
|
return new Status(IStatus.CANCEL, "unknown", //$NON-NLS-1$
|
80
|
null);
|
81
|
}
|
82
|
|
83
|
|
84
|
}else{
|
85
|
String[] buttonLables = {YES, CANCEL};
|
86
|
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);
|
87
|
int returnCode = dialog.open();
|
88
|
|
89
|
if (returnCode == 0){
|
90
|
deleteChildren = false;
|
91
|
} else if (returnCode == 1){
|
92
|
return new Status(IStatus.CANCEL, "unknown", //$NON-NLS-1$
|
93
|
null);
|
94
|
}
|
95
|
}
|
96
|
} else {
|
97
|
MessageDialog.openInformation(
|
98
|
shell,
|
99
|
PolytomousKeyEditorLabels.NO_KEY_NODE_SELECTED,
|
100
|
PolytomousKeyEditorLabels.NO_KEY_NODE_FOR_INSERT_NODE_SELECTED_MESSAGE
|
101
|
);
|
102
|
}
|
103
|
|
104
|
return Status.OK_STATUS;
|
105
|
}
|
106
|
|
107
|
@Override
|
108
|
public AbstractOperation prepareOperation(@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
|
109
|
@Named(IServiceConstants.ACTIVE_SHELL)Shell shell,
|
110
|
@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
|
111
|
MHandledMenuItem menuItem) {
|
112
|
IUndoContext undoContext = EditorUtil.getUndoContext();
|
113
|
PolytomousKeyListEditorE4 editor = (PolytomousKeyListEditorE4) activePart.getObject();
|
114
|
if (editor.isDirty()){
|
115
|
boolean proceed = MessageDialog.openQuestion(null,
|
116
|
Messages.DeleteNodeHandler_SAVE_CHANGES_TITLE, Messages.DeleteNodeHandler_SAVE_CHANGES_MESSAGE);
|
117
|
if (!proceed) {
|
118
|
return null;
|
119
|
}else{
|
120
|
editor.save(AbstractUtility.getMonitor());
|
121
|
}
|
122
|
}
|
123
|
|
124
|
label = menuItem.getLocalizedLabel();
|
125
|
DeleteNodeOperation operation ;
|
126
|
if (deleteChildren){
|
127
|
operation = new DeleteNodeOperation(label, undoContext, nodeToBeDeleted, editor, true);
|
128
|
}else{
|
129
|
operation = new DeleteNodeOperation(label, undoContext, nodeToBeDeleted, editor, false);
|
130
|
}
|
131
|
return operation;
|
132
|
}
|
133
|
|
134
|
@Override
|
135
|
public void onComplete() {
|
136
|
}
|
137
|
|
138
|
@Override
|
139
|
protected Object getTrigger() {
|
140
|
return this;
|
141
|
}
|
142
|
|
143
|
@CanExecute
|
144
|
public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
|
145
|
MHandledMenuItem menuItem){
|
146
|
boolean canExecute = false;
|
147
|
canExecute = selection!=null && selection.size()==1;
|
148
|
menuItem.setVisible(canExecute);
|
149
|
return canExecute;
|
150
|
}
|
151
|
}
|