diff options
author | Artur Signell <artur.signell@itmill.com> | 2012-01-12 16:22:25 +0000 |
---|---|---|
committer | Artur Signell <artur.signell@itmill.com> | 2012-01-12 16:22:25 +0000 |
commit | c661750fbc7149becd34548ac3a0a844df39b3b7 (patch) | |
tree | 106b35552b6e57de5089d66265a7bb1003d90fee | |
parent | fa81141d2230f37af17b20718a82709e004160be (diff) | |
download | vaadin-framework-c661750fbc7149becd34548ac3a0a844df39b3b7.tar.gz vaadin-framework-c661750fbc7149becd34548ac3a0a844df39b3b7.zip |
#7614 Added getComponent(int) and getComponentIndex(Component) to CssLayout
svn changeset:22620/svn branch:6.8
5 files changed, 211 insertions, 1 deletions
diff --git a/src/com/vaadin/ui/CssLayout.java b/src/com/vaadin/ui/CssLayout.java index b952609b20..b9432df6b6 100644 --- a/src/com/vaadin/ui/CssLayout.java +++ b/src/com/vaadin/ui/CssLayout.java @@ -125,7 +125,7 @@ public class CssLayout extends AbstractLayout implements LayoutClickNotifier { // see ticket #7668 if (c.getParent() == this) { // When c is removed, all components after it are shifted down - if (index > components.indexOf(c)) { + if (index > getComponentIndex(c)) { index--; } removeComponent(c); @@ -275,4 +275,28 @@ public class CssLayout extends AbstractLayout implements LayoutClickNotifier { removeListener(CLICK_EVENT, LayoutClickEvent.class, listener); } + /** + * Returns the index of the given component. + * + * @param component + * The component to look up. + * @return The index of the component or -1 if the component is not a child. + */ + public int getComponentIndex(Component component) { + return components.indexOf(component); + } + + /** + * Returns the component at the given position. + * + * @param index + * The position of the component. + * @return The component at the given index. + * @throws IndexOutOfBoundsException + * If the index is out of range. + */ + public Component getComponent(int index) throws IndexOutOfBoundsException { + return components.get(index); + } + } diff --git a/tests/server-side/com/vaadin/tests/server/componentcontainer/AbstractIndexedLayoutTest.java b/tests/server-side/com/vaadin/tests/server/componentcontainer/AbstractIndexedLayoutTest.java new file mode 100644 index 0000000000..9271e9f1b3 --- /dev/null +++ b/tests/server-side/com/vaadin/tests/server/componentcontainer/AbstractIndexedLayoutTest.java @@ -0,0 +1,84 @@ +package com.vaadin.tests.server.componentcontainer;
+
+import junit.framework.TestCase;
+
+import com.vaadin.ui.Component;
+import com.vaadin.ui.Label;
+import com.vaadin.ui.Layout;
+
+public abstract class AbstractIndexedLayoutTest extends TestCase {
+
+ private Layout layout;
+
+ protected abstract Layout createLayout();
+
+ @Override
+ protected void setUp() throws Exception {
+ layout = createLayout();
+ }
+
+ public Layout getLayout() {
+ return layout;
+ }
+
+ public void testAddRemoveComponent() {
+ Label c1 = new Label();
+ Label c2 = new Label();
+
+ layout.addComponent(c1);
+
+ assertEquals(c1, getComponent(0));
+ assertEquals(1, getComponentCount());
+ layout.addComponent(c2);
+ assertEquals(c1, getComponent(0));
+ assertEquals(c2, getComponent(1));
+ assertEquals(2, getComponentCount());
+ layout.removeComponent(c1);
+ assertEquals(c2, getComponent(0));
+ assertEquals(1, getComponentCount());
+ layout.removeComponent(c2);
+ assertEquals(0, getComponentCount());
+ }
+
+ protected abstract int getComponentCount();
+
+ protected abstract Component getComponent(int index);
+
+ protected abstract int getComponentIndex(Component c);
+
+ public void testGetComponentIndex() {
+ Label c1 = new Label();
+ Label c2 = new Label();
+
+ layout.addComponent(c1);
+ assertEquals(0, getComponentIndex(c1));
+ layout.addComponent(c2);
+ assertEquals(0, getComponentIndex(c1));
+ assertEquals(1, getComponentIndex(c2));
+ layout.removeComponent(c1);
+ assertEquals(0, getComponentIndex(c2));
+ layout.removeComponent(c2);
+ assertEquals(-1, getComponentIndex(c2));
+ assertEquals(-1, getComponentIndex(c1));
+ }
+
+ public void testGetComponent() {
+ Label c1 = new Label();
+ Label c2 = new Label();
+
+ layout.addComponent(c1);
+ assertEquals(c1, getComponent(0));
+ layout.addComponent(c2);
+ assertEquals(c1, getComponent(0));
+ assertEquals(c2, getComponent(1));
+ layout.removeComponent(c1);
+ assertEquals(c2, getComponent(0));
+ layout.removeComponent(c2);
+ try {
+ getComponent(0);
+ fail();
+ } catch (IndexOutOfBoundsException e) {
+ // Expected
+ }
+ }
+}
diff --git a/tests/server-side/com/vaadin/tests/server/componentcontainer/CssLayoutTest.java b/tests/server-side/com/vaadin/tests/server/componentcontainer/CssLayoutTest.java new file mode 100644 index 0000000000..dc9667c38e --- /dev/null +++ b/tests/server-side/com/vaadin/tests/server/componentcontainer/CssLayoutTest.java @@ -0,0 +1,34 @@ +package com.vaadin.tests.server.componentcontainer; + +import com.vaadin.ui.Component; +import com.vaadin.ui.CssLayout; +import com.vaadin.ui.Layout; + +public class CssLayoutTest extends AbstractIndexedLayoutTest { + + @Override + protected Layout createLayout() { + return new CssLayout(); + } + + @Override + public CssLayout getLayout() { + return (CssLayout) super.getLayout(); + } + + @Override + protected Component getComponent(int index) { + return getLayout().getComponent(index); + } + + @Override + protected int getComponentIndex(Component c) { + return getLayout().getComponentIndex(c); + } + + @Override + protected int getComponentCount() { + return getLayout().getComponentCount(); + } + +} diff --git a/tests/server-side/com/vaadin/tests/server/componentcontainer/FormLayoutTest.java b/tests/server-side/com/vaadin/tests/server/componentcontainer/FormLayoutTest.java new file mode 100644 index 0000000000..71a813d423 --- /dev/null +++ b/tests/server-side/com/vaadin/tests/server/componentcontainer/FormLayoutTest.java @@ -0,0 +1,34 @@ +package com.vaadin.tests.server.componentcontainer; + +import com.vaadin.ui.Component; +import com.vaadin.ui.FormLayout; +import com.vaadin.ui.Layout; + +public class FormLayoutTest extends AbstractIndexedLayoutTest { + + @Override + protected Layout createLayout() { + return new FormLayout(); + } + + @Override + public FormLayout getLayout() { + return (FormLayout) super.getLayout(); + } + + @Override + protected Component getComponent(int index) { + return getLayout().getComponent(index); + } + + @Override + protected int getComponentIndex(Component c) { + return getLayout().getComponentIndex(c); + } + + @Override + protected int getComponentCount() { + return getLayout().getComponentCount(); + } + +} diff --git a/tests/server-side/com/vaadin/tests/server/componentcontainer/VerticalLayoutTest.java b/tests/server-side/com/vaadin/tests/server/componentcontainer/VerticalLayoutTest.java new file mode 100644 index 0000000000..0e3a1d5734 --- /dev/null +++ b/tests/server-side/com/vaadin/tests/server/componentcontainer/VerticalLayoutTest.java @@ -0,0 +1,34 @@ +package com.vaadin.tests.server.componentcontainer; + +import com.vaadin.ui.Component; +import com.vaadin.ui.Layout; +import com.vaadin.ui.VerticalLayout; + +public class VerticalLayoutTest extends AbstractIndexedLayoutTest { + + @Override + protected Layout createLayout() { + return new VerticalLayout(); + } + + @Override + public VerticalLayout getLayout() { + return (VerticalLayout) super.getLayout(); + } + + @Override + protected Component getComponent(int index) { + return getLayout().getComponent(index); + } + + @Override + protected int getComponentIndex(Component c) { + return getLayout().getComponentIndex(c); + } + + @Override + protected int getComponentCount() { + return getLayout().getComponentCount(); + } + +} |