ref #6805 Remove e3 drag and drop adapters
[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.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;
16
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;
22
23 public class FeatureNodeDropAdapter extends ViewerDropAdapter {
24
25 private final MDirtyable dirtyable;
26
27 public FeatureNodeDropAdapter(MDirtyable dirtyable, Viewer viewer) {
28 super(viewer);
29 this.dirtyable = dirtyable;
30 }
31
32 @Override
33 public boolean performDrop(Object data) {
34 FeatureNode target = (FeatureNode) getCurrentTarget();
35 int position = 0;
36
37 if (target != null) {
38 int location = getCurrentLocation();
39 FeatureNode parent = target.getParent();
40 if (location == LOCATION_BEFORE) {
41 position = Math.max(0, parent.getIndex(target) - 1);
42 target = parent;
43 }
44
45 if (location == LOCATION_AFTER) {
46 position = parent.getIndex(target);
47 target = parent;
48 }
49 }
50
51 // set target to root node if there is no target specified
52 if (target == null) {
53 FeatureTree featureTree = (FeatureTree) getViewer().getInput();
54 target = featureTree.getRoot();
55 }
56
57 Object[] droppedObjects = (Object[]) data;
58 TreeViewer viewer = (TreeViewer) getViewer();
59
60 // cannot drop a feature node onto itself
61 for (Object droppedObject : droppedObjects) {
62 if (droppedObject.equals(target)) {
63 return false;
64 }
65 }
66 for (Object droppedObject : droppedObjects) {
67 FeatureNode droppedNode = (FeatureNode) droppedObject;
68 CdmStore.getService(IFeatureNodeService.class).moveFeatureNode(droppedNode.getUuid(), target.getUuid(), position);
69 viewer.refresh();
70 viewer.reveal(droppedNode);
71 }
72 this.dirtyable.setDirty(true);
73 return true;
74 }
75
76 @Override
77 public boolean validateDrop(Object target, int operation, TransferData transferData) {
78 return FeatureNodeTransfer.getInstance().isSupportedType(transferData);
79 }
80
81 }