Project

General

Profile

Download (5.68 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.TransferData;
23

    
24
import eu.etaxonomy.cdm.api.service.IFeatureNodeService;
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.taxeditor.editor.definedterm.TermTransfer;
31
import eu.etaxonomy.taxeditor.featuretree.FeatureNodeTransfer;
32
import eu.etaxonomy.taxeditor.l10n.Messages;
33
import eu.etaxonomy.taxeditor.model.MessagingUtils;
34
import eu.etaxonomy.taxeditor.store.CdmStore;
35
import eu.etaxonomy.taxeditor.ui.dialog.selection.TermVocabularySelectionDialog;
36
import eu.etaxonomy.taxeditor.view.webimport.termimport.parser.ParserUtil;
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

    
81
		Collection<Object> droppedObjects = Collections.emptyList();
82
		if(data instanceof Object[]){
83
		    droppedObjects = Arrays.asList((Object[])data);
84
		}
85
		else if(data instanceof IStructuredSelection){
86
		    droppedObjects = ((IStructuredSelection) data).toList();
87
		}
88
		TreeViewer viewer = (TreeViewer) getViewer();
89

    
90
		// cannot drop a feature node onto itself
91
		for (Object droppedObject : droppedObjects) {
92
			if (droppedObject.equals(target)) {
93
				return false;
94
			}
95
		}
96
		for (Object droppedObject : droppedObjects) {
97
		    if(droppedObject instanceof FeatureNode){
98
		        FeatureNode droppedNode = (FeatureNode) droppedObject;
99
		        CdmStore.getService(IFeatureNodeService.class).moveFeatureNode(droppedNode.getUuid(), target.getUuid(), position);
100
		        viewer.reveal(droppedNode);
101
		    }
102
		    else if(droppedObject instanceof Feature){
103
		        Feature droppedFeature = (Feature) droppedObject;
104
		        CdmStore.getService(IFeatureNodeService.class).addChildFeatureNode(target.getUuid(), droppedFeature.getUuid());
105
		    }
106
		    else if(droppedObject instanceof OntologyTermWrapper){
107
		        OntologyTermWrapper wrapper = (OntologyTermWrapper)droppedObject;
108
		        TermVocabulary vocabulary = wrapper.getTermVocabulary();
109
		        if(vocabulary==null){
110
		            vocabulary =  TermVocabularySelectionDialog.select(
111
		                    "Choose vocabulary for import", viewer.getControl().getShell(), null);
112
		            if(vocabulary instanceof OrderedTermVocabulary){
113
		                MessagingUtils.warningDialog("Import not possible", this,
114
		                        "The chosen vocabulary is an ordered vocabulary.\n"
115
		                                + "Importing into ordered vocabularies is currently not supported.");
116
		                return false;
117
		            }
118
		        }
119
		        if(vocabulary!=null){
120
		            if(wrapper.getDescription()==null && MessagingUtils.confirmDialog("Load Details?", "Not all details for this term have been loaded yet.\n"
121
		                    + "Do you want to load the details before adding it?")){
122
		                ParserUtil.loadDetails(wrapper);
123
		            }
124
		            Feature feature = Feature.NewInstance(wrapper.getDescription(), wrapper.getLabel(), null);
125
		            feature.setUri(URI.create(wrapper.getUri()));
126
		            vocabulary.addTerm(feature);
127
		            CdmStore.getService(IFeatureNodeService.class).createChildFeatureNode(target, feature);
128
		        }
129
		    }
130
		}
131
		dirtyable.setDirty(true);
132
		viewer.refresh();
133
		return true;
134
	}
135

    
136
    @Override
137
    public boolean validateDrop(Object target, int operation, TransferData transferData) {
138
        boolean isSupported = FeatureNodeTransfer.getInstance().isSupportedType(transferData);
139
        isSupported |= TermTransfer.getInstance().isSupportedType(transferData);
140
        isSupported |= LocalSelectionTransfer.getTransfer().isSupportedType(transferData);
141
        isSupported &= getViewer().getInput()!=null;
142
        return isSupported;
143
    }
144

    
145
}
(2-2/5)