From: Artur Signell Date: Sun, 14 Feb 2016 11:50:12 +0000 (+0200) Subject: Read GridLayout component alignments from declarative files (#19536) X-Git-Tag: 7.7.0.alpha1~26^2~42 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=98180afe5766912d55c28b44ec231e8c359cba0e;p=vaadin-framework.git Read GridLayout component alignments from declarative files (#19536) Change-Id: I04524b71f6cdc2a31cb87958c30b6f698789511c --- diff --git a/server/src/com/vaadin/ui/AbstractOrderedLayout.java b/server/src/com/vaadin/ui/AbstractOrderedLayout.java index e0dbcb004b..f517ab0af5 100644 --- a/server/src/com/vaadin/ui/AbstractOrderedLayout.java +++ b/server/src/com/vaadin/ui/AbstractOrderedLayout.java @@ -31,7 +31,6 @@ import com.vaadin.server.Sizeable; import com.vaadin.shared.Connector; import com.vaadin.shared.EventId; import com.vaadin.shared.MouseEventDetails; -import com.vaadin.shared.ui.AlignmentInfo; import com.vaadin.shared.ui.MarginInfo; import com.vaadin.shared.ui.orderedlayout.AbstractOrderedLayoutServerRpc; import com.vaadin.shared.ui.orderedlayout.AbstractOrderedLayoutState; @@ -486,22 +485,8 @@ public abstract class AbstractOrderedLayout extends AbstractLayout implements Component newChild = designContext.readDesign(childComponent); addComponent(newChild); // handle alignment - int bitMask = 0; - if (attr.hasKey(":middle")) { - bitMask += AlignmentInfo.Bits.ALIGNMENT_VERTICAL_CENTER; - } else if (attr.hasKey(":bottom")) { - bitMask += AlignmentInfo.Bits.ALIGNMENT_BOTTOM; - } else { - bitMask += AlignmentInfo.Bits.ALIGNMENT_TOP; - } - if (attr.hasKey(":center")) { - bitMask += AlignmentInfo.Bits.ALIGNMENT_HORIZONTAL_CENTER; - } else if (attr.hasKey(":right")) { - bitMask += AlignmentInfo.Bits.ALIGNMENT_RIGHT; - } else { - bitMask += AlignmentInfo.Bits.ALIGNMENT_LEFT; - } - setComponentAlignment(newChild, new Alignment(bitMask)); + setComponentAlignment(newChild, + DesignAttributeHandler.readAlignment(attr)); // handle expand ratio if (attr.hasKey(":expand")) { String value = attr.get(":expand"); diff --git a/server/src/com/vaadin/ui/GridLayout.java b/server/src/com/vaadin/ui/GridLayout.java index 8517962e91..148fd85fff 100644 --- a/server/src/com/vaadin/ui/GridLayout.java +++ b/server/src/com/vaadin/ui/GridLayout.java @@ -1330,7 +1330,7 @@ public class GridLayout extends AbstractLayout implements } } setRows(Math.max(rows.size(), 1)); - + Map alignments = new HashMap(); List columnExpandRatios = new ArrayList(); for (int row = 0; row < rowElements.size(); ++row) { Element rowElement = rowElements.get(row); @@ -1357,7 +1357,10 @@ public class GridLayout extends AbstractLayout implements Component child = null; if (col.children().size() > 0) { - child = designContext.readDesign(col.child(0)); + Element childElement = col.child(0); + child = designContext.readDesign(childElement); + alignments.put(child, DesignAttributeHandler + .readAlignment(childElement.attributes())); // TODO: Currently ignoring any extra children. // Needs Error handling? } // Else: Empty placeholder. No child component. @@ -1441,6 +1444,7 @@ public class GridLayout extends AbstractLayout implements // Add component with area addComponent(child, j, i, j + colspan, i + rowspan); + setComponentAlignment(child, alignments.get(child)); } } // Set cursor position explicitly @@ -1511,16 +1515,8 @@ public class GridLayout extends AbstractLayout implements ChildComponentData coords = childData.get(child); Alignment alignment = getComponentAlignment(child); - if (alignment.isMiddle()) { - childElement.attr(":middle", true); - } else if (alignment.isBottom()) { - childElement.attr(":bottom", true); - } - if (alignment.isCenter()) { - childElement.attr(":center", true); - } else if (alignment.isRight()) { - childElement.attr(":right", true); - } + DesignAttributeHandler.writeAlignment(childElement, + alignment); col.appendChild(childElement); if (coords.row1 != coords.row2) { diff --git a/server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java b/server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java index cee2ebe381..4e9617c018 100644 --- a/server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java +++ b/server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java @@ -37,7 +37,9 @@ import org.jsoup.nodes.Element; import org.jsoup.nodes.Node; import com.vaadin.data.util.converter.Converter; +import com.vaadin.shared.ui.AlignmentInfo; import com.vaadin.shared.util.SharedUtil; +import com.vaadin.ui.Alignment; /** * Default attribute handler implementation used when parsing designs to @@ -452,4 +454,55 @@ public class DesignAttributeHandler implements Serializable { return (methods != null && methods.length > 1) ? methods[1] : null; } } + + /** + * Read the alignment from the given child component attributes. + * + * @since + * @param attr + * the child component attributes + * @return the component alignment + */ + public static Alignment readAlignment(Attributes attr) { + int bitMask = 0; + if (attr.hasKey(":middle")) { + bitMask += AlignmentInfo.Bits.ALIGNMENT_VERTICAL_CENTER; + } else if (attr.hasKey(":bottom")) { + bitMask += AlignmentInfo.Bits.ALIGNMENT_BOTTOM; + } else { + bitMask += AlignmentInfo.Bits.ALIGNMENT_TOP; + } + if (attr.hasKey(":center")) { + bitMask += AlignmentInfo.Bits.ALIGNMENT_HORIZONTAL_CENTER; + } else if (attr.hasKey(":right")) { + bitMask += AlignmentInfo.Bits.ALIGNMENT_RIGHT; + } else { + bitMask += AlignmentInfo.Bits.ALIGNMENT_LEFT; + } + + return new Alignment(bitMask); + } + + /** + * Writes the alignment to the given child element attributes. + * + * @since + * @param childElement + * the child element + * @param alignment + * the component alignment + */ + public static void writeAlignment(Element childElement, Alignment alignment) { + if (alignment.isMiddle()) { + childElement.attr(":middle", true); + } else if (alignment.isBottom()) { + childElement.attr(":bottom", true); + } + if (alignment.isCenter()) { + childElement.attr(":center", true); + } else if (alignment.isRight()) { + childElement.attr(":right", true); + } + } + } diff --git a/server/tests/src/com/vaadin/tests/server/component/gridlayout/GridLayoutDeclarativeTest.java b/server/tests/src/com/vaadin/tests/server/component/gridlayout/GridLayoutDeclarativeTest.java index 0e4293481e..e3975e41a8 100644 --- a/server/tests/src/com/vaadin/tests/server/component/gridlayout/GridLayoutDeclarativeTest.java +++ b/server/tests/src/com/vaadin/tests/server/component/gridlayout/GridLayoutDeclarativeTest.java @@ -70,7 +70,7 @@ public class GridLayoutDeclarativeTest extends @Test public void testOneBigComponentGridLayout() { - Button b1 = new Button("Button 0,0 -> 1,1"); + Button b1 = new Button("Button 0,0 -> 1,1"); b1.setCaptionAsHtml(true); String design = "" // + "" + writeChild(b1) + "" // @@ -86,19 +86,19 @@ public class GridLayoutDeclarativeTest extends @Test public void testMultipleSpannedComponentsGridLayout() { GridLayout gl = new GridLayout(5, 5); - Button b1 = new Button("Button 0,0 -> 0,2"); + Button b1 = new Button("Button 0,0 -> 0,2"); b1.setCaptionAsHtml(true); gl.addComponent(b1, 0, 0, 2, 0); - Button b2 = new Button("Button 0,3 -> 3,3"); + Button b2 = new Button("Button 0,3 -> 3,3"); b2.setCaptionAsHtml(true); gl.addComponent(b2, 3, 0, 3, 3); - Button b3 = new Button("Button 0,4 -> 1,4"); + Button b3 = new Button("Button 0,4 -> 1,4"); b3.setCaptionAsHtml(true); gl.addComponent(b3, 4, 0, 4, 1); - Button b4 = new Button("Button 1,0 -> 3,1"); + Button b4 = new Button("Button 1,0 -> 3,1"); b4.setCaptionAsHtml(true); gl.addComponent(b4, 0, 1, 1, 3); @@ -106,11 +106,11 @@ public class GridLayoutDeclarativeTest extends b5.setCaptionAsHtml(true); gl.addComponent(b5, 2, 2); - Button b6 = new Button("Button 3,4 -> 4,4"); + Button b6 = new Button("Button 3,4 -> 4,4"); b6.setCaptionAsHtml(true); gl.addComponent(b6, 4, 3, 4, 4); - Button b7 = new Button("Button 4,1 -> 4,2"); + Button b7 = new Button("Button 4,1 -> 4,2"); b7.setCaptionAsHtml(true); gl.addComponent(b7, 2, 4, 3, 4); @@ -146,7 +146,7 @@ public class GridLayoutDeclarativeTest extends @Test public void testManyExtraGridLayoutSlots() { GridLayout gl = new GridLayout(5, 5); - Button b1 = new Button("Button 0,4 -> 4,4"); + Button b1 = new Button("Button 0,4 -> 4,4"); b1.setCaptionAsHtml(true); gl.addComponent(b1, 4, 0, 4, 4); gl.setColumnExpandRatio(2, 2.0f); @@ -166,7 +166,7 @@ public class GridLayoutDeclarativeTest extends @Test public void testManyEmptyColumnsWithOneExpand() { GridLayout gl = new GridLayout(5, 5); - Button b1 = new Button("Button 0,4 -> 4,4"); + Button b1 = new Button("Button 0,4 -> 4,4"); b1.setCaptionAsHtml(true); gl.addComponent(b1, 0, 0, 0, 4); gl.setColumnExpandRatio(4, 2.0f); @@ -202,13 +202,29 @@ public class GridLayoutDeclarativeTest extends GridLayout result = super.testRead(design, expected); for (int row = 0; row < expected.getRows(); ++row) { - Assert.assertTrue(Math.abs(expected.getRowExpandRatio(row) - - result.getRowExpandRatio(row)) < 0.00001); + Assert.assertEquals(expected.getRowExpandRatio(row), + result.getRowExpandRatio(row), 0.00001); } for (int col = 0; col < expected.getColumns(); ++col) { - Assert.assertTrue(Math.abs(expected.getColumnExpandRatio(col) - - result.getColumnExpandRatio(col)) < 0.00001); + Assert.assertEquals(expected.getColumnExpandRatio(col), + result.getColumnExpandRatio(col), 0.00001); } + for (int row = 0; row < expected.getRows(); ++row) { + for (int col = 0; col < expected.getColumns(); ++col) { + Component eC = expected.getComponent(col, row); + Component rC = result.getComponent(col, row); + + assertEquals(eC, rC); + if (eC == null) { + continue; + } + + Assert.assertEquals(expected.getComponentAlignment(eC), + result.getComponentAlignment(rC)); + + } + } + return result; }