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.featuretree.e4;
|
10
|
|
11
|
import java.net.URI;
|
12
|
import java.util.Arrays;
|
13
|
import java.util.Collection;
|
14
|
import java.util.Collections;
|
15
|
|
16
|
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
|
17
|
import org.eclipse.jface.util.LocalSelectionTransfer;
|
18
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
19
|
import org.eclipse.jface.viewers.TreeViewer;
|
20
|
import org.eclipse.jface.viewers.Viewer;
|
21
|
import org.eclipse.jface.viewers.ViewerDropAdapter;
|
22
|
import org.eclipse.swt.dnd.TransferData;
|
23
|
|
24
|
import eu.etaxonomy.cdm.api.service.IFeatureNodeService;
|
25
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
26
|
import eu.etaxonomy.cdm.model.description.Feature;
|
27
|
import eu.etaxonomy.cdm.model.description.FeatureNode;
|
28
|
import eu.etaxonomy.cdm.model.description.FeatureTree;
|
29
|
import eu.etaxonomy.taxeditor.editor.definedterm.TermTransfer;
|
30
|
import eu.etaxonomy.taxeditor.featuretree.FeatureNodeTransfer;
|
31
|
import eu.etaxonomy.taxeditor.l10n.Messages;
|
32
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
33
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
34
|
import eu.etaxonomy.taxeditor.ui.dialog.selection.TermVocabularySelectionDialog;
|
35
|
import eu.etaxonomy.taxeditor.view.webimport.termimport.wrapper.OntologyTermWrapper;
|
36
|
|
37
|
public class FeatureNodeDropAdapter extends ViewerDropAdapter {
|
38
|
|
39
|
private MDirtyable dirtyable;
|
40
|
|
41
|
public FeatureNodeDropAdapter(MDirtyable dirtyable, Viewer viewer) {
|
42
|
super(viewer);
|
43
|
this.dirtyable = dirtyable;
|
44
|
}
|
45
|
|
46
|
@Override
|
47
|
public boolean performDrop(Object data) {
|
48
|
Object currentTarget = getCurrentTarget();
|
49
|
FeatureNode target = null;
|
50
|
if(currentTarget instanceof FeatureTree){
|
51
|
target = ((FeatureTree) currentTarget).getRoot();
|
52
|
}
|
53
|
else if(currentTarget instanceof FeatureNode){
|
54
|
target = (FeatureNode) currentTarget;
|
55
|
}
|
56
|
int position = 0;
|
57
|
|
58
|
if (target != null) {
|
59
|
int location = getCurrentLocation();
|
60
|
FeatureNode parent = target.getParent();
|
61
|
if (location == LOCATION_BEFORE) {
|
62
|
position = Math.max(0, parent.getIndex(target) - 1);
|
63
|
target = parent;
|
64
|
}
|
65
|
|
66
|
if (location == LOCATION_AFTER) {
|
67
|
position = parent.getIndex(target);
|
68
|
target = parent;
|
69
|
}
|
70
|
}
|
71
|
|
72
|
if(target==null){
|
73
|
MessagingUtils.warningDialog(Messages.FeatureNodeDropAdapter_INVALID_TARGET, this, Messages.FeatureNodeDropAdapter_INVALID_TARGET_MESSAGE);
|
74
|
return false;
|
75
|
}
|
76
|
|
77
|
Collection<Object> droppedObjects = Collections.emptyList();
|
78
|
if(data instanceof Object[]){
|
79
|
droppedObjects = Arrays.asList((Object[])data);
|
80
|
}
|
81
|
else if(data instanceof IStructuredSelection){
|
82
|
droppedObjects = ((IStructuredSelection) data).toList();
|
83
|
}
|
84
|
TreeViewer viewer = (TreeViewer) getViewer();
|
85
|
|
86
|
// cannot drop a feature node onto itself
|
87
|
for (Object droppedObject : droppedObjects) {
|
88
|
if (droppedObject.equals(target)) {
|
89
|
return false;
|
90
|
}
|
91
|
}
|
92
|
for (Object droppedObject : droppedObjects) {
|
93
|
if(droppedObject instanceof FeatureNode){
|
94
|
FeatureNode droppedNode = (FeatureNode) droppedObject;
|
95
|
CdmStore.getService(IFeatureNodeService.class).moveFeatureNode(droppedNode.getUuid(), target.getUuid(), position);
|
96
|
viewer.reveal(droppedNode);
|
97
|
}
|
98
|
else if(droppedObject instanceof Feature){
|
99
|
Feature droppedFeature = (Feature) droppedObject;
|
100
|
CdmStore.getService(IFeatureNodeService.class).addChildFeatureNode(target.getUuid(), droppedFeature.getUuid());
|
101
|
}
|
102
|
else if(droppedObject instanceof OntologyTermWrapper){
|
103
|
OntologyTermWrapper wrapper = (OntologyTermWrapper)droppedObject;
|
104
|
TermVocabulary vocabulary = TermVocabularySelectionDialog.select(
|
105
|
"Choose vocabulary for import", viewer.getControl().getShell(), null);
|
106
|
if(vocabulary!=null){
|
107
|
Feature feature = Feature.NewInstance(wrapper.getLabel(), wrapper.getLabel(), wrapper.getLabel());
|
108
|
feature.setUri(URI.create(wrapper.getUri()));
|
109
|
vocabulary.addTerm(feature);
|
110
|
CdmStore.getService(IFeatureNodeService.class).createChildFeatureNode(target, feature);
|
111
|
}
|
112
|
}
|
113
|
}
|
114
|
dirtyable.setDirty(true);
|
115
|
viewer.refresh();
|
116
|
return true;
|
117
|
}
|
118
|
|
119
|
@Override
|
120
|
public boolean validateDrop(Object target, int operation, TransferData transferData) {
|
121
|
boolean isSupported = FeatureNodeTransfer.getInstance().isSupportedType(transferData);
|
122
|
isSupported |= TermTransfer.getInstance().isSupportedType(transferData);
|
123
|
isSupported |= LocalSelectionTransfer.getTransfer().isSupportedType(transferData);
|
124
|
isSupported &= getViewer().getInput()!=null;
|
125
|
return isSupported;
|
126
|
}
|
127
|
|
128
|
}
|