@@ -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); | |||
} | |||
} | |||
} |
@@ -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); | |||
} | |||
} | |||
} |
@@ -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); | |||
} | |||
} |
@@ -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); | |||
} | |||
} |