ref #8774: avoid d&d to child of the dropped node
authorKatja Luther <k.luther@bgbm.org>
Thu, 3 Dec 2020 09:40:36 +0000 (10:40 +0100)
committerKatja Luther <k.luther@bgbm.org>
Thu, 3 Dec 2020 09:40:36 +0000 (10:40 +0100)
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/featuretree/e4/FeatureTreeDtoDropAdapter.java

index 9daaff8aeb686a2cc351e6c8a05651811bf17d54..7b80ee4c8c624bc04485985595540368774b23f0 100755 (executable)
@@ -116,6 +116,7 @@ public class FeatureTreeDtoDropAdapter extends ViewerDropAdapter {
                for (Object droppedObject : droppedObjects) {
                    //check term type compatibility
                    TermType targetType = target.getType();
+
                    TermType droppedType = null;
                    if(droppedObject instanceof IHasTermType){
                        droppedType = ((IHasTermType)droppedObject).getTermType();
@@ -139,6 +140,10 @@ public class FeatureTreeDtoDropAdapter extends ViewerDropAdapter {
                        }
                        TermNodeDto droppedNode = editor.getNodeDtoForUuid(((TermNodeDto) droppedObject).getUuid());
                        TermNodeDto oldParent = editor.getNodeDtoForUuid(droppedNode.getParentUuid());
+                       boolean isCircle = checkCircle(droppedNode, target);
+                       if (isCircle){
+                           return false;
+                       }
                        int currentPosition = oldParent.getIndex(droppedNode);
 
                        if(currentPosition<position && target.equals(oldParent)){
@@ -147,6 +152,7 @@ public class FeatureTreeDtoDropAdapter extends ViewerDropAdapter {
                        MoveFeatureOperation operation = new MoveFeatureOperation(droppedNode.getUuid(), droppedNode.getTerm().getUuid(), target.getUuid(), position, getCurrentOperation(), editor, editor);
 
                        Object o = oldParent.getChildren().remove(currentPosition);
+
                        target.getChildren().add(position, droppedNode);
                        droppedNode.setParentUuid(target.getUuid());
                        editor.setNodeDtoForUuid(droppedNode);
@@ -213,6 +219,24 @@ public class FeatureTreeDtoDropAdapter extends ViewerDropAdapter {
                return true;
        }
 
+    /**
+     * @param droppedNode
+     * @param target
+     */
+    private boolean checkCircle(TermNodeDto droppedNode, TermNodeDto newParent) {
+        boolean result = false;
+       if (droppedNode.getChildren().contains(newParent)){
+           return true;
+       }
+
+       if (newParent.getParentUuid() != null){
+           result = checkCircle(droppedNode, editor.getNodeDtoForUuid(newParent.getParentUuid()));
+
+       }
+       return result;
+
+    }
+
     @Override
     public boolean validateDrop(Object target, int operation, TransferData transferData) {
         boolean
@@ -220,6 +244,7 @@ public class FeatureTreeDtoDropAdapter extends ViewerDropAdapter {
         isSupported |= TermTransfer.getInstance().isSupportedType(transferData);
         isSupported |= LocalSelectionTransfer.getTransfer().isSupportedType(transferData);
         isSupported &= getViewer().getInput()!=null;
+
         if(target instanceof TermTreeDto && getCurrentLocation()!=ViewerDropAdapter.LOCATION_ON){
             isSupported = false;
         }