ref #7837 Make feature tree export experimental
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / featuretree / e4 / FeatureNodeDropAdapter.java
index a9832364dc0dc7a38271a4cf44809e929496b0f0..078e9cd5e483ec526789eeaad44303139e1e4d88 100644 (file)
 */
 package eu.etaxonomy.taxeditor.featuretree.e4;
 
+import java.net.URI;
+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;
+import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
+import eu.etaxonomy.cdm.model.common.TermVocabulary;
+import eu.etaxonomy.cdm.model.description.Feature;
 import eu.etaxonomy.cdm.model.description.FeatureNode;
 import eu.etaxonomy.cdm.model.description.FeatureTree;
+import eu.etaxonomy.taxeditor.editor.definedterm.TermTransfer;
 import eu.etaxonomy.taxeditor.featuretree.FeatureNodeTransfer;
+import eu.etaxonomy.taxeditor.l10n.Messages;
+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 {
 
-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 final FeatureTreeEditor featureTreeEditor;
+    private MDirtyable dirtyable;
+    private IE4SavablePart savablePart;
 
-    protected FeatureNodeDropAdapter(FeatureTreeEditor featureTreeEditor, Viewer viewer) {
+    public FeatureNodeDropAdapter(MDirtyable dirtyable, IE4SavablePart savablePart, Viewer viewer) {
                super(viewer);
-        this.featureTreeEditor = featureTreeEditor;
+               this.dirtyable = dirtyable;
+               this.savablePart = savablePart;
        }
 
        @Override
        public boolean performDrop(Object data) {
-               FeatureNode target = (FeatureNode) getCurrentTarget();
+        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){
+                   target = ((FeatureTree) currentTarget).getRoot();
+               }
+               else if(currentTarget instanceof FeatureNode){
+                   target = (FeatureNode) currentTarget;
+               }
                int position = 0;
 
                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;
+                           }
                        }
                }
 
-               // set target to root node if there is no target specified
-               if (target == null) {
-                       FeatureTree featureTree = (FeatureTree) getViewer().getInput();
-                       target = featureTree.getRoot();
+        if(target==null){
+            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);
+               }
+               else if(data instanceof IStructuredSelection){
+                   droppedObjects = ((IStructuredSelection) data).toList();
                }
-
-               Object[] droppedObjects = (Object[]) data;
                TreeViewer viewer = (TreeViewer) getViewer();
 
                // 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;
                        }
                }
                for (Object droppedObject : droppedObjects) {
-                       FeatureNode droppedNode = (FeatureNode) droppedObject;
-                       CdmStore.getService(IFeatureNodeService.class).moveFeatureNode(droppedNode.getUuid(), target.getUuid(), position);
-                       viewer.refresh();
-                       viewer.reveal(droppedNode);
+                   if(droppedObject instanceof FeatureNode){
+                       FeatureNode droppedNode = (FeatureNode) droppedObject;
+                       //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){
+                       Feature droppedFeature = (Feature) droppedObject;
+                       CdmStore.getService(IFeatureNodeService.class).addChildFeatureNode(target.getUuid(), droppedFeature.getUuid());
+                   }
+                   else if(droppedObject instanceof OntologyTermWrapper){
+                       OntologyTermWrapper wrapper = (OntologyTermWrapper)droppedObject;
+                       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;
+                       }
+                       else{
+                           Feature feature = Feature.NewInstance(wrapper.getDescription(), wrapper.getLabel(), null);
+                           feature.setUri(URI.create(wrapper.getUri()));
+                           vocabulary.addTerm(feature);
+                           CdmStore.getService(IFeatureNodeService.class).createChildFeatureNode(target, feature);
+                       }
+                   }
                }
-        this.featureTreeEditor.setDirty(true);
+               viewer.refresh();
                return true;
        }
 
     @Override
     public boolean validateDrop(Object target, int operation, TransferData transferData) {
-        return FeatureNodeTransfer.getInstance().isSupportedType(transferData);
+        boolean isSupported = FeatureNodeTransfer.getInstance().isSupportedType(transferData);
+        isSupported |= TermTransfer.getInstance().isSupportedType(transferData);
+        isSupported |= LocalSelectionTransfer.getTransfer().isSupportedType(transferData);
+        isSupported &= getViewer().getInput()!=null;
+        return isSupported;
     }
 
 }
\ No newline at end of file