ref #7873 Allow paste on FeatureTrees
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / featuretree / e4 / FeatureTreeDropAdapter.java
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.OrderedTermVocabulary;
28 import eu.etaxonomy.cdm.model.common.TermVocabulary;
29 import eu.etaxonomy.cdm.model.description.Feature;
30 import eu.etaxonomy.cdm.model.description.FeatureNode;
31 import eu.etaxonomy.cdm.model.description.FeatureTree;
32 import eu.etaxonomy.cdm.persistence.dto.TermDto;
33 import eu.etaxonomy.taxeditor.editor.definedterm.TermTransfer;
34 import eu.etaxonomy.taxeditor.featuretree.FeatureNodeTransfer;
35 import eu.etaxonomy.taxeditor.featuretree.e4.operation.AddFeatureOperation;
36 import eu.etaxonomy.taxeditor.l10n.Messages;
37 import eu.etaxonomy.taxeditor.model.AbstractUtility;
38 import eu.etaxonomy.taxeditor.model.MessagingUtils;
39 import eu.etaxonomy.taxeditor.store.CdmStore;
40 import eu.etaxonomy.taxeditor.store.StoreUtil;
41 import eu.etaxonomy.taxeditor.ui.dialog.selection.TermVocabularySelectionDialog;
42 import eu.etaxonomy.taxeditor.view.webimport.termimport.wrapper.OntologyTermWrapper;
43
44 public class FeatureTreeDropAdapter extends ViewerDropAdapter {
45
46 protected IFeatureTreeEditor editor;
47 private UISynchronize sync;
48
49 public FeatureTreeDropAdapter(IFeatureTreeEditor editor, Viewer viewer, UISynchronize sync) {
50 super(viewer);
51 this.editor = editor;
52 this.sync = sync;
53 }
54
55 @Override
56 public boolean performDrop(Object data) {
57 if(StoreUtil.promptCheckIsDirty(editor)){
58 return false;
59 }
60 Object currentTarget = getCurrentTarget();
61 FeatureNode target = null;
62 if(currentTarget instanceof FeatureTree){
63 target = ((FeatureTree) currentTarget).getRoot();
64 }
65 else if(currentTarget instanceof FeatureNode){
66 target = (FeatureNode) currentTarget;
67 }
68 int position = 0;
69
70 if (target != null) {
71 int location = getCurrentLocation();
72 FeatureNode parent = target.getParent();
73 if(parent!=null){
74 if (location == LOCATION_BEFORE) {
75 position = Math.max(0, parent.getIndex(target) - 1);
76 target = parent;
77 }
78
79 if (location == LOCATION_AFTER) {
80 position = parent.getIndex(target);
81 target = parent;
82 }
83 }
84 }
85
86 if(target==null){
87 MessagingUtils.warningDialog(Messages.FeatureNodeDropAdapter_INVALID_TARGET, this, Messages.FeatureNodeDropAdapter_INVALID_TARGET_MESSAGE);
88 return false;
89 }
90 Collection<Object> droppedObjects = Collections.emptyList();
91 if(data instanceof Object[]){
92 droppedObjects = Arrays.asList((Object[])data);
93 }
94 else if(data instanceof IStructuredSelection){
95 droppedObjects = ((IStructuredSelection) data).toList();
96 }
97 TreeViewer viewer = (TreeViewer) getViewer();
98
99 // cannot drop a feature node onto itself
100 for (Object droppedObject : droppedObjects) {
101 if(droppedObject==null){
102 MessagingUtils.warningDialog("Move failed", this.getClass(),
103 "Moving the feature node failed. Try saving before.");
104 return false;
105 }
106 if (droppedObject.equals(target)) {
107 return false;
108 }
109 }
110 for (Object droppedObject : droppedObjects) {
111 if(droppedObject instanceof FeatureNode){
112 FeatureNode droppedNode = (FeatureNode) droppedObject;
113 //move operation
114 if(getCurrentOperation()==DND.DROP_MOVE){
115 CdmStore.getService(IFeatureNodeService.class).moveFeatureNode(droppedNode.getUuid(), target.getUuid(), position);
116 }
117 //copy operation
118 else if(getCurrentOperation()==DND.DROP_COPY){
119 CdmStore.getService(IFeatureNodeService.class).addChildFeatureNode(target.getUuid(), droppedNode.getFeature().getUuid());
120 }
121 viewer.reveal(droppedNode);
122 }
123 else if(droppedObject instanceof Feature){
124 Feature droppedFeature = (Feature) droppedObject;
125 AddFeatureOperation operation = new AddFeatureOperation(
126 (Feature) CdmStore.getService(ITermService.class).load(droppedFeature.getUuid()),
127 target, editor, editor);
128 AbstractUtility.executeOperation(operation, sync);
129 }
130 else if(droppedObject instanceof TermDto){
131 TermDto termDto = (TermDto) droppedObject;
132 AddFeatureOperation operation = new AddFeatureOperation(
133 (Feature) CdmStore.getService(ITermService.class).load(termDto.getUuid()),
134 target, editor, editor);
135 AbstractUtility.executeOperation(operation, sync);
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 "Choose vocabulary for import", viewer.getControl().getShell(), null);
143 if(vocabulary instanceof OrderedTermVocabulary){
144 MessagingUtils.warningDialog("Import not possible", this,
145 "The chosen vocabulary is an ordered vocabulary.\n"
146 + "Importing into ordered vocabularies is currently not supported.");
147 return false;
148 }
149 }
150 if(vocabulary==null){
151 return false;
152 }
153 else{
154 Feature feature = Feature.NewInstance(wrapper.getDescription(), wrapper.getLabel(), null);
155 feature.setUri(URI.create(wrapper.getUri()));
156 vocabulary.addTerm(feature);
157 CdmStore.getService(IFeatureNodeService.class).createChildFeatureNode(target, feature);
158 }
159 }
160 }
161 viewer.refresh();
162 return true;
163 }
164
165 @Override
166 public boolean validateDrop(Object target, int operation, TransferData transferData) {
167 boolean isSupported = FeatureNodeTransfer.getInstance().isSupportedType(transferData);
168 isSupported |= TermTransfer.getInstance().isSupportedType(transferData);
169 isSupported |= LocalSelectionTransfer.getTransfer().isSupportedType(transferData);
170 isSupported |= TermTransfer.getInstance().isSupportedType(transferData);
171 isSupported &= getViewer().getInput()!=null;
172 return isSupported;
173 }
174
175 }