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