Project

General

Profile

Download (7.84 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.common.IHasTermType;
24
import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
25
import eu.etaxonomy.cdm.model.common.TermType;
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.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
		    if(droppedObject instanceof IHasTermType){
114
		        TermType droppedType = ((IHasTermType)droppedObject).getTermType();
115
		        if(!droppedType.equals(targetType)){
116
                    MessagingUtils.warningDialog(DefinedTermDropAdapterE4.TERM_TYPE_ERROR_TITLE, this, DefinedTermDropAdapterE4.TERM_TYPE_ERROR_MESSAGE);
117
		            continue;
118
		        }
119
		    }
120
		    if(droppedObject instanceof FeatureNode){
121
		        FeatureNode droppedNode = (FeatureNode) droppedObject;
122
		        int currentPosition = target.getIndex(droppedNode);
123
		        if(currentPosition<position){
124
		            position -= 1;
125
		        }
126
		        MoveFeatureOperation operation = new MoveFeatureOperation(droppedNode, target, position, getCurrentOperation(), editor, editor);
127
		        AbstractUtility.executeOperation(operation, sync);
128
		    }
129
		    else if(droppedObject instanceof Feature){
130
		        Feature droppedFeature = (Feature) droppedObject;
131
		        AddFeatureOperation operation = new AddFeatureOperation(
132
		                droppedFeature.getUuid(),
133
		                target, position, editor, editor);
134
		        AbstractUtility.executeOperation(operation, sync);
135
		    }
136
		    else if(droppedObject instanceof TermDto){
137
		            TermDto termDto = (TermDto) droppedObject;
138
		            // check if a feature or sub type is dropped
139
		            if(termDto.getTermType().equals(TermType.Feature) || termDto.getTermType().isKindOf(TermType.Feature)){
140
		                // check if target feature tree has the same type as the dropped term
141
		                if(!termDto.getTermType().equals(targetType)){
142
		                    MessagingUtils.warningDialog(DefinedTermDropAdapterE4.TERM_TYPE_ERROR_TITLE, this, DefinedTermDropAdapterE4.TERM_TYPE_ERROR_MESSAGE);
143
		                    continue;
144
		                }
145
		                AddFeatureOperation operation = new AddFeatureOperation(
146
		                        termDto.getUuid(),
147
		                        target, position, editor, editor);
148
		                AbstractUtility.executeOperation(operation, sync);
149
		            }
150
		            else{
151
		                MessagingUtils.warningDialog(DefinedTermDropAdapterE4.MOVE_FAILED, this, Messages.FeatureTreeDropAdapter_ONLY_MOVE_FEATURES);
152
		            }
153
		    }
154
		    else if(droppedObject instanceof OntologyTermWrapper){
155
		        OntologyTermWrapper wrapper = (OntologyTermWrapper)droppedObject;
156
		        TermVocabulary vocabulary = wrapper.getTermVocabulary();
157
		        if(vocabulary==null){
158
		            vocabulary =  TermVocabularySelectionDialog.select(
159
		                    Messages.FeatureTreeDropAdapter_CHOOSE_VOC, viewer.getControl().getShell(), null);
160
		            if(vocabulary instanceof OrderedTermVocabulary){
161
		                MessagingUtils.warningDialog(Messages.FeatureTreeDropAdapter_IMPORT_NOT_POSSIBLE, this,
162
		                        Messages.FeatureTreeDropAdapter_ORDER_VOC_NOT_POSSIBLE);
163
		                return false;
164
		            }
165
		        }
166
		        if(vocabulary==null){
167
		            return false;
168
		        }
169
		        else{
170
		            AddOntologyTermOperation operation = new AddOntologyTermOperation(wrapper, target, vocabulary, editor, editor);
171
		            AbstractUtility.executeOperation(operation, sync);
172
		        }
173
		    }
174
		}
175
		viewer.refresh();
176
		return true;
177
	}
178

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

    
192
}
(2-2/5)