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.HashSet;
|
14
|
import java.util.Iterator;
|
15
|
import java.util.Set;
|
16
|
|
17
|
import org.apache.log4j.Logger;
|
18
|
import org.eclipse.core.commands.operations.IUndoContext;
|
19
|
import org.eclipse.core.runtime.IStatus;
|
20
|
import org.eclipse.core.runtime.Status;
|
21
|
import org.eclipse.jface.dialogs.MessageDialog;
|
22
|
import org.eclipse.jface.util.LocalSelectionTransfer;
|
23
|
import org.eclipse.jface.viewers.ISelection;
|
24
|
import org.eclipse.jface.viewers.TreeSelection;
|
25
|
import org.eclipse.swt.dnd.DropTargetEvent;
|
26
|
import org.eclipse.swt.dnd.TransferData;
|
27
|
import org.eclipse.ui.navigator.CommonDropAdapter;
|
28
|
import org.eclipse.ui.navigator.CommonDropAdapterAssistant;
|
29
|
|
30
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
31
|
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
|
32
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
33
|
import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
|
34
|
import eu.etaxonomy.taxeditor.navigation.navigator.operation.MoveTaxonOperation;
|
35
|
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
|
36
|
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
|
37
|
|
38
|
/**
|
39
|
* <p>TaxonNodeDropAdapterAssistant class.</p>
|
40
|
*
|
41
|
* @author p.ciardelli
|
42
|
* @created 03.06.2009
|
43
|
* @version 1.0
|
44
|
*/
|
45
|
public class TreeNodeDropAdapterAssistant extends CommonDropAdapterAssistant implements IPostOperationEnabled{
|
46
|
private static final Logger logger = Logger
|
47
|
.getLogger(TreeNodeDropAdapterAssistant.class);
|
48
|
|
49
|
/** Constant <code>ID="eu.etaxonomy.taxeditor.navigation.navig"{trunked}</code> */
|
50
|
public static final String ID = "eu.etaxonomy.taxeditor.navigation.navigator.dropassistant"; //$NON-NLS-1$
|
51
|
|
52
|
/* (non-Javadoc)
|
53
|
* @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#handleDrop(org.eclipse.ui.navigator.CommonDropAdapter, org.eclipse.swt.dnd.DropTargetEvent, java.lang.Object)
|
54
|
*/
|
55
|
/** {@inheritDoc} */
|
56
|
@Override
|
57
|
public IStatus handleDrop(CommonDropAdapter dropAdapter,
|
58
|
DropTargetEvent dropTargetEvent, Object target) {
|
59
|
|
60
|
if (target instanceof ITaxonTreeNode) {
|
61
|
Set<TaxonNode> taxonNodes = getSelectedTaxa();
|
62
|
ITaxonTreeNode targetTreeNode = (ITaxonTreeNode) target;
|
63
|
if(taxonNodes != null) {
|
64
|
return moveTaxon(taxonNodes, targetTreeNode);
|
65
|
}
|
66
|
}
|
67
|
|
68
|
return Status.CANCEL_STATUS;
|
69
|
}
|
70
|
|
71
|
private Set<TaxonNode> getSelectedTaxa(){
|
72
|
HashSet<TaxonNode> taxonNodes = new HashSet<TaxonNode>();
|
73
|
|
74
|
ISelection selection = LocalSelectionTransfer.getTransfer().getSelection();
|
75
|
if (selection instanceof TreeSelection) {
|
76
|
|
77
|
Iterator selectionIterator = ((TreeSelection) selection).iterator();
|
78
|
|
79
|
while (selectionIterator.hasNext()){
|
80
|
Object object = selectionIterator.next();
|
81
|
if(object instanceof TaxonNode){
|
82
|
TaxonNode taxonNode = (TaxonNode) object;
|
83
|
taxonNodes.add(taxonNode);
|
84
|
}
|
85
|
}
|
86
|
}
|
87
|
return taxonNodes.size() > 0 ? taxonNodes : null;
|
88
|
}
|
89
|
|
90
|
/* (non-Javadoc)
|
91
|
* @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#validateDrop(java.lang.Object, int, org.eclipse.swt.dnd.TransferData)
|
92
|
*/
|
93
|
/** {@inheritDoc} */
|
94
|
@Override
|
95
|
public IStatus validateDrop(Object target, int operation,
|
96
|
TransferData transferType) {
|
97
|
if (target instanceof ITaxonTreeNode) {
|
98
|
// do not allow to drop onto itself
|
99
|
for(TaxonNode taxonNode : getSelectedTaxa()){
|
100
|
if (taxonNode.equals(target)) {
|
101
|
return Status.CANCEL_STATUS;
|
102
|
}
|
103
|
}
|
104
|
return Status.OK_STATUS;
|
105
|
}
|
106
|
return Status.CANCEL_STATUS;
|
107
|
}
|
108
|
|
109
|
|
110
|
/**
|
111
|
* @param childTaxonNode
|
112
|
* @param parentTaxon
|
113
|
* @return
|
114
|
*/
|
115
|
private IStatus moveTaxon(Set<TaxonNode> taxonNodes, ITaxonTreeNode targetITaxonTreeNode) {
|
116
|
|
117
|
TaxonNavigator taxonNavigator;
|
118
|
taxonNavigator = (TaxonNavigator) NavigationUtil.showView(TaxonNavigator.ID);
|
119
|
|
120
|
if(targetITaxonTreeNode instanceof TaxonNode){
|
121
|
|
122
|
TaxonNode targetTaxonNode = (TaxonNode) targetITaxonTreeNode;
|
123
|
|
124
|
// for(TaxonNode taxonNode : taxonNodes){
|
125
|
// if (taxonNode.equals(targetTaxonNode)) {
|
126
|
// return Status.CANCEL_STATUS;
|
127
|
// }
|
128
|
// }
|
129
|
|
130
|
// Make sure parent taxon does not have unsaved changes
|
131
|
if (NavigationUtil.isDirty(targetTaxonNode)){
|
132
|
MessageDialog.openWarning(NavigationUtil.getShell(), "Unsaved Parent Taxon", "There are unsaved " +
|
133
|
"changes in the parent taxon. Pleas save first.");
|
134
|
return Status.CANCEL_STATUS;
|
135
|
}
|
136
|
|
137
|
|
138
|
// Make sure parentTaxon is not the drop target
|
139
|
// if (!childTaxonNode.isTopmostNode() && childTaxonNode.getParent().equals(targetTaxonNode)){
|
140
|
// return Status.CANCEL_STATUS;
|
141
|
// }
|
142
|
|
143
|
// Make sure taxon is not being dropped onto itself
|
144
|
// if (childTaxonNode.equals(targetTaxonNode)) {
|
145
|
// return Status.CANCEL_STATUS;
|
146
|
// }
|
147
|
|
148
|
|
149
|
|
150
|
}
|
151
|
|
152
|
IUndoContext workspaceUndoContext = NavigationUtil.getWorkbenchUndoContext();
|
153
|
if (workspaceUndoContext == null) {
|
154
|
logger.error("Workspace undo context is null. DND operation cancelled");
|
155
|
return Status.CANCEL_STATUS;
|
156
|
}
|
157
|
|
158
|
AbstractPostOperation operation = new MoveTaxonOperation
|
159
|
("Move Taxon", workspaceUndoContext, taxonNodes, targetITaxonTreeNode, this, taxonNavigator);
|
160
|
NavigationUtil.executeOperation(operation);
|
161
|
|
162
|
logger.info("Moved taxa to new parent " + targetITaxonTreeNode);
|
163
|
return Status.OK_STATUS;
|
164
|
}
|
165
|
|
166
|
/* (non-Javadoc)
|
167
|
* @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
|
168
|
*/
|
169
|
/** {@inheritDoc} */
|
170
|
@Override
|
171
|
public boolean postOperation(CdmBase objectAffectedByOperation) {
|
172
|
return true;
|
173
|
}
|
174
|
|
175
|
/**
|
176
|
* <p>onComplete</p>
|
177
|
*
|
178
|
* @return a boolean.
|
179
|
*/
|
180
|
@Override
|
181
|
public boolean onComplete() {
|
182
|
// TODO Auto-generated method stub
|
183
|
return false;
|
184
|
}
|
185
|
}
|