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.OrderedTermVocabulary;
|
26
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
27
|
import eu.etaxonomy.cdm.model.description.Feature;
|
28
|
import eu.etaxonomy.cdm.model.description.FeatureNode;
|
29
|
import eu.etaxonomy.cdm.model.description.FeatureTree;
|
30
|
import eu.etaxonomy.taxeditor.editor.definedterm.TermTransfer;
|
31
|
import eu.etaxonomy.taxeditor.featuretree.FeatureNodeTransfer;
|
32
|
import eu.etaxonomy.taxeditor.l10n.Messages;
|
33
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
34
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
35
|
import eu.etaxonomy.taxeditor.ui.dialog.selection.TermVocabularySelectionDialog;
|
36
|
import eu.etaxonomy.taxeditor.view.webimport.termimport.parser.ParserUtil;
|
37
|
import eu.etaxonomy.taxeditor.view.webimport.termimport.wrapper.OntologyTermWrapper;
|
38
|
|
39
|
public class FeatureNodeDropAdapter extends ViewerDropAdapter {
|
40
|
|
41
|
private MDirtyable dirtyable;
|
42
|
|
43
|
public FeatureNodeDropAdapter(MDirtyable dirtyable, Viewer viewer) {
|
44
|
super(viewer);
|
45
|
this.dirtyable = dirtyable;
|
46
|
}
|
47
|
|
48
|
@Override
|
49
|
public boolean performDrop(Object data) {
|
50
|
Object currentTarget = getCurrentTarget();
|
51
|
FeatureNode target = null;
|
52
|
if(currentTarget instanceof FeatureTree){
|
53
|
target = ((FeatureTree) currentTarget).getRoot();
|
54
|
}
|
55
|
else if(currentTarget instanceof FeatureNode){
|
56
|
target = (FeatureNode) currentTarget;
|
57
|
}
|
58
|
int position = 0;
|
59
|
|
60
|
if (target != null) {
|
61
|
int location = getCurrentLocation();
|
62
|
FeatureNode parent = target.getParent();
|
63
|
if (location == LOCATION_BEFORE) {
|
64
|
position = Math.max(0, parent.getIndex(target) - 1);
|
65
|
target = parent;
|
66
|
}
|
67
|
|
68
|
if (location == LOCATION_AFTER) {
|
69
|
position = parent.getIndex(target);
|
70
|
target = parent;
|
71
|
}
|
72
|
}
|
73
|
|
74
|
if(target==null){
|
75
|
MessagingUtils.warningDialog(Messages.FeatureNodeDropAdapter_INVALID_TARGET, this, Messages.FeatureNodeDropAdapter_INVALID_TARGET_MESSAGE);
|
76
|
return false;
|
77
|
}
|
78
|
|
79
|
Collection<Object> droppedObjects = Collections.emptyList();
|
80
|
if(data instanceof Object[]){
|
81
|
droppedObjects = Arrays.asList((Object[])data);
|
82
|
}
|
83
|
else if(data instanceof IStructuredSelection){
|
84
|
droppedObjects = ((IStructuredSelection) data).toList();
|
85
|
}
|
86
|
TreeViewer viewer = (TreeViewer) getViewer();
|
87
|
|
88
|
// cannot drop a feature node onto itself
|
89
|
for (Object droppedObject : droppedObjects) {
|
90
|
if (droppedObject.equals(target)) {
|
91
|
return false;
|
92
|
}
|
93
|
}
|
94
|
for (Object droppedObject : droppedObjects) {
|
95
|
if(droppedObject instanceof FeatureNode){
|
96
|
FeatureNode droppedNode = (FeatureNode) droppedObject;
|
97
|
CdmStore.getService(IFeatureNodeService.class).moveFeatureNode(droppedNode.getUuid(), target.getUuid(), position);
|
98
|
viewer.reveal(droppedNode);
|
99
|
}
|
100
|
else if(droppedObject instanceof Feature){
|
101
|
Feature droppedFeature = (Feature) droppedObject;
|
102
|
CdmStore.getService(IFeatureNodeService.class).addChildFeatureNode(target.getUuid(), droppedFeature.getUuid());
|
103
|
}
|
104
|
else if(droppedObject instanceof OntologyTermWrapper){
|
105
|
OntologyTermWrapper wrapper = (OntologyTermWrapper)droppedObject;
|
106
|
TermVocabulary vocabulary = TermVocabularySelectionDialog.select(
|
107
|
"Choose vocabulary for import", viewer.getControl().getShell(), null);
|
108
|
if(vocabulary instanceof OrderedTermVocabulary){
|
109
|
MessagingUtils.warningDialog("Import not possible", this,
|
110
|
"The chosen vocabulary is an ordered vocabulary.\n"
|
111
|
+ "Importing into ordered vocabularies is currently not supported.");
|
112
|
return false;
|
113
|
}
|
114
|
if(vocabulary!=null){
|
115
|
if(wrapper.getDescription()==null && MessagingUtils.confirmDialog("Load Details?", "Not all details for this term have been loaded yet.\n"
|
116
|
+ "Do you want to load the details before adding it?")){
|
117
|
ParserUtil.loadDetails(wrapper);
|
118
|
}
|
119
|
Feature feature = Feature.NewInstance(wrapper.getDescription(), wrapper.getLabel(), null);
|
120
|
feature.setUri(URI.create(wrapper.getUri()));
|
121
|
vocabulary.addTerm(feature);
|
122
|
CdmStore.getService(IFeatureNodeService.class).createChildFeatureNode(target, feature);
|
123
|
}
|
124
|
}
|
125
|
}
|
126
|
dirtyable.setDirty(true);
|
127
|
viewer.refresh();
|
128
|
return true;
|
129
|
}
|
130
|
|
131
|
@Override
|
132
|
public boolean validateDrop(Object target, int operation, TransferData transferData) {
|
133
|
boolean isSupported = FeatureNodeTransfer.getInstance().isSupportedType(transferData);
|
134
|
isSupported |= TermTransfer.getInstance().isSupportedType(transferData);
|
135
|
isSupported |= LocalSelectionTransfer.getTransfer().isSupportedType(transferData);
|
136
|
isSupported &= getViewer().getInput()!=null;
|
137
|
return isSupported;
|
138
|
}
|
139
|
|
140
|
}
|