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
;
11 import java
.util
.Arrays
;
12 import java
.util
.Collection
;
13 import java
.util
.Collections
;
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
;
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
.featuretree
.FeatureNodeTransfer
;
35 import eu
.etaxonomy
.taxeditor
.featuretree
.e4
.operation
.AddFeatureOperation
;
36 import eu
.etaxonomy
.taxeditor
.featuretree
.e4
.operation
.AddOntologyTermOperation
;
37 import eu
.etaxonomy
.taxeditor
.featuretree
.e4
.operation
.MoveFeatureOperation
;
38 import eu
.etaxonomy
.taxeditor
.l10n
.Messages
;
39 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
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 TermNode target
= null;
62 if(currentTarget
instanceof TermTree
){
63 target
= ((TermTree
) currentTarget
).getRoot();
65 else if(currentTarget
instanceof TermNode
){
66 target
= (TermNode
) currentTarget
;
69 if (target
.getUuid() == null){
70 if(StoreUtil
.promptCheckIsDirty(editor
)){
77 int location
= getCurrentLocation();
78 TermNode parent
= target
.getParent();
81 if (location
== LOCATION_BEFORE
) {
82 position
= Math
.max(0, parent
.getIndex(target
));
86 if (location
== LOCATION_AFTER
) {
87 position
= parent
.getIndex(target
)+1;
94 MessagingUtils
.warningDialog("", this, ""); //$NON-NLS-1$ //$NON-NLS-1$
97 Collection
<Object
> droppedObjects
= Collections
.emptyList();
98 if(data
instanceof Object
[]){
99 droppedObjects
= Arrays
.asList((Object
[])data
);
101 else if(data
instanceof IStructuredSelection
){
102 droppedObjects
= ((IStructuredSelection
) data
).toList();
104 TreeViewer viewer
= (TreeViewer
) getViewer();
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
);
113 if (droppedObject
.equals(target
)) {
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
);
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
);
136 if(droppedObject
instanceof TermNodeDto
){
137 if (((TermNodeDto
)droppedObject
).getUuid() == null){
138 if(StoreUtil
.promptCheckIsDirty(editor
)){
142 TermNodeDto droppedNode
= (TermNodeDto
) droppedObject
;
143 TermNodeDto oldParent
= editor
.getNodeDtoForUuid(droppedNode
.getParentUuid());
144 int currentPosition
= oldParent
.getIndex(droppedNode
);
146 if(currentPosition
<position
&& target
.equals(oldParent
)){
149 MoveFeatureOperation operation
= new MoveFeatureOperation(droppedNode
.getUuid(), target
.getUuid(), droppedNode
.getTerm().getUuid(), position
, getCurrentOperation(), editor
, editor
);
151 Object o
= oldParent
.getChildren().remove(currentPosition
);
152 target
.getChildNodes().add(position
, droppedNode
);
153 droppedNode
.setParentUuid(target
.getUuid());
155 editor
.addOperation(operation
);
157 if(droppedObject
instanceof TermNode
){
158 if (((TermNode
)droppedObject
).getUuid() == null){
159 if(StoreUtil
.promptCheckIsDirty(editor
)){
163 TermNode droppedNode
= (TermNode
) droppedObject
;
164 TermNode oldParent
= droppedNode
.getParent();
165 int currentPosition
= oldParent
.getIndex(droppedNode
);
167 if(currentPosition
<position
&& target
.equals(oldParent
)){
170 MoveFeatureOperation operation
= new MoveFeatureOperation(droppedNode
.getUuid(), target
.getUuid(), droppedNode
.getTerm().getUuid(), position
, getCurrentOperation(), editor
, editor
);
172 oldParent
.removeChild(droppedNode
);
173 target
.addChild(droppedNode
, position
);
176 editor
.addOperation(operation
);
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 // AbstractUtility.executeOperation(operation, sync);
184 target
.addChild(droppedFeature
, position
);
186 editor
.addOperation(operation
);
187 } else if(droppedObject
instanceof OntologyTermWrapper
){
188 OntologyTermWrapper wrapper
= (OntologyTermWrapper
)droppedObject
;
189 TermVocabulary vocabulary
= wrapper
.getTermVocabulary();
190 if(vocabulary
==null){
191 vocabulary
= TermVocabularySelectionDialog
.select(
192 Messages
.FeatureTreeDropAdapter_CHOOSE_VOC
, viewer
.getControl().getShell(), null);
194 if(vocabulary
instanceof OrderedTermVocabulary
){
195 MessagingUtils
.warningDialog(Messages
.FeatureTreeDropAdapter_IMPORT_NOT_POSSIBLE
, this,
196 Messages
.FeatureTreeDropAdapter_ORDER_VOC_NOT_POSSIBLE
);
199 if(vocabulary
== null){
202 // vocabulary = new TermVocabulary(voc.getUuid(), voc.getRepresentations(), voc.getTermType(), voc.getTitleCache(), voc.isAllowDuplicates(), voc.isOrderRelevant(), voc.isFlat());
207 AddOntologyTermOperation operation
= new AddOntologyTermOperation(wrapper
, target
.getUuid(), vocabulary
, editor
, editor
);
208 // AbstractUtility.executeOperation(operation, sync);
210 editor
.addOperation(operation
);
220 public boolean validateDrop(Object target
, int operation
, TransferData transferData
) {
222 isSupported
= FeatureNodeTransfer
.getInstance().isSupportedType(transferData
);
223 isSupported
|= TermTransfer
.getInstance().isSupportedType(transferData
);
224 isSupported
|= LocalSelectionTransfer
.getTransfer().isSupportedType(transferData
);
225 isSupported
&= getViewer().getInput()!=null;
226 if(target
instanceof TermTree
&& getCurrentLocation()!=ViewerDropAdapter
.LOCATION_ON
){