fix #6805 Handle copy/move DND in operations
[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.TransferData;
23
24 import eu.etaxonomy.cdm.api.service.IFeatureNodeService;
25 import eu.etaxonomy.cdm.api.service.ITermService;
26 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
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.editor.definedterm.e4.DefinedTermDropAdapterE4;
35 import eu.etaxonomy.taxeditor.featuretree.FeatureNodeTransfer;
36 import eu.etaxonomy.taxeditor.featuretree.e4.operation.AddFeatureOperation;
37 import eu.etaxonomy.taxeditor.featuretree.e4.operation.MoveFeatureOperation;
38 import eu.etaxonomy.taxeditor.l10n.Messages;
39 import eu.etaxonomy.taxeditor.model.AbstractUtility;
40 import eu.etaxonomy.taxeditor.model.MessagingUtils;
41 import eu.etaxonomy.taxeditor.store.CdmStore;
42 import eu.etaxonomy.taxeditor.store.StoreUtil;
43 import eu.etaxonomy.taxeditor.ui.dialog.selection.TermVocabularySelectionDialog;
44 import eu.etaxonomy.taxeditor.view.webimport.termimport.wrapper.OntologyTermWrapper;
45
46 public class FeatureTreeDropAdapter extends ViewerDropAdapter {
47
48 protected IFeatureTreeEditor editor;
49 private UISynchronize sync;
50
51 public FeatureTreeDropAdapter(IFeatureTreeEditor editor, Viewer viewer, UISynchronize sync) {
52 super(viewer);
53 this.editor = editor;
54 this.sync = sync;
55 }
56
57 @Override
58 public boolean performDrop(Object data) {
59 if(StoreUtil.promptCheckIsDirty(editor)){
60 return false;
61 }
62 Object currentTarget = getCurrentTarget();
63 FeatureNode target = null;
64 if(currentTarget instanceof FeatureTree){
65 target = ((FeatureTree) currentTarget).getRoot();
66 }
67 else if(currentTarget instanceof FeatureNode){
68 target = (FeatureNode) currentTarget;
69 }
70 int position = 0;
71
72 if (target != null) {
73 int location = getCurrentLocation();
74 FeatureNode parent = target.getParent();
75 if(parent!=null){
76 if (location == LOCATION_BEFORE) {
77 position = Math.max(0, parent.getIndex(target) - 1);
78 target = parent;
79 }
80
81 if (location == LOCATION_AFTER) {
82 position = parent.getIndex(target);
83 target = parent;
84 }
85 }
86 }
87
88 if(target==null){
89 MessagingUtils.warningDialog("", this, ""); //$NON-NLS-1$ //$NON-NLS-1$
90 return false;
91 }
92 Collection<Object> droppedObjects = Collections.emptyList();
93 if(data instanceof Object[]){
94 droppedObjects = Arrays.asList((Object[])data);
95 }
96 else if(data instanceof IStructuredSelection){
97 droppedObjects = ((IStructuredSelection) data).toList();
98 }
99 TreeViewer viewer = (TreeViewer) getViewer();
100
101 // cannot drop a feature node onto itself
102 for (Object droppedObject : droppedObjects) {
103 if(droppedObject==null){
104 MessagingUtils.warningDialog(DefinedTermDropAdapterE4.MOVE_FAILED, this.getClass(),
105 Messages.FeatureTreeDropAdapter_MOVE_FAILED_SAVE_MESSAGE);
106 return false;
107 }
108 if (droppedObject.equals(target)) {
109 return false;
110 }
111 }
112 for (Object droppedObject : droppedObjects) {
113 if(droppedObject instanceof FeatureNode){
114 FeatureNode droppedNode = (FeatureNode) droppedObject;
115 MoveFeatureOperation operation = new MoveFeatureOperation(droppedNode, target, position, getCurrentOperation(), editor, editor);
116 AbstractUtility.executeOperation(operation, sync);
117 }
118 else if(droppedObject instanceof Feature){
119 Feature droppedFeature = (Feature) droppedObject;
120 AddFeatureOperation operation = new AddFeatureOperation(
121 (Feature) CdmStore.getService(ITermService.class).load(droppedFeature.getUuid()),
122 target, editor, editor);
123 AbstractUtility.executeOperation(operation, sync);
124 }
125 else if(droppedObject instanceof TermDto){
126 TermDto termDto = (TermDto) droppedObject;
127 DefinedTermBase term = CdmStore.getService(ITermService.class).load(termDto.getUuid());
128 if(term.isInstanceOf(Feature.class)){
129 AddFeatureOperation operation = new AddFeatureOperation(
130 (Feature)term,
131 target, editor, editor);
132 AbstractUtility.executeOperation(operation, sync);
133 }
134 else{
135 MessagingUtils.warningDialog(DefinedTermDropAdapterE4.MOVE_FAILED, this, Messages.FeatureTreeDropAdapter_ONLY_MOVE_FEATURES);
136 }
137 }
138 else if(droppedObject instanceof OntologyTermWrapper){
139 OntologyTermWrapper wrapper = (OntologyTermWrapper)droppedObject;
140 TermVocabulary vocabulary = wrapper.getTermVocabulary();
141 if(vocabulary==null){
142 vocabulary = TermVocabularySelectionDialog.select(
143 Messages.FeatureTreeDropAdapter_CHOOSE_VOC, viewer.getControl().getShell(), null);
144 if(vocabulary instanceof OrderedTermVocabulary){
145 MessagingUtils.warningDialog(Messages.FeatureTreeDropAdapter_IMPORT_NOT_POSSIBLE, this,
146 Messages.FeatureTreeDropAdapter_ORDER_VOC_NOT_POSSIBLE);
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
168 isSupported = FeatureNodeTransfer.getInstance().isSupportedType(transferData);
169 isSupported |= TermTransfer.getInstance().isSupportedType(transferData);
170 isSupported |= LocalSelectionTransfer.getTransfer().isSupportedType(transferData);
171 isSupported &= getViewer().getInput()!=null;
172 return isSupported;
173 }
174
175 }