]> source.dussan.org Git - vaadin-framework.git/commitdiff
Write all empty cells for empty GridLayout in declarative #18805
authorMaciej Przepióra <mac@vaadin.com>
Mon, 21 Sep 2015 09:05:32 +0000 (11:05 +0200)
committerVaadin Code Review <review@vaadin.com>
Thu, 24 Sep 2015 07:40:24 +0000 (07:40 +0000)
Change-Id: I589ba0dbb4aea7578f85894046e99903d9202e37

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

index 792ad72dcc115332810db5f1e8d6b715387435eb..8837ad71d17f6940dbb497b5d21230f167be5019 100644 (file)
@@ -1456,8 +1456,12 @@ public class GridLayout extends AbstractLayout implements
 
         writeMargin(design, getMargin(), def.getMargin(), designContext);
 
-        if (components.isEmpty()
-                || !designContext.shouldWriteChildren(this, def)) {
+        if (!designContext.shouldWriteChildren(this, def)) {
+            return;
+        }
+
+        if (components.isEmpty()) {
+            writeEmptyColsAndRows(design, designContext);
             return;
         }
 
@@ -1593,6 +1597,35 @@ public class GridLayout extends AbstractLayout implements
         }
     }
 
+    /**
+     * Fills in the design with rows and empty columns. This needs to be done
+     * for empty {@link GridLayout}, because there's no other way to serialize
+     * info about number of columns and rows if there are absolutely no
+     * components in the {@link GridLayout}
+     * 
+     * @since
+     * @param design
+     * @param designContext
+     */
+    private void writeEmptyColsAndRows(Element design,
+            DesignContext designContext) {
+        int rowCount = getState(false).rows;
+        int colCount = getState(false).columns;
+
+        // only write cols and rows tags if size is not 1x1
+        if (rowCount == 1 && colCount == 1) {
+            return;
+        }
+
+        for (int i = 0; i < rowCount; i++) {
+            Element row = design.appendElement("row");
+            for (int j = 0; j < colCount; j++) {
+                row.appendElement("column");
+            }
+        }
+
+    }
+
     private int getRowSpan(Component[][] compMap, int i, int j, int colspan,
             Component child) {
         int rowspan = 1;
index d69fd929848bd0341978fb02a5a4c007093f9b38..fe2d1c4a2381b0ee65ecb2bf0b8b2443df567aed 100644 (file)
  */
 package com.vaadin.tests.server.component.gridlayout;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -22,6 +26,7 @@ import com.vaadin.tests.server.component.DeclarativeMarginTestBase;
 import com.vaadin.ui.Button;
 import com.vaadin.ui.Component;
 import com.vaadin.ui.GridLayout;
+import com.vaadin.ui.declarative.Design;
 import com.vaadin.ui.declarative.DesignContext;
 
 public class GridLayoutDeclarativeTest extends
@@ -236,4 +241,22 @@ public class GridLayoutDeclarativeTest extends
         testWrite(design, outer);
 
     }
+
+    @Test
+    public void testEmptyGridLayoutWithColsAndRowsSet() throws IOException {
+        GridLayout layout = new GridLayout();
+        layout.setRows(2);
+        layout.setColumns(2);
+
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        DesignContext context = new DesignContext();
+        context.setRootComponent(layout);
+        Design.write(context, out);
+
+        ByteArrayInputStream input = new ByteArrayInputStream(out.toByteArray());
+        Component component = Design.read(input);
+        GridLayout readLayout = (GridLayout) component;
+
+        Assert.assertEquals(layout.getRows(), readLayout.getRows());
+    }
 }