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
.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
.taxeditor
.editor
.definedterm
.TermTransfer
;
30 import eu
.etaxonomy
.taxeditor
.featuretree
.FeatureNodeTransfer
;
31 import eu
.etaxonomy
.taxeditor
.l10n
.Messages
;
32 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
33 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
34 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.TermVocabularySelectionDialog
;
35 import eu
.etaxonomy
.taxeditor
.view
.webimport
.termimport
.wrapper
.OntologyTermWrapper
;
37 public class FeatureNodeDropAdapter
extends ViewerDropAdapter
{
39 private MDirtyable dirtyable
;
41 public FeatureNodeDropAdapter(MDirtyable dirtyable
, Viewer viewer
) {
43 this.dirtyable
= dirtyable
;
47 public boolean performDrop(Object data
) {
48 Object currentTarget
= getCurrentTarget();
49 FeatureNode target
= null;
50 if(currentTarget
instanceof FeatureTree
){
51 target
= ((FeatureTree
) currentTarget
).getRoot();
53 else if(currentTarget
instanceof FeatureNode
){
54 target
= (FeatureNode
) currentTarget
;
59 int location
= getCurrentLocation();
60 FeatureNode parent
= target
.getParent();
61 if (location
== LOCATION_BEFORE
) {
62 position
= Math
.max(0, parent
.getIndex(target
) - 1);
66 if (location
== LOCATION_AFTER
) {
67 position
= parent
.getIndex(target
);
73 MessagingUtils
.warningDialog(Messages
.FeatureNodeDropAdapter_INVALID_TARGET
, this, Messages
.FeatureNodeDropAdapter_INVALID_TARGET_MESSAGE
);
77 Collection
<Object
> droppedObjects
= Collections
.emptyList();
78 if(data
instanceof Object
[]){
79 droppedObjects
= Arrays
.asList((Object
[])data
);
81 else if(data
instanceof IStructuredSelection
){
82 droppedObjects
= ((IStructuredSelection
) data
).toList();
84 TreeViewer viewer
= (TreeViewer
) getViewer();
86 // cannot drop a feature node onto itself
87 for (Object droppedObject
: droppedObjects
) {
88 if (droppedObject
.equals(target
)) {
92 for (Object droppedObject
: droppedObjects
) {
93 if(droppedObject
instanceof FeatureNode
){
94 FeatureNode droppedNode
= (FeatureNode
) droppedObject
;
95 CdmStore
.getService(IFeatureNodeService
.class).moveFeatureNode(droppedNode
.getUuid(), target
.getUuid(), position
);
96 viewer
.reveal(droppedNode
);
98 else if(droppedObject
instanceof Feature
){
99 Feature droppedFeature
= (Feature
) droppedObject
;
100 CdmStore
.getService(IFeatureNodeService
.class).addChildFeatureNode(target
.getUuid(), droppedFeature
.getUuid());
102 else if(droppedObject
instanceof OntologyTermWrapper
){
103 OntologyTermWrapper wrapper
= (OntologyTermWrapper
)droppedObject
;
104 TermVocabulary vocabulary
= TermVocabularySelectionDialog
.select(
105 "Choose vocabulary for import", viewer
.getControl().getShell(), null);
106 if(vocabulary
!=null){
107 Feature feature
= Feature
.NewInstance(wrapper
.getLabel(), wrapper
.getLabel(), wrapper
.getLabel());
108 feature
.setUri(URI
.create(wrapper
.getUri()));
109 vocabulary
.addTerm(feature
);
110 CdmStore
.getService(IFeatureNodeService
.class).createChildFeatureNode(target
, feature
);
114 dirtyable
.setDirty(true);
120 public boolean validateDrop(Object target
, int operation
, TransferData transferData
) {
121 boolean isSupported
= FeatureNodeTransfer
.getInstance().isSupportedType(transferData
);
122 isSupported
|= TermTransfer
.getInstance().isSupportedType(transferData
);
123 isSupported
|= LocalSelectionTransfer
.getTransfer().isSupportedType(transferData
);
124 isSupported
&= getViewer().getInput()!=null;