ref #7837 Make feature tree export experimental
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / featuretree / e4 / FeatureNodeDropAdapter.java
index 9a046b9ddfe5842b9a026786e5917170c6169931..078e9cd5e483ec526789eeaad44303139e1e4d88 100644 (file)
@@ -13,12 +13,14 @@ import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 
+import org.eclipse.core.runtime.NullProgressMonitor;
 import org.eclipse.e4.ui.model.application.ui.MDirtyable;
 import org.eclipse.jface.util.LocalSelectionTransfer;
 import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.jface.viewers.TreeViewer;
 import org.eclipse.jface.viewers.Viewer;
 import org.eclipse.jface.viewers.ViewerDropAdapter;
+import org.eclipse.swt.dnd.DND;
 import org.eclipse.swt.dnd.TransferData;
 
 import eu.etaxonomy.cdm.api.service.IFeatureNodeService;
@@ -34,18 +36,32 @@ import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.store.CdmStore;
 import eu.etaxonomy.taxeditor.ui.dialog.selection.TermVocabularySelectionDialog;
 import eu.etaxonomy.taxeditor.view.webimport.termimport.wrapper.OntologyTermWrapper;
+import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
 
 public class FeatureNodeDropAdapter extends ViewerDropAdapter {
 
+    public static final String SAVE_CHANGES_TITLE = "Unsaved changes";
+    public static final String SAVE_CHANGES_MESSAGE = "The editor has unsaved changes. You need to save before performind this operation. Save now?";
+
     private MDirtyable dirtyable;
+    private IE4SavablePart savablePart;
 
-    public FeatureNodeDropAdapter(MDirtyable dirtyable, Viewer viewer) {
+    public FeatureNodeDropAdapter(MDirtyable dirtyable, IE4SavablePart savablePart, Viewer viewer) {
                super(viewer);
                this.dirtyable = dirtyable;
+               this.savablePart = savablePart;
        }
 
        @Override
        public boolean performDrop(Object data) {
+        if(dirtyable.isDirty()){
+            if(MessagingUtils.confirmDialog(SAVE_CHANGES_TITLE, SAVE_CHANGES_MESSAGE)){
+                savablePart.save(new NullProgressMonitor());
+            }
+            else{
+                return false;
+            }
+        }
                Object currentTarget = getCurrentTarget();
                FeatureNode target = null;
                if(currentTarget instanceof FeatureTree){
@@ -59,14 +75,16 @@ public class FeatureNodeDropAdapter extends ViewerDropAdapter {
                if (target != null) {
                        int location = getCurrentLocation();
                        FeatureNode parent = target.getParent();
-                       if (location == LOCATION_BEFORE) {
-                               position = Math.max(0, parent.getIndex(target) - 1);
-                               target = parent;
-                       }
+                       if(parent!=null){
+                           if (location == LOCATION_BEFORE) {
+                               position = Math.max(0, parent.getIndex(target) - 1);
+                               target = parent;
+                           }
 
-                       if (location == LOCATION_AFTER) {
-                               position = parent.getIndex(target);
-                               target = parent;
+                           if (location == LOCATION_AFTER) {
+                               position = parent.getIndex(target);
+                               target = parent;
+                           }
                        }
                }
 
@@ -74,7 +92,6 @@ public class FeatureNodeDropAdapter extends ViewerDropAdapter {
             MessagingUtils.warningDialog(Messages.FeatureNodeDropAdapter_INVALID_TARGET, this, Messages.FeatureNodeDropAdapter_INVALID_TARGET_MESSAGE);
             return false;
         }
-
                Collection<Object> droppedObjects = Collections.emptyList();
                if(data instanceof Object[]){
                    droppedObjects = Arrays.asList((Object[])data);
@@ -86,6 +103,11 @@ public class FeatureNodeDropAdapter extends ViewerDropAdapter {
 
                // cannot drop a feature node onto itself
                for (Object droppedObject : droppedObjects) {
+                   if(droppedObject==null){
+                       MessagingUtils.warningDialog("Move failed", this.getClass(),
+                               "Moving the feature node failed. Try saving before.");
+                       return false;
+                   }
                        if (droppedObject.equals(target)) {
                                return false;
                        }
@@ -93,7 +115,14 @@ public class FeatureNodeDropAdapter extends ViewerDropAdapter {
                for (Object droppedObject : droppedObjects) {
                    if(droppedObject instanceof FeatureNode){
                        FeatureNode droppedNode = (FeatureNode) droppedObject;
-                       CdmStore.getService(IFeatureNodeService.class).moveFeatureNode(droppedNode.getUuid(), target.getUuid(), position);
+                       //move operation
+                       if(getCurrentOperation()==DND.DROP_MOVE){
+                           CdmStore.getService(IFeatureNodeService.class).moveFeatureNode(droppedNode.getUuid(), target.getUuid(), position);
+                       }
+                       //copy operation
+                       else if(getCurrentOperation()==DND.DROP_COPY){
+                           CdmStore.getService(IFeatureNodeService.class).addChildFeatureNode(target.getUuid(), droppedNode.getFeature().getUuid());
+                       }
                        viewer.reveal(droppedNode);
                    }
                    else if(droppedObject instanceof Feature){
@@ -102,15 +131,21 @@ public class FeatureNodeDropAdapter extends ViewerDropAdapter {
                    }
                    else if(droppedObject instanceof OntologyTermWrapper){
                        OntologyTermWrapper wrapper = (OntologyTermWrapper)droppedObject;
-                       TermVocabulary vocabulary = TermVocabularySelectionDialog.select(
-                               "Choose vocabulary for import", viewer.getControl().getShell(), null);
-                       if(vocabulary instanceof OrderedTermVocabulary){
-                           MessagingUtils.warningDialog("Import not possible", this,
-                                   "The chosen vocabulary is an ordered vocabulary.\n"
-                                   + "Importing into ordered vocabularies is currently not supported.");
+                       TermVocabulary vocabulary = wrapper.getTermVocabulary();
+                       if(vocabulary==null){
+                           vocabulary =  TermVocabularySelectionDialog.select(
+                                   "Choose vocabulary for import", viewer.getControl().getShell(), null);
+                           if(vocabulary instanceof OrderedTermVocabulary){
+                               MessagingUtils.warningDialog("Import not possible", this,
+                                       "The chosen vocabulary is an ordered vocabulary.\n"
+                                               + "Importing into ordered vocabularies is currently not supported.");
+                               return false;
+                           }
+                       }
+                       if(vocabulary==null){
                            return false;
                        }
-                       if(vocabulary!=null){
+                       else{
                            Feature feature = Feature.NewInstance(wrapper.getDescription(), wrapper.getLabel(), null);
                            feature.setUri(URI.create(wrapper.getUri()));
                            vocabulary.addTerm(feature);
@@ -118,7 +153,6 @@ public class FeatureNodeDropAdapter extends ViewerDropAdapter {
                        }
                    }
                }
-               dirtyable.setDirty(true);
                viewer.refresh();
                return true;
        }