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
|
|
10
|
package eu.etaxonomy.taxeditor.navigation.navigator.e4;
|
11
|
|
12
|
import java.util.EnumSet;
|
13
|
import java.util.HashSet;
|
14
|
import java.util.Iterator;
|
15
|
import java.util.Set;
|
16
|
import java.util.UUID;
|
17
|
|
18
|
import org.apache.log4j.Logger;
|
19
|
import org.eclipse.core.commands.operations.IUndoContext;
|
20
|
import org.eclipse.jface.dialogs.MessageDialog;
|
21
|
import org.eclipse.jface.util.LocalSelectionTransfer;
|
22
|
import org.eclipse.jface.viewers.ISelection;
|
23
|
import org.eclipse.jface.viewers.TreeSelection;
|
24
|
import org.eclipse.jface.viewers.ViewerDropAdapter;
|
25
|
import org.eclipse.swt.dnd.DND;
|
26
|
import org.eclipse.swt.dnd.DropTargetEvent;
|
27
|
import org.eclipse.swt.dnd.TransferData;
|
28
|
import org.eclipse.swt.widgets.Display;
|
29
|
|
30
|
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
|
31
|
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
|
32
|
import eu.etaxonomy.cdm.api.service.UpdateResult;
|
33
|
import eu.etaxonomy.cdm.common.monitor.IRemotingProgressMonitor;
|
34
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
35
|
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
|
36
|
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
|
37
|
import eu.etaxonomy.taxeditor.event.EventUtility;
|
38
|
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
|
39
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
40
|
import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
|
41
|
import eu.etaxonomy.taxeditor.operation.IPostMoniteredOperationEnabled;
|
42
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
43
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
44
|
|
45
|
/**
|
46
|
* @author k.luther
|
47
|
* @date 02.06.2015
|
48
|
*/
|
49
|
public class TreeNodeDropAdapterE4 extends ViewerDropAdapter implements IPostMoniteredOperationEnabled{
|
50
|
|
51
|
protected static final String TREE_NODE_DROP_ADAPTER_UNSAVED_PARENT_MESSAGE = Messages.TreeNodeDropAdapter_UNSAVED_PARENT_MESSAGE;
|
52
|
protected static final String TREE_NODE_DROP_ADAPTER_UNSAVED_PARENT = Messages.TreeNodeDropAdapter_UNSAVED_PARENT;
|
53
|
protected static final String TREE_NODE_DROP_ADAPTER_MOVE_TAXON = Messages.TreeNodeDropAdapter_MOVE_TAXON;
|
54
|
protected static final String TREE_NODE_DROP_ADAPTER_CANCEL = Messages.TreeNodeDropAdapter_CANCEL;
|
55
|
protected static final String TREE_NODE_DROP_ADAPTER_BEHIND = Messages.TreeNodeDropAdapter_BEHIND;
|
56
|
protected static final String TREE_NODE_DROP_ADAPTER_CHILD = Messages.TreeNodeDropAdapter_CHILD;
|
57
|
protected static final String DO_YOU_WANT_TO_MOVE_THE_TAXONNODE_AS_CHILD_OR_BEHIND_THE_TARGET_NODE = Messages.TreeNodeDropAdapter_MOVE_BEHIND;
|
58
|
protected static final String TARGET_NODE = Messages.TreeNodeDropAdapter_TARGET_NODE;
|
59
|
|
60
|
private static final Logger logger = Logger.getLogger(TreeNodeDropAdapterE4.class);
|
61
|
|
62
|
private TaxonNavigatorE4 taxonNavigator;
|
63
|
UpdateResult result;
|
64
|
|
65
|
public static final String ID = "eu.etaxonomy.taxeditor.navigation.navigator.dropassistant"; //$NON-NLS-1$
|
66
|
|
67
|
private static final EnumSet<CRUD> UPDATE = EnumSet.of(CRUD.UPDATE);
|
68
|
|
69
|
public enum MovingType{
|
70
|
CHILD, PREVIOUS, BEHIND
|
71
|
}
|
72
|
|
73
|
protected TreeNodeDropAdapterE4(TaxonNavigatorE4 navigator) {
|
74
|
super(navigator.getViewer());
|
75
|
this.taxonNavigator = navigator;
|
76
|
}
|
77
|
|
78
|
/** {@inheritDoc} */
|
79
|
@Override
|
80
|
public boolean performDrop(Object data) {
|
81
|
Object target = getCurrentTarget();
|
82
|
if (getCurrentTarget() instanceof TaxonNodeDto) {
|
83
|
Set<TaxonNodeDto> taxonNodes = getSelectedTaxa();
|
84
|
TaxonNodeDto targetTreeNode = (TaxonNodeDto) target;
|
85
|
// if (targetTreeNode.getType() != null && targetTreeNode.getType().equals(Classification.class)){
|
86
|
// targetTreeNode = ((Classification)targetTreeNode).getRootNode();
|
87
|
// targetTreeNode = HibernateProxyHelper.deproxy(targetTreeNode, TaxonNode.class);
|
88
|
// }
|
89
|
if(taxonNodes != null) {
|
90
|
// if (taxonNodes.size() == 1){
|
91
|
boolean success = moveTaxon(taxonNodes, targetTreeNode);
|
92
|
// if (success){
|
93
|
// taxonNavigator.refresh();
|
94
|
// }
|
95
|
return success;
|
96
|
// } else{
|
97
|
// if( MessageDialog.openConfirm(null, Messages.TreeNodeDropAdapter_MOVING, Messages.TreeNodeDropAdapter_MOVING_MESSAGE)){
|
98
|
// return true;
|
99
|
// }
|
100
|
// }
|
101
|
}
|
102
|
}
|
103
|
return false;
|
104
|
}
|
105
|
|
106
|
private Set<TaxonNodeDto> getSelectedTaxa(){
|
107
|
HashSet<TaxonNodeDto> taxonNodes = new HashSet();
|
108
|
|
109
|
ISelection selection = LocalSelectionTransfer.getTransfer().getSelection();
|
110
|
if (selection instanceof TreeSelection) {
|
111
|
|
112
|
Iterator<?> selectionIterator = ((TreeSelection) selection).iterator();
|
113
|
|
114
|
while (selectionIterator.hasNext()){
|
115
|
Object object = selectionIterator.next();
|
116
|
if(object instanceof TaxonNodeDto){
|
117
|
TaxonNodeDto taxonNode = (TaxonNodeDto) object;
|
118
|
taxonNodes.add(taxonNode);
|
119
|
}
|
120
|
}
|
121
|
}
|
122
|
return taxonNodes;
|
123
|
}
|
124
|
|
125
|
/** {@inheritDoc} */
|
126
|
@Override
|
127
|
public boolean validateDrop(Object target, int operation,
|
128
|
TransferData transferType) {
|
129
|
|
130
|
if (target instanceof TaxonNodeDto) {
|
131
|
|
132
|
// check users permissions with target taxonnode and taxon
|
133
|
if (target instanceof TaxonNodeDto) {
|
134
|
TaxonNodeDto targetNode = (TaxonNodeDto)target;
|
135
|
TaxonNode node = CdmStore.getService(ITaxonNodeService.class).load(targetNode.getUuid());
|
136
|
boolean hasTargetNodePermission = CdmStore.currentAuthentiationHasPermission(node, UPDATE);
|
137
|
boolean hasTargetTaxonPermission = node.getTaxon() == null ?
|
138
|
true :
|
139
|
CdmStore.currentAuthentiationHasPermission(node.getTaxon(), UPDATE);
|
140
|
|
141
|
if(!hasTargetNodePermission || ! hasTargetNodePermission){
|
142
|
if(logger.isDebugEnabled()){
|
143
|
logger.debug("CANCEL_STATUS for target node: " + hasTargetNodePermission + " " + hasTargetTaxonPermission + " "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
144
|
}
|
145
|
return false;
|
146
|
}
|
147
|
}
|
148
|
|
149
|
// do not allow to drop onto itself and
|
150
|
// check users permissions with all selected taxon nodes and taxa
|
151
|
for(TaxonNodeDto taxonNode : getSelectedTaxa()){
|
152
|
if (logger.isDebugEnabled()){
|
153
|
logger.debug("selectedTaxa: " + taxonNode.getTaxonUuid() == null? "-" : taxonNode.getTitleCache()); //$NON-NLS-1$
|
154
|
}
|
155
|
boolean isClassification = taxonNode.getTaxonUuid()== null;
|
156
|
if (isClassification) {
|
157
|
if(logger.isDebugEnabled()){
|
158
|
logger.debug("CANCEL_STATUS for selected classification " + taxonNode.getClassificationUUID()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
159
|
}
|
160
|
return false;
|
161
|
}
|
162
|
boolean isSameTaxonNode = taxonNode.equals(target);
|
163
|
TaxonNode node = CdmStore.getService(ITaxonNodeService.class).load(taxonNode.getUuid());
|
164
|
boolean hasTaxonNodePermission = CdmStore.currentAuthentiationHasPermission(node, UPDATE);
|
165
|
|
166
|
boolean hasTaxonPermission =
|
167
|
CdmStore.currentAuthentiationHasPermission(node.getTaxon(), UPDATE);CdmStore.currentAuthentiationHasPermission(node.getTaxon(), UPDATE);
|
168
|
if (
|
169
|
isSameTaxonNode
|
170
|
|| !hasTaxonNodePermission
|
171
|
|| !hasTaxonPermission
|
172
|
) {
|
173
|
if(logger.isDebugEnabled()){
|
174
|
logger.debug("CANCEL_STATUS for selected " + isSameTaxonNode + Messages.TreeNodeDropAdapter_10 + hasTaxonNodePermission + " " + hasTaxonPermission + " "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
175
|
}
|
176
|
return false;
|
177
|
}
|
178
|
}
|
179
|
logger.debug("OK_STATUS"); //$NON-NLS-1$
|
180
|
return true;
|
181
|
}
|
182
|
logger.debug("CANCEL_STATUS"); //$NON-NLS-1$
|
183
|
return false;
|
184
|
}
|
185
|
|
186
|
|
187
|
private boolean moveTaxon(Set<TaxonNodeDto> taxonNodes, TaxonNodeDto targetITaxonTreeNode) {
|
188
|
Iterator<TaxonNodeDto> taxIterator = taxonNodes.iterator();
|
189
|
Set<UUID> uuids = new HashSet<UUID>();
|
190
|
TaxonNodeDto node = null;
|
191
|
while(taxIterator.hasNext()){
|
192
|
node = taxIterator.next();
|
193
|
uuids.add(node.getUuid());
|
194
|
}
|
195
|
IUndoContext workspaceUndoContext = taxonNavigator.getUndoContext();
|
196
|
int movingTypeInt = 0;
|
197
|
if (!PreferencesUtil.getSortNodesNaturally()){
|
198
|
// if (workspaceUndoContext == null) {
|
199
|
// logger.error("Workspace undo context is null. DND operation cancelled"); //$NON-NLS-1$
|
200
|
// return false;
|
201
|
// }
|
202
|
// result =CdmStore.getService(ITaxonNodeService.class).moveTaxonNodes(uuids,targetITaxonTreeNode.getUuid(), 0);
|
203
|
//
|
204
|
// logger.info("Moved taxa to new parent " + targetITaxonTreeNode); //$NON-NLS-1$
|
205
|
// return true;
|
206
|
}else{
|
207
|
String[] buttonLables = {TREE_NODE_DROP_ADAPTER_CHILD, TREE_NODE_DROP_ADAPTER_BEHIND,TREE_NODE_DROP_ADAPTER_CANCEL};
|
208
|
MessageDialog dialog = new MessageDialog(null, TARGET_NODE, null, DO_YOU_WANT_TO_MOVE_THE_TAXONNODE_AS_CHILD_OR_BEHIND_THE_TARGET_NODE, MessageDialog.QUESTION_WITH_CANCEL, buttonLables, 0);
|
209
|
dialog.open();
|
210
|
int returnCode = dialog.getReturnCode();
|
211
|
|
212
|
if (returnCode == 0){
|
213
|
if (workspaceUndoContext == null) {
|
214
|
logger.error("Workspace undo context is null. DND operation cancelled"); //$NON-NLS-1$
|
215
|
return false;
|
216
|
}
|
217
|
|
218
|
// result = CdmStore.getService(ITaxonNodeService.class).moveTaxonNodes(uuids,targetITaxonTreeNode.getUuid(), 0);
|
219
|
//
|
220
|
// logger.info("Moved taxa to new parent " + targetITaxonTreeNode); //$NON-NLS-1$
|
221
|
// return true;
|
222
|
}else if (returnCode == 1){
|
223
|
if (workspaceUndoContext == null) {
|
224
|
logger.error("Workspace undo context is null. DND operation cancelled"); //$NON-NLS-1$
|
225
|
return false;
|
226
|
}
|
227
|
|
228
|
movingTypeInt = 2;
|
229
|
// CdmStore.getService(ITaxonNodeService.class).moveTaxonNodes(uuids,targetITaxonTreeNode.getUuid(), 2);
|
230
|
// logger.info("Moved taxa to new parent " + targetITaxonTreeNode); //$NON-NLS-1$
|
231
|
// return true;
|
232
|
}
|
233
|
// } else if (returnCode == 2){
|
234
|
// IUndoContext workspaceUndoContext = NavigationUtil.getWorkbenchUndoContext();
|
235
|
// if (workspaceUndoContext == null) {
|
236
|
// logger.error("Workspace undo context is null. DND operation cancelled");
|
237
|
// return false;
|
238
|
// }
|
239
|
// TaxonNode targetNode = (TaxonNode) targetITaxonTreeNode;
|
240
|
//
|
241
|
// AbstractPostOperation operation = new MoveTaxonOperation
|
242
|
// ("Move Taxon", workspaceUndoContext, uuids, targetNode, this, taxonNavigator, MovingType.BEHIND);
|
243
|
// NavigationUtil.executeOperation(operation);
|
244
|
//
|
245
|
// logger.info("Moved taxa to new parent " + targetITaxonTreeNode);
|
246
|
// return true;
|
247
|
// }
|
248
|
else{
|
249
|
return false;
|
250
|
}
|
251
|
|
252
|
|
253
|
}
|
254
|
moveNodes(uuids, targetITaxonTreeNode.getUuid(), movingTypeInt);
|
255
|
return true;
|
256
|
}
|
257
|
|
258
|
private void moveNodes(Set<UUID> taxonNodesToMoveUuid, UUID newParentTreeNodeUuid, int movingTypeInt){
|
259
|
UUID uuid = CdmApplicationState.getLongRunningTasksService().monitLongRunningTask(taxonNodesToMoveUuid,
|
260
|
newParentTreeNodeUuid, movingTypeInt);
|
261
|
|
262
|
Display.getDefault().asyncExec(new Runnable() {
|
263
|
@Override
|
264
|
public void run() {
|
265
|
AbstractUtility.executeMoniteredOperation("Move Taxon to new parent: ",
|
266
|
uuid,
|
267
|
1000,
|
268
|
false,
|
269
|
TreeNodeDropAdapterE4.this,
|
270
|
null);
|
271
|
}
|
272
|
});
|
273
|
}
|
274
|
|
275
|
|
276
|
@Override
|
277
|
public void dragOver(DropTargetEvent event) {
|
278
|
super.dragOver(event);
|
279
|
event.feedback = DND.FEEDBACK_SELECT | DND.FEEDBACK_INSERT_AFTER;
|
280
|
}
|
281
|
|
282
|
/**
|
283
|
* {@inheritDoc}
|
284
|
*/
|
285
|
@Override
|
286
|
public void postOperation(IRemotingProgressMonitor monitor) {
|
287
|
EventUtility.postEvent(WorkbenchEventConstants.REFRESH_NAVIGATOR, true);
|
288
|
}
|
289
|
|
290
|
}
|