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
.TransferData
;
24 import eu
.etaxonomy
.cdm
.api
.service
.IFeatureNodeService
;
25 import eu
.etaxonomy
.cdm
.api
.service
.ITermService
;
26 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
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
.editor
.definedterm
.e4
.DefinedTermDropAdapterE4
;
35 import eu
.etaxonomy
.taxeditor
.featuretree
.FeatureNodeTransfer
;
36 import eu
.etaxonomy
.taxeditor
.featuretree
.e4
.operation
.AddFeatureOperation
;
37 import eu
.etaxonomy
.taxeditor
.featuretree
.e4
.operation
.MoveFeatureOperation
;
38 import eu
.etaxonomy
.taxeditor
.l10n
.Messages
;
39 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
40 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
41 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
42 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
43 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.TermVocabularySelectionDialog
;
44 import eu
.etaxonomy
.taxeditor
.view
.webimport
.termimport
.wrapper
.OntologyTermWrapper
;
46 public class FeatureTreeDropAdapter
extends ViewerDropAdapter
{
48 protected IFeatureTreeEditor editor
;
49 private UISynchronize sync
;
51 public FeatureTreeDropAdapter(IFeatureTreeEditor editor
, Viewer viewer
, UISynchronize sync
) {
58 public boolean performDrop(Object data
) {
59 if(StoreUtil
.promptCheckIsDirty(editor
)){
62 Object currentTarget
= getCurrentTarget();
63 FeatureNode target
= null;
64 if(currentTarget
instanceof FeatureTree
){
65 target
= ((FeatureTree
) currentTarget
).getRoot();
67 else if(currentTarget
instanceof FeatureNode
){
68 target
= (FeatureNode
) currentTarget
;
73 int location
= getCurrentLocation();
74 FeatureNode parent
= target
.getParent();
76 if (location
== LOCATION_BEFORE
) {
77 position
= Math
.max(0, parent
.getIndex(target
) - 1);
81 if (location
== LOCATION_AFTER
) {
82 position
= parent
.getIndex(target
);
89 MessagingUtils
.warningDialog("", this, ""); //$NON-NLS-1$ //$NON-NLS-1$
92 Collection
<Object
> droppedObjects
= Collections
.emptyList();
93 if(data
instanceof Object
[]){
94 droppedObjects
= Arrays
.asList((Object
[])data
);
96 else if(data
instanceof IStructuredSelection
){
97 droppedObjects
= ((IStructuredSelection
) data
).toList();
99 TreeViewer viewer
= (TreeViewer
) getViewer();
101 // cannot drop a feature node onto itself
102 for (Object droppedObject
: droppedObjects
) {
103 if(droppedObject
==null){
104 MessagingUtils
.warningDialog(DefinedTermDropAdapterE4
.MOVE_FAILED
, this.getClass(),
105 Messages
.FeatureTreeDropAdapter_MOVE_FAILED_SAVE_MESSAGE
);
108 if (droppedObject
.equals(target
)) {
112 for (Object droppedObject
: droppedObjects
) {
113 if(droppedObject
instanceof FeatureNode
){
114 FeatureNode droppedNode
= (FeatureNode
) droppedObject
;
115 MoveFeatureOperation operation
= new MoveFeatureOperation(droppedNode
, target
, position
, getCurrentOperation(), editor
, editor
);
116 AbstractUtility
.executeOperation(operation
, sync
);
118 else if(droppedObject
instanceof Feature
){
119 Feature droppedFeature
= (Feature
) droppedObject
;
120 AddFeatureOperation operation
= new AddFeatureOperation(
121 (Feature
) CdmStore
.getService(ITermService
.class).load(droppedFeature
.getUuid()),
122 target
, editor
, editor
);
123 AbstractUtility
.executeOperation(operation
, sync
);
125 else if(droppedObject
instanceof TermDto
){
126 TermDto termDto
= (TermDto
) droppedObject
;
127 DefinedTermBase term
= CdmStore
.getService(ITermService
.class).load(termDto
.getUuid());
128 if(term
.isInstanceOf(Feature
.class)){
129 AddFeatureOperation operation
= new AddFeatureOperation(
131 target
, 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 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
) {
168 isSupported
= FeatureNodeTransfer
.getInstance().isSupportedType(transferData
);
169 isSupported
|= TermTransfer
.getInstance().isSupportedType(transferData
);
170 isSupported
|= LocalSelectionTransfer
.getTransfer().isSupportedType(transferData
);
171 isSupported
&= getViewer().getInput()!=null;