ref #9815: when removing all elements of description, do not remove it automatically
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / descriptive / DescriptionElementDropAdapter.java
index ce60deeca9b8383cce8cc3a4143f9be8e41247bd..cf1ee03e7d4a75575ac66ec6ad4b390d94a62ec8 100644 (file)
-// $Id$
 /**
 * Copyright (C) 2007 EDIT
-* European Distributed Institute of Taxonomy 
+* European Distributed Institute of Taxonomy
 * http://www.e-taxonomy.eu
-* 
+*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * See LICENSE.TXT at the top of this package for the full license terms.
 */
-
 package eu.etaxonomy.taxeditor.editor.view.descriptive;
 
 import java.util.ArrayList;
 import java.util.Collection;
 
+import org.eclipse.e4.ui.model.application.ui.basic.MPart;
 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.model.description.DescriptionBase;
 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
+import eu.etaxonomy.cdm.model.description.SpecimenDescription;
 import eu.etaxonomy.cdm.model.description.TaxonDescription;
+import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
 import eu.etaxonomy.taxeditor.editor.EditorUtil;
+import eu.etaxonomy.taxeditor.editor.l10n.Messages;
+import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
+import eu.etaxonomy.taxeditor.editor.view.descriptive.e4.FactualDataPartE4;
 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.MoveDescriptionElementsOperation;
-import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
-import eu.etaxonomy.taxeditor.store.StoreUtil;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
+import eu.etaxonomy.taxeditor.operation.AbstractPostTaxonOperation;
+import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
+import eu.etaxonomy.taxeditor.ui.EditViewerDropAdapter;
 
 /**
  * @author n.hoffmann
  * @created Feb 8, 2011
- * @version 1.0
  */
