ref #6806 Fix dirty state handling and saving in feature tree editor
[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.core.runtime.NullProgressMonitor;
17 import org.eclipse.e4.ui.model.application.ui.MDirtyable;
18 import org.eclipse.jface.util.LocalSelectionTransfer;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.jface.viewers.TreeViewer;
21 import org.eclipse.jface.viewers.Viewer;
22 import org.eclipse.jface.viewers.ViewerDropAdapter;
23 import org.eclipse.swt.dnd.DND;
24 import org.eclipse.swt.dnd.TransferData;
25
26 import eu.etaxonomy.cdm.api.service.IFeatureNodeService;
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.taxeditor.editor.definedterm.TermTransfer;
33 import eu.etaxonomy.taxeditor.featuretree.FeatureNodeTransfer;
34 import eu.etaxonomy.taxeditor.l10n.Messages;
35 import eu.etaxonomy.taxeditor.model.MessagingUtils;
36 import eu.etaxonomy.taxeditor.store.CdmStore;
37 import eu.etaxonomy.taxeditor.ui.dialog.selection.TermVocabularySelectionDialog;
38 import eu.etaxonomy.taxeditor.view.webimport.termimport.wrapper.OntologyTermWrapper;
39 import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
40
41 public class FeatureNodeDropAdapter extends ViewerDropAdapter {
42
43 public static final String SAVE_CHANGES_TITLE = "Unsaved changes";
44 public static final String SAVE_CHANGES_MESSAGE = "The editor has unsaved changes. You need to save before performind this operation. Save now?";
45
46 private MDirtyable dirtyable;
47 private IE4SavablePart savablePart;
48
49 public FeatureNodeDropAdapter(MDirtyable dirtyable, IE4SavablePart savablePart, Viewer viewer) {
50 super(viewer);
51 this.dirtyable = dirtyable;
52 this.savablePart = savablePart;
53 }
54
55 @Override
56 public boolean performDrop(Object data) {
57 if(dirtyable.isDirty()){
58 if(MessagingUtils.confirmDialog(SAVE_CHANGES_TITLE, SAVE_CHANGES_MESSAGE)){
59 savablePart.save(new NullProgressMonitor());
60 }
61 else{
62 return false;
63 }
64 }
65 Object currentTarget = getCurrentTarget();
66 FeatureNode target = null;
67 if(currentTarget instanceof FeatureTree){
68 target = ((FeatureTree) currentTarget).getRoot();
69 }
70 else if(currentTarget instanceof FeatureNode){
71 target = (FeatureNode) currentTarget;
72 }
73 int position = 0;
74
75 if (target != null) {
76 int location = getCurrentLocation();
77 FeatureNode parent = target.getParent();
78 if(parent!=null){
79 if (location == LOCATION_BEFORE) {
80 position = Math.max(0, parent.getIndex(target) - 1);
81 target = parent;
82 }
83
84 if (location == LOCATION_AFTER) {
85 position = parent.getIndex(target);
86 target = parent;
87 }
88 }
89 }
90
91 if(target==null){
92 MessagingUtils.warningDialog(Messages.FeatureNodeDropAdapter_INVALID_TARGET, this, Messages.FeatureNodeDropAdapter_INVALID_TARGET_MESSAGE);
93 return false;
94 }
95 Collection<Object> droppedObjects = Collections.emptyList();
96 if(data instanceof Object[]){
97 droppedObjects = Arrays.asList((Object[])data);
98 }
99 else if(data instanceof IStructuredSelection){
100 droppedObjects = ((IStructuredSelection) data).toList();
101 }
102 TreeViewer viewer = (TreeViewer) getViewer();
103
104 // cannot drop a feature node onto itself
105 for (Object droppedObject : droppedObjects) {
106 if(droppedObject==null){
107 MessagingUtils.warningDialog("Move failed", this.getClass(),
108 "Moving the feature node failed. Try saving before.");
109 return false;
110 }
111 if (droppedObject.equals(target)) {
112 return false;
113 }
114 }
115 for (Object droppedObject : droppedObjects) {
116 if(droppedObject instanceof FeatureNode){
117 FeatureNode droppedNode = (FeatureNode) droppedObject;
118 //move operation
119 if(getCurrentOperation()==DND.DROP_MOVE){
120 CdmStore.getService(IFeatureNodeService.class).moveFeatureNode(droppedNode.getUuid(), target.getUuid(), position);
121 }
122 //copy operation
123 else if(getCurrentOperation()==DND.DROP_COPY){
124 CdmStore.getService(IFeatureNodeService.class).addChildFeatureNode(target.getUuid(), droppedNode.getFeature().getUuid());
125 }
126 viewer.reveal(droppedNode);
127 }
128 else if(droppedObject instanceof Feature){
129 Feature droppedFeature = (Feature) droppedObject;
130 CdmStore.getService(IFeatureNodeService.class).addChildFeatureNode(target.getUuid(), droppedFeature.getUuid());
131 }
132 else if(droppedObject instanceof OntologyTermWrapper){
133 OntologyTermWrapper wrapper = (OntologyTermWrapper)droppedObject;
134 TermVocabulary vocabulary = wrapper.getTermVocabulary();
135 if(vocabulary==null){
136 vocabulary = TermVocabularySelectionDialog.select(
137 "Choose vocabulary for import", viewer.getControl().getShell(), null);
138 if(vocabulary instanceof OrderedTermVocabulary){
139 MessagingUtils.warningDialog("Import not possible", this,
140 "The chosen vocabulary is an ordered vocabulary.\n"
141 + "Importing into ordered vocabularies is currently not supported.");
142 return false;
143 }
144 }
145 if(vocabulary==null){
146 return false;
147 }
148 else{
149 Feature feature = Feature.NewInstance(wrapper.getDescription(), wrapper.getLabel(), null);
150 feature.setUri(URI.create(wrapper.getUri()));
151 vocabulary.addTerm(feature);
152 CdmStore.getService(IFeatureNodeService.class).createChildFeatureNode(target, feature);
153 }
154 }
155 }
156 viewer.refresh();
157 return true;
158 }
159
160 @Override
161 public boolean validateDrop(Object target, int operation, TransferData transferData) {
162 boolean isSupported = FeatureNodeTransfer.getInstance().isSupportedType(transferData);
163 isSupported |= TermTransfer.getInstance().isSupportedType(transferData);
164 isSupported |= LocalSelectionTransfer.getTransfer().isSupportedType(transferData);
165 isSupported &= getViewer().getInput()!=null;
166 return isSupported;
167 }
168
169 }