Project

General

Profile

Download (7.17 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.editor.definedterm.e4.DefinedTermDropAdapterE4;
36
import eu.etaxonomy.taxeditor.featuretree.FeatureNodeTransfer;
37
import eu.etaxonomy.taxeditor.featuretree.e4.operation.AddFeatureOperation;
38
import eu.etaxonomy.taxeditor.l10n.Messages;
39
import eu.etaxonomy.taxeditor.model.AbstractUtility;
40
import eu.etaxonomy.taxeditor.model.MessagingUtils;
41
import eu.etaxonomy.taxeditor.store.CdmStore;
42
import eu.etaxonomy.taxeditor.store.StoreUtil;
43
import eu.etaxonomy.taxeditor.ui.dialog.selection.TermVocabularySelectionDialog;
44
import eu.etaxonomy.taxeditor.view.webimport.termimport.wrapper.OntologyTermWrapper;
45

    
46
public class FeatureTreeDropAdapter extends ViewerDropAdapter {
47

    
48
    protected IFeatureTreeEditor editor;
49
    private UISynchronize sync;
50

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

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

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

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

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

    
101
		// cannot drop a feature node onto itself
102
		for (Object droppedObject : droppedObjects) {
103
		    if(droppedObject==null){
104
		        MessagingUtils.warningDialog(DefinedTermDropAdapterE4.MOVE_FAILED, this.getClass(),
105
		                Messages.FeatureTreeDropAdapter_MOVE_FAILED_SAVE_MESSAGE);
106
		        return false;
107
		    }
108
			if (droppedObject.equals(target)) {
109
				return false;
110
			}
111
		}
112
		for (Object droppedObject : droppedObjects) {
113
		    if(droppedObject instanceof FeatureNode){
114
		        FeatureNode droppedNode = (FeatureNode) droppedObject;
115
		        //move operation
116
		        if(getCurrentOperation()==DND.DROP_MOVE){
117
		            CdmStore.getService(IFeatureNodeService.class).moveFeatureNode(droppedNode.getUuid(), target.getUuid(), position);
118
		        }
119
		        //copy operation
120
		        else if(getCurrentOperation()==DND.DROP_COPY){
121
		            CdmStore.getService(IFeatureNodeService.class).addChildFeatureNode(target.getUuid(), droppedNode.getFeature().getUuid());
122
		        }
123
		        viewer.reveal(droppedNode);
124
		    }
125
		    else if(droppedObject instanceof Feature){
126
		        Feature droppedFeature = (Feature) droppedObject;
127
		        AddFeatureOperation operation = new AddFeatureOperation(
128
		                (Feature) CdmStore.getService(ITermService.class).load(droppedFeature.getUuid()),
129
		                target, editor, editor);
130
		        AbstractUtility.executeOperation(operation, sync);
131
		    }
132
		    else if(droppedObject instanceof TermDto){
133
		            TermDto termDto = (TermDto) droppedObject;
134
		            DefinedTermBase term = CdmStore.getService(ITermService.class).load(termDto.getUuid());
135
		            if(term.isInstanceOf(Feature.class)){
136
		                AddFeatureOperation operation = new AddFeatureOperation(
137
		                        (Feature)term,
138
		                        target, editor, editor);
139
		                AbstractUtility.executeOperation(operation, sync);
140
		            }
141
		            else{
142
		                MessagingUtils.warningDialog(DefinedTermDropAdapterE4.MOVE_FAILED, this, Messages.FeatureTreeDropAdapter_ONLY_MOVE_FEATURES);
143
		            }
144
		    }
145
		    else if(droppedObject instanceof OntologyTermWrapper){
146
		        OntologyTermWrapper wrapper = (OntologyTermWrapper)droppedObject;
147
		        TermVocabulary vocabulary = wrapper.getTermVocabulary();
148
		        if(vocabulary==null){
149
		            vocabulary =  TermVocabularySelectionDialog.select(
150
		                    Messages.FeatureTreeDropAdapter_CHOOSE_VOC, viewer.getControl().getShell(), null);
151
		            if(vocabulary instanceof OrderedTermVocabulary){
152
		                MessagingUtils.warningDialog(Messages.FeatureTreeDropAdapter_IMPORT_NOT_POSSIBLE, this,
153
		                        Messages.FeatureTreeDropAdapter_ORDER_VOC_NOT_POSSIBLE);
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)