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