1
|
/**
|
2
|
* Copyright (C) 2015 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
|
package eu.etaxonomy.taxeditor.navigation.navigator.e4.handler;
|
10
|
|
11
|
import java.util.HashSet;
|
12
|
import java.util.Iterator;
|
13
|
import java.util.Set;
|
14
|
|
15
|
import javax.inject.Named;
|
16
|
|
17
|
import org.eclipse.core.commands.operations.AbstractOperation;
|
18
|
import org.eclipse.core.runtime.IStatus;
|
19
|
import org.eclipse.core.runtime.Status;
|
20
|
import org.eclipse.e4.core.di.annotations.CanExecute;
|
21
|
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
|
22
|
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
|
23
|
import org.eclipse.e4.ui.services.IServiceConstants;
|
24
|
import org.eclipse.jface.dialogs.MessageDialog;
|
25
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
26
|
import org.eclipse.jface.viewers.TreeSelection;
|
27
|
import org.eclipse.swt.widgets.Shell;
|
28
|
|
29
|
import eu.etaxonomy.cdm.api.service.config.NodeDeletionConfigurator.ChildHandling;
|
30
|
import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
|
31
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
32
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
33
|
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
|
34
|
import eu.etaxonomy.taxeditor.editor.EditorUtil;
|
35
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
36
|
import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
|
37
|
import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigatorLabels;
|
38
|
import eu.etaxonomy.taxeditor.navigation.navigator.operation.DeleteTaxonNodeOperation;
|
39
|
import eu.etaxonomy.taxeditor.operation.e4.CdmHandlerE4;
|
40
|
import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
|
41
|
|
42
|
/**
|
43
|
* @author cmathew
|
44
|
* @date 22 Jun 2015
|
45
|
*
|
46
|
*/
|
47
|
public class DeleteTaxonNodeHandlerE4 extends CdmHandlerE4 {
|
48
|
|
49
|
protected static final String SKIP = Messages.DeleteHandler_SKIP;
|
50
|
protected static final String MOVE_CHILDREN_TO_PARENT_NODE = Messages.DeleteHandler_MOVE_TO_PARENT;
|
51
|
protected static final String DELETE_ALL_CHILDREN = Messages.DeleteHandler_DELETE_ALL;
|
52
|
protected static final String THERE_ARE_CHILDNODES_WHICH_WILL_BE_DELETED_TOO = Messages.DeleteHandler_THERE_ARE_CHILDNODES;
|
53
|
protected static final String THE_TREE_HAS_CHILDREN_THEY_WILL_BE_DELETED_TOO = Messages.DeleteHandler_THERE_ARE_CHILDREN;
|
54
|
|
55
|
protected static final String DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE_S = Messages.DeleteHandler_DELETE_NODES;
|
56
|
protected static final String DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE = Messages.DeleteHandler_DELETE_NODE;
|
57
|
|
58
|
protected static final String DO_YOU_REALLY_WANT_TO_DELETE_THE_CLASSIFICATION = Messages.DeleteHandler_DELETE_CLASSIFICATION;
|
59
|
protected static final String CONFIRM_DELETION = Messages.DeleteHandler_CONFIRM_DELETE;
|
60
|
protected static final String DELETE = Messages.DeleteHandler_DELETE;
|
61
|
|
62
|
private TaxonDeletionConfigurator config;
|
63
|
private Set<TaxonNodeDto> treeNodes;
|
64
|
private Set<TaxonNodeDto> classifications;
|
65
|
|
66
|
private Set<TaxonNode> taxonNodes;
|
67
|
private Set<Classification> classificationList;
|
68
|
|
69
|
public DeleteTaxonNodeHandlerE4() {
|
70
|
super(TaxonNavigatorLabels.DELETE_TAXON_NODE_LABEL);
|
71
|
}
|
72
|
|
73
|
@Override
|
74
|
public IStatus allowOperations(IStructuredSelection selection,
|
75
|
Shell shell,
|
76
|
MPart activePart,
|
77
|
MHandledMenuItem menuItem) {
|
78
|
|
79
|
Iterator<?> selectionIterator = selection.iterator();
|
80
|
treeNodes = new HashSet();
|
81
|
|
82
|
boolean containsNodeWithChildren = false;
|
83
|
boolean containsClassification = false;
|
84
|
boolean containsTaxonNodes = false;
|
85
|
classifications = new HashSet();
|
86
|
while (selectionIterator.hasNext()){
|
87
|
Object object = selectionIterator.next();
|
88
|
if (object instanceof TaxonNodeDto && ((TaxonNodeDto)object).getTaxonUuid() == null){
|
89
|
TaxonNodeDto dto = (TaxonNodeDto)object;
|
90
|
classifications.add(dto);
|
91
|
treeNodes.add(dto);
|
92
|
containsClassification = true;
|
93
|
}else if(object instanceof TaxonNodeDto) {
|
94
|
treeNodes.add((TaxonNodeDto) object);
|
95
|
}
|
96
|
}
|
97
|
for (TaxonNodeDto treeNode : treeNodes) {
|
98
|
EditorUtil.closeObsoleteEditor(treeNode, partService);
|
99
|
}
|
100
|
|
101
|
config = new TaxonDeletionConfigurator();
|
102
|
Iterator it = treeNodes.iterator();
|
103
|
|
104
|
treeNodes = new HashSet();
|
105
|
if (!containsClassification){
|
106
|
while (it.hasNext()){
|
107
|
TaxonNodeDto treeNode = (TaxonNodeDto) it.next();
|
108
|
|
109
|
treeNodes.add(treeNode);
|
110
|
if (treeNode == null){
|
111
|
MessagingUtils.informationDialog(Messages.RemotingDeleteTaxonNodeHandler_NODE_DELETED, Messages.RemotingDeleteTaxonNodeHandler_NODE_DELETED_MESSAGE);
|
112
|
return Status.CANCEL_STATUS;
|
113
|
|
114
|
}
|
115
|
if (treeNode instanceof TaxonNodeDto){
|
116
|
containsTaxonNodes = true;
|
117
|
if (treeNode.getTaxonomicChildrenCount()>0){
|
118
|
containsNodeWithChildren = true;
|
119
|
}
|
120
|
}
|
121
|
|
122
|
|
123
|
}
|
124
|
}
|
125
|
|
126
|
for (TaxonNodeDto rootNode: classifications){
|
127
|
|
128
|
if (rootNode.getTaxonomicChildrenCount() > 0) {
|
129
|
containsNodeWithChildren = true;
|
130
|
}
|
131
|
|
132
|
}
|
133
|
|
134
|
if (containsClassification && !containsTaxonNodes) {
|
135
|
String message;
|
136
|
if (containsClassification && containsNodeWithChildren) {
|
137
|
message = DO_YOU_REALLY_WANT_TO_DELETE_THE_CLASSIFICATION+THE_TREE_HAS_CHILDREN_THEY_WILL_BE_DELETED_TOO;
|
138
|
} else {
|
139
|
message = DO_YOU_REALLY_WANT_TO_DELETE_THE_CLASSIFICATION;
|
140
|
}
|
141
|
if (!MessageDialog.openConfirm(
|
142
|
shell, CONFIRM_DELETION,
|
143
|
message)) {
|
144
|
return Status.CANCEL_STATUS;
|
145
|
}
|
146
|
}
|
147
|
else {
|
148
|
String confirmMessage= treeNodes.size() == 1?DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE: DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE_S;
|
149
|
if (containsNodeWithChildren){
|
150
|
|
151
|
DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(
|
152
|
config,
|
153
|
shell,
|
154
|
CONFIRM_DELETION,
|
155
|
null,
|
156
|
DO_YOU_REALLY_WANT_TO_DELETE_THE_SELECTED_NODE+THERE_ARE_CHILDNODES_WHICH_WILL_BE_DELETED_TOO,
|
157
|
MessageDialog.WARNING, new String[] { DELETE_ALL_CHILDREN,
|
158
|
MOVE_CHILDREN_TO_PARENT_NODE, SKIP }, 0);
|
159
|
int result = dialog.open();
|
160
|
|
161
|
if (result == 0){
|
162
|
//delete all children
|
163
|
config.getTaxonNodeConfig().setChildHandling(ChildHandling.DELETE);
|
164
|
} else if (result == 1){
|
165
|
//move children
|
166
|
config.getTaxonNodeConfig().setChildHandling(ChildHandling.MOVE_TO_PARENT);
|
167
|
} else {
|
168
|
return Status.CANCEL_STATUS;
|
169
|
}
|
170
|
} else{
|
171
|
|
172
|
DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(
|
173
|
config,
|
174
|
shell,
|
175
|
CONFIRM_DELETION,
|
176
|
null,
|
177
|
confirmMessage,
|
178
|
MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0);
|
179
|
int result = dialog.open();
|
180
|
if (result == 0){
|
181
|
//delete all children
|
182
|
|
183
|
config.getTaxonNodeConfig().setChildHandling(ChildHandling.DELETE);
|
184
|
} else {
|
185
|
return Status.CANCEL_STATUS;
|
186
|
}
|
187
|
}
|
188
|
}
|
189
|
|
190
|
return Status.OK_STATUS;
|
191
|
}
|
192
|
|
193
|
@Override
|
194
|
public AbstractOperation prepareOperation(IStructuredSelection selection,
|
195
|
Shell shell,
|
196
|
MPart activePart,
|
197
|
MHandledMenuItem menuItem) {
|
198
|
return new DeleteTaxonNodeOperation(getTrigger(),
|
199
|
false,
|
200
|
treeNodes,
|
201
|
classifications,
|
202
|
config);
|
203
|
}
|
204
|
|
205
|
@CanExecute
|
206
|
private boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection, MHandledMenuItem menuItem){
|
207
|
boolean canExecute = false;
|
208
|
canExecute = selection.size()==1
|
209
|
&& (selection.getFirstElement() instanceof TaxonNodeDto)
|
210
|
||
|
211
|
(selection.getFirstElement() instanceof TaxonNode
|
212
|
&& ((TaxonNode)selection.getFirstElement()).getTaxon() == null);
|
213
|
menuItem.setVisible(canExecute);
|
214
|
return canExecute;
|
215
|
}
|
216
|
|
217
|
@Override
|
218
|
public void onComplete() {
|
219
|
|
220
|
// CdmApplicationState.getCurrentDataChangeService()
|
221
|
// .fireChangeEvent(new CdmChangeEvent(Action.Delete, treeNodes, this), true);
|
222
|
}
|
223
|
|
224
|
/**
|
225
|
* {@inheritDoc}
|
226
|
*/
|
227
|
@Override
|
228
|
protected Object getTrigger() {
|
229
|
return this;
|
230
|
}
|
231
|
|
232
|
}
|