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 org
.eclipse
.e4
.ui
.model
.application
.ui
.MDirtyable
;
12 import org
.eclipse
.jface
.viewers
.TreeViewer
;
13 import org
.eclipse
.jface
.viewers
.Viewer
;
14 import org
.eclipse
.jface
.viewers
.ViewerDropAdapter
;
15 import org
.eclipse
.swt
.dnd
.TransferData
;
17 import eu
.etaxonomy
.cdm
.api
.service
.IFeatureNodeService
;
18 import eu
.etaxonomy
.cdm
.model
.description
.FeatureNode
;
19 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
20 import eu
.etaxonomy
.taxeditor
.featuretree
.FeatureNodeTransfer
;
21 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
23 public class FeatureNodeDropAdapter
extends ViewerDropAdapter
{
25 private final MDirtyable dirtyable
;
27 public FeatureNodeDropAdapter(MDirtyable dirtyable
, Viewer viewer
) {
29 this.dirtyable
= dirtyable
;
33 public boolean performDrop(Object data
) {
34 FeatureNode target
= (FeatureNode
) getCurrentTarget();
38 int location
= getCurrentLocation();
39 FeatureNode parent
= target
.getParent();
40 if (location
== LOCATION_BEFORE
) {
41 position
= Math
.max(0, parent
.getIndex(target
) - 1);
45 if (location
== LOCATION_AFTER
) {
46 position
= parent
.getIndex(target
);
51 // set target to root node if there is no target specified
53 FeatureTree featureTree
= (FeatureTree
) getViewer().getInput();
54 target
= featureTree
.getRoot();
57 Object
[] droppedObjects
= (Object
[]) data
;
58 TreeViewer viewer
= (TreeViewer
) getViewer();
60 // cannot drop a feature node onto itself
61 for (Object droppedObject
: droppedObjects
) {
62 if (droppedObject
.equals(target
)) {
66 for (Object droppedObject
: droppedObjects
) {
67 FeatureNode droppedNode
= (FeatureNode
) droppedObject
;
68 CdmStore
.getService(IFeatureNodeService
.class).moveFeatureNode(droppedNode
.getUuid(), target
.getUuid(), position
);
70 viewer
.reveal(droppedNode
);
72 this.dirtyable
.setDirty(true);
77 public boolean validateDrop(Object target
, int operation
, TransferData transferData
) {
78 return FeatureNodeTransfer
.getInstance().isSupportedType(transferData
);