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
.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
;
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
.wrapper
.OntologyTermWrapper
;
38 public class FeatureNodeDropAdapter
extends ViewerDropAdapter
{
40 private MDirtyable dirtyable
;
42 public FeatureNodeDropAdapter(MDirtyable dirtyable
, Viewer viewer
) {
44 this.dirtyable
= dirtyable
;
48 public boolean performDrop(Object data
) {
49 Object currentTarget
= getCurrentTarget();
50 FeatureNode target
= null;
51 if(currentTarget
instanceof FeatureTree
){
52 target
= ((FeatureTree
) currentTarget
).getRoot();
54 else if(currentTarget
instanceof FeatureNode
){
55 target
= (FeatureNode
) currentTarget
;
60 int location
= getCurrentLocation();
61 FeatureNode parent
= target
.getParent();
63 if (location
== LOCATION_BEFORE
) {
64 position
= Math
.max(0, parent
.getIndex(target
) - 1);
68 if (location
== LOCATION_AFTER
) {
69 position
= parent
.getIndex(target
);
76 MessagingUtils
.warningDialog(Messages
.FeatureNodeDropAdapter_INVALID_TARGET
, this, Messages
.FeatureNodeDropAdapter_INVALID_TARGET_MESSAGE
);
79 Collection
<Object
> droppedObjects
= Collections
.emptyList();
80 if(data
instanceof Object
[]){
81 droppedObjects
= Arrays
.asList((Object
[])data
);
83 else if(data
instanceof IStructuredSelection
){
84 droppedObjects
= ((IStructuredSelection
) data
).toList();
86 TreeViewer viewer
= (TreeViewer
) getViewer();
88 // cannot drop a feature node onto itself
89 for (Object droppedObject
: droppedObjects
) {
90 if(droppedObject
==null){
91 MessagingUtils
.warningDialog("Move failed", this.getClass(),
92 "Moving the feature node failed. Try saving before.");
95 if (droppedObject
.equals(target
)) {
99 for (Object droppedObject
: droppedObjects
) {
100 if(droppedObject
instanceof FeatureNode
){
101 FeatureNode droppedNode
= (FeatureNode
) droppedObject
;
102 CdmStore
.getService(IFeatureNodeService
.class).moveFeatureNode(droppedNode
.getUuid(), target
.getUuid(), position
);
103 viewer
.reveal(droppedNode
);
105 else if(droppedObject
instanceof Feature
){
106 Feature droppedFeature
= (Feature
) droppedObject
;
107 CdmStore
.getService(IFeatureNodeService
.class).addChildFeatureNode(target
.getUuid(), droppedFeature
.getUuid());
109 else if(droppedObject
instanceof OntologyTermWrapper
){
110 OntologyTermWrapper wrapper
= (OntologyTermWrapper
)droppedObject
;
111 TermVocabulary vocabulary
= wrapper
.getTermVocabulary();
112 if(vocabulary
==null){
113 vocabulary
= TermVocabularySelectionDialog
.select(
114 "Choose vocabulary for import", viewer
.getControl().getShell(), null);
115 if(vocabulary
instanceof OrderedTermVocabulary
){
116 MessagingUtils
.warningDialog("Import not possible", this,
117 "The chosen vocabulary is an ordered vocabulary.\n"
118 + "Importing into ordered vocabularies is currently not supported.");
122 if(vocabulary
==null){
126 Feature feature
= Feature
.NewInstance(wrapper
.getDescription(), wrapper
.getLabel(), null);
127 feature
.setUri(URI
.create(wrapper
.getUri()));
128 vocabulary
.addTerm(feature
);
129 CdmStore
.getService(IFeatureNodeService
.class).createChildFeatureNode(target
, feature
);
133 dirtyable
.setDirty(true);
139 public boolean validateDrop(Object target
, int operation
, TransferData transferData
) {
140 boolean isSupported
= FeatureNodeTransfer
.getInstance().isSupportedType(transferData
);
141 isSupported
|= TermTransfer
.getInstance().isSupportedType(transferData
);
142 isSupported
|= LocalSelectionTransfer
.getTransfer().isSupportedType(transferData
);
143 isSupported
&= getViewer().getInput()!=null;