Project

General

Profile

Download (7.14 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.di.UISynchronize;
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.DND;
23
import org.eclipse.swt.dnd.TransferData;
24

    
25
import eu.etaxonomy.cdm.api.service.IFeatureNodeService;
26
import eu.etaxonomy.cdm.api.service.ITermService;
27
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
28
import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
29
import eu.etaxonomy.cdm.model.common.TermVocabulary;
30
import eu.etaxonomy.cdm.model.description.Feature;
31
import eu.etaxonomy.cdm.model.description.FeatureNode;
32
import eu.etaxonomy.cdm.model.description.FeatureTree;
33
import eu.etaxonomy.cdm.persistence.dto.TermDto;
34
import eu.etaxonomy.taxeditor.editor.definedterm.TermTransfer;
35
import eu.etaxonomy.taxeditor.featuretree.FeatureNodeTransfer;
36
import eu.etaxonomy.taxeditor.featuretree.e4.operation.AddFeatureOperation;
37
import eu.etaxonomy.taxeditor.l10n.Messages;
38
import eu.etaxonomy.taxeditor.model.AbstractUtility;
39
import eu.etaxonomy.taxeditor.model.MessagingUtils;
40
import eu.etaxonomy.taxeditor.store.CdmStore;
41
import eu.etaxonomy.taxeditor.store.StoreUtil;
42
import eu.etaxonomy.taxeditor.ui.dialog.selection.TermVocabularySelectionDialog;
43
import eu.etaxonomy.taxeditor.view.webimport.termimport.wrapper.OntologyTermWrapper;
44

    
45
public class FeatureTreeDropAdapter extends ViewerDropAdapter {
46

    
47
    protected IFeatureTreeEditor editor;
48
    private UISynchronize sync;
49

    
50
    public FeatureTreeDropAdapter(IFeatureTreeEditor editor, Viewer viewer, UISynchronize sync) {
51
		super(viewer);
52
		this.editor = editor;
53
		this.sync = sync;
54
	}
55

    
56
	@Override
57
	public boolean performDrop(Object data) {
58
	    if(StoreUtil.promptCheckIsDirty(editor)){
59
	        return false;
60
	    }
61
		Object currentTarget = getCurrentTarget();
62
		FeatureNode target = null;
63
		if(currentTarget instanceof FeatureTree){
64
		    target = ((FeatureTree) currentTarget).getRoot();
65
		}
66
		else if(currentTarget instanceof FeatureNode){
67
		    target = (FeatureNode) currentTarget;
68
		}
69
		int position = 0;
70

    
71
		if (target != null) {
72
			int location = getCurrentLocation();
73
			FeatureNode parent = target.getParent();
74
			if(parent!=null){
75
			    if (location == LOCATION_BEFORE) {
76
			        position = Math.max(0, parent.getIndex(target) - 1);
77
			        target = parent;
78
			    }
79

    
80
			    if (location == LOCATION_AFTER) {
81
			        position = parent.getIndex(target);
82
			        target = parent;
83
			    }
84
			}
85
		}
86

    
87
        if(target==null){
88
            MessagingUtils.warningDialog(Messages.FeatureNodeDropAdapter_INVALID_TARGET, this, Messages.FeatureNodeDropAdapter_INVALID_TARGET_MESSAGE);
89
            return false;
90
        }
91
		Collection<Object> droppedObjects = Collections.emptyList();
92
		if(data instanceof Object[]){
93
		    droppedObjects = Arrays.asList((Object[])data);
94
		}
95
		else if(data instanceof IStructuredSelection){
96
		    droppedObjects = ((IStructuredSelection) data).toList();
97
		}
98
		TreeViewer viewer = (TreeViewer) getViewer();
99

    
100
		// cannot drop a feature node onto itself
101
		for (Object droppedObject : droppedObjects) {
102
		    if(droppedObject==null){
103
		        MessagingUtils.warningDialog("Move failed", this.getClass(),
104
		                "Moving the feature node failed. Try saving before.");
105
		        return false;
106
		    }
107
			if (droppedObject.equals(target)) {
108
				return false;
109
			}
110
		}
111
		for (Object droppedObject : droppedObjects) {
112
		    if(droppedObject instanceof FeatureNode){
113
		        FeatureNode droppedNode = (FeatureNode) droppedObject;
114
		        //move operation
115
		        if(getCurrentOperation()==DND.DROP_MOVE){
116
		            CdmStore.getService(IFeatureNodeService.class).moveFeatureNode(droppedNode.getUuid(), target.getUuid(), position);
117
		        }
118
		        //copy operation
119
		        else if(getCurrentOperation()==DND.DROP_COPY){
120
		            CdmStore.getService(IFeatureNodeService.class).addChildFeatureNode(target.getUuid(), droppedNode.getFeature().getUuid());
121
		        }
122
		        viewer.reveal(droppedNode);
123
		    }
124
		    else if(droppedObject instanceof Feature){
125
		        Feature droppedFeature = (Feature) droppedObject;
126
		        AddFeatureOperation operation = new AddFeatureOperation(
127
		                (Feature) CdmStore.getService(ITermService.class).load(droppedFeature.getUuid()),
128
		                target, editor, editor);
129
		        AbstractUtility.executeOperation(operation, sync);
130
		    }
131
		    else if(droppedObject instanceof TermDto){
132
		            TermDto termDto = (TermDto) droppedObject;
133
		            DefinedTermBase term = CdmStore.getService(ITermService.class).load(termDto.getUuid());
134
		            if(term.isInstanceOf(Feature.class)){
135
		                AddFeatureOperation operation = new AddFeatureOperation(
136
		                        (Feature)term,
137
		                        target, editor, editor);
138
		                AbstractUtility.executeOperation(operation, sync);
139
		            }
140
		            else{
141
		                MessagingUtils.warningDialog("Move failed", this, "Can only move Features");
142
		            }
143
		    }
144
		    else if(droppedObject instanceof OntologyTermWrapper){
145
		        OntologyTermWrapper wrapper = (OntologyTermWrapper)droppedObject;
146
		        TermVocabulary vocabulary = wrapper.getTermVocabulary();
147
		        if(vocabulary==null){
148
		            vocabulary =  TermVocabularySelectionDialog.select(
149
		                    "Choose vocabulary for import", viewer.getControl().getShell(), null);
150
		            if(vocabulary instanceof OrderedTermVocabulary){
151
		                MessagingUtils.warningDialog("Import not possible", this,
152
		                        "The chosen vocabulary is an ordered vocabulary.\n"
153
		                                + "Importing into ordered vocabularies is currently not supported.");
154
		                return false;
155
		            }
156
		        }
157
		        if(vocabulary==null){
158
		            return false;
159
		        }
160
		        else{
161
		            Feature feature = Feature.NewInstance(wrapper.getDescription(), wrapper.getLabel(), null);
162
		            feature.setUri(URI.create(wrapper.getUri()));
163
		            vocabulary.addTerm(feature);
164
		            CdmStore.getService(IFeatureNodeService.class).createChildFeatureNode(target, feature);
165
		        }
166
		    }
167
		}
168
		viewer.refresh();
169
		return true;
170
	}
171

    
172
    @Override
173
    public boolean validateDrop(Object target, int operation, TransferData transferData) {
174
        boolean
175
        isSupported = FeatureNodeTransfer.getInstance().isSupportedType(transferData);
176
        isSupported |= TermTransfer.getInstance().isSupportedType(transferData);
177
        isSupported |= LocalSelectionTransfer.getTransfer().isSupportedType(transferData);
178
        isSupported &= getViewer().getInput()!=null;
179
        return isSupported;
180
    }
181

    
182
}
(2-2/5)