Merge branch 'develop' into featureTreeEditor
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / featuretree / FeatureNodeDropAdapter.java
1 /**
2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9 package eu.etaxonomy.taxeditor.featuretree;
10
11 import org.eclipse.jface.viewers.TreeViewer;
12 import org.eclipse.jface.viewers.Viewer;
13 import org.eclipse.jface.viewers.ViewerDropAdapter;
14 import org.eclipse.swt.dnd.TransferData;
15
16 import eu.etaxonomy.cdm.model.description.FeatureNode;
17 import eu.etaxonomy.cdm.model.description.FeatureTree;
18 import eu.etaxonomy.taxeditor.model.MessagingUtils;
19
20 /**
21 * @author pplitzner
22 * @since Jun 2, 2017
23 *
24 */
25 public class FeatureNodeDropAdapter extends ViewerDropAdapter {
26
27 public FeatureNodeDropAdapter(Viewer viewer) {
28 super(viewer);
29 }
30
31 @Override
32 public boolean performDrop(Object data) {
33 FeatureNode target = (FeatureNode) getCurrentTarget();
34 int position = 0;
35
36 if (target != null) {
37 int location = getCurrentLocation();
38 FeatureNode parent = target.getParent();
39 if (location == LOCATION_BEFORE) {
40 position = Math.max(0, parent.getIndex(target) - 1);
41 target = parent;
42 }
43
44 if (location == LOCATION_AFTER) {
45 position = parent.getIndex(target);
46 target = parent;
47 }
48 }
49
50 // set target to root node if there is no target specified
51 if (target == null) {
52 FeatureTree featureTree = (FeatureTree) getViewer().getInput();
53 target = featureTree.getRoot();
54 }
55
56 Object[] droppedObjects = (Object[]) data;
57 TreeViewer viewer = (TreeViewer) getViewer();
58
59 // cannot drop a feature node onto itself
60 for (Object droppedObject : droppedObjects) {
61 if (droppedObject == null) {
62 MessagingUtils.warningDialog(
63 "Operation not supported yet",
64 this,
65 "We are currently unable to change the order of freshly created "
66 + "feature trees nodes. Please close and reopen the dialog to change the order of features.");
67 return false;
68 }
69 if (droppedObject.equals(target)) {
70 return false;
71 }
72 }
73 for (Object droppedObject : droppedObjects) {
74 FeatureNode droppedNode = (FeatureNode) droppedObject;
75 target.addChild(droppedNode, position);
76 viewer.add(target, droppedNode);
77 viewer.reveal(droppedNode);
78 }
79 return true;
80 }
81
82 @Override
83 public boolean validateDrop(Object target, int operation,
84 TransferData transferData) {
85 return FeatureNodeTransfer.getInstance().isSupportedType(
86 transferData);
87 }
88
89 }