Project

General

Profile

Download (6.96 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.api.service.ITermService;
24
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
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.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.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("", this, ""); //$NON-NLS-1$ //$NON-NLS-1$
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(DefinedTermDropAdapterE4.MOVE_FAILED, this.getClass(),
104
		                Messages.FeatureTreeDropAdapter_MOVE_FAILED_SAVE_MESSAGE);
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
		        MoveFeatureOperation operation = new MoveFeatureOperation(droppedNode, target, position, getCurrentOperation(), editor, editor);
115
		        AbstractUtility.executeOperation(operation, sync);
116
		    }
117
		    else if(droppedObject instanceof Feature){
118
		        Feature droppedFeature = (Feature) droppedObject;
119
		        AddFeatureOperation operation = new AddFeatureOperation(
120
		                (Feature) CdmStore.getService(ITermService.class).load(droppedFeature.getUuid()),
121
		                target, editor, editor);
122
		        AbstractUtility.executeOperation(operation, sync);
123
		    }
124
		    else if(droppedObject instanceof TermDto){
125
		            TermDto termDto = (TermDto) droppedObject;
126
		            DefinedTermBase term = CdmStore.getService(ITermService.class).load(termDto.getUuid());
127
		            if(term.isInstanceOf(Feature.class)){
128
		                AddFeatureOperation operation = new AddFeatureOperation(
129
		                        (Feature)term,
130
		                        target, editor, editor);
131
		                AbstractUtility.executeOperation(operation, sync);
132
		            }
133
		            else{
134
		                MessagingUtils.warningDialog(DefinedTermDropAdapterE4.MOVE_FAILED, this, Messages.FeatureTreeDropAdapter_ONLY_MOVE_FEATURES);
135
		            }
136
		    }
137
		    else if(droppedObject instanceof OntologyTermWrapper){
138
		        OntologyTermWrapper wrapper = (OntologyTermWrapper)droppedObject;
139
		        TermVocabulary vocabulary = wrapper.getTermVocabulary();
140
		        if(vocabulary==null){
141
		            vocabulary =  TermVocabularySelectionDialog.select(
142
		                    Messages.FeatureTreeDropAdapter_CHOOSE_VOC, viewer.getControl().getShell(), null);
143
		            if(vocabulary instanceof OrderedTermVocabulary){
144
		                MessagingUtils.warningDialog(Messages.FeatureTreeDropAdapter_IMPORT_NOT_POSSIBLE, this,
145
		                        Messages.FeatureTreeDropAdapter_ORDER_VOC_NOT_POSSIBLE);
146
		                return false;
147
		            }
148
		        }
149
		        if(vocabulary==null){
150
		            return false;
151
		        }
152
		        else{
153
		            AddOntologyTermOperation operation = new AddOntologyTermOperation(wrapper, target, vocabulary, editor, editor);
154
		            AbstractUtility.executeOperation(operation, sync);
155
		        }
156
		    }
157
		}
158
		viewer.refresh();
159
		return true;
160
	}
161

    
162
    @Override
163
    public boolean validateDrop(Object target, int operation, TransferData transferData) {
164
        boolean
165
        isSupported = FeatureNodeTransfer.getInstance().isSupportedType(transferData);
166
        isSupported |= TermTransfer.getInstance().isSupportedType(transferData);
167
        isSupported |= LocalSelectionTransfer.getTransfer().isSupportedType(transferData);
168
        isSupported &= getViewer().getInput()!=null;
169
        if(target instanceof FeatureTree && getCurrentLocation()!=ViewerDropAdapter.LOCATION_ON){
170
            isSupported = false;
171
        }
172
        return isSupported;
173
    }
174

    
175
}
(2-2/5)