2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.featuretree
.e4
;
12 import java
.util
.Arrays
;
13 import java
.util
.Collection
;
14 import java
.util
.Collections
;
16 import org
.eclipse
.e4
.ui
.di
.UISynchronize
;
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
.DND
;
23 import org
.eclipse
.swt
.dnd
.TransferData
;
25 import eu
.etaxonomy
.cdm
.api
.service
.IFeatureNodeService
;
26 import eu
.etaxonomy
.cdm
.api
.service
.ITermService
;
27 import eu
.etaxonomy
.cdm
.model
.common
.OrderedTermVocabulary
;
28 import eu
.etaxonomy
.cdm
.model
.common
.TermVocabulary
;
29 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
30 import eu
.etaxonomy
.cdm
.model
.description
.FeatureNode
;
31 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
32 import eu
.etaxonomy
.cdm
.persistence
.dto
.TermDto
;
33 import eu
.etaxonomy
.taxeditor
.editor
.definedterm
.TermTransfer
;
34 import eu
.etaxonomy
.taxeditor
.featuretree
.FeatureNodeTransfer
;
35 import eu
.etaxonomy
.taxeditor
.featuretree
.e4
.operation
.AddFeatureOperation
;
36 import eu
.etaxonomy
.taxeditor
.l10n
.Messages
;
37 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
38 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
39 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
40 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
41 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.TermVocabularySelectionDialog
;
42 import eu
.etaxonomy
.taxeditor
.view
.webimport
.termimport
.wrapper
.OntologyTermWrapper
;
44 public class FeatureTreeDropAdapter
extends ViewerDropAdapter
{
46 protected IFeatureTreeEditor editor
;
47 private UISynchronize sync
;
49 public FeatureTreeDropAdapter(IFeatureTreeEditor editor
, Viewer viewer
, UISynchronize sync
) {
56 public boolean performDrop(Object data
) {
57 if(StoreUtil
.promptCheckIsDirty(editor
)){
60 Object currentTarget
= getCurrentTarget();
61 FeatureNode target
= null;
62 if(currentTarget
instanceof FeatureTree
){
63 target
= ((FeatureTree
) currentTarget
).getRoot();
65 else if(currentTarget
instanceof FeatureNode
){
66 target
= (FeatureNode
) currentTarget
;
71 int location
= getCurrentLocation();
72 FeatureNode parent
= target
.getParent();
74 if (location
== LOCATION_BEFORE
) {
75 position
= Math
.max(0, parent
.getIndex(target
) - 1);
79 if (location
== LOCATION_AFTER
) {
80 position
= parent
.getIndex(target
);
87 MessagingUtils
.warningDialog(Messages
.FeatureNodeDropAdapter_INVALID_TARGET
, this, Messages
.FeatureNodeDropAdapter_INVALID_TARGET_MESSAGE
);
90 Collection
<Object
> droppedObjects
= Collections
.emptyList();
91 if(data
instanceof Object
[]){
92 droppedObjects
= Arrays
.asList((Object
[])data
);
94 else if(data
instanceof IStructuredSelection
){
95 droppedObjects
= ((IStructuredSelection
) data
).toList();
97 TreeViewer viewer
= (TreeViewer
) getViewer();
99 // cannot drop a feature node onto itself
100 for (Object droppedObject
: droppedObjects
) {
101 if(droppedObject
==null){
102 MessagingUtils
.warningDialog("Move failed", this.getClass(),
103 "Moving the feature node failed. Try saving before.");
106 if (droppedObject
.equals(target
)) {
110 for (Object droppedObject
: droppedObjects
) {
111 if(droppedObject
instanceof FeatureNode
){
112 FeatureNode droppedNode
= (FeatureNode
) droppedObject
;
114 if(getCurrentOperation()==DND
.DROP_MOVE
){
115 CdmStore
.getService(IFeatureNodeService
.class).moveFeatureNode(droppedNode
.getUuid(), target
.getUuid(), position
);
118 else if(getCurrentOperation()==DND
.DROP_COPY
){
119 CdmStore
.getService(IFeatureNodeService
.class).addChildFeatureNode(target
.getUuid(), droppedNode
.getFeature().getUuid());
121 viewer
.reveal(droppedNode
);
123 else if(droppedObject
instanceof Feature
){
124 Feature droppedFeature
= (Feature
) droppedObject
;
125 AddFeatureOperation operation
= new AddFeatureOperation(
126 (Feature
) CdmStore
.getService(ITermService
.class).load(droppedFeature
.getUuid()),
127 target
, editor
, editor
);
128 AbstractUtility
.executeOperation(operation
, sync
);
130 else if(droppedObject
instanceof TermDto
){
131 TermDto termDto
= (TermDto
) droppedObject
;
132 AddFeatureOperation operation
= new AddFeatureOperation(
133 (Feature
) CdmStore
.getService(ITermService
.class).load(termDto
.getUuid()),
134 target
, editor
, editor
);
135 AbstractUtility
.executeOperation(operation
, sync
);
137 else if(droppedObject
instanceof OntologyTermWrapper
){
138 OntologyTermWrapper wrapper
= (OntologyTermWrapper
)droppedObject
;
139 TermVocabulary vocabulary
= wrapper
.getTermVocabulary();
140 if(vocabulary
==null){
141 vocabulary
= TermVocabularySelectionDialog
.select(
142 "Choose vocabulary for import", viewer
.getControl().getShell(), null);
143 if(vocabulary
instanceof OrderedTermVocabulary
){
144 MessagingUtils
.warningDialog("Import not possible", this,
145 "The chosen vocabulary is an ordered vocabulary.\n"
146 + "Importing into ordered vocabularies is currently not supported.");
150 if(vocabulary
==null){
154 Feature feature
= Feature
.NewInstance(wrapper
.getDescription(), wrapper
.getLabel(), null);
155 feature
.setUri(URI
.create(wrapper
.getUri()));
156 vocabulary
.addTerm(feature
);
157 CdmStore
.getService(IFeatureNodeService
.class).createChildFeatureNode(target
, feature
);
166 public boolean validateDrop(Object target
, int operation
, TransferData transferData
) {
167 boolean isSupported
= FeatureNodeTransfer
.getInstance().isSupportedType(transferData
);
168 isSupported
|= TermTransfer
.getInstance().isSupportedType(transferData
);
169 isSupported
|= LocalSelectionTransfer
.getTransfer().isSupportedType(transferData
);
170 isSupported
|= TermTransfer
.getInstance().isSupportedType(transferData
);
171 isSupported
&= getViewer().getInput()!=null;