]> source.dussan.org Git - vaadin-framework.git/commitdiff
Read GridLayout component alignments from declarative files (#19536)
authorArtur Signell <artur@vaadin.com>
Sun, 14 Feb 2016 11:50:12 +0000 (13:50 +0200)
committerVaadin Code Review <review@vaadin.com>
Mon, 29 Feb 2016 12:20:38 +0000 (12:20 +0000)
Change-Id: I04524b71f6cdc2a31cb87958c30b6f698789511c

server/src/com/vaadin/ui/AbstractOrderedLayout.java
server/src/com/vaadin/ui/GridLayout.java
server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java
server/tests/src/com/vaadin/tests/server/component/gridlayout/GridLayoutDeclarativeTest.java

index e0dbcb004b63c43f8aa9503f50f5556c6196727a..f517ab0af5ff149eee9fb275ccb8eeb7ca96bde9 100644 (file)
@@ -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");
index 8517962e9131961d16b07ce19467a040e2030f02..148fd85fffcb07a16e0152460dea5ac2511e8658 100644 (file)
@@ -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) {
index cee2ebe381342a38588de99c78536dc41d0d56d7..4e9617c0185ee009fcce6c330d37728dd7816e9e 100644 (file)
@@ -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);
+        }
+    }
+
 }
index 0e4293481efaeaf1aa1778873df662804a3bcd76..e3975e41a8dc0a265323c38f77d0fc4245172238 100644 (file)
@@ -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 -&gt; 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 -&gt; 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 -&gt; 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 -&gt; 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 -&gt; 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 -&gt; 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 -&gt; 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 -&gt; 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 -&gt; 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;
     }