ref #7806 Do not set dirty flag when voc selection was canceled
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / featuretree / e4 / FeatureNodeDropAdapter.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.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.wrapper.OntologyTermWrapper;
37
38 public class FeatureNodeDropAdapter extends ViewerDropAdapter {
39
40 private MDirtyable dirtyable;
41
42 public FeatureNodeDropAdapter(MDirtyable dirtyable, Viewer viewer) {
43 super(viewer);
44 this.dirtyable = dirtyable;
45 }
46
47 @Override
48 public boolean performDrop(Object data) {
49 Object currentTarget = getCurrentTarget();
50 FeatureNode target = null;
51 if(currentTarget instanceof FeatureTree){
52 target = ((FeatureTree) currentTarget).getRoot();
53 }
54 else if(currentTarget instanceof FeatureNode){
55 target = (FeatureNode) currentTarget;
56 }
57 int position = 0;
58
59 if (target != null) {
60 int location = getCurrentLocation();
61 FeatureNode parent = target.getParent();
62 if(parent!=null){
63 if (location == LOCATION_BEFORE) {
64 position = Math.max(0, parent.getIndex(target) - 1);
65 target = parent;
66 }
67
68 if (location == LOCATION_AFTER) {
69 position = parent.getIndex(target);
70 target = parent;
71 }
72 }
73 }
74
75 if(target==null){
76 MessagingUtils.warningDialog(Messages.FeatureNodeDropAdapter_INVALID_TARGET, this, Messages.FeatureNodeDropAdapter_INVALID_TARGET_MESSAGE);
77 return false;
78 }
79 Collection<Object> droppedObjects = Collections.emptyList();
80 if(data instanceof Object[]){
81 droppedObjects = Arrays.asList((Object[])data);
82 }
83 else if(data instanceof IStructuredSelection){
84 droppedObjects = ((IStructuredSelection) data).toList();
85 }
86 TreeViewer viewer = (TreeViewer) getViewer();
87
88 // cannot drop a feature node onto itself
89 for (Object droppedObject : droppedObjects) {
90 if(droppedObject==null){
91 MessagingUtils.warningDialog("Move failed", this.getClass(),
92 "Moving the feature node failed. Try saving before.");
93 return false;
94 }
95 if (droppedObject.equals(target)) {
96 return false;
97 }
98 }
99 for (Object droppedObject : droppedObjects) {
100 if(droppedObject instanceof FeatureNode){
101 FeatureNode droppedNode = (FeatureNode) droppedObject;
102 CdmStore.getService(IFeatureNodeService.class).moveFeatureNode(droppedNode.getUuid(), target.getUuid(), position);
103 viewer.reveal(droppedNode);
104 }
105 else if(droppedObject instanceof Feature){
106 Feature droppedFeature = (Feature) droppedObject;
107 CdmStore.getService(IFeatureNodeService.class).addChildFeatureNode(target.getUuid(), droppedFeature.getUuid());
108 }
109 else if(droppedObject instanceof OntologyTermWrapper){
110 OntologyTermWrapper wrapper = (OntologyTermWrapper)droppedObject;
111 TermVocabulary vocabulary = wrapper.getTermVocabulary();
112 if(vocabulary==null){
113 vocabulary = TermVocabularySelectionDialog.select(
114 "Choose vocabulary for import", viewer.getControl().getShell(), null);
115 if(vocabulary instanceof OrderedTermVocabulary){
116 MessagingUtils.warningDialog("Import not possible", this,
117 "The chosen vocabulary is an ordered vocabulary.\n"
118 + "Importing into ordered vocabularies is currently not supported.");
119 return false;
120 }
121 }
122 if(vocabulary==null){
123 return false;
124 }
125 else{
126 Feature feature = Feature.NewInstance(wrapper.getDescription(), wrapper.getLabel(), null);
127 feature.setUri(URI.create(wrapper.getUri()));
128 vocabulary.addTerm(feature);
129 CdmStore.getService(IFeatureNodeService.class).createChildFeatureNode(target, feature);
130 }
131 }
132 }
133 dirtyable.setDirty(true);
134 viewer.refresh();
135 return true;
136 }
137
138 @Override
139 public boolean validateDrop(Object target, int operation, TransferData transferData) {
140 boolean isSupported = FeatureNodeTransfer.getInstance().isSupportedType(transferData);
141 isSupported |= TermTransfer.getInstance().isSupportedType(transferData);
142 isSupported |= LocalSelectionTransfer.getTransfer().isSupportedType(transferData);
143 isSupported &= getViewer().getInput()!=null;
144 return isSupported;
145 }
146
147 }