diff options
author | Artur Signell <artur.signell@itmill.com> | 2010-02-19 12:18:01 +0000 |
---|---|---|
committer | Artur Signell <artur.signell@itmill.com> | 2010-02-19 12:18:01 +0000 |
commit | 0912071d8c6a65be05f1fe81bb3b998fb56ce63e (patch) | |
tree | 0c0cd8ab754c135cec9e7dfa02713134836328a3 | |
parent | 02c8f54ea97f812bac899e565a98f29a83bf2977 (diff) | |
download | vaadin-framework-0912071d8c6a65be05f1fe81bb3b998fb56ce63e.tar.gz vaadin-framework-0912071d8c6a65be05f1fe81bb3b998fb56ce63e.zip |
Moved Expand/Collapse method references to listener class and added Tree listener test case
svn changeset:11401/svn branch:6.3
-rw-r--r-- | src/com/vaadin/ui/Tree.java | 37 | ||||
-rw-r--r-- | tests/src/com/vaadin/tests/server/component/tree/TestListeners.java | 137 |
2 files changed, 152 insertions, 22 deletions
diff --git a/src/com/vaadin/ui/Tree.java b/src/com/vaadin/ui/Tree.java index 28a410fa13..7b1dad3911 100644 --- a/src/com/vaadin/ui/Tree.java +++ b/src/com/vaadin/ui/Tree.java @@ -32,6 +32,7 @@ import com.vaadin.terminal.PaintTarget; import com.vaadin.terminal.Resource; import com.vaadin.terminal.gwt.client.MouseEventDetails; import com.vaadin.terminal.gwt.client.ui.VTree; +import com.vaadin.tools.ReflectTools; /** * Tree component. A Tree can be used to select an item (or multiple items) from @@ -47,29 +48,12 @@ import com.vaadin.terminal.gwt.client.ui.VTree; public class Tree extends AbstractSelect implements Container.Hierarchical, Action.Container, ItemClickSource { - private static final Method EXPAND_METHOD; - - private static final Method COLLAPSE_METHOD; - - static { - try { - EXPAND_METHOD = ExpandListener.class.getDeclaredMethod( - "nodeExpand", new Class[] { ExpandEvent.class }); - COLLAPSE_METHOD = CollapseListener.class.getDeclaredMethod( - "nodeCollapse", new Class[] { CollapseEvent.class }); - } catch (final java.lang.NoSuchMethodException e) { - // This should never happen - throw new java.lang.RuntimeException( - "Internal error finding methods in Tree"); - } - } - /* Private members */ /** * Set of expanded nodes. */ - private final HashSet expanded = new HashSet(); + private final HashSet<Object> expanded = new HashSet<Object>(); /** * List of action handlers. @@ -743,6 +727,9 @@ public class Tree extends AbstractSelect implements Container.Hierarchical, */ public interface ExpandListener extends Serializable { + public static final Method EXPAND_METHOD = ReflectTools.findMethod( + ExpandListener.class, "nodeExpand", ExpandEvent.class); + /** * A node has been expanded. * @@ -759,7 +746,7 @@ public class Tree extends AbstractSelect implements Container.Hierarchical, * the Listener to be added. */ public void addListener(ExpandListener listener) { - addListener(ExpandEvent.class, listener, EXPAND_METHOD); + addListener(ExpandEvent.class, listener, ExpandListener.EXPAND_METHOD); } /** @@ -769,7 +756,8 @@ public class Tree extends AbstractSelect implements Container.Hierarchical, * the Listener to be removed. */ public void removeListener(ExpandListener listener) { - removeListener(ExpandEvent.class, listener, EXPAND_METHOD); + removeListener(ExpandEvent.class, listener, + ExpandListener.EXPAND_METHOD); } /** @@ -828,6 +816,9 @@ public class Tree extends AbstractSelect implements Container.Hierarchical, */ public interface CollapseListener extends Serializable { + public static final Method COLLAPSE_METHOD = ReflectTools.findMethod( + CollapseListener.class, "nodeCollapse", CollapseEvent.class); + /** * A node has been collapsed. * @@ -844,7 +835,8 @@ public class Tree extends AbstractSelect implements Container.Hierarchical, * the Listener to be added. */ public void addListener(CollapseListener listener) { - addListener(CollapseEvent.class, listener, COLLAPSE_METHOD); + addListener(CollapseEvent.class, listener, + CollapseListener.COLLAPSE_METHOD); } /** @@ -854,7 +846,8 @@ public class Tree extends AbstractSelect implements Container.Hierarchical, * the Listener to be removed. */ public void removeListener(CollapseListener listener) { - removeListener(CollapseEvent.class, listener, COLLAPSE_METHOD); + removeListener(CollapseEvent.class, listener, + CollapseListener.COLLAPSE_METHOD); } /** diff --git a/tests/src/com/vaadin/tests/server/component/tree/TestListeners.java b/tests/src/com/vaadin/tests/server/component/tree/TestListeners.java new file mode 100644 index 0000000000..06d0b65fba --- /dev/null +++ b/tests/src/com/vaadin/tests/server/component/tree/TestListeners.java @@ -0,0 +1,137 @@ +package com.vaadin.tests.server.component.tree;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import com.vaadin.ui.Tree;
+import com.vaadin.ui.Tree.CollapseEvent;
+import com.vaadin.ui.Tree.CollapseListener;
+import com.vaadin.ui.Tree.ExpandEvent;
+import com.vaadin.ui.Tree.ExpandListener;
+
+public class TestListeners extends TestCase implements ExpandListener,
+ CollapseListener {
+ private int expandCalled;
+ private int collapseCalled;
+ private Object lastExpanded;
+ private Object lastCollapsed;
+
+ @Override
+ protected void setUp() {
+ expandCalled = 0;
+ }
+
+ public void testExpandListener() {
+ Tree tree = createTree(10, 20, false);
+ tree.addListener((ExpandListener) this);
+ List<String> rootIds = new ArrayList<String>(tree.rootItemIds());
+
+ assertEquals(10, rootIds.size());
+ assertEquals(10 + 10 * 20 + 10, tree.size());
+
+ // Expanding should send one expand event for the root item id
+ tree.expandItem(rootIds.get(0));
+ assertEquals(1, expandCalled);
+ assertEquals(rootIds.get(0), lastExpanded);
+
+ // Expand should send one event for each expanded item id.
+ // In this case root + child 4
+ expandCalled = 0;
+ tree.expandItemsRecursively(rootIds.get(1));
+ assertEquals(2, expandCalled);
+ List<Object> c = new ArrayList<Object>(tree.getChildren(rootIds.get(1)));
+
+ assertEquals(c.get(4), lastExpanded);
+
+ // Expanding an already expanded item should send no expand event
+ expandCalled = 0;
+ tree.expandItem(rootIds.get(0));
+ assertEquals(0, expandCalled);
+ }
+
+ /**
+ * Creates a tree with "rootItems" roots, each with "children" children,
+ * each with 1 child.
+ *
+ * @param rootItems
+ * @param children
+ * @param expand
+ * @return
+ */
+ private Tree createTree(int rootItems, int children, boolean expand) {
+ Tree tree = new Tree();
+ for (int i = 0; i < rootItems; i++) {
+ String rootId = "root " + i;
+ tree.addItem(rootId);
+ if (expand) {
+ tree.expandItemsRecursively(rootId);
+ } else {
+ tree.collapseItemsRecursively(rootId);
+
+ }
+ for (int j = 0; j < children; j++) {
+ String childId = "child " + i + "/" + j;
+ tree.addItem(childId);
+ tree.setParent(childId, rootId);
+ tree.setChildrenAllowed(childId, false);
+ if (j == 4) {
+ tree.setChildrenAllowed(childId, true);
+ Object grandChildId = tree.addItem();
+ tree.setParent(grandChildId, childId);
+ tree.setChildrenAllowed(grandChildId, false);
+ if (expand) {
+ tree.expandItemsRecursively(childId);
+ } else {
+ tree.collapseItemsRecursively(childId);
+ }
+ }
+ }
+ }
+
+ return tree;
+ }
+
+ public void testCollapseListener() {
+ Tree tree = createTree(7, 15, true);
+ tree.addListener((CollapseListener) this);
+
+ List<String> rootIds = new ArrayList<String>(tree.rootItemIds());
+
+ assertEquals(7, rootIds.size());
+ assertEquals(7 + 7 * 15 + 7, tree.size());
+
+ // Expanding should send one expand event for the root item id
+ tree.collapseItem(rootIds.get(0));
+ assertEquals(1, collapseCalled);
+ assertEquals(rootIds.get(0), lastCollapsed);
+
+ // Collapse sends one event for each collapsed node.
+ // In this case root + child 4
+ collapseCalled = 0;
+ tree.collapseItemsRecursively(rootIds.get(1));
+ assertEquals(2, collapseCalled);
+ List<Object> c = new ArrayList<Object>(tree.getChildren(rootIds.get(1)));
+ assertEquals(c.get(4), lastCollapsed);
+
+ // Collapsing an already expanded item should send no expand event
+ collapseCalled = 0;
+ tree.collapseItem(rootIds.get(0));
+ assertEquals(0, collapseCalled);
+ }
+
+ @Override
+ public void nodeExpand(ExpandEvent event) {
+ lastExpanded = event.getItemId();
+ expandCalled++;
+
+ }
+
+ @Override
+ public void nodeCollapse(CollapseEvent event) {
+ lastCollapsed = event.getItemId();
+ collapseCalled++;
+
+ }
+}
|