1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2007 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
|
11
|
package eu.etaxonomy.taxeditor.navigation.navigator;
|
12
|
|
13
|
import java.util.EnumSet;
|
14
|
import java.util.HashSet;
|
15
|
import java.util.Iterator;
|
16
|
import java.util.Set;
|
17
|
import java.util.UUID;
|
18
|
|
19
|
import org.apache.log4j.Logger;
|
20
|
import org.eclipse.core.commands.operations.AbstractOperation;
|
21
|
import org.eclipse.core.commands.operations.IUndoContext;
|
22
|
import org.eclipse.core.runtime.IStatus;
|
23
|
import org.eclipse.core.runtime.Status;
|
24
|
import org.eclipse.jface.dialogs.MessageDialog;
|
25
|
import org.eclipse.jface.util.LocalSelectionTransfer;
|
26
|
import org.eclipse.jface.viewers.ISelection;
|
27
|
import org.eclipse.jface.viewers.TreeSelection;
|
28
|
import org.eclipse.swt.dnd.DropTargetEvent;
|
29
|
import org.eclipse.swt.dnd.TransferData;
|
30
|
import org.eclipse.ui.navigator.CommonDropAdapter;
|
31
|
import org.eclipse.ui.navigator.CommonDropAdapterAssistant;
|
32
|
|
33
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
34
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
35
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
36
|
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
|
37
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
38
|
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
|
39
|
import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
|
40
|
import eu.etaxonomy.taxeditor.navigation.navigator.TreeNodeDropAdapter.MovingType;
|
41
|
import eu.etaxonomy.taxeditor.navigation.navigator.operation.MoveTaxonOperation;
|
42
|
import eu.etaxonomy.taxeditor.navigation.navigator.operation.RemotingMoveTaxonOperation;
|
43
|
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
|
44
|
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
|
45
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
46
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
47
|
|
48
|
/**
|
49
|
* <p>TaxonNodeDropAdapterAssistant class.</p>
|
50
|
*
|
51
|
* @author p.ciardelli
|
52
|
* @created 03.06.2009
|
53
|
* @version 1.0
|
54
|
*/
|
55
|
public class TreeNodeDropAdapterAssistant extends CommonDropAdapterAssistant implements IPostOperationEnabled {
|
56
|
|
57
|
private static final Logger logger = Logger.getLogger(TreeNodeDropAdapterAssistant.class);
|
58
|
|
59
|
/** Constant <code>ID="eu.etaxonomy.taxeditor.navigation.navig"{trunked}</code> */
|
60
|
public static final String ID = "eu.etaxonomy.taxeditor.navigation.navigator.dropassistant"; //$NON-NLS-1$
|
61
|
|
62
|
private static final EnumSet<CRUD> UPDATE = EnumSet.of(CRUD.UPDATE);
|
63
|
|
64
|
/* (non-Javadoc)
|
65
|
* @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#handleDrop(org.eclipse.ui.navigator.CommonDropAdapter, org.eclipse.swt.dnd.DropTargetEvent, java.lang.Object)
|
66
|
*/
|
67
|
/** {@inheritDoc} */
|
68
|
@Override
|
69
|
public IStatus handleDrop(CommonDropAdapter dropAdapter,
|
70
|
DropTargetEvent dropTargetEvent, Object target) {
|
71
|
|
72
|
|
73
|
if (target instanceof ITaxonTreeNode) {
|
74
|
Set<TaxonNode> taxonNodes = getSelectedTaxa();
|
75
|
ITaxonTreeNode targetTreeNode = (ITaxonTreeNode) target;
|
76
|
if (targetTreeNode instanceof Classification){
|
77
|
targetTreeNode = ((Classification)targetTreeNode).getRootNode();
|
78
|
targetTreeNode = HibernateProxyHelper.deproxy(targetTreeNode, TaxonNode.class);
|
79
|
}
|
80
|
//if(taxonNodes != null) {
|
81
|
if (taxonNodes.size() >= 1){
|
82
|
return moveTaxon(taxonNodes, targetTreeNode);
|
83
|
/*} else{
|
84
|
if( MessageDialog.openConfirm(null, "Moving taxon", "The operation move accepted taxon to other parent is available only for a single taxon.")){
|
85
|
return null;
|
86
|
}
|
87
|
}*/
|
88
|
}
|
89
|
}
|
90
|
|
91
|
return Status.CANCEL_STATUS;
|
92
|
}
|
93
|
|
94
|
private Set<TaxonNode> getSelectedTaxa(){
|
95
|
HashSet<TaxonNode> taxonNodes = new HashSet<TaxonNode>();
|
96
|
|
97
|
ISelection selection = LocalSelectionTransfer.getTransfer().getSelection();
|
98
|
if (selection instanceof TreeSelection) {
|
99
|
|
100
|
Iterator selectionIterator = ((TreeSelection) selection).iterator();
|
101
|
|
102
|
while (selectionIterator.hasNext()){
|
103
|
Object object = selectionIterator.next();
|
104
|
if(object instanceof TaxonNode){
|
105
|
TaxonNode taxonNode = (TaxonNode) object;
|
106
|
taxonNodes.add(taxonNode);
|
107
|
}
|
108
|
}
|
109
|
}
|
110
|
return taxonNodes.size() > 0 ? taxonNodes : null;
|
111
|
}
|
112
|
|
113
|
/* (non-Javadoc)
|
114
|
* @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#validateDrop(java.lang.Object, int, org.eclipse.swt.dnd.TransferData)
|
115
|
*/
|
116
|
/** {@inheritDoc} */
|
117
|
@Override
|
118
|
public IStatus validateDrop(Object target, int operation,
|
119
|
TransferData transferType) {
|
120
|
if (target instanceof ITaxonTreeNode) {
|
121
|
|
122
|
// check users permissions with target taxonnode and taxon
|
123
|
if (target instanceof TaxonNode) {
|
124
|
TaxonNode targetNode = (TaxonNode)target;
|
125
|
Boolean hasTargetNodePermission = CdmStore.currentAuthentiationHasPermission(targetNode, UPDATE);
|
126
|
Boolean hasTargetTaxonPermission = CdmStore.currentAuthentiationHasPermission(targetNode.getTaxon(), UPDATE);
|
127
|
|
128
|
if(logger.isDebugEnabled()){
|
129
|
logger.debug("target: " + targetNode.getTaxon().getTitleCache());
|
130
|
}
|
131
|
|
132
|
if(!hasTargetNodePermission || ! hasTargetNodePermission){
|
133
|
if(logger.isDebugEnabled()){
|
134
|
logger.debug("CANCEL_STATUS for target node: " + hasTargetNodePermission.toString() + " " + hasTargetTaxonPermission.toString() + " ");
|
135
|
}
|
136
|
return Status.CANCEL_STATUS;
|
137
|
}
|
138
|
}
|
139
|
|
140
|
// do not allow to drop onto itself and
|
141
|
// check users permissions with all selected taxon nodes and taxa
|
142
|
for(TaxonNode taxonNode : getSelectedTaxa()){
|
143
|
logger.debug("selectedTaxa: " + taxonNode.getTaxon().getTitleCache());
|
144
|
Boolean isSameTaxonNode = taxonNode.equals(target);
|
145
|
Boolean hasTaxonNodePermission = CdmStore.currentAuthentiationHasPermission(taxonNode, UPDATE);
|
146
|
Boolean hasTaxonPermission = CdmStore.currentAuthentiationHasPermission(taxonNode.getTaxon(), UPDATE);
|
147
|
if (
|
148
|
isSameTaxonNode
|
149
|
|| !hasTaxonNodePermission
|
150
|
|| !hasTaxonPermission
|
151
|
) {
|
152
|
if(logger.isDebugEnabled()){
|
153
|
logger.debug("CANCEL_STATUS for selected " + isSameTaxonNode.toString() + " " + hasTaxonNodePermission.toString() + " " + hasTaxonPermission.toString() + " ");
|
154
|
}
|
155
|
return Status.CANCEL_STATUS;
|
156
|
}
|
157
|
}
|
158
|
logger.debug("OK_STATUS");
|
159
|
return Status.OK_STATUS;
|
160
|
}
|
161
|
logger.debug("CANCEL_STATUS");
|
162
|
return Status.CANCEL_STATUS;
|
163
|
}
|
164
|
|
165
|
|
166
|
/**
|
167
|
* @param childTaxonNode
|
168
|
* @param parentTaxon
|
169
|
* @return
|
170
|
*/
|
171
|
private IStatus moveTaxon(Set<TaxonNode> taxonNodes, ITaxonTreeNode targetITaxonTreeNode) {
|
172
|
|
173
|
TaxonNavigator taxonNavigator;
|
174
|
taxonNavigator = (TaxonNavigator) NavigationUtil.showView(TaxonNavigator.ID);
|
175
|
|
176
|
if(targetITaxonTreeNode instanceof TaxonNode){
|
177
|
|
178
|
TaxonNode targetTaxonNode = (TaxonNode) targetITaxonTreeNode;
|
179
|
// Make sure parent taxon does not have unsaved changes
|
180
|
if (NavigationUtil.isDirty(targetTaxonNode)){
|
181
|
MessageDialog.openWarning(NavigationUtil.getShell(), "Unsaved Parent Taxon", "There are unsaved " +
|
182
|
"changes in the parent taxon. Pleas save first.");
|
183
|
return Status.CANCEL_STATUS;
|
184
|
}
|
185
|
|
186
|
}
|
187
|
Iterator<TaxonNode> taxIterator = taxonNodes.iterator();
|
188
|
Set<UUID> uuids = new HashSet<UUID>();
|
189
|
TaxonNode node = null;
|
190
|
while(taxIterator.hasNext()){
|
191
|
node = taxIterator.next();
|
192
|
uuids.add(node.getUuid());
|
193
|
}
|
194
|
if (!PreferencesUtil.getSortNodesNaturally()){
|
195
|
IUndoContext workspaceUndoContext = NavigationUtil.getWorkbenchUndoContext();
|
196
|
if (workspaceUndoContext == null) {
|
197
|
logger.error("Workspace undo context is null. DND operation cancelled");
|
198
|
return Status.CANCEL_STATUS;
|
199
|
}
|
200
|
|
201
|
AbstractOperation operation = new RemotingMoveTaxonOperation(taxonNavigator, false, uuids, (TaxonNode)targetITaxonTreeNode, MovingType.CHILD);
|
202
|
NavigationUtil.executeOperation(operation, null);
|
203
|
|
204
|
|
205
|
logger.info("Moved taxa to new parent " + targetITaxonTreeNode);
|
206
|
return Status.OK_STATUS;
|
207
|
}else{
|
208
|
String[] buttonLables = {"Child", "Behind", "Cancel"};
|
209
|
MessageDialog dialog = new MessageDialog(null, "Target node", null, "Do you want to move the taxon as child, before or behind the target.", MessageDialog.QUESTION_WITH_CANCEL, buttonLables, 0);
|
210
|
dialog.open();
|
211
|
int returnCode = dialog.getReturnCode();
|
212
|
if (returnCode == 0){
|
213
|
IUndoContext workspaceUndoContext = NavigationUtil.getWorkbenchUndoContext();
|
214
|
if (workspaceUndoContext == null) {
|
215
|
logger.error("Workspace undo context is null. DND operation cancelled");
|
216
|
return Status.CANCEL_STATUS;
|
217
|
}
|
218
|
|
219
|
AbstractOperation operation = new RemotingMoveTaxonOperation(taxonNavigator, false, uuids, (TaxonNode)targetITaxonTreeNode, MovingType.CHILD);
|
220
|
NavigationUtil.executeOperation(operation, null);
|
221
|
|
222
|
|
223
|
logger.info("Moved taxa to new parent " + targetITaxonTreeNode);
|
224
|
return Status.OK_STATUS;
|
225
|
}else if (returnCode == 1){
|
226
|
IUndoContext workspaceUndoContext = NavigationUtil.getWorkbenchUndoContext();
|
227
|
if (workspaceUndoContext == null) {
|
228
|
logger.error("Workspace undo context is null. DND operation cancelled");
|
229
|
return Status.CANCEL_STATUS;
|
230
|
}
|
231
|
TaxonNode targetNode = (TaxonNode) targetITaxonTreeNode;
|
232
|
|
233
|
AbstractOperation operation = new RemotingMoveTaxonOperation(taxonNavigator, false, uuids, targetNode, MovingType.BEHIND);
|
234
|
NavigationUtil.executeOperation(operation, null);
|
235
|
|
236
|
logger.info("Moved taxa to new parent " + targetITaxonTreeNode);
|
237
|
return Status.OK_STATUS;
|
238
|
// }else if (returnCode == 2){
|
239
|
// IUndoContext workspaceUndoContext = NavigationUtil.getWorkbenchUndoContext();
|
240
|
// if (workspaceUndoContext == null) {
|
241
|
// logger.error("Workspace undo context is null. DND operation cancelled");
|
242
|
// return Status.CANCEL_STATUS;
|
243
|
// }
|
244
|
// TaxonNode targetNode = (TaxonNode) targetITaxonTreeNode;
|
245
|
//
|
246
|
// if(CdmStore.getCurrentSessionManager().isRemoting()) {
|
247
|
// AbstractOperation operation = new RemotingMoveTaxonOperation(taxonNavigator, false, uuids, targetNode, MovingType.PREVIOUS);
|
248
|
// NavigationUtil.executeOperation(operation, null);
|
249
|
// } else {
|
250
|
// AbstractPostOperation operation = new MoveTaxonOperation
|
251
|
// ("Move Taxon", workspaceUndoContext, uuids, targetNode, this, taxonNavigator, MovingType.PREVIOUS);
|
252
|
// NavigationUtil.executeOperation(operation);
|
253
|
// }
|
254
|
// logger.info("Moved taxa to new parent " + targetITaxonTreeNode);
|
255
|
// return Status.OK_STATUS;
|
256
|
} else{
|
257
|
return Status.CANCEL_STATUS;
|
258
|
}
|
259
|
|
260
|
|
261
|
}
|
262
|
}
|
263
|
|
264
|
/* (non-Javadoc)
|
265
|
* @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
|
266
|
*/
|
267
|
/** {@inheritDoc} */
|
268
|
@Override
|
269
|
public boolean postOperation(CdmBase objectAffectedByOperation) {
|
270
|
return true;
|
271
|
}
|
272
|
|
273
|
/**
|
274
|
* <p>onComplete</p>
|
275
|
*
|
276
|
* @return a boolean.
|
277
|
*/
|
278
|
@Override
|
279
|
public boolean onComplete() {
|
280
|
// TODO Auto-generated method stub
|
281
|
return false;
|
282
|
}
|
283
|
|
284
|
}
|