Project

General

Profile

Download (5.92 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.model.application.ui.MDirtyable;
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.model.common.OrderedTermVocabulary;
27
import eu.etaxonomy.cdm.model.common.TermVocabulary;
28
import eu.etaxonomy.cdm.model.description.Feature;
29
import eu.etaxonomy.cdm.model.description.FeatureNode;
30
import eu.etaxonomy.cdm.model.description.FeatureTree;
31
import eu.etaxonomy.taxeditor.editor.definedterm.TermTransfer;
32
import eu.etaxonomy.taxeditor.featuretree.FeatureNodeTransfer;
33
import eu.etaxonomy.taxeditor.l10n.Messages;
34
import eu.etaxonomy.taxeditor.model.MessagingUtils;
35
import eu.etaxonomy.taxeditor.store.CdmStore;
36
import eu.etaxonomy.taxeditor.ui.dialog.selection.TermVocabularySelectionDialog;
37
import eu.etaxonomy.taxeditor.view.webimport.termimport.wrapper.OntologyTermWrapper;
38

    
39
public class FeatureNodeDropAdapter extends ViewerDropAdapter {
40

    
41
    private MDirtyable dirtyable;
42

    
43
    public FeatureNodeDropAdapter(MDirtyable dirtyable, Viewer viewer) {
44
		super(viewer);
45
		this.dirtyable = dirtyable;
46
	}
47

    
48
	@Override
49
	public boolean performDrop(Object data) {
50
		Object currentTarget = getCurrentTarget();
51
		FeatureNode target = null;
52
		if(currentTarget instanceof FeatureTree){
53
		    target = ((FeatureTree) currentTarget).getRoot();
54
		}
55
		else if(currentTarget instanceof FeatureNode){
56
		    target = (FeatureNode) currentTarget;
57
		}
58
		int position = 0;
59

    
60
		if (target != null) {
61
			int location = getCurrentLocation();
62
			FeatureNode parent = target.getParent();
63
			if(parent!=null){
64
			    if (location == LOCATION_BEFORE) {
65
			        position = Math.max(0, parent.getIndex(target) - 1);
66
			        target = parent;
67
			    }
68

    
69
			    if (location == LOCATION_AFTER) {
70
			        position = parent.getIndex(target);
71
			        target = parent;
72
			    }
73
			}
74
		}
75

    
76
        if(target==null){
77
            MessagingUtils.warningDialog(Messages.FeatureNodeDropAdapter_INVALID_TARGET, this, Messages.FeatureNodeDropAdapter_INVALID_TARGET_MESSAGE);
78
            return false;
79
        }
80
		Collection<Object> droppedObjects = Collections.emptyList();
81
		if(data instanceof Object[]){
82
		    droppedObjects = Arrays.asList((Object[])data);
83
		}
84
		else if(data instanceof IStructuredSelection){
85
		    droppedObjects = ((IStructuredSelection) data).toList();
86
		}
87
		TreeViewer viewer = (TreeViewer) getViewer();
88

    
89
		// cannot drop a feature node onto itself
90
		for (Object droppedObject : droppedObjects) {
91
		    if(droppedObject==null){
92
		        MessagingUtils.warningDialog("Move failed", this.getClass(),
93
		                "Moving the feature node failed. Try saving before.");
94
		        return false;
95
		    }
96
			if (droppedObject.equals(target)) {
97
				return false;
98
			}
99
		}
100
		for (Object droppedObject : droppedObjects) {
101
		    if(droppedObject instanceof FeatureNode){
102
		        FeatureNode droppedNode = (FeatureNode) droppedObject;
103
		        //move operation
104
		        if(getCurrentOperation()==DND.DROP_MOVE){
105
		            CdmStore.getService(IFeatureNodeService.class).moveFeatureNode(droppedNode.getUuid(), target.getUuid(), position);
106
		        }
107
		        //copy operation
108
		        else if(getCurrentOperation()==DND.DROP_COPY){
109
		            CdmStore.getService(IFeatureNodeService.class).addChildFeatureNode(target.getUuid(), droppedNode.getFeature().getUuid());
110
		        }
111
		        viewer.reveal(droppedNode);
112
		    }
113
		    else if(droppedObject instanceof Feature){
114
		        Feature droppedFeature = (Feature) droppedObject;
115
		        CdmStore.getService(IFeatureNodeService.class).addChildFeatureNode(target.getUuid(), droppedFeature.getUuid());
116
		    }
117
		    else if(droppedObject instanceof OntologyTermWrapper){
118
		        OntologyTermWrapper wrapper = (OntologyTermWrapper)droppedObject;
119
		        TermVocabulary vocabulary = wrapper.getTermVocabulary();
120
		        if(vocabulary==null){
121
		            vocabulary =  TermVocabularySelectionDialog.select(
122
		                    "Choose vocabulary for import", viewer.getControl().getShell(), null);
123
		            if(vocabulary instanceof OrderedTermVocabulary){
124
		                MessagingUtils.warningDialog("Import not possible", this,
125
		                        "The chosen vocabulary is an ordered vocabulary.\n"
126
		                                + "Importing into ordered vocabularies is currently not supported.");
127
		                return false;
128
		            }
129
		        }
130
		        if(vocabulary==null){
131
		            return false;
132
		        }
133
		        else{
134
		            Feature feature = Feature.NewInstance(wrapper.getDescription(), wrapper.getLabel(), null);
135
		            feature.setUri(URI.create(wrapper.getUri()));
136
		            vocabulary.addTerm(feature);
137
		            CdmStore.getService(IFeatureNodeService.class).createChildFeatureNode(target, feature);
138
		        }
139
		    }
140
		}
141
		dirtyable.setDirty(true);
142
		viewer.refresh();
143
		return true;
144
	}
145

    
146
    @Override
147
    public boolean validateDrop(Object target, int operation, TransferData transferData) {
148
        boolean isSupported = FeatureNodeTransfer.getInstance().isSupportedType(transferData);
149
        isSupported |= TermTransfer.getInstance().isSupportedType(transferData);
150
        isSupported |= LocalSelectionTransfer.getTransfer().isSupportedType(transferData);
151
        isSupported &= getViewer().getInput()!=null;
152
        return isSupported;
153
    }
154

    
155
}
(2-2/4)