diff options
author | Leif Åstrand <legioth@gmail.com> | 2017-02-08 19:05:31 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-08 19:05:31 +0200 |
commit | 5b4b93133d51d3c0ccf31d8ecb41505c7d2a45f9 (patch) | |
tree | 17da9e5fab387e0c3daaa4896b265b90838978ed | |
parent | c09b84d89fb5d7f724e93a1934158e042759aafe (diff) | |
download | vaadin-framework-5b4b93133d51d3c0ccf31d8ecb41505c7d2a45f9.tar.gz vaadin-framework-5b4b93133d51d3c0ccf31d8ecb41505c7d2a45f9.zip |
Add addComponentsAndExpand to horizontal and vertical layout (#8480)
4 files changed, 182 insertions, 0 deletions
diff --git a/server/src/main/java/com/vaadin/ui/HorizontalLayout.java b/server/src/main/java/com/vaadin/ui/HorizontalLayout.java index 07583c6712..ccee900ca1 100644 --- a/server/src/main/java/com/vaadin/ui/HorizontalLayout.java +++ b/server/src/main/java/com/vaadin/ui/HorizontalLayout.java @@ -60,4 +60,26 @@ public class HorizontalLayout extends AbstractOrderedLayout { return (HorizontalLayoutState) super.getState(markAsDirty); } + /** + * Adds the given components to this layout and sets them as expanded. The + * width of all added child components are set to 100% so that the expansion + * will be effective. The width of this layout is also set to 100% if it is + * currently undefined. + * + * @param components + * the components to set, not <code>null</code> + */ + public void addComponentsAndExpand(Component... components) { + addComponents(components); + + if (getWidth() < 0) { + setWidth(100, Unit.PERCENTAGE); + } + + for (Component child : components) { + child.setWidth(100, Unit.PERCENTAGE); + setExpandRatio(child, 1); + } + } + } diff --git a/server/src/main/java/com/vaadin/ui/VerticalLayout.java b/server/src/main/java/com/vaadin/ui/VerticalLayout.java index d441e8a664..5d8bda3b4c 100644 --- a/server/src/main/java/com/vaadin/ui/VerticalLayout.java +++ b/server/src/main/java/com/vaadin/ui/VerticalLayout.java @@ -62,4 +62,26 @@ public class VerticalLayout extends AbstractOrderedLayout { protected VerticalLayoutState getState(boolean markAsDirty) { return (VerticalLayoutState) super.getState(markAsDirty); } + + /** + * Adds the given components to this layout and sets them as expanded. The + * height of all added child components are set to 100% so that the + * expansion will be effective. The height of this layout is also set to + * 100% if it is currently undefined. + * + * @param components + * the components to set, not <code>null</code> + */ + public void addComponentsAndExpand(Component... components) { + addComponents(components); + + if (getHeight() < 0) { + setHeight(100, Unit.PERCENTAGE); + } + + for (Component child : components) { + child.setHeight(100, Unit.PERCENTAGE); + setExpandRatio(child, 1); + } + } } diff --git a/server/src/test/java/com/vaadin/tests/server/componentcontainer/HorizontalLayoutTest.java b/server/src/test/java/com/vaadin/tests/server/componentcontainer/HorizontalLayoutTest.java new file mode 100644 index 0000000000..12f235011e --- /dev/null +++ b/server/src/test/java/com/vaadin/tests/server/componentcontainer/HorizontalLayoutTest.java @@ -0,0 +1,86 @@ +package com.vaadin.tests.server.componentcontainer; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.ui.Button; +import com.vaadin.ui.Component; +import com.vaadin.ui.HorizontalLayout; +import com.vaadin.ui.Layout; + +public class HorizontalLayoutTest extends AbstractIndexedLayoutTestBase { + + @Override + protected Layout createLayout() { + return new HorizontalLayout(); + } + + @Override + public HorizontalLayout getLayout() { + return (HorizontalLayout) 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(); + } + + @Test + public void addAndExpand_basicCase() { + Button b1 = new Button(); + Button b2 = new Button(); + Button b3 = new Button(); + + HorizontalLayout layout = getLayout(); + layout.addComponents(b3, b2); + + layout.addComponentsAndExpand(b1, b2); + + Assert.assertEquals(3, layout.getComponentCount()); + + Assert.assertSame(b3, layout.getComponent(0)); + Assert.assertSame(b1, layout.getComponent(1)); + Assert.assertSame(b2, layout.getComponent(2)); + + Assert.assertEquals(0, layout.getExpandRatio(b3), 0); + Assert.assertEquals(1, layout.getExpandRatio(b1), 0); + Assert.assertEquals(1, layout.getExpandRatio(b2), 0); + + Assert.assertEquals(-1, b3.getWidth(), 0); + Assert.assertEquals(100, b1.getWidth(), 0); + Assert.assertEquals(100, b2.getWidth(), 0); + } + + @Test + public void addAndExpand_undefinedHeightUpdated() { + HorizontalLayout layout = getLayout(); + + Assert.assertEquals(-1, layout.getWidth(), 0); + + layout.addComponentsAndExpand(); + + Assert.assertEquals(100, layout.getWidth(), 0); + } + + @Test + public void addAndExpand_definedHeightPreserved() { + HorizontalLayout layout = getLayout(); + + layout.setWidth("150px"); + + layout.addComponentsAndExpand(); + + Assert.assertEquals(150, layout.getWidth(), 0); + } + +} diff --git a/server/src/test/java/com/vaadin/tests/server/componentcontainer/VerticalLayoutTest.java b/server/src/test/java/com/vaadin/tests/server/componentcontainer/VerticalLayoutTest.java index 565cc8e8ab..2bb5d3a433 100644 --- a/server/src/test/java/com/vaadin/tests/server/componentcontainer/VerticalLayoutTest.java +++ b/server/src/test/java/com/vaadin/tests/server/componentcontainer/VerticalLayoutTest.java @@ -1,5 +1,9 @@ package com.vaadin.tests.server.componentcontainer; +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.ui.Button; import com.vaadin.ui.Component; import com.vaadin.ui.Layout; import com.vaadin.ui.VerticalLayout; @@ -31,4 +35,52 @@ public class VerticalLayoutTest extends AbstractIndexedLayoutTestBase { return getLayout().getComponentCount(); } + @Test + public void addAndExpand_basicCase() { + Button b1 = new Button(); + Button b2 = new Button(); + Button b3 = new Button(); + + VerticalLayout layout = getLayout(); + layout.addComponents(b3, b2); + + layout.addComponentsAndExpand(b1, b2); + + Assert.assertEquals(3, layout.getComponentCount()); + + Assert.assertSame(b3, layout.getComponent(0)); + Assert.assertSame(b1, layout.getComponent(1)); + Assert.assertSame(b2, layout.getComponent(2)); + + Assert.assertEquals(0, layout.getExpandRatio(b3), 0); + Assert.assertEquals(1, layout.getExpandRatio(b1), 0); + Assert.assertEquals(1, layout.getExpandRatio(b2), 0); + + Assert.assertEquals(-1, b3.getHeight(), 0); + Assert.assertEquals(100, b1.getHeight(), 0); + Assert.assertEquals(100, b2.getHeight(), 0); + } + + @Test + public void addAndExpand_undefinedHeightUpdated() { + VerticalLayout layout = getLayout(); + + Assert.assertEquals(-1, layout.getHeight(), 0); + + layout.addComponentsAndExpand(); + + Assert.assertEquals(100, layout.getHeight(), 0); + } + + @Test + public void addAndExpand_definedHeightPreserved() { + VerticalLayout layout = getLayout(); + + layout.setHeight("150px"); + + layout.addComponentsAndExpand(); + + Assert.assertEquals(150, layout.getHeight(), 0); + } + } |