/* Child component alignments */
/**
- * Mapping from components to alignments (horizontal + vertical).
+ * Constructs an empty AbstractOrderedLayout.
*/
public AbstractOrderedLayout() {
registerRpc(rpc);
}
+ /**
+ * 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.
*
*/
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.
}
}
+ /**
+ * 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.
* 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.
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);
+ }
+
}
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();
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.
@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);
+ }
+
}
@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);
+ }
}
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.
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.
--- /dev/null
+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");
+ }
+ }
+}
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 {
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");
+ }
+ }
+
}