1c6e8da4e98b9855bf228a2b2cff68ae643df296
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / featuretree / e4 / 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.e4;
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.featuretree.FeatureNodeTransfer;
19 import eu.etaxonomy.taxeditor.model.MessagingUtils;
20
21 class FeatureNodeDropAdapter extends ViewerDropAdapter {
22
23 /**
24 *
25 */
26 private final FeatureTreeEditor featureTreeEditor;
27
28 protected FeatureNodeDropAdapter(FeatureTreeEditor featureTreeEditor, Viewer viewer) {
29 super(viewer);
30 this.featureTreeEditor = featureTreeEditor;
31 }
32
33 @Override
34 public boolean performDrop(Object data) {
35 FeatureNode target = (FeatureNode) getCurrentTarget();
36 int position = 0;
37
38 if (target != null) {
39 int location = getCurrentLocation();
40 FeatureNode parent = target.getParent();
41 if (location == LOCATION_BEFORE) {
42 position = Math.max(0, parent.getIndex(target) - 1);
43 target = parent;
44 }
45
46 if (location == LOCATION_AFTER) {
47 position = parent.getIndex(target);
48 target = parent;
49 }
50 }
51
52 // set target to root node if there is no target specified
53 if (target == null) {
54 FeatureTree featureTree = (FeatureTree) getViewer().getInput();
55 target = featureTree.getRoot();
56 }
57
58 Object[] droppedObjects = (Object[]) data;
59 TreeViewer viewer = (TreeViewer) getViewer();
60
61 // cannot drop a feature node onto itself
62 for (Object droppedObject : droppedObjects) {
63 if (droppedObject == null) {
64 MessagingUtils.warningDialog(
65 "Operation not supported yet",
66 this,
67 "We are currently unable to change the order of freshly created "
68 + "feature trees nodes. Please close and reopen the dialog to change the order of features.");
69 return false;
70 }
71 if (droppedObject.equals(target)) {
72 return false;
73 }
74 }
75 for (Object droppedObject : droppedObjects) {
76 FeatureNode droppedNode = (FeatureNode) droppedObject;
77 target.addChild(droppedNode, position);
78 viewer.add(target, droppedNode);
79 viewer.reveal(droppedNode);
80 }
81 this.featureTreeEditor.setDirty(true);
82 return true;
83 }
84
85 @Override
86 public boolean validateDrop(Object target, int operation,
87 TransferData transferData) {
88 return FeatureNodeTransfer.getInstance().isSupportedType(
89 transferData);
90 }
91
92 }