diff options
5 files changed, 181 insertions, 1 deletions
diff --git a/server/src/com/vaadin/ui/AbstractOrderedLayout.java b/server/src/com/vaadin/ui/AbstractOrderedLayout.java index 8c2f86926d..b06b2e0871 100644 --- a/server/src/com/vaadin/ui/AbstractOrderedLayout.java +++ b/server/src/com/vaadin/ui/AbstractOrderedLayout.java @@ -53,6 +53,8 @@ public abstract class AbstractOrderedLayout extends AbstractLayout implements */ protected LinkedList<Component> components = new LinkedList<Component>(); + private Alignment defaultComponentAlignment = Alignment.TOP_LEFT; + /* Child component alignments */ /** @@ -147,7 +149,9 @@ public abstract class AbstractOrderedLayout extends AbstractLayout implements } private void componentAdded(Component c) { - getState().childData.put(c, new ChildComponentData()); + ChildComponentData ccd = new ChildComponentData(); + ccd.alignmentBitmask = getDefaultComponentAlignment().getBitMask(); + getState().childData.put(c, ccd); } /** @@ -417,4 +421,26 @@ public abstract class AbstractOrderedLayout extends AbstractLayout implements public void setMargin(MarginInfo marginInfo) { getState().marginsBitmask = marginInfo.getBitMask(); } + + /* + * (non-Javadoc) + * + * @see com.vaadin.ui.Layout.AlignmentHandler#getDefaultComponentAlignment() + */ + @Override + public Alignment getDefaultComponentAlignment() { + return defaultComponentAlignment; + } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.ui.Layout.AlignmentHandler#setDefaultComponentAlignment(com + * .vaadin.ui.Alignment) + */ + public void setDefaultComponentAlignment(Alignment defaultAlignment) { + defaultComponentAlignment = defaultAlignment; + } + } diff --git a/server/src/com/vaadin/ui/GridLayout.java b/server/src/com/vaadin/ui/GridLayout.java index e60d9c676a..60664c8937 100644 --- a/server/src/com/vaadin/ui/GridLayout.java +++ b/server/src/com/vaadin/ui/GridLayout.java @@ -92,6 +92,7 @@ public class GridLayout extends AbstractLayout implements private Map<Integer, Float> columnExpandRatio = new HashMap<Integer, Float>(); private Map<Integer, Float> rowExpandRatio = new HashMap<Integer, Float>(); + private Alignment defaultComponentAlignment = Alignment.TOP_LEFT; /** * Constructor for a grid of given size (number of columns and rows). @@ -573,6 +574,7 @@ public class GridLayout extends AbstractLayout implements int row2) { this.component = component; childData = new ChildComponentData(); + childData.alignment = getDefaultComponentAlignment().getBitMask(); childData.column1 = column1; childData.row1 = row1; childData.column2 = column2; @@ -1226,4 +1228,25 @@ public class GridLayout extends AbstractLayout implements return new MarginInfo(getState().marginsBitmask); } + /* + * (non-Javadoc) + * + * @see com.vaadin.ui.Layout.AlignmentHandler#getDefaultComponentAlignment() + */ + @Override + public Alignment getDefaultComponentAlignment() { + return defaultComponentAlignment; + } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.ui.Layout.AlignmentHandler#setDefaultComponentAlignment(com + * .vaadin.ui.Alignment) + */ + public void setDefaultComponentAlignment(Alignment defaultAlignment) { + defaultComponentAlignment = defaultAlignment; + } + } diff --git a/server/src/com/vaadin/ui/Layout.java b/server/src/com/vaadin/ui/Layout.java index cd6ffc42d2..dc16b186f2 100644 --- a/server/src/com/vaadin/ui/Layout.java +++ b/server/src/com/vaadin/ui/Layout.java @@ -61,6 +61,23 @@ public interface Layout extends ComponentContainer, Serializable { */ public Alignment getComponentAlignment(Component childComponent); + /** + * Sets the alignment used for new components added to this layout. The + * default is {@link Alignment#TOP_LEFT}. + * + * @param defaultComponentAlignment + * The new default alignment + */ + public void setDefaultComponentAlignment( + Alignment defaultComponentAlignment); + + /** + * Returns the alignment used for new components added to this layout + * + * @return The default alignment + */ + public Alignment getDefaultComponentAlignment(); + } /** diff --git a/server/tests/src/com/vaadin/tests/server/component/gridlayout/DefaultAlignment.java b/server/tests/src/com/vaadin/tests/server/component/gridlayout/DefaultAlignment.java new file mode 100644 index 0000000000..2faa65d1f2 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/gridlayout/DefaultAlignment.java @@ -0,0 +1,46 @@ +package com.vaadin.tests.server.component.gridlayout; + +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; + +import com.vaadin.ui.Alignment; +import com.vaadin.ui.GridLayout; +import com.vaadin.ui.Label; +import com.vaadin.ui.TextField; + +public class DefaultAlignment { + + private GridLayout gridLayout; + + @Before + public void setup() { + gridLayout = new GridLayout(2, 2); + } + + @Test + public void testDefaultAlignment() { + Label label = new Label("A label"); + TextField tf = new TextField("A TextField"); + gridLayout.addComponent(label); + gridLayout.addComponent(tf); + Assert.assertEquals(Alignment.TOP_LEFT, + gridLayout.getComponentAlignment(label)); + Assert.assertEquals(Alignment.TOP_LEFT, + gridLayout.getComponentAlignment(tf)); + } + + @Test + public void testAlteredDefaultAlignment() { + Label label = new Label("A label"); + TextField tf = new TextField("A TextField"); + gridLayout.setDefaultComponentAlignment(Alignment.MIDDLE_CENTER); + gridLayout.addComponent(label); + gridLayout.addComponent(tf); + Assert.assertEquals(Alignment.MIDDLE_CENTER, + gridLayout.getComponentAlignment(label)); + Assert.assertEquals(Alignment.MIDDLE_CENTER, + gridLayout.getComponentAlignment(tf)); + } +} diff --git a/server/tests/src/com/vaadin/tests/server/component/orderedlayout/DefaultAlignment.java b/server/tests/src/com/vaadin/tests/server/component/orderedlayout/DefaultAlignment.java new file mode 100644 index 0000000000..701373aba0 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/orderedlayout/DefaultAlignment.java @@ -0,0 +1,68 @@ +package com.vaadin.tests.server.component.orderedlayout; + +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; + +import com.vaadin.ui.AbstractOrderedLayout; +import com.vaadin.ui.Alignment; +import com.vaadin.ui.HorizontalLayout; +import com.vaadin.ui.Label; +import com.vaadin.ui.TextField; +import com.vaadin.ui.VerticalLayout; + +public class DefaultAlignment { + + private VerticalLayout verticalLayout; + private HorizontalLayout horizontalLayout; + + @Before + public void setup() { + verticalLayout = new VerticalLayout(); + horizontalLayout = new HorizontalLayout(); + } + + @Test + public void testDefaultAlignmentVerticalLayout() { + testDefaultAlignment(verticalLayout); + } + + @Test + public void testDefaultAlignmentHorizontalLayout() { + testDefaultAlignment(horizontalLayout); + } + + public void testDefaultAlignment(AbstractOrderedLayout layout) { + Label label = new Label("A label"); + TextField tf = new TextField("A TextField"); + layout.addComponent(label); + layout.addComponent(tf); + Assert.assertEquals(Alignment.TOP_LEFT, + layout.getComponentAlignment(label)); + Assert.assertEquals(Alignment.TOP_LEFT, + layout.getComponentAlignment(tf)); + } + + @Test + public void testAlteredDefaultAlignmentVerticalLayout() { + testAlteredDefaultAlignment(verticalLayout); + } + + @Test + public void testAlteredDefaultAlignmentHorizontalLayout() { + testAlteredDefaultAlignment(horizontalLayout); + } + + public void testAlteredDefaultAlignment(AbstractOrderedLayout layout) { + Label label = new Label("A label"); + TextField tf = new TextField("A TextField"); + layout.setDefaultComponentAlignment(Alignment.MIDDLE_CENTER); + layout.addComponent(label); + layout.addComponent(tf); + Assert.assertEquals(Alignment.MIDDLE_CENTER, + layout.getComponentAlignment(label)); + Assert.assertEquals(Alignment.MIDDLE_CENTER, + layout.getComponentAlignment(tf)); + } +} |