Merge branch 'release/5.18.0'
[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.util.Arrays;
12 import java.util.Collection;
13 import java.util.Collections;
14
15 import org.eclipse.e4.ui.di.UISynchronize;
16 import org.eclipse.jface.util.LocalSelectionTransfer;
17 import org.eclipse.jface.viewers.IStructuredSelection;
18 import org.eclipse.jface.viewers.TreeViewer;
19 import org.eclipse.jface.viewers.Viewer;
20 import org.eclipse.jface.viewers.ViewerDropAdapter;
21 import org.eclipse.swt.dnd.TransferData;
22
23 import eu.etaxonomy.cdm.model.description.Feature;
24 import eu.etaxonomy.cdm.model.term.IHasTermType;
25 import eu.etaxonomy.cdm.model.term.OrderedTermVocabulary;
26 import eu.etaxonomy.cdm.model.term.TermNode;
27 import eu.etaxonomy.cdm.model.term.TermTree;
28 import eu.etaxonomy.cdm.model.term.TermType;
29 import eu.etaxonomy.cdm.model.term.TermVocabulary;
30 import eu.etaxonomy.cdm.persistence.dto.TermDto;
31 import eu.etaxonomy.cdm.persistence.dto.TermNodeDto;
32 import eu.etaxonomy.taxeditor.editor.definedterm.TermTransfer;
33 import eu.etaxonomy.taxeditor.editor.definedterm.e4.DefinedTermDropAdapterE4;
34 import eu.etaxonomy.taxeditor.featuretree.FeatureNodeTransfer;
35 import eu.etaxonomy.taxeditor.featuretree.e4.operation.AddFeatureOperation;
36 import eu.etaxonomy.taxeditor.featuretree.e4.operation.AddOntologyTermOperation;
37 import eu.etaxonomy.taxeditor.featuretree.e4.operation.MoveFeatureOperation;
38 import eu.etaxonomy.taxeditor.l10n.Messages;
39 import eu.etaxonomy.taxeditor.model.MessagingUtils;
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 TermNode target = null;
62 if(currentTarget instanceof TermTree){
63 target = ((TermTree) currentTarget).getRoot();
64 }
65 else if(currentTarget instanceof TermNode){
66 target = (TermNode) currentTarget;
67 }
68
69 if (target.getUuid() == null){
70 if(StoreUtil.promptCheckIsDirty(editor)){
71 return false;
72 }
73 }
74 int position = 0;
75
76 if (target != null) {
77 int location = getCurrentLocation();
78 TermNode parent = target.getParent();
79 if(parent !=null){
80
81 if (location == LOCATION_BEFORE) {
82 position = Math.max(0, parent.getIndex(target));
83 target = parent;
84 }
85
86 if (location == LOCATION_AFTER) {
87 position = parent.getIndex(target)+1;
88 target = parent;
89 }
90 }
91 }
92
93 if(target==null){
94 MessagingUtils.warningDialog("", this, ""); //$NON-NLS-1$ //$NON-NLS-1$
95 return false;
96 }
97 Collection<Object> droppedObjects = Collections.emptyList();
98 if(data instanceof Object[]){
99 droppedObjects = Arrays.asList((Object[])data);
100 }
101 else if(data instanceof IStructuredSelection){
102 droppedObjects = ((IStructuredSelection) data).toList();
103 }
104 TreeViewer viewer = (TreeViewer) getViewer();
105
106 // cannot drop a feature node onto itself
107 for (Object droppedObject : droppedObjects) {
108 if(droppedObject==null){
109 MessagingUtils.warningDialog(DefinedTermDropAdapterE4.MOVE_FAILED, this.getClass(),
110 Messages.FeatureTreeDropAdapter_MOVE_FAILED_SAVE_MESSAGE);
111 return false;
112 }
113 if (droppedObject.equals(target)) {
114 return false;
115 }
116
117 }
118 for (Object droppedObject : droppedObjects) {
119 //check term type compatibility
120 TermType targetType = target.getTermType();
121 TermType droppedType = null;
122 if(droppedObject instanceof IHasTermType){
123 droppedType = ((IHasTermType)droppedObject).getTermType();
124 if(droppedType!=null && !droppedType.equals(targetType) && !droppedType.isKindOf(targetType)){
125 MessagingUtils.warningDialog(DefinedTermDropAdapterE4.TERM_TYPE_ERROR_TITLE, this, DefinedTermDropAdapterE4.TERM_TYPE_ERROR_MESSAGE);
126 continue;
127 }
128 }
129 else if(droppedObject instanceof TermDto){
130 droppedType = ((TermDto)droppedObject).getTermType();
131 if(droppedType!=null && !droppedType.equals(targetType) && !droppedType.isKindOf(targetType)){
132 MessagingUtils.warningDialog(DefinedTermDropAdapterE4.TERM_TYPE_ERROR_TITLE, this, DefinedTermDropAdapterE4.TERM_TYPE_ERROR_MESSAGE);
133 continue;
134 }
135 }
136 if(droppedObject instanceof TermNodeDto){
137 if (((TermNodeDto)droppedObject).getUuid() == null){
138 if(StoreUtil.promptCheckIsDirty(editor)){
139 return false;
140 }
141 }
142 TermNodeDto droppedNode = (TermNodeDto) droppedObject;
143 TermNodeDto oldParent = editor.getNodeDtoForUuid(droppedNode.getParentUuid());
144 int currentPosition = oldParent.getIndex(droppedNode);
145
146 if(currentPosition<position && target.equals(oldParent)){
147 position -= 1;
148 }
149 MoveFeatureOperation operation = new MoveFeatureOperation(droppedNode.getUuid(), target.getUuid(), droppedNode.getTerm().getUuid(), position, getCurrentOperation(), editor, editor);
150
151 Object o = oldParent.getChildren().remove(currentPosition);
152 target.getChildNodes().add(position, droppedNode);
153 droppedNode.setParentUuid(target.getUuid());
154 editor.setDirty();
155 editor.addOperation(operation);
156 }
157 if(droppedObject instanceof TermNode){
158 if (((TermNode)droppedObject).getUuid() == null){
159 if(StoreUtil.promptCheckIsDirty(editor)){
160 return false;
161 }
162 }
163 TermNode droppedNode = (TermNode) droppedObject;
164 TermNode oldParent = droppedNode.getParent();
165 int currentPosition = oldParent.getIndex(droppedNode);
166
167 if(currentPosition<position && target.equals(oldParent)){
168 position -= 1;
169 }
170 MoveFeatureOperation operation = new MoveFeatureOperation(droppedNode.getUuid(), target.getUuid(), droppedNode.getTerm().getUuid(), position, getCurrentOperation(), editor, editor);
171
172 oldParent.removeChild(droppedNode);
173 target.addChild(droppedNode, position);
174
175 editor.setDirty();
176 editor.addOperation(operation);
177 }
178 else if(droppedObject instanceof Feature){
179 Feature droppedFeature = (Feature) droppedObject;
180 AddFeatureOperation operation = new AddFeatureOperation(
181 droppedFeature.getUuid(),
182 target.getUuid(), position, editor, editor);
183 // AbstractUtility.executeOperation(operation, sync);
184 target.addChild(droppedFeature, position);
185 editor.setDirty();
186 editor.addOperation(operation);
187 } else if(droppedObject instanceof OntologyTermWrapper){
188 OntologyTermWrapper wrapper = (OntologyTermWrapper)droppedObject;
189 TermVocabulary vocabulary = wrapper.getTermVocabulary();
190 if(vocabulary==null){
191 vocabulary = TermVocabularySelectionDialog.select(
192 Messages.FeatureTreeDropAdapter_CHOOSE_VOC, viewer.getControl().getShell(), null);
193
194 if(vocabulary instanceof OrderedTermVocabulary){
195 MessagingUtils.warningDialog(Messages.FeatureTreeDropAdapter_IMPORT_NOT_POSSIBLE, this,
196 Messages.FeatureTreeDropAdapter_ORDER_VOC_NOT_POSSIBLE);
197 return false;
198 }
199 if(vocabulary == null){
200 return false;
201 }
202 // vocabulary = new TermVocabulary(voc.getUuid(), voc.getRepresentations(), voc.getTermType(), voc.getTitleCache(), voc.isAllowDuplicates(), voc.isOrderRelevant(), voc.isFlat());
203
204 }
205
206
207 AddOntologyTermOperation operation = new AddOntologyTermOperation(wrapper, target.getUuid(), vocabulary, editor, editor);
208 // AbstractUtility.executeOperation(operation, sync);
209 editor.setDirty();
210 editor.addOperation(operation);
211
212 }
213 }
214 viewer.refresh();
215 // viewer.refresh();
216 return true;
217 }
218
219 @Override
220 public boolean validateDrop(Object target, int operation, TransferData transferData) {
221 boolean
222 isSupported = FeatureNodeTransfer.getInstance().isSupportedType(transferData);
223 isSupported |= TermTransfer.getInstance().isSupportedType(transferData);
224 isSupported |= LocalSelectionTransfer.getTransfer().isSupportedType(transferData);
225 isSupported &= getViewer().getInput()!=null;
226 if(target instanceof TermTree && getCurrentLocation()!=ViewerDropAdapter.LOCATION_ON){
227 isSupported = false;
228 }
229 return isSupported;
230 }
231
232
233
234 }