Project

General

Profile

Download (5.11 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 java.net.URI;
12
import java.util.Arrays;
13
import java.util.Collection;
14
import java.util.Collections;
15

    
16
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
17
import org.eclipse.jface.util.LocalSelectionTransfer;
18
import org.eclipse.jface.viewers.IStructuredSelection;
19
import org.eclipse.jface.viewers.TreeViewer;
20
import org.eclipse.jface.viewers.Viewer;
21
import org.eclipse.jface.viewers.ViewerDropAdapter;
22
import org.eclipse.swt.dnd.TransferData;
23

    
24
import eu.etaxonomy.cdm.api.service.IFeatureNodeService;
25
import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
26
import eu.etaxonomy.cdm.model.common.TermVocabulary;
27
import eu.etaxonomy.cdm.model.description.Feature;
28
import eu.etaxonomy.cdm.model.description.FeatureNode;
29
import eu.etaxonomy.cdm.model.description.FeatureTree;
30
import eu.etaxonomy.taxeditor.editor.definedterm.TermTransfer;
31
import eu.etaxonomy.taxeditor.featuretree.FeatureNodeTransfer;
32
import eu.etaxonomy.taxeditor.l10n.Messages;
33
import eu.etaxonomy.taxeditor.model.MessagingUtils;
34
import eu.etaxonomy.taxeditor.store.CdmStore;
35
import eu.etaxonomy.taxeditor.ui.dialog.selection.TermVocabularySelectionDialog;
36
import eu.etaxonomy.taxeditor.view.webimport.termimport.wrapper.OntologyTermWrapper;
37

    
38
public class FeatureNodeDropAdapter extends ViewerDropAdapter {
39

    
40
    private MDirtyable dirtyable;
41

    
42
    public FeatureNodeDropAdapter(MDirtyable dirtyable, Viewer viewer) {
43
		super(viewer);
44
		this.dirtyable = dirtyable;
45
	}
46

    
47
	@Override
48
	public boolean performDrop(Object data) {
49
		Object currentTarget = getCurrentTarget();
50
		FeatureNode target = null;
51
		if(currentTarget instanceof FeatureTree){
52
		    target = ((FeatureTree) currentTarget).getRoot();
53
		}
54
		else if(currentTarget instanceof FeatureNode){
55
		    target = (FeatureNode) currentTarget;
56
		}
57
		int position = 0;
58

    
59
		if (target != null) {
60
			int location = getCurrentLocation();
61
			FeatureNode parent = target.getParent();
62
			if (location == LOCATION_BEFORE) {
63
				position = Math.max(0, parent.getIndex(target) - 1);
64
				target = parent;
65
			}
66

    
67
			if (location == LOCATION_AFTER) {
68
				position = parent.getIndex(target);
69
				target = parent;
70
			}
71
		}
72

    
73
        if(target==null){
74
            MessagingUtils.warningDialog(Messages.FeatureNodeDropAdapter_INVALID_TARGET, this, Messages.FeatureNodeDropAdapter_INVALID_TARGET_MESSAGE);
75
            return false;
76
        }
77

    
78
		Collection<Object> droppedObjects = Collections.emptyList();
79
		if(data instanceof Object[]){
80
		    droppedObjects = Arrays.asList((Object[])data);
81
		}
82
		else if(data instanceof IStructuredSelection){
83
		    droppedObjects = ((IStructuredSelection) data).toList();
84
		}
85
		TreeViewer viewer = (TreeViewer) getViewer();
86

    
87
		// cannot drop a feature node onto itself
88
		for (Object droppedObject : droppedObjects) {
89
			if (droppedObject.equals(target)) {
90
				return false;
91
			}
92
		}
93
		for (Object droppedObject : droppedObjects) {
94
		    if(droppedObject instanceof FeatureNode){
95
		        FeatureNode droppedNode = (FeatureNode) droppedObject;
96
		        CdmStore.getService(IFeatureNodeService.class).moveFeatureNode(droppedNode.getUuid(), target.getUuid(), position);
97
		        viewer.reveal(droppedNode);
98
		    }
99
		    else if(droppedObject instanceof Feature){
100
		        Feature droppedFeature = (Feature) droppedObject;
101
		        CdmStore.getService(IFeatureNodeService.class).addChildFeatureNode(target.getUuid(), droppedFeature.getUuid());
102
		    }
103
		    else if(droppedObject instanceof OntologyTermWrapper){
104
		        OntologyTermWrapper wrapper = (OntologyTermWrapper)droppedObject;
105
		        TermVocabulary vocabulary = TermVocabularySelectionDialog.select(
106
		                "Choose vocabulary for import", viewer.getControl().getShell(), null);
107
		        if(vocabulary instanceof OrderedTermVocabulary){
108
		            MessagingUtils.warningDialog("Import not possible", this,
109
		                    "The chosen vocabulary is an ordered vocabulary.\n"
110
		                    + "Importing into ordered vocabularies is currently not supported.");
111
		            return false;
112
		        }
113
		        if(vocabulary!=null){
114
		            Feature feature = Feature.NewInstance(wrapper.getDescription(), wrapper.getLabel(), null);
115
		            feature.setUri(URI.create(wrapper.getUri()));
116
		            vocabulary.addTerm(feature);
117
		            CdmStore.getService(IFeatureNodeService.class).createChildFeatureNode(target, feature);
118
		        }
119
		    }
120
		}
121
		dirtyable.setDirty(true);
122
		viewer.refresh();
123
		return true;
124
	}
125

    
126
    @Override
127
    public boolean validateDrop(Object target, int operation, TransferData transferData) {
128
        boolean isSupported = FeatureNodeTransfer.getInstance().isSupportedType(transferData);
129
        isSupported |= TermTransfer.getInstance().isSupportedType(transferData);
130
        isSupported |= LocalSelectionTransfer.getTransfer().isSupportedType(transferData);
131
        isSupported &= getViewer().getInput()!=null;
132
        return isSupported;
133
    }
134

    
135
}
(2-2/5)