Project

General

Profile

Download (3 KB) Statistics
| Branch: | Tag: | Revision:
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.Feature;
19
import eu.etaxonomy.cdm.model.description.FeatureNode;
20
import eu.etaxonomy.cdm.model.description.FeatureTree;
21
import eu.etaxonomy.taxeditor.editor.definedterm.TermTransfer;
22
import eu.etaxonomy.taxeditor.featuretree.FeatureNodeTransfer;
23
import eu.etaxonomy.taxeditor.store.CdmStore;
24

    
25
public class FeatureNodeDropAdapter extends ViewerDropAdapter {
26

    
27
    private final MDirtyable dirtyable;
28

    
29
    public FeatureNodeDropAdapter(MDirtyable dirtyable, Viewer viewer) {
30
		super(viewer);
31
        this.dirtyable = dirtyable;
32
	}
33

    
34
	@Override
35
	public boolean performDrop(Object data) {
36
		FeatureNode target = (FeatureNode) getCurrentTarget();
37
		int position = 0;
38

    
39
		if (target != null) {
40
			int location = getCurrentLocation();
41
			FeatureNode parent = target.getParent();
42
			if (location == LOCATION_BEFORE) {
43
				position = Math.max(0, parent.getIndex(target) - 1);
44
				target = parent;
45
			}
46

    
47
			if (location == LOCATION_AFTER) {
48
				position = parent.getIndex(target);
49
				target = parent;
50
			}
51
		}
52

    
53
		// set target to root node if there is no target specified
54
		if (target == null) {
55
			FeatureTree featureTree = (FeatureTree) getViewer().getInput();
56
			target = featureTree.getRoot();
57
		}
58

    
59
		Object[] droppedObjects = (Object[]) data;
60
		TreeViewer viewer = (TreeViewer) getViewer();
61

    
62
		// cannot drop a feature node onto itself
63
		for (Object droppedObject : droppedObjects) {
64
			if (droppedObject.equals(target)) {
65
				return false;
66
			}
67
		}
68
		for (Object droppedObject : droppedObjects) {
69
		    if(droppedObject instanceof FeatureNode){
70
		        FeatureNode droppedNode = (FeatureNode) droppedObject;
71
		        CdmStore.getService(IFeatureNodeService.class).moveFeatureNode(droppedNode.getUuid(), target.getUuid(), position);
72
		        viewer.reveal(droppedNode);
73
		    }
74
		    else if(droppedObject instanceof Feature){
75
		        Feature droppedFeature = (Feature) droppedObject;
76
		        CdmStore.getService(IFeatureNodeService.class).addChildFeatureNode(target.getUuid(), droppedFeature.getUuid());
77

    
78
		    }
79
		}
80
		viewer.refresh();
81
		return true;
82
	}
83

    
84
    @Override
85
    public boolean validateDrop(Object target, int operation, TransferData transferData) {
86
        boolean isSupported = FeatureNodeTransfer.getInstance().isSupportedType(transferData);
87
        isSupported |= TermTransfer.getInstance().isSupportedType(transferData);
88
        isSupported &= getViewer().getInput()!=null;
89
        return isSupported;
90
    }
91

    
92
}
(2-2/5)