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