-public class DescriptionElementDropAdapter extends ViewerDropAdapter {
+public class DescriptionElementDropAdapter extends EditViewerDropAdapter {
+
+    private static final String OPERATION_NOT_SUPPORTED_YET = Messages.DescriptionElementDropAdapter_NOT_SUPPORTED;
 
-       /**
-        * @param viewer
-        */
        public DescriptionElementDropAdapter(Viewer viewer) {
                super(viewer);
        }
 
-       /* (non-Javadoc)
-        * @see org.eclipse.jface.viewers.ViewerDropAdapter#performDrop(java.lang.Object)
-        */
        @Override
        public boolean performDrop(Object data) {
-               TaxonDescription target = (TaxonDescription) getCurrentTarget();
+               DescriptionBase target = (DescriptionBase) getCurrentTarget();
                Object[] droppedElements = (Object[]) data;
-               
+
                Collection<DescriptionElementBase> descriptionElements = new ArrayList<DescriptionElementBase>();
-               
+
                boolean isCopy = getCurrentOperation() == DND.DROP_COPY ? true : false;
-               
+               DescriptionBase<?> description = null;
                // cannot drop a feature node onto itself
-               for (Object droppedElement : droppedElements) {                 
-                       if (droppedElement == null){
-                               StoreUtil.warningDialog("Operation not supported yet", this, "We are currently unable to drag and drop a newly created element. Please save the editor to make this work.");
-                               return false;
-                       }
-                       if(! (droppedElement instanceof DescriptionElementBase)){
-                               return false;
-                       }else{
-                               DescriptionElementBase descriptionElement = (DescriptionElementBase) droppedElement;
-                               
-                               if (descriptionElement.getInDescription().equals(target)) {
+               if (droppedElements != null){
+                       for (Object droppedElement : droppedElements) {
+                               if (droppedElement == null){
+                                       MessagingUtils.warningDialog(OPERATION_NOT_SUPPORTED_YET, this, Messages.DescriptionElementDropAdapter_NOT_SUPPORTED_NEW_ELEMENT);
                                        return false;
                                }
-                               
-                               descriptionElements.add(descriptionElement);
-                       }                       
+                               if(! (droppedElement instanceof DescriptionElementBase)){
+                                       return false;
+                               }else{
+                                       DescriptionElementBase descriptionElement = (DescriptionElementBase) droppedElement;
+
+                                       if (descriptionElement.getInDescription().equals(target)) {
+                                               return false;
+                                       }
+                                       for (Object element : target.getElements()){
+                                           if (element instanceof DescriptionElementBase){
+                                              if( !((DescriptionElementBase)element).isPersited()){
+                                                  MessagingUtils.warningDialog(OPERATION_NOT_SUPPORTED_YET, this, Messages.DescriptionElementDropAdapter_NOT_SUPPORTED_NEW_ELEMENT_IN_DESCRIPTION);
+                                           return false;
+                                              }
+
+                                           }
+                                       }
+                                       for (Object element : descriptionElement.getInDescription().getElements()){
+                        if (element instanceof DescriptionElementBase){
+                           if( !((DescriptionElementBase)element).isPersited()){
+                               MessagingUtils.warningDialog(OPERATION_NOT_SUPPORTED_YET, this, Messages.DescriptionElementDropAdapter_NOT_SUPPORTED_NEW_ELEMENT_IN_DESCRIPTION);
+                                return false;
+                           }
+
+                        }
+                    }
+                                       description = descriptionElement.getInDescription();
+                                       description.removeElement(descriptionElement);
+                                       target.addElement(descriptionElement);
+
+                                       descriptionElements.add(descriptionElement);
+                               }
+                       }
+                       TaxonNameEditorE4 editor = null;
+                       Object activePart = EditorUtil.getActivePart();
+                       if (activePart instanceof FactualDataPartE4){
+                           MPart selectionProvidingPart =((FactualDataPartE4)activePart).getSelectionProvidingPart();
+                           Object obj = selectionProvidingPart.getObject();
+                           if (obj instanceof TaxonNameEditorE4){
+                               editor = (TaxonNameEditorE4) obj;
+                           }
+                       }
+                       AbstractPostTaxonOperation operation = new MoveDescriptionElementsOperation(Messages.DescriptionElementDropAdapter_MOVE_DESC, EditorUtil.getUndoContext(), target, description, descriptionElements, isCopy, null, sync, false);
+
+                       editor.getEditorInput().addOperation(operation);
+                       editor.setDirty();
+//                     EditorUtil.executeOperation(operation, sync);
+                       return true;
                }
-               
-               AbstractPostOperation operation = new MoveDescriptionElementsOperation("Move Descriptions", EditorUtil.getUndoContext(), target, descriptionElements, isCopy, null);
-               
-               EditorUtil.executeOperation(operation);
-               
-               return true;
+               MessagingUtils.warningDialog(OPERATION_NOT_SUPPORTED_YET, this, Messages.DescriptionElementDropAdapter_NOT_SUPPORTED_EMPTY_ELEMENT);
+
+               return false;
+
        }
 
-       /* (non-Javadoc)
-        * @see org.eclipse.jface.viewers.ViewerDropAdapter#validateDrop(java.lang.Object, int, org.eclipse.swt.dnd.TransferData)
-        */
        @Override
        public boolean validateDrop(Object target, int operation,
                        TransferData transferData) {
-               boolean transferDataIsSupported = DescriptionElementTransfer.getInstance().isSupportedType(
-                               transferData);
-               System.out.println(target);
-               return target instanceof TaxonDescription && transferDataIsSupported;
+               boolean transferDataIsSupported = true;
+               if (target instanceof DescriptionBase){
+                       if((((DescriptionBase)target).isComputed() || ((DescriptionBase)target).isCloneForSource())&& PreferencesUtil.isComputedDesciptionHandlingDisabled()){
+                               transferDataIsSupported = false;
+                       }
+               }
+               transferDataIsSupported = DescriptionElementTransfer.getInstance().isSupportedType(
+                               transferData) && transferDataIsSupported;
+               return target instanceof DescriptionBase && transferDataIsSupported;
        }
-
-}
+}
\ No newline at end of file