diff options
author | Artur Signell <artur@vaadin.com> | 2016-02-14 13:50:12 +0200 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2016-02-29 12:20:38 +0000 |
commit | 98180afe5766912d55c28b44ec231e8c359cba0e (patch) | |
tree | 0e5858520c8d8e0a3a5dfea8c735fe8c2e7e3ff9 /server | |
parent | 90beab7e88b387c72f2a9eb183b92e34ae818d9c (diff) | |
download | vaadin-framework-98180afe5766912d55c28b44ec231e8c359cba0e.tar.gz vaadin-framework-98180afe5766912d55c28b44ec231e8c359cba0e.zip |
Read GridLayout component alignments from declarative files (#19536)
Change-Id: I04524b71f6cdc2a31cb87958c30b6f698789511c
Diffstat (limited to 'server')
4 files changed, 92 insertions, 42 deletions
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<Component, Alignment> alignments = new HashMap<Component, Alignment>(); List<Integer> columnExpandRatios = new ArrayList<Integer>(); 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 = "<vaadin-grid-layout><row>" // + "<column colspan=2 rowspan=2>" + writeChild(b1) + "</column>" // @@ -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; } |