]> source.dussan.org Git - vaadin-framework.git/commitdiff
refactored AbstractSelect criteria (hopefully better names now)
authorMatti Tahvonen <matti.tahvonen@itmill.com>
Tue, 23 Mar 2010 10:39:44 +0000 (10:39 +0000)
committerMatti Tahvonen <matti.tahvonen@itmill.com>
Tue, 23 Mar 2010 10:39:44 +0000 (10:39 +0000)
svn changeset:12034/svn branch:6.3

src/com/vaadin/terminal/gwt/client/ui/dd/VIsOverId.java
src/com/vaadin/terminal/gwt/client/ui/dd/VItemIdIs.java
src/com/vaadin/ui/AbstractSelect.java
tests/src/com/vaadin/tests/dd/DDTest7.java
tests/src/com/vaadin/tests/dd/DDTest8.java [new file with mode: 0644]

index 6ed4b1b7231d5132bea984230d46879cff487c7b..73591493ea28d9ef40ba6f7e0a805db38669c542 100644 (file)
@@ -6,8 +6,7 @@
  */
 package com.vaadin.terminal.gwt.client.ui.dd;
 
-import java.util.Set;
-
+import com.vaadin.terminal.gwt.client.Paintable;
 import com.vaadin.terminal.gwt.client.UIDL;
 
 final public class VIsOverId extends VAcceptCriterion {
@@ -15,10 +14,22 @@ final public class VIsOverId extends VAcceptCriterion {
     @Override
     protected boolean accept(VDragEvent drag, UIDL configuration) {
         try {
-            Set<String> stringArrayVariableAsSet = configuration
-                    .getStringArrayVariableAsSet("keys");
-            return stringArrayVariableAsSet.contains(drag.getDropDetails().get(
-                    "itemIdOver"));
+
+            String pid = configuration.getStringAttribute("s");
+            Paintable paintable = VDragAndDropManager.get()
+                    .getCurrentDropHandler().getPaintable();
+            String pid2 = VDragAndDropManager.get().getCurrentDropHandler()
+                    .getApplicationConnection().getPid(paintable);
+            if (pid2.equals(pid)) {
+                Object searchedId = drag.getDropDetails().get("itemIdOver");
+                String[] stringArrayAttribute = configuration
+                        .getStringArrayAttribute("keys");
+                for (String string : stringArrayAttribute) {
+                    if (string.equals(searchedId)) {
+                        return true;
+                    }
+                }
+            }
         } catch (Exception e) {
         }
         return false;
index 53a0b68837bf230ed6728ce3b0880684f0333519..a00c6fdd984cffbda496c257f497db20ed434ada 100644 (file)
@@ -6,8 +6,7 @@
  */
 package com.vaadin.terminal.gwt.client.ui.dd;
 
-import java.util.Set;
-
+import com.vaadin.terminal.gwt.client.Paintable;
 import com.vaadin.terminal.gwt.client.UIDL;
 
 final public class VItemIdIs extends VAcceptCriterion {
@@ -15,10 +14,20 @@ final public class VItemIdIs extends VAcceptCriterion {
     @Override
     protected boolean accept(VDragEvent drag, UIDL configuration) {
         try {
-            Object data = drag.getTransferable().getData("itemId");
-            Set<String> stringArrayVariableAsSet = configuration
-                    .getStringArrayVariableAsSet("keys");
-            return stringArrayVariableAsSet.contains(data);
+            String pid = configuration.getStringAttribute("s");
+            Paintable dragSource = drag.getTransferable().getDragSource();
+            String pid2 = VDragAndDropManager.get().getCurrentDropHandler()
+                    .getApplicationConnection().getPid(dragSource);
+            if (pid2.equals(pid)) {
+                Object searchedId = drag.getTransferable().getData("itemId");
+                String[] stringArrayAttribute = configuration
+                        .getStringArrayAttribute("keys");
+                for (String string : stringArrayAttribute) {
+                    if (string.equals(searchedId)) {
+                        return true;
+                    }
+                }
+            }
         } catch (Exception e) {
         }
         return false;
index 550cf3913ec15ff1f5a347c4d85fc411eb3eced6..f9f76a1f954a57e99040dd1c34795e2e27c1f40c 100644 (file)
@@ -20,6 +20,7 @@ import com.vaadin.data.Item;
 import com.vaadin.data.Property;
 import com.vaadin.data.util.IndexedContainer;
 import com.vaadin.event.DataBoundTransferable;
+import com.vaadin.event.Transferable;
 import com.vaadin.event.dd.DragAndDropEvent;
 import com.vaadin.event.dd.DropTarget;
 import com.vaadin.event.dd.DropTargetDetailsImpl;
@@ -1692,28 +1693,40 @@ public abstract class AbstractSelect extends AbstractField implements
 
     /**
      * Criterion which accepts a drop only if the drop target is (one of) the
-     * given item identifier(s). Meaningful only for drop targets that extends
-     * AbstractSelect.
+     * given item identifier(s). Criterion can be used only on drop targets that
+     * extends AbstractSelect like {@link Table} and {@link Tree}. The target
+     * and identifiers of valid Items are given in constructor.
      * 
      * @since 6.3
      */
     @ClientCriterion(VIsOverId.class)
-    public static class DropTargetItemId extends AbstractItemSetCriterion {
+    public static class OverItem extends AbstractItemSetCriterion {
 
-        public DropTargetItemId(AbstractSelect select, Object... itemId) {
+        /**
+         * @param select
+         *            the select implementation that is used as a drop target
+         * @param itemId
+         *            the identifier(s) that are valid drop locations
+         */
+        public OverItem(AbstractSelect select, Object... itemId) {
             super(select, itemId);
         }
 
         public boolean accept(DragAndDropEvent dragEvent) {
             AbstractSelectDropTargetDetails dropTargetData = (AbstractSelectDropTargetDetails) dragEvent
                     .getDropTargetDetails();
+            if (dropTargetData.getTarget() != select) {
+                return false;
+            }
             return itemIds.contains(dropTargetData.getItemIdOver());
         }
 
     }
 
     /**
-     * TODO Javadoc!
+     * Abstract helper class to implement item id based criterion.
+     * 
+     * Note, inner class used not to open itemIdMapper for public access.
      * 
      * @since 6.3
      * 
@@ -1721,7 +1734,7 @@ public abstract class AbstractSelect extends AbstractField implements
     private static abstract class AbstractItemSetCriterion extends
             ClientSideCriterion {
         protected final Collection<Object> itemIds = new HashSet<Object>();
-        private AbstractSelect select;
+        protected AbstractSelect select;
 
         public AbstractItemSetCriterion(AbstractSelect select, Object... itemId) {
             if (itemIds == null || select == null) {
@@ -1741,43 +1754,53 @@ public abstract class AbstractSelect extends AbstractField implements
                 String key = select.itemIdMapper.key(itemId);
                 keys[i++] = key;
             }
-            target.addVariable(select, "keys", keys);
+            target.addAttribute("keys", keys);
+            target.addAttribute("s", select);
         }
 
     }
 
     /**
-     * Criterion which accepts a drop only if the transferable contains the
-     * given item identifier(s). The item ids relate to the drag source
-     * (AbstractSelect).
+     * Accept criterion which accepts a drop only if the {@link Transferable}
+     * contains the given Item(s) or practically its identifier(s) from a
+     * specific AbstractSelect.
      * 
      * @since 6.3
      */
     @ClientCriterion(VItemIdIs.class)
-    public static class TransferableContainsItemId extends
-            AbstractItemSetCriterion {
-        public TransferableContainsItemId(AbstractSelect select,
-                Object... itemId) {
+    public static class ContainsItem extends AbstractItemSetCriterion {
+
+        /**
+         * @param select
+         *            the select from which the item id's are checked
+         * @param itemId
+         *            the item identifier(s) of the select that are accepted
+         */
+        public ContainsItem(AbstractSelect select, Object... itemId) {
             super(select, itemId);
         }
 
         public boolean accept(DragAndDropEvent dragEvent) {
             DataBoundTransferable transferable = (DataBoundTransferable) dragEvent
                     .getTransferable();
+            if (transferable.getSourceComponent() != select) {
+                return false;
+            }
             return itemIds.contains(transferable.getItemId());
         }
 
     }
 
     /**
-     * Helper implementation for subclasses that implement {@link DropTarget}.
+     * DropTargetDetails implementation for subclasses of {@link AbstractSelect}
+     * that implement {@link DropTarget}.
      * 
      * @since 6.3
      */
     public class AbstractSelectDropTargetDetails extends DropTargetDetailsImpl {
 
         /**
-         * The treenode id over which the drag event happened.
+         * The item id over which the drag event happened.
          */
         protected Object idOver;
 
index 8f0e3827fea9ef32765c79a08610c721022b72b2..116a94b235ac85c029ad2e11c67e73387ca4c813 100644 (file)
@@ -26,7 +26,7 @@ public class DDTest7 extends TestBase {
 
     HorizontalLayout hl = new HorizontalLayout();
     Table table = new Table(
-            "Drag and drop sortable table with lazy loading rule. Dragged row can only be accepted after hevier row (weigh column). If starting from topmost row in viewport, hevies will end up on top.");
+            "Drag and drop sortable table with lazy loading rule. Dragged row can only be accepted after hevier row (weigh column). If starting from topmost row in viewport, heviests will end up on top.");
 
     @Override
     protected void setup() {
@@ -151,9 +151,7 @@ public class DDTest7 extends TestBase {
                                 item.getItemProperty(propId).getValue());
                     }
 
-                    // TODO Auto-generated method stub
                 } catch (CloneNotSupportedException e) {
-                    // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
 
diff --git a/tests/src/com/vaadin/tests/dd/DDTest8.java b/tests/src/com/vaadin/tests/dd/DDTest8.java
new file mode 100644 (file)
index 0000000..9bad36e
--- /dev/null
@@ -0,0 +1,174 @@
+package com.vaadin.tests.dd;
+
+import java.util.Collection;
+
+import com.vaadin.data.Item;
+import com.vaadin.data.util.HierarchicalContainer;
+import com.vaadin.event.DataBoundTransferable;
+import com.vaadin.event.Transferable;
+import com.vaadin.event.dd.DragAndDropEvent;
+import com.vaadin.event.dd.DropHandler;
+import com.vaadin.event.dd.acceptcriteria.AcceptCriterion;
+import com.vaadin.event.dd.acceptcriteria.Or;
+import com.vaadin.terminal.gwt.client.ui.dd.VerticalDropLocation;
+import com.vaadin.tests.components.TestBase;
+import com.vaadin.ui.AbstractSelect;
+import com.vaadin.ui.Tree;
+import com.vaadin.ui.Tree.TreeDragMode;
+import com.vaadin.ui.Tree.TreeDropTargetDetails;
+
+/**
+ * DD playground. Better quality example/prototype codes in {@link DDTest2}.
+ */
+public class DDTest8 extends TestBase {
+
+    @Override
+    protected void setup() {
+        final Tree t = new Tree(
+                "Tree with criteria from AbstractSelect (OverItem, ContainsItem). Foo can be dragged anywhere, anything can be dropped on Foo or Bar.");
+
+        final HierarchicalContainer idx = new HierarchicalContainer();
+        t.setContainerDataSource(idx);
+        t.setDebugId("perseys");
+        t.addItem("Foo");
+        t.addItem("Bar");
+        t.addItem("Bar1");
+        t.addItem("Bar2");
+        t.addItem("Bar3");
+        t.addItem("Bar4");
+        t.addItem("Bar5");
+        t.addItem("Child");
+        t.setParent("Child", "Foo");
+        t.setSizeFull();
+        t.setDragMode(TreeDragMode.NODE);
+
+        /*
+         * Moves items in tree (and could work in Table too). Also supports
+         * "building" tree.
+         * 
+         * TODO fix algorithm, broken in some cases.
+         */
+        DropHandler itemSorter = new DropHandler() {
+
+            private void populateSubTree(HierarchicalContainer idx,
+                    HierarchicalContainer subtree, Object itemId) {
+                Collection children = subtree.getChildren(itemId);
+                if (children != null) {
+
+                    for (Object childId : children) {
+                        Item addItem = idx.addItem(childId);
+                        if (addItem != null) {
+                            // did not exist, populate properties
+                            Item item = subtree.getItem(itemId);
+                            Collection<?> itemPropertyIds = item
+                                    .getItemPropertyIds();
+                            for (Object propId : itemPropertyIds) {
+                                addItem.getItemProperty(propId)
+                                        .setValue(
+                                                item.getItemProperty(propId)
+                                                        .getValue());
+                            }
+                        }
+                        idx.setParent(childId, itemId);
+                        populateSubTree(idx, subtree, childId);
+                    }
+                }
+
+            }
+
+            private HierarchicalContainer getSubTree(HierarchicalContainer idx,
+                    Object itemId) {
+                HierarchicalContainer hierarchicalContainer = new HierarchicalContainer();
+                Collection containerPropertyIds = idx.getContainerPropertyIds();
+                for (Object object : containerPropertyIds) {
+                    hierarchicalContainer.addContainerProperty(object, idx
+                            .getType(object), null);
+                }
+                hierarchicalContainer.addItem(itemId);
+                copyChildren(idx, hierarchicalContainer, itemId);
+                return hierarchicalContainer;
+            }
+
+            private void copyChildren(HierarchicalContainer source,
+                    HierarchicalContainer target, Object itemId) {
+                Collection children = source.getChildren(itemId);
+                if (children != null) {
+                    for (Object childId : children) {
+                        Item item = source.getItem(childId);
+                        Item addedItem = target.addItem(childId);
+                        target.setParent(childId, itemId);
+                        Collection<?> itemPropertyIds = item
+                                .getItemPropertyIds();
+                        for (Object propertyId : itemPropertyIds) {
+                            addedItem.getItemProperty(propertyId)
+                                    .setValue(
+                                            item.getItemProperty(propertyId)
+                                                    .getValue());
+                        }
+                        copyChildren(source, target, childId);
+                    }
+                }
+
+            }
+
+            public void drop(DragAndDropEvent event) {
+                TreeDropTargetDetails details = (TreeDropTargetDetails) event
+                        .getDropTargetDetails();
+                // TODO set properties, so same sorter could be used in Table
+                Transferable transferable = event.getTransferable();
+                if (transferable instanceof DataBoundTransferable) {
+                    DataBoundTransferable transferrable2 = (DataBoundTransferable) transferable;
+
+                    Object itemId = transferrable2.getItemId();
+
+                    Object itemIdOver = details.getItemIdOver();
+
+                    // TODO could use the "folder" node id to make the drop
+                    // logic simpler
+                    Object itemIdInto = details.getItemIdInto();
+                    VerticalDropLocation dropLocation = details
+                            .getDropLocation();
+
+                    Object itemIdAfter = details.getItemIdAfter();
+
+                    if (itemIdOver.equals(itemIdInto)) { // directly on a node
+                        t.setParent(itemId, itemIdOver);
+                        return;
+                    }
+
+                    idx.setParent(itemId, itemIdInto);
+
+                    if (dropLocation == null) {
+                        System.err.println("No detail of drop place available");
+                    }
+                    idx.moveAfterSibling(itemId, itemIdAfter);
+                }
+
+                return;
+            }
+
+            public AcceptCriterion getAcceptCriterion() {
+                return new Or(new AbstractSelect.OverItem(t, "Foo", "Bar"),
+                        new AbstractSelect.ContainsItem(t, "Foo"));
+            }
+
+        };
+
+        t.setDropHandler(itemSorter);
+
+        getLayout().setSizeFull();
+        addComponent(t);
+
+    }
+
+    @Override
+    protected String getDescription() {
+        return "Random DD tests";
+    }
+
+    @Override
+    protected Integer getTicketNumber() {
+        return 119;
+    }
+
+}