Project

General

Profile

Download (6.02 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
    private IE4SavablePart savablePart;
43

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

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

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

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

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

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

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

    
157
}
(2-2/4)