Project

General

Profile

Download (6.03 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.jface.util.LocalSelectionTransfer;
17
import org.eclipse.jface.viewers.IStructuredSelection;
18
import org.eclipse.jface.viewers.TreeViewer;
19
import org.eclipse.jface.viewers.Viewer;
20
import org.eclipse.jface.viewers.ViewerDropAdapter;
21
import org.eclipse.swt.dnd.DND;
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.store.StoreUtil;
36
import eu.etaxonomy.taxeditor.ui.dialog.selection.TermVocabularySelectionDialog;
37
import eu.etaxonomy.taxeditor.view.webimport.termimport.wrapper.OntologyTermWrapper;
38
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
39

    
40
public class FeatureNodeDropAdapter extends ViewerDropAdapter {
41

    
42
    protected IE4SavablePart savablePart;
43

    
44
    public FeatureNodeDropAdapter(IE4SavablePart savablePart, Viewer viewer) {
45
		super(viewer);
46
		this.savablePart = savablePart;
47
	}
48

    
49
	@Override
50
	public boolean performDrop(Object data) {
51
	    if(StoreUtil.checkDirty(savablePart)){
52
	        return false;
53
	    }
54
		Object currentTarget = getCurrentTarget();
55
		FeatureNode target = null;
56
		if(currentTarget instanceof FeatureTree){
57
		    target = ((FeatureTree) currentTarget).getRoot();
58
		}
59
		else if(currentTarget instanceof FeatureNode){
60
		    target = (FeatureNode) currentTarget;
61
		}
62
		int position = 0;
63

    
64
		if (target != null) {
65
			int location = getCurrentLocation();
66
			FeatureNode parent = target.getParent();
67
			if(parent!=null){
68
			    if (location == LOCATION_BEFORE) {
69
			        position = Math.max(0, parent.getIndex(target) - 1);
70
			        target = parent;
71
			    }
72

    
73
			    if (location == LOCATION_AFTER) {
74
			        position = parent.getIndex(target);
75
			        target = parent;
76
			    }
77
			}
78
		}
79

    
80
        if(target==null){
81
            MessagingUtils.warningDialog(Messages.FeatureNodeDropAdapter_INVALID_TARGET, this, Messages.FeatureNodeDropAdapter_INVALID_TARGET_MESSAGE);
82
            return false;
83
        }
84
		Collection<Object> droppedObjects = Collections.emptyList();
85
		if(data instanceof Object[]){
86
		    droppedObjects = Arrays.asList((Object[])data);
87
		}
88
		else if(data instanceof IStructuredSelection){
89
		    droppedObjects = ((IStructuredSelection) data).toList();
90
		}
91
		TreeViewer viewer = (TreeViewer) getViewer();
92

    
93
		// cannot drop a feature node onto itself
94
		for (Object droppedObject : droppedObjects) {
95
		    if(droppedObject==null){
96
		        MessagingUtils.warningDialog("Move failed", this.getClass(),
97
		                "Moving the feature node failed. Try saving before.");
98
		        return false;
99
		    }
100
			if (droppedObject.equals(target)) {
101
				return false;
102
			}
103
		}
104
		for (Object droppedObject : droppedObjects) {
105
		    if(droppedObject instanceof FeatureNode){
106
		        FeatureNode droppedNode = (FeatureNode) droppedObject;
107
		        //move operation
108
		        if(getCurrentOperation()==DND.DROP_MOVE){
109
		            CdmStore.getService(IFeatureNodeService.class).moveFeatureNode(droppedNode.getUuid(), target.getUuid(), position);
110
		        }
111
		        //copy operation
112
		        else if(getCurrentOperation()==DND.DROP_COPY){
113
		            CdmStore.getService(IFeatureNodeService.class).addChildFeatureNode(target.getUuid(), droppedNode.getFeature().getUuid());
114
		        }
115
		        viewer.reveal(droppedNode);
116
		    }
117
		    else if(droppedObject instanceof Feature){
118
		        Feature droppedFeature = (Feature) droppedObject;
119
		        CdmStore.getService(IFeatureNodeService.class).addChildFeatureNode(target.getUuid(), droppedFeature.getUuid());
120
		    }
121
		    else if(droppedObject instanceof OntologyTermWrapper){
122
		        OntologyTermWrapper wrapper = (OntologyTermWrapper)droppedObject;
123
		        TermVocabulary vocabulary = wrapper.getTermVocabulary();
124
		        if(vocabulary==null){
125
		            vocabulary =  TermVocabularySelectionDialog.select(
126
		                    "Choose vocabulary for import", viewer.getControl().getShell(), null);
127
		            if(vocabulary instanceof OrderedTermVocabulary){
128
		                MessagingUtils.warningDialog("Import not possible", this,
129
		                        "The chosen vocabulary is an ordered vocabulary.\n"
130
		                                + "Importing into ordered vocabularies is currently not supported.");
131
		                return false;
132
		            }
133
		        }
134
		        if(vocabulary==null){
135
		            return false;
136
		        }
137
		        else{
138
		            Feature feature = Feature.NewInstance(wrapper.getDescription(), wrapper.getLabel(), null);
139
		            feature.setUri(URI.create(wrapper.getUri()));
140
		            vocabulary.addTerm(feature);
141
		            CdmStore.getService(IFeatureNodeService.class).createChildFeatureNode(target, feature);
142
		        }
143
		    }
144
		}
145
		viewer.refresh();
146
		return true;
147
	}
148

    
149
    @Override
150
    public boolean validateDrop(Object target, int operation, TransferData transferData) {
151
        boolean isSupported = FeatureNodeTransfer.getInstance().isSupportedType(transferData);
152
        isSupported |= TermTransfer.getInstance().isSupportedType(transferData);
153
        isSupported |= LocalSelectionTransfer.getTransfer().isSupportedType(transferData);
154
        isSupported &= getViewer().getInput()!=null;
155
        return isSupported;
156
    }
157

    
158
}
(2-2/5)