1
|
/**
|
2
|
* Copyright (C) 2017 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
|
package eu.etaxonomy.taxeditor.termtree.e4;
|
10
|
|
11
|
import java.util.Arrays;
|
12
|
import java.util.Collection;
|
13
|
import java.util.Collections;
|
14
|
|
15
|
import org.eclipse.e4.ui.di.UISynchronize;
|
16
|
import org.eclipse.jface.util.LocalSelectionTransfer;
|
17
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
18
|
import org.eclipse.jface.viewers.TreeViewer;
|
19
|
import org.eclipse.jface.viewers.Viewer;
|
20
|
import org.eclipse.jface.viewers.ViewerDropAdapter;
|
21
|
import org.eclipse.swt.dnd.TransferData;
|
22
|
|
23
|
import eu.etaxonomy.cdm.model.description.Feature;
|
24
|
import eu.etaxonomy.cdm.model.term.IHasTermType;
|
25
|
import eu.etaxonomy.cdm.model.term.OrderedTermVocabulary;
|
26
|
import eu.etaxonomy.cdm.model.term.TermNode;
|
27
|
import eu.etaxonomy.cdm.model.term.TermTree;
|
28
|
import eu.etaxonomy.cdm.model.term.TermType;
|
29
|
import eu.etaxonomy.cdm.model.term.TermVocabulary;
|
30
|
import eu.etaxonomy.cdm.persistence.dto.TermDto;
|
31
|
import eu.etaxonomy.cdm.persistence.dto.TermNodeDto;
|
32
|
import eu.etaxonomy.taxeditor.editor.definedterm.TermTransfer;
|
33
|
import eu.etaxonomy.taxeditor.editor.definedterm.e4.DefinedTermDropAdapterE4;
|
34
|
import eu.etaxonomy.taxeditor.l10n.Messages;
|
35
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
36
|
import eu.etaxonomy.taxeditor.store.StoreUtil;
|
37
|
import eu.etaxonomy.taxeditor.termtree.TermNodeTransfer;
|
38
|
import eu.etaxonomy.taxeditor.termtree.e4.operation.AddFeatureOperation;
|
39
|
import eu.etaxonomy.taxeditor.termtree.e4.operation.AddOntologyTermOperation;
|
40
|
import eu.etaxonomy.taxeditor.termtree.e4.operation.MoveFeatureOperation;
|
41
|
import eu.etaxonomy.taxeditor.ui.dialog.selection.TermVocabularySelectionDialog;
|
42
|
import eu.etaxonomy.taxeditor.view.webimport.termimport.wrapper.OntologyTermWrapper;
|
43
|
|
44
|
public class TermTreeDropAdapter extends ViewerDropAdapter {
|
45
|
|
46
|
protected ITermTreeEditor editor;
|
47
|
private UISynchronize sync;
|
48
|
|
49
|
public TermTreeDropAdapter(ITermTreeEditor editor, Viewer viewer, UISynchronize sync) {
|
50
|
super(viewer);
|
51
|
this.editor = editor;
|
52
|
this.sync = sync;
|
53
|
}
|
54
|
|
55
|
@Override
|
56
|
public boolean performDrop(Object data) {
|
57
|
// if(StoreUtil.promptCheckIsDirty(editor)){
|
58
|
// return false;
|
59
|
// }
|
60
|
Object currentTarget = getCurrentTarget();
|
61
|
TermNode target = null;
|
62
|
if(currentTarget instanceof TermTree){
|
63
|
target = ((TermTree) currentTarget).getRoot();
|
64
|
}
|
65
|
else if(currentTarget instanceof TermNode){
|
66
|
target = (TermNode) currentTarget;
|
67
|
}
|
68
|
|
69
|
if (target.getUuid() == null){
|
70
|
if(StoreUtil.promptCheckIsDirty(editor)){
|
71
|
return false;
|
72
|
}
|
73
|
}
|
74
|
int position = 0;
|
75
|
|
76
|
if (target != null) {
|
77
|
int location = getCurrentLocation();
|
78
|
TermNode parent = target.getParent();
|
79
|
if(parent !=null){
|
80
|
|
81
|
if (location == LOCATION_BEFORE) {
|
82
|
position = Math.max(0, parent.getIndex(target));
|
83
|
target = parent;
|
84
|
}
|
85
|
|
86
|
if (location == LOCATION_AFTER) {
|
87
|
position = parent.getIndex(target)+1;
|
88
|
target = parent;
|
89
|
}
|
90
|
}
|
91
|
}
|
92
|
|
93
|
if(target==null){
|
94
|
MessagingUtils.warningDialog("", this, ""); //$NON-NLS-1$ //$NON-NLS-1$
|
95
|
return false;
|
96
|
}
|
97
|
Collection<Object> droppedObjects = Collections.emptyList();
|
98
|
if(data instanceof Object[]){
|
99
|
droppedObjects = Arrays.asList((Object[])data);
|
100
|
}
|
101
|
else if(data instanceof IStructuredSelection){
|
102
|
droppedObjects = ((IStructuredSelection) data).toList();
|
103
|
}
|
104
|
TreeViewer viewer = (TreeViewer) getViewer();
|
105
|
|
106
|
// cannot drop a feature node onto itself
|
107
|
for (Object droppedObject : droppedObjects) {
|
108
|
if(droppedObject==null){
|
109
|
MessagingUtils.warningDialog(DefinedTermDropAdapterE4.MOVE_FAILED, this.getClass(),
|
110
|
Messages.FeatureTreeDropAdapter_MOVE_FAILED_SAVE_MESSAGE);
|
111
|
return false;
|
112
|
}
|
113
|
if (droppedObject.equals(target)) {
|
114
|
return false;
|
115
|
}
|
116
|
|
117
|
}
|
118
|
for (Object droppedObject : droppedObjects) {
|
119
|
//check term type compatibility
|
120
|
TermType targetType = target.getTermType();
|
121
|
TermType droppedType = null;
|
122
|
if(droppedObject instanceof IHasTermType){
|
123
|
droppedType = ((IHasTermType)droppedObject).getTermType();
|
124
|
if(droppedType!=null && !droppedType.equals(targetType) && !droppedType.isKindOf(targetType)){
|
125
|
MessagingUtils.warningDialog(DefinedTermDropAdapterE4.TERM_TYPE_ERROR_TITLE, this, DefinedTermDropAdapterE4.TERM_TYPE_ERROR_MESSAGE);
|
126
|
continue;
|
127
|
}
|
128
|
}
|
129
|
else if(droppedObject instanceof TermDto){
|
130
|
droppedType = ((TermDto)droppedObject).getTermType();
|
131
|
if(droppedType!=null && !droppedType.equals(targetType) && !droppedType.isKindOf(targetType)){
|
132
|
MessagingUtils.warningDialog(DefinedTermDropAdapterE4.TERM_TYPE_ERROR_TITLE, this, DefinedTermDropAdapterE4.TERM_TYPE_ERROR_MESSAGE);
|
133
|
continue;
|
134
|
}
|
135
|
}
|
136
|
if(droppedObject instanceof TermNodeDto){
|
137
|
if (((TermNodeDto)droppedObject).getUuid() == null){
|
138
|
if(StoreUtil.promptCheckIsDirty(editor)){
|
139
|
return false;
|
140
|
}
|
141
|
}
|
142
|
TermNodeDto droppedNode = (TermNodeDto) droppedObject;
|
143
|
TermNodeDto oldParent = editor.getNodeDtoForUuid(droppedNode.getParentUuid());
|
144
|
int currentPosition = oldParent.getIndex(droppedNode);
|
145
|
|
146
|
if(currentPosition<position && target.equals(oldParent)){
|
147
|
position -= 1;
|
148
|
}
|
149
|
MoveFeatureOperation operation = new MoveFeatureOperation(droppedNode.getUuid(), target.getUuid(), droppedNode.getTerm().getUuid(), position, getCurrentOperation(), editor, editor);
|
150
|
|
151
|
Object o = oldParent.getChildren().remove(currentPosition);
|
152
|
target.getChildNodes().add(position, droppedNode);
|
153
|
droppedNode.setParentUuid(target.getUuid());
|
154
|
editor.setDirty();
|
155
|
editor.addOperation(operation);
|
156
|
}
|
157
|
if(droppedObject instanceof TermNode){
|
158
|
if (((TermNode)droppedObject).getUuid() == null){
|
159
|
if(StoreUtil.promptCheckIsDirty(editor)){
|
160
|
return false;
|
161
|
}
|
162
|
}
|
163
|
TermNode droppedNode = (TermNode) droppedObject;
|
164
|
TermNode oldParent = droppedNode.getParent();
|
165
|
int currentPosition = oldParent.getIndex(droppedNode);
|
166
|
|
167
|
if(currentPosition<position && target.equals(oldParent)){
|
168
|
position -= 1;
|
169
|
}
|
170
|
MoveFeatureOperation operation = new MoveFeatureOperation(droppedNode.getUuid(), target.getUuid(), droppedNode.getTerm().getUuid(), position, getCurrentOperation(), editor, editor);
|
171
|
|
172
|
oldParent.removeChild(droppedNode);
|
173
|
target.addChild(droppedNode, position);
|
174
|
|
175
|
editor.setDirty();
|
176
|
editor.addOperation(operation);
|
177
|
}
|
178
|
else if(droppedObject instanceof Feature){
|
179
|
Feature droppedFeature = (Feature) droppedObject;
|
180
|
AddFeatureOperation operation = new AddFeatureOperation(
|
181
|
droppedFeature.getUuid(),
|
182
|
target.getUuid(), position, editor, editor);
|
183
|
target.addChild(droppedFeature, position);
|
184
|
editor.setDirty();
|
185
|
editor.addOperation(operation);
|
186
|
} else if(droppedObject instanceof OntologyTermWrapper){
|
187
|
OntologyTermWrapper wrapper = (OntologyTermWrapper)droppedObject;
|
188
|
TermVocabulary vocabulary = wrapper.getTermVocabulary();
|
189
|
if(vocabulary==null){
|
190
|
vocabulary = TermVocabularySelectionDialog.select(
|
191
|
Messages.FeatureTreeDropAdapter_CHOOSE_VOC, viewer.getControl().getShell(), null);
|
192
|
|
193
|
if(vocabulary instanceof OrderedTermVocabulary){
|
194
|
MessagingUtils.warningDialog(Messages.FeatureTreeDropAdapter_IMPORT_NOT_POSSIBLE, this,
|
195
|
Messages.FeatureTreeDropAdapter_ORDER_VOC_NOT_POSSIBLE);
|
196
|
return false;
|
197
|
}
|
198
|
if(vocabulary == null){
|
199
|
return false;
|
200
|
}
|
201
|
|
202
|
}
|
203
|
|
204
|
|
205
|
AddOntologyTermOperation operation = new AddOntologyTermOperation(wrapper, target.getUuid(), vocabulary, editor, editor);
|
206
|
|
207
|
editor.setDirty();
|
208
|
editor.addOperation(operation);
|
209
|
|
210
|
}
|
211
|
}
|
212
|
viewer.refresh();
|
213
|
|
214
|
return true;
|
215
|
}
|
216
|
|
217
|
@Override
|
218
|
public boolean validateDrop(Object target, int operation, TransferData transferData) {
|
219
|
boolean
|
220
|
isSupported = TermNodeTransfer.getInstance().isSupportedType(transferData);
|
221
|
isSupported |= TermTransfer.getInstance().isSupportedType(transferData);
|
222
|
isSupported |= LocalSelectionTransfer.getTransfer().isSupportedType(transferData);
|
223
|
isSupported &= getViewer().getInput()!=null;
|
224
|
if(target instanceof TermTree && getCurrentLocation()!=ViewerDropAdapter.LOCATION_ON){
|
225
|
isSupported = false;
|
226
|
}
|
227
|
return isSupported;
|
228
|
}
|
229
|
|
230
|
|
231
|
|
232
|
}
|