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
.common
.OrderedTermVocabulary
;
24 import eu
.etaxonomy
.cdm
.model
.common
.TermType
;
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
.cdm
.persistence
.dto
.TermDto
;
30 import eu
.etaxonomy
.taxeditor
.editor
.definedterm
.TermTransfer
;
31 import eu
.etaxonomy
.taxeditor
.editor
.definedterm
.e4
.DefinedTermDropAdapterE4
;
32 import eu
.etaxonomy
.taxeditor
.featuretree
.FeatureNodeTransfer
;
33 import eu
.etaxonomy
.taxeditor
.featuretree
.e4
.operation
.AddFeatureOperation
;
34 import eu
.etaxonomy
.taxeditor
.featuretree
.e4
.operation
.AddOntologyTermOperation
;
35 import eu
.etaxonomy
.taxeditor
.featuretree
.e4
.operation
.MoveFeatureOperation
;
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
.StoreUtil
;
40 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.TermVocabularySelectionDialog
;
41 import eu
.etaxonomy
.taxeditor
.view
.webimport
.termimport
.wrapper
.OntologyTermWrapper
;
43 public class FeatureTreeDropAdapter
extends ViewerDropAdapter
{
45 protected IFeatureTreeEditor editor
;
46 private UISynchronize sync
;
48 public FeatureTreeDropAdapter(IFeatureTreeEditor editor
, Viewer viewer
, UISynchronize sync
) {
55 public boolean performDrop(Object data
) {
56 if(StoreUtil
.promptCheckIsDirty(editor
)){
59 Object currentTarget
= getCurrentTarget();
60 FeatureNode target
= null;
61 if(currentTarget
instanceof FeatureTree
){
62 target
= ((FeatureTree
) currentTarget
).getRoot();
64 else if(currentTarget
instanceof FeatureNode
){
65 target
= (FeatureNode
) currentTarget
;
70 int location
= getCurrentLocation();
71 FeatureNode parent
= target
.getParent();
73 if (location
== LOCATION_BEFORE
) {
74 position
= Math
.max(0, parent
.getIndex(target
));
78 if (location
== LOCATION_AFTER
) {
79 position
= parent
.getIndex(target
)+1;
86 MessagingUtils
.warningDialog("", this, ""); //$NON-NLS-1$ //$NON-NLS-1$
89 Collection
<Object
> droppedObjects
= Collections
.emptyList();
90 if(data
instanceof Object
[]){
91 droppedObjects
= Arrays
.asList((Object
[])data
);
93 else if(data
instanceof IStructuredSelection
){
94 droppedObjects
= ((IStructuredSelection
) data
).toList();
96 TreeViewer viewer
= (TreeViewer
) getViewer();
98 // cannot drop a feature node onto itself
99 for (Object droppedObject
: droppedObjects
) {
100 if(droppedObject
==null){
101 MessagingUtils
.warningDialog(DefinedTermDropAdapterE4
.MOVE_FAILED
, this.getClass(),
102 Messages
.FeatureTreeDropAdapter_MOVE_FAILED_SAVE_MESSAGE
);
105 if (droppedObject
.equals(target
)) {
109 for (Object droppedObject
: droppedObjects
) {
110 if(droppedObject
instanceof FeatureNode
){
111 FeatureNode droppedNode
= (FeatureNode
) droppedObject
;
112 int currentPosition
= target
.getIndex(droppedNode
);
113 if(currentPosition
<position
){
116 MoveFeatureOperation operation
= new MoveFeatureOperation(droppedNode
, target
, position
, getCurrentOperation(), editor
, editor
);
117 AbstractUtility
.executeOperation(operation
, sync
);
119 else if(droppedObject
instanceof Feature
){
120 Feature droppedFeature
= (Feature
) droppedObject
;
121 AddFeatureOperation operation
= new AddFeatureOperation(
122 droppedFeature
.getUuid(),
123 target
, position
, editor
, editor
);
124 AbstractUtility
.executeOperation(operation
, sync
);
126 else if(droppedObject
instanceof TermDto
){
127 TermDto termDto
= (TermDto
) droppedObject
;
128 if(termDto
.getTermType().equals(TermType
.Feature
)){
129 AddFeatureOperation operation
= new AddFeatureOperation(
131 target
, position
, editor
, editor
);
132 AbstractUtility
.executeOperation(operation
, sync
);
135 MessagingUtils
.warningDialog(DefinedTermDropAdapterE4
.MOVE_FAILED
, this, Messages
.FeatureTreeDropAdapter_ONLY_MOVE_FEATURES
);
138 else if(droppedObject
instanceof OntologyTermWrapper
){
139 OntologyTermWrapper wrapper
= (OntologyTermWrapper
)droppedObject
;
140 TermVocabulary vocabulary
= wrapper
.getTermVocabulary();
141 if(vocabulary
==null){
142 vocabulary
= TermVocabularySelectionDialog
.select(
143 Messages
.FeatureTreeDropAdapter_CHOOSE_VOC
, viewer
.getControl().getShell(), null);
144 if(vocabulary
instanceof OrderedTermVocabulary
){
145 MessagingUtils
.warningDialog(Messages
.FeatureTreeDropAdapter_IMPORT_NOT_POSSIBLE
, this,
146 Messages
.FeatureTreeDropAdapter_ORDER_VOC_NOT_POSSIBLE
);
150 if(vocabulary
==null){
154 AddOntologyTermOperation operation
= new AddOntologyTermOperation(wrapper
, target
, vocabulary
, editor
, editor
);
155 AbstractUtility
.executeOperation(operation
, sync
);
164 public boolean validateDrop(Object target
, int operation
, TransferData transferData
) {
166 isSupported
= FeatureNodeTransfer
.getInstance().isSupportedType(transferData
);
167 isSupported
|= TermTransfer
.getInstance().isSupportedType(transferData
);
168 isSupported
|= LocalSelectionTransfer
.getTransfer().isSupportedType(transferData
);
169 isSupported
&= getViewer().getInput()!=null;
170 if(target
instanceof FeatureTree
&& getCurrentLocation()!=ViewerDropAdapter
.LOCATION_ON
){