diff options
author | Johannes Dahlström <johannesd@vaadin.com> | 2012-09-07 16:31:16 +0300 |
---|---|---|
committer | Johannes Dahlström <johannesd@vaadin.com> | 2012-09-07 17:12:24 +0300 |
commit | 1fb7d999a09bea25256eef0e9837523c7c81841f (patch) | |
tree | 62e239d911bdc064a07621a4b4203c54c7c11349 /server | |
parent | 544152d1adc3a098445f7b11b4651fcc3c2f13bc (diff) | |
download | vaadin-framework-1fb7d999a09bea25256eef0e9837523c7c81841f.tar.gz vaadin-framework-1fb7d999a09bea25256eef0e9837523c7c81841f.zip |
Commit Pekka's patch for #8030 ((Component...) constructor for layouts) and #5422 (addComponents(Component...) for layouts) with minor formatting/javadoc changes
Diffstat (limited to 'server')
10 files changed, 329 insertions, 8 deletions
diff --git a/server/src/com/vaadin/ui/AbstractOrderedLayout.java b/server/src/com/vaadin/ui/AbstractOrderedLayout.java index fb345d30ec..cb3d9cfe9f 100644 --- a/server/src/com/vaadin/ui/AbstractOrderedLayout.java +++ b/server/src/com/vaadin/ui/AbstractOrderedLayout.java @@ -56,7 +56,7 @@ public abstract class AbstractOrderedLayout extends AbstractLayout implements /* Child component alignments */ /** - * Mapping from components to alignments (horizontal + vertical). + * Constructs an empty AbstractOrderedLayout. */ public AbstractOrderedLayout() { registerRpc(rpc); @@ -113,6 +113,18 @@ public abstract class AbstractOrderedLayout extends AbstractLayout implements } /** + * Adds the given components in the given order to the container. + * + * @param components + * The components to add. + */ + public void addComponents(Component... components) { + for (Component c : components) { + addComponent(c); + } + } + + /** * Adds a component into indexed position in this container. * * @param c diff --git a/server/src/com/vaadin/ui/CssLayout.java b/server/src/com/vaadin/ui/CssLayout.java index 9ac29c4deb..401eafe6e9 100644 --- a/server/src/com/vaadin/ui/CssLayout.java +++ b/server/src/com/vaadin/ui/CssLayout.java @@ -84,13 +84,29 @@ public class CssLayout extends AbstractLayout implements LayoutClickNotifier { */ protected LinkedList<Component> components = new LinkedList<Component>(); + /** + * Constructs an empty CssLayout. + */ public CssLayout() { registerRpc(rpc); } /** + * Constructs a CssLayout with the given components in the given order. + * + * @see #addComponents(Component...) + * + * @param children + * Components to add to the container. + */ + public CssLayout(Component... children) { + this(); + addComponents(children); + } + + /** * Add a component into this container. The component is added to the right - * or under the previous component. + * or below the previous component. * * @param c * the component to be added. @@ -110,6 +126,19 @@ public class CssLayout extends AbstractLayout implements LayoutClickNotifier { } /** + * Adds the given components into this container. Each component is added to + * the right or below the previous component. + * + * @param components + * The components to add. + */ + public void addComponents(Component... components) { + for (Component c : components) { + addComponent(c); + } + } + + /** * Adds a component into this container. The component is added to the left * or on top of the other components. * diff --git a/server/src/com/vaadin/ui/FormLayout.java b/server/src/com/vaadin/ui/FormLayout.java index 15ea478597..4df9e8f3c0 100644 --- a/server/src/com/vaadin/ui/FormLayout.java +++ b/server/src/com/vaadin/ui/FormLayout.java @@ -22,12 +22,9 @@ import com.vaadin.shared.ui.MarginInfo; * FormLayout is used by {@link Form} to layout fields. It may also be used * separately without {@link Form}. * - * FormLayout is a close relative to vertical {@link OrderedLayout}, but in - * FormLayout caption is rendered on left side of component. Required and - * validation indicators are between captions and fields. - * - * FormLayout does not currently support some advanced methods from - * OrderedLayout like setExpandRatio and setComponentAlignment. + * FormLayout is a close relative of {@link VerticalLayout}, but in FormLayout + * captions are rendered to the left of their respective components. Required + * and validation indicators are shown between the captions and the fields. * * FormLayout by default has component spacing on. Also margin top and margin * bottom are by default on. @@ -42,4 +39,17 @@ public class FormLayout extends AbstractOrderedLayout { setWidth(100, UNITS_PERCENTAGE); } + /** + * Constructs a FormLayout and adds the given components to it. + * + * @see AbstractOrderedLayout#addComponents(Component...) + * + * @param children + * Components to add to the FormLayout + */ + public FormLayout(Component... children) { + this(); + addComponents(children); + } + } diff --git a/server/src/com/vaadin/ui/GridLayout.java b/server/src/com/vaadin/ui/GridLayout.java index de167962e7..566bd812a3 100644 --- a/server/src/com/vaadin/ui/GridLayout.java +++ b/server/src/com/vaadin/ui/GridLayout.java @@ -140,6 +140,24 @@ public class GridLayout extends AbstractLayout implements this(1, 1); } + /** + * Constructs a GridLayout of given size (number of columns and rows) and + * adds the given components in order to the grid. + * + * @see #addComponents(Component...) + * + * @param columns + * Number of columns in the grid. + * @param rows + * Number of rows in the grid. + * @param children + * Components to add to the grid. + */ + public GridLayout(int columns, int rows, Component... children) { + this(columns, rows); + addComponents(children); + } + @Override protected GridLayoutState getState() { return (GridLayoutState) super.getState(); @@ -258,6 +276,19 @@ public class GridLayout extends AbstractLayout implements } /** + * Adds the given components to the grid starting from the current cursor + * position. + * + * @param components + * Components to add. + */ + public void addComponents(Component... components) { + for (Component c : components) { + this.addComponent(c); + } + } + + /** * Tests if the given area overlaps with any of the items already on the * grid. * diff --git a/server/src/com/vaadin/ui/HorizontalLayout.java b/server/src/com/vaadin/ui/HorizontalLayout.java index 9207b59353..3797d5252a 100644 --- a/server/src/com/vaadin/ui/HorizontalLayout.java +++ b/server/src/com/vaadin/ui/HorizontalLayout.java @@ -27,8 +27,25 @@ package com.vaadin.ui; @SuppressWarnings("serial") public class HorizontalLayout extends AbstractOrderedLayout { + /** + * Constructs an empty HorizontalLayout. + */ public HorizontalLayout() { } + /** + * Constructs a HorizontalLayout with the given components. The components + * are added in the given order. + * + * @see AbstractOrderedLayout#addComponents(Component...) + * + * @param children + * The components to add. + */ + public HorizontalLayout(Component... children) { + this(); + addComponents(children); + } + } diff --git a/server/src/com/vaadin/ui/VerticalLayout.java b/server/src/com/vaadin/ui/VerticalLayout.java index 0a13ca9a36..ba07603166 100644 --- a/server/src/com/vaadin/ui/VerticalLayout.java +++ b/server/src/com/vaadin/ui/VerticalLayout.java @@ -28,8 +28,24 @@ package com.vaadin.ui; @SuppressWarnings("serial") public class VerticalLayout extends AbstractOrderedLayout { + /** + * Constructs an empty VerticalLayout. + */ public VerticalLayout() { setWidth("100%"); } + /** + * Constructs a VerticalLayout with the given components. The components are + * added in the given order. + * + * @see AbstractOrderedLayout#addComponents(Component...) + * + * @param children + * The components to add. + */ + public VerticalLayout(Component... children) { + this(); + addComponents(children); + } } diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractorderedlayout/AddComponentsTest.java b/server/tests/src/com/vaadin/tests/server/component/abstractorderedlayout/AddComponentsTest.java index bd67841f33..81c29995f5 100644 --- a/server/tests/src/com/vaadin/tests/server/component/abstractorderedlayout/AddComponentsTest.java +++ b/server/tests/src/com/vaadin/tests/server/component/abstractorderedlayout/AddComponentsTest.java @@ -97,6 +97,45 @@ public class AddComponentsTest { assertOrder(layout, new int[] { 2, 3, 1, 0 }); } + @Test + public void testConstructorsWithComponents() { + AbstractOrderedLayout layout1 = new HorizontalLayout(children); + assertOrder(layout1, new int[] { 0, 1, 2, 3 }); + shuffleChildComponents(layout1); + + AbstractOrderedLayout layout2 = new VerticalLayout(children); + assertOrder(layout2, new int[] { 0, 1, 2, 3 }); + shuffleChildComponents(layout2); + } + + @Test + public void testAddComponents() { + HorizontalLayout layout1 = new HorizontalLayout(); + layout1.addComponents(children); + assertOrder(layout1, new int[] { 0, 1, 2, 3 }); + + Label extra = new Label("Extra"); + layout1.addComponents(extra); + assertSame(extra, layout1.getComponent(4)); + + layout1.removeAllComponents(); + layout1.addComponents(children[3], children[2], children[1], + children[0]); + assertOrder(layout1, new int[] { 3, 2, 1, 0 }); + + VerticalLayout layout2 = new VerticalLayout(children); + layout2.addComponents(children); + assertOrder(layout2, new int[] { 0, 1, 2, 3 }); + + layout2.addComponents(extra); + assertSame(extra, layout2.getComponent(4)); + + layout2.removeAllComponents(); + layout2.addComponents(children[3], children[2], children[1], + children[0]); + assertOrder(layout2, new int[] { 3, 2, 1, 0 }); + } + /** * Asserts that layout has the components in children in the order specified * by indices. diff --git a/server/tests/src/com/vaadin/tests/server/component/csslayout/AddComponentsTest.java b/server/tests/src/com/vaadin/tests/server/component/csslayout/AddComponentsTest.java index ce1357647b..120cf20070 100644 --- a/server/tests/src/com/vaadin/tests/server/component/csslayout/AddComponentsTest.java +++ b/server/tests/src/com/vaadin/tests/server/component/csslayout/AddComponentsTest.java @@ -91,6 +91,27 @@ public class AddComponentsTest { assertOrder(layout, new int[] { 2, 3, 1, 0 }); } + @Test + public void testConstructorWithComponents() { + Layout layout = new CssLayout(children); + assertOrder(layout, new int[] { 0, 1, 2, 3 }); + } + + @Test + public void testAddComponents() { + CssLayout layout = new CssLayout(); + layout.addComponents(children); + assertOrder(layout, new int[] { 0, 1, 2, 3 }); + + Label extra = new Label("Extra"); + layout.addComponents(extra); + assertSame(extra, layout.getComponent(4)); + + layout.removeAllComponents(); + layout.addComponents(children[3], children[2], children[1], children[0]); + assertOrder(layout, new int[] { 3, 2, 1, 0 }); + } + /** * Asserts that layout has the components in children in the order specified * by indices. diff --git a/server/tests/src/com/vaadin/tests/server/component/gridlayout/GridLayoutTest.java b/server/tests/src/com/vaadin/tests/server/component/gridlayout/GridLayoutTest.java new file mode 100644 index 0000000000..1eabf2fb62 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/gridlayout/GridLayoutTest.java @@ -0,0 +1,108 @@ +package com.vaadin.tests.server.component.gridlayout; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.fail; + +import java.util.Iterator; +import java.util.NoSuchElementException; + +import org.junit.Test; + +import com.vaadin.ui.Component; +import com.vaadin.ui.GridLayout; +import com.vaadin.ui.Label; +import com.vaadin.ui.Layout; + +public class GridLayoutTest { + Component[] children = new Component[] { new Label("A"), new Label("B"), + new Label("C"), new Label("D") }; + + @Test + public void testConstructorWithComponents() { + GridLayout grid = new GridLayout(2, 2, children); + assertContentPositions(grid); + assertOrder(grid, new int[] { 0, 1, 2, 3 }); + + grid = new GridLayout(1, 1, children); + assertContentPositions(grid); + assertOrder(grid, new int[] { 0, 1, 2, 3 }); + } + + @Test + public void testAddComponents() { + GridLayout grid = new GridLayout(2, 2); + grid.addComponents(children); + assertContentPositions(grid); + assertOrder(grid, new int[] { 0, 1, 2, 3 }); + + Label extra = new Label("Extra"); + Label extra2 = new Label("Extra2"); + grid.addComponents(extra, extra2); + assertSame(grid.getComponent(0, 2), extra); + assertSame(grid.getComponent(1, 2), extra2); + + grid.removeAllComponents(); + grid.addComponents(extra, extra2); + assertSame(grid.getComponent(0, 0), extra); + assertSame(grid.getComponent(1, 0), extra2); + + grid.addComponents(children); + assertOrder(grid, new int[] { -1, -1, 0, 1, 2, 3 }); + + grid.removeComponent(extra); + grid.removeComponent(extra2); + assertOrder(grid, new int[] { 0, 1, 2, 3 }); + + grid.addComponents(extra2, extra); + assertSame(grid.getComponent(0, 3), extra2); + assertSame(grid.getComponent(1, 3), extra); + assertOrder(grid, new int[] { 0, 1, 2, 3, -1, -1 }); + + grid.removeComponent(extra2); + grid.removeComponent(extra); + grid.setCursorX(0); + grid.setCursorY(0); + grid.addComponents(extra, extra2); + assertSame(grid.getComponent(0, 0), extra); + assertSame(grid.getComponent(1, 0), extra2); + assertOrder(grid, new int[] { -1, -1, 0, 1, 2, 3 }); + + grid = new GridLayout(); + grid.addComponents(children); + assertContentPositions(grid); + assertOrder(grid, new int[] { 0, 1, 2, 3 }); + } + + private void assertContentPositions(GridLayout grid) { + assertEquals(grid.getComponentCount(), children.length); + int c = 0; + for (int i = 0; i < grid.getRows(); i++) { + for (int j = 0; j < grid.getColumns(); j++) { + assertSame(grid.getComponent(j, i), children[c]); + c++; + } + } + } + + /** + * Asserts that layout has the components in children in the order specified + * by indices. + */ + private void assertOrder(Layout layout, int[] indices) { + Iterator<?> i = layout.iterator(); + try { + for (int index : indices) { + if (index != -1) { + assertSame(children[index], i.next()); + } else { + i.next(); + } + } + assertFalse("Too many components in layout", i.hasNext()); + } catch (NoSuchElementException e) { + fail("Too few components in layout"); + } + } +} diff --git a/server/tests/src/com/vaadin/tests/server/componentcontainer/FormLayoutTest.java b/server/tests/src/com/vaadin/tests/server/componentcontainer/FormLayoutTest.java index 71a813d423..50de871106 100644 --- a/server/tests/src/com/vaadin/tests/server/componentcontainer/FormLayoutTest.java +++ b/server/tests/src/com/vaadin/tests/server/componentcontainer/FormLayoutTest.java @@ -1,7 +1,13 @@ package com.vaadin.tests.server.componentcontainer; +import java.util.Iterator; +import java.util.NoSuchElementException; + +import org.junit.Test; + import com.vaadin.ui.Component; import com.vaadin.ui.FormLayout; +import com.vaadin.ui.Label; import com.vaadin.ui.Layout; public class FormLayoutTest extends AbstractIndexedLayoutTest { @@ -31,4 +37,36 @@ public class FormLayoutTest extends AbstractIndexedLayoutTest { return getLayout().getComponentCount(); } + Component[] children = new Component[] { new Label("A"), new Label("B"), + new Label("C"), new Label("D") }; + + @Test + public void testConstructorWithComponents() { + FormLayout l = new FormLayout(children); + assertOrder(l, new int[] { 0, 1, 2, 3 }); + } + + @Test + public void testAddComponents() { + FormLayout l = new FormLayout(); + l.addComponents(children); + assertOrder(l, new int[] { 0, 1, 2, 3 }); + } + + private void assertOrder(Layout layout, int[] indices) { + Iterator<?> i = layout.iterator(); + try { + for (int index : indices) { + if (index != -1) { + assertSame(children[index], i.next()); + } else { + i.next(); + } + } + assertFalse("Too many components in layout", i.hasNext()); + } catch (NoSuchElementException e) { + fail("Too few components in layout"); + } + } + } |