]> source.dussan.org Git - vaadin-framework.git/commitdiff
Commit Pekka's patch for #8030 ((Component...) constructor for layouts) and #5422...
authorJohannes Dahlström <johannesd@vaadin.com>
Fri, 7 Sep 2012 13:31:16 +0000 (16:31 +0300)
committerJohannes Dahlström <johannesd@vaadin.com>
Fri, 7 Sep 2012 14:12:24 +0000 (17:12 +0300)
server/src/com/vaadin/ui/AbstractOrderedLayout.java
server/src/com/vaadin/ui/CssLayout.java
server/src/com/vaadin/ui/FormLayout.java
server/src/com/vaadin/ui/GridLayout.java
server/src/com/vaadin/ui/HorizontalLayout.java
server/src/com/vaadin/ui/VerticalLayout.java
server/tests/src/com/vaadin/tests/server/component/abstractorderedlayout/AddComponentsTest.java
server/tests/src/com/vaadin/tests/server/component/csslayout/AddComponentsTest.java
server/tests/src/com/vaadin/tests/server/component/gridlayout/GridLayoutTest.java [new file with mode: 0644]
server/tests/src/com/vaadin/tests/server/componentcontainer/FormLayoutTest.java

index fb345d30ec04369f472dbe1d22234e2fe8df76b7..cb3d9cfe9fa0e1f301a52f844e20def080243c19 100644 (file)
@@ -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);
@@ -112,6 +112,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.
      * 
index 9ac29c4deb0f4836cde6304df89bbdc4e15d4d23..401eafe6e9fb701f3b7f0829069f38d9f2c1d34b 100644 (file)
@@ -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.
@@ -109,6 +125,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.
index 15ea4785972466abe6b05ec23d0f7d74ddac1463..4df9e8f3c0b2fef2208ce7361954082e65784eb0 100644 (file)
@@ -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);
+    }
+
 }
index de167962e7ca7b5f624e8eb2d29c36d378e9379d..566bd812a3e00d2f8984e05992379c66ab901de6 100644 (file)
@@ -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();
@@ -257,6 +275,19 @@ public class GridLayout extends AbstractLayout implements
         markAsDirty();
     }
 
+    /**
+     * 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.
index 9207b593536bc60423bfbd6adc07e590a8254999..3797d5252a13c60649a6b01095b911a11c7e9531 100644 (file)
@@ -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);
+    }
+
 }
index 0a13ca9a36eeeb3f00a9705ac89d8a723dd4f0f8..ba07603166d6938f228eb42622af42bb92f9f445 100644 (file)
@@ -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);
+    }
 }
index bd67841f3364b62ed1badca76c60f5fe2a85f7fb..81c29995f5406cf016e1c8d9343d6e04609efa3d 100644 (file)
@@ -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.
index ce1357647b2559eb150e7ca149e4794e176fe95c..120cf200701933478e8fd08f307cad42b3adf3e5 100644 (file)
@@ -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 (file)
index 0000000..1eabf2f
--- /dev/null
@@ -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");
+        }
+    }
+}
index 71a813d4239d8cb1e077229b50d9f5743c7634bd..50de871106daf9e8335e6124eb3f1db614744e11 100644 (file)
@@ -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");
+        }
+    }
+
 }