Project

General

Profile

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

    
15
import org.eclipse.e4.ui.di.UISynchronize;
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.TransferData;
22

    
23
import eu.etaxonomy.cdm.model.description.Feature;
24
import eu.etaxonomy.cdm.model.term.FeatureNode;
25
import eu.etaxonomy.cdm.model.term.FeatureTree;
26
import eu.etaxonomy.cdm.model.term.IHasTermType;
27
import eu.etaxonomy.cdm.model.term.OrderedTermVocabulary;
28
import eu.etaxonomy.cdm.model.term.TermType;
29
import eu.etaxonomy.cdm.model.term.TermVocabulary;
30
import eu.etaxonomy.cdm.persistence.dto.TermDto;
31
import eu.etaxonomy.taxeditor.editor.definedterm.TermTransfer;
32
import eu.etaxonomy.taxeditor.editor.definedterm.e4.DefinedTermDropAdapterE4;
33
import eu.etaxonomy.taxeditor.featuretree.FeatureNodeTransfer;
34
import eu.etaxonomy.taxeditor.featuretree.e4.operation.AddFeatureOperation;
35
import eu.etaxonomy.taxeditor.featuretree.e4.operation.AddOntologyTermOperation;
36
import eu.etaxonomy.taxeditor.featuretree.e4.operation.MoveFeatureOperation;
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.StoreUtil;
41
import eu.etaxonomy.taxeditor.ui.dialog.selection.TermVocabularySelectionDialog;
42
import eu.etaxonomy.taxeditor.view.webimport.termimport.wrapper.OntologyTermWrapper;
43

    
44
public class FeatureTreeDropAdapter extends ViewerDropAdapter {
45

    
46
    protected IFeatureTreeEditor editor;
47
    private UISynchronize sync;
48

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

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

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

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

    
86
        if(target==null){
87
            MessagingUtils.warningDialog("", this, ""); //$NON-NLS-1$ //$NON-NLS-1$
88
            return false;
89
        }
90
		Collection<Object> droppedObjects = Collections.emptyList();
91
		if(data instanceof Object[]){
92
		    droppedObjects = Arrays.asList((Object[])data);
93
		}
94
		else if(data instanceof IStructuredSelection){
95
		    droppedObjects = ((IStructuredSelection) data).toList();
96
		}
97
		TreeViewer viewer = (TreeViewer) getViewer();
98

    
99
		// cannot drop a feature node onto itself
100
		for (Object droppedObject : droppedObjects) {
101
		    if(droppedObject==null){
102
		        MessagingUtils.warningDialog(DefinedTermDropAdapterE4.MOVE_FAILED, this.getClass(),
103
		                Messages.FeatureTreeDropAdapter_MOVE_FAILED_SAVE_MESSAGE);
104
		        return false;
105
		    }
106
			if (droppedObject.equals(target)) {
107
				return false;
108
			}
109
		}
110
		for (Object droppedObject : droppedObjects) {
111
		    //check term type compatibility
112
		    TermType targetType = target.getTermType();
113
		    TermType droppedType = null;
114
		    if(droppedObject instanceof IHasTermType){
115
		        droppedType = ((IHasTermType)droppedObject).getTermType();
116
		        if(droppedType!=null && !droppedType.equals(targetType)){
117
		            MessagingUtils.warningDialog(DefinedTermDropAdapterE4.TERM_TYPE_ERROR_TITLE, this, DefinedTermDropAdapterE4.TERM_TYPE_ERROR_MESSAGE);
118
		            continue;
119
		        }
120
		    }
121
		    else if(droppedObject instanceof TermDto){
122
		        droppedType = ((TermDto)droppedObject).getTermType();
123
		        if(droppedType!=null && !droppedType.equals(targetType) && !droppedType.isKindOf(targetType)){
124
                    MessagingUtils.warningDialog(DefinedTermDropAdapterE4.TERM_TYPE_ERROR_TITLE, this, DefinedTermDropAdapterE4.TERM_TYPE_ERROR_MESSAGE);
125
                    continue;
126
                }
127
		    }
128
		    if(droppedObject instanceof FeatureNode){
129
		        FeatureNode droppedNode = (FeatureNode) droppedObject;
130
		        int currentPosition = target.getIndex(droppedNode);
131
		        if(currentPosition<position){
132
		            position -= 1;
133
		        }
134
		        MoveFeatureOperation operation = new MoveFeatureOperation(droppedNode, target, position, getCurrentOperation(), editor, editor);
135
		        AbstractUtility.executeOperation(operation, sync);
136
		    }
137
		    else if(droppedObject instanceof Feature){
138
		        Feature droppedFeature = (Feature) droppedObject;
139
		        AddFeatureOperation operation = new AddFeatureOperation(
140
		                droppedFeature.getUuid(),
141
		                target, position, editor, editor);
142
		        AbstractUtility.executeOperation(operation, sync);
143
		    }
144
		    else if(droppedObject instanceof TermDto){
145
		        TermDto termDto = (TermDto) droppedObject;
146
		        // check if target feature tree has the same type as the dropped term
147
		        if(!termDto.getTermType().equals(targetType)){
148
		            MessagingUtils.warningDialog(DefinedTermDropAdapterE4.TERM_TYPE_ERROR_TITLE, this, DefinedTermDropAdapterE4.TERM_TYPE_ERROR_MESSAGE);
149
		            continue;
150
		        }
151
		        AddFeatureOperation operation = new AddFeatureOperation(
152
		                termDto.getUuid(),
153
		                target, position, editor, editor);
154
		        AbstractUtility.executeOperation(operation, sync);
155
		    }
156
		    else if(droppedObject instanceof OntologyTermWrapper){
157
		        OntologyTermWrapper wrapper = (OntologyTermWrapper)droppedObject;
158
		        TermVocabulary vocabulary = wrapper.getTermVocabulary();
159
		        if(vocabulary==null){
160
		            vocabulary =  TermVocabularySelectionDialog.select(
161
		                    Messages.FeatureTreeDropAdapter_CHOOSE_VOC, viewer.getControl().getShell(), null);
162
		            if(vocabulary instanceof OrderedTermVocabulary){
163
		                MessagingUtils.warningDialog(Messages.FeatureTreeDropAdapter_IMPORT_NOT_POSSIBLE, this,
164
		                        Messages.FeatureTreeDropAdapter_ORDER_VOC_NOT_POSSIBLE);
165
		                return false;
166
		            }
167
		        }
168
		        if(vocabulary==null){
169
		            return false;
170
		        }
171
		        else{
172
		            AddOntologyTermOperation operation = new AddOntologyTermOperation(wrapper, target, vocabulary, editor, editor);
173
		            AbstractUtility.executeOperation(operation, sync);
174
		        }
175
		    }
176
		}
177
		viewer.refresh();
178
		return true;
179
	}
180

    
181
    @Override
182
    public boolean validateDrop(Object target, int operation, TransferData transferData) {
183
        boolean
184
        isSupported = FeatureNodeTransfer.getInstance().isSupportedType(transferData);
185
        isSupported |= TermTransfer.getInstance().isSupportedType(transferData);
186
        isSupported |= LocalSelectionTransfer.getTransfer().isSupportedType(transferData);
187
        isSupported &= getViewer().getInput()!=null;
188
        if(target instanceof FeatureTree && getCurrentLocation()!=ViewerDropAdapter.LOCATION_ON){
189
            isSupported = false;
190
        }
191
        return isSupported;
192
    }
193

    
194
}
(2-2/5)