]> source.dussan.org Git - vaadin-framework.git/commitdiff
Add option for collapsing empty rows/columns in GridLayout (#14392)
authorArtur Signell <artur@vaadin.com>
Fri, 8 Aug 2014 13:01:30 +0000 (16:01 +0300)
committerVaadin Code Review <review@vaadin.com>
Thu, 21 Aug 2014 05:37:44 +0000 (05:37 +0000)
This reverts the new default behavior introduced in fix for #8855.
If you want empty rows/columns to be ignored when rendering,
use GridLayout.setHideEmptyRowsAndColumns(true)

Change-Id: I38a8717d79cec7739b649174654b615db9d8dc7e

client/src/com/vaadin/client/ui/VGridLayout.java
client/src/com/vaadin/client/ui/gridlayout/GridLayoutConnector.java
server/src/com/vaadin/ui/GridLayout.java
shared/src/com/vaadin/shared/ui/gridlayout/GridLayoutState.java
uitest/src/com/vaadin/tests/components/gridlayout/GridLayoutExpandRatio.java
uitest/src/com/vaadin/tests/components/gridlayout/GridLayoutHideMiddleCells.java
uitest/src/com/vaadin/tests/components/gridlayout/MoveComponentsFromGridLayoutToInnerLayout.java
uitest/src/com/vaadin/tests/components/gridlayout/UniformGridLayoutUI.java [new file with mode: 0644]
uitest/src/com/vaadin/tests/components/gridlayout/UniformGridLayoutUITest.java [new file with mode: 0644]
uitest/src/com/vaadin/tests/layouts/gridlayout/GridLayoutWidthChange.java
uitest/src/com/vaadin/tests/layouts/layouttester/GridLayout/GridBaseLayoutTestUI.java

index 17131ce63bbd61234657aa921616c3470364b79b..4929b02c9a01f90d7c7f0f2ecb4ba0a1eed21a5f 100644 (file)
@@ -75,6 +75,8 @@ public class VGridLayout extends ComplexPanel {
     public Set<Integer> explicitRowRatios;
     public Set<Integer> explicitColRatios;
 
+    public boolean hideEmptyRowsAndColumns = false;
+
     public VGridLayout() {
         super();
         setElement(Document.get().createDivElement());
@@ -170,27 +172,47 @@ public class VGridLayout extends ComplexPanel {
     private int[] calcRowExpandRatio() {
         int[] actualExpandRatio = new int[minRowHeights.length];
         for (int i = 0; i < minRowHeights.length; i++) {
-            if (rowHasComponentsOrRowSpan(i)) {
-                actualExpandRatio[i] = rowExpandRatioArray[i];
+            if (hiddenEmptyRow(i)) {
+                actualExpandRatio[i] = 0;
             } else {
-                // Should not do this if this has explicitly been
-                // expanded
-                if (explicitRowRatios.contains(i)) {
-                    actualExpandRatio[i] = rowExpandRatioArray[i];
-                } else {
-                    actualExpandRatio[i] = 0;
-                }
+                actualExpandRatio[i] = rowExpandRatioArray[i];
             }
         }
         return actualExpandRatio;
     }
 
+    /**
+     * Checks if it is ok to hide (or ignore) the given row.
+     * 
+     * @param rowIndex
+     *            the row to check
+     * @return true, if the row should be interpreted as non-existant (hides
+     *         extra spacing)
+     */
+    private boolean hiddenEmptyRow(int rowIndex) {
+        return hideEmptyRowsAndColumns && !rowHasComponentsOrRowSpan(rowIndex)
+                && !explicitRowRatios.contains(rowIndex);
+    }
+
+    /**
+     * Checks if it is ok to hide (or ignore) the given column.
+     * 
+     * @param columnIndex
+     *            the column to check
+     * @return true, if the column should be interpreted as non-existant (hides
+     *         extra spacing)
+     */
+    private boolean hiddenEmptyColumn(int columnIndex) {
+        return hideEmptyRowsAndColumns
+                && !colHasComponentsOrColSpan(columnIndex)
+                && !explicitColRatios.contains(columnIndex);
+    }
+
     private int calcRowUsedSpace() {
         int usedSpace = minRowHeights[0];
         int verticalSpacing = getVerticalSpacing();
         for (int i = 1; i < minRowHeights.length; i++) {
-            if (rowHasComponentsOrRowSpan(i) || minRowHeights[i] > 0
-                    || explicitRowRatios.contains(i)) {
+            if (minRowHeights[i] > 0 || !hiddenEmptyRow(i)) {
                 usedSpace += verticalSpacing + minRowHeights[i];
             }
         }
@@ -233,16 +255,10 @@ public class VGridLayout extends ComplexPanel {
     private int[] calcColumnExpandRatio() {
         int[] actualExpandRatio = new int[minColumnWidths.length];
         for (int i = 0; i < minColumnWidths.length; i++) {
-            if (colHasComponentsOrColSpan(i)) {
+            if (!hiddenEmptyColumn(i)) {
                 actualExpandRatio[i] = colExpandRatioArray[i];
             } else {
-                // Should not do this if this has explicitly been
-                // expanded
-                if (explicitColRatios.contains(i)) {
-                    actualExpandRatio[i] = colExpandRatioArray[i];
-                } else {
-                    actualExpandRatio[i] = 0;
-                }
+                actualExpandRatio[i] = 0;
             }
         }
         return actualExpandRatio;
@@ -255,8 +271,7 @@ public class VGridLayout extends ComplexPanel {
         int usedSpace = minColumnWidths[0];
         int horizontalSpacing = getHorizontalSpacing();
         for (int i = 1; i < minColumnWidths.length; i++) {
-            if (colHasComponentsOrColSpan(i) || minColumnWidths[i] > 0
-                    || explicitColRatios.contains(i)) {
+            if (minColumnWidths[i] > 0 || !hiddenEmptyColumn(i)) {
                 usedSpace += horizontalSpacing + minColumnWidths[i];
             }
         }
index 786bd18bf9214d1ab0201f76b061e32c591b486a..36262ef3d5a761f92ac8005afb8444800f7da3c8 100644 (file)
@@ -93,6 +93,8 @@ public class GridLayoutConnector extends AbstractComponentContainerConnector
 
         clickEventHandler.handleEventHandlerRegistration();
 
+        getWidget().hideEmptyRowsAndColumns = getState().hideEmptyRowsAndColumns;
+
     }
 
     @Override
index 0c097abc837c93ad4dfb72c143482c70d4d00dd3..0dd16a03e7a8218d9a9b0b110c066a663ef83ee5 100644 (file)
@@ -1256,4 +1256,33 @@ public class GridLayout extends AbstractLayout implements
         defaultComponentAlignment = defaultAlignment;
     }
 
+    /**
+     * Sets whether empty rows and columns should be considered as non-existent
+     * when rendering or not. If this is set to true then the spacing between
+     * multiple empty columns (or rows) will be collapsed.
+     * 
+     * The default behavior is to consider all rows and columns as visible
+     * 
+     * NOTE that this must be set before the initial rendering takes place.
+     * Updating this on the fly is not supported.
+     * 
+     * @since 7.3
+     * @param hideEmptyRowsAndColumns
+     *            true to hide empty rows and columns, false to leave them as-is
+     */
+    public void setHideEmptyRowsAndColumns(boolean hideEmptyRowsAndColumns) {
+        getState().hideEmptyRowsAndColumns = hideEmptyRowsAndColumns;
+    }
+
+    /**
+     * Checks whether whether empty rows and columns should be considered as
+     * non-existent when rendering or not.
+     * 
+     * @see #setHideEmptyRowsAndColumns(boolean)
+     * @since 7.3
+     * @return true if empty rows and columns are hidden, false otherwise
+     */
+    public boolean isHideEmptyRowsAndColumns() {
+        return getState(false).hideEmptyRowsAndColumns;
+    }
 }
index 768183cf737274f98dbb5cf4dec11e5a05d1281f..b84c2673f4bad59d632c24082e977bbe09ddbca7 100644 (file)
@@ -39,6 +39,7 @@ public class GridLayoutState extends AbstractLayoutState {
     public Set<Integer> explicitRowRatios = new HashSet<Integer>();;
     public Set<Integer> explicitColRatios = new HashSet<Integer>();
     public Map<Connector, ChildComponentData> childData = new HashMap<Connector, GridLayoutState.ChildComponentData>();
+    public boolean hideEmptyRowsAndColumns = false;
 
     public static class ChildComponentData implements Serializable {
         public int column1;
index c20148743a1e38215cb91e4fb0c256a508c89b67..2a533e4eda7d37418708cc807c409259dc478052 100644 (file)
@@ -37,9 +37,12 @@ public class GridLayoutExpandRatio extends AbstractTestUI {
         labels = new Label[ROWS][COLS];
         layout = new HorizontalLayout();
         gridLayout = new GridLayout(ROWS, COLS);
+        gridLayout.setHideEmptyRowsAndColumns(true);
+
         layout.setImmediate(true);
         gridLayout.setImmediate(true);
         gridLayout2 = new GridLayout(4, 4);
+        gridLayout2.setHideEmptyRowsAndColumns(true);
         for (int i = 0; i < ROWS; i++) {
             for (int j = 0; j < COLS; j++) {
                 Label label = new Label("Slot " + i + " " + j);
index 16b3742c64e1a7445041e9ea83fe9b3d8d0e397c..4712b8fb26397d9943be8054510d362174d282ab 100644 (file)
@@ -39,7 +39,11 @@ public class GridLayoutHideMiddleCells extends AbstractTestUI {
         VerticalLayout mainLayout = new VerticalLayout();
         HorizontalLayout horLayout = new HorizontalLayout();
         gridLayout = new GridLayout(ROWS, COLS);
+        gridLayout.setHideEmptyRowsAndColumns(true);
+
         gridLayout2 = new GridLayout(4, 4);
+        gridLayout2.setHideEmptyRowsAndColumns(true);
+
         for (int i = 0; i < 5; i++) {
             for (int j = 0; j < 5; j++) {
                 Label label = new Label("Slot " + i + " " + j);
index 4f5b29c91ff37418b03cc2935d1f5a7bcfa4e832..1da2322dc95d19bf0cb73bc801458d24596851d8 100644 (file)
@@ -2,9 +2,12 @@ package com.vaadin.tests.components.gridlayout;
 
 import com.vaadin.server.VaadinRequest;
 import com.vaadin.tests.components.AbstractTestUI;
-import com.vaadin.tests.components.TestBase;
-import com.vaadin.ui.*;
+import com.vaadin.ui.Button;
 import com.vaadin.ui.Button.ClickEvent;
+import com.vaadin.ui.ComponentContainer;
+import com.vaadin.ui.GridLayout;
+import com.vaadin.ui.Label;
+import com.vaadin.ui.VerticalLayout;
 
 public class MoveComponentsFromGridLayoutToInnerLayout extends AbstractTestUI {
 
@@ -15,6 +18,7 @@ public class MoveComponentsFromGridLayoutToInnerLayout extends AbstractTestUI {
     @Override
     protected void setup(VaadinRequest request) {
         gl = new GridLayout();
+        gl.setHideEmptyRowsAndColumns(true);
         gl.setWidth("200px");
         gl.setHeight("200px");
 
diff --git a/uitest/src/com/vaadin/tests/components/gridlayout/UniformGridLayoutUI.java b/uitest/src/com/vaadin/tests/components/gridlayout/UniformGridLayoutUI.java
new file mode 100644 (file)
index 0000000..07abb61
--- /dev/null
@@ -0,0 +1,77 @@
+package com.vaadin.tests.components.gridlayout;
+
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.ui.Alignment;
+import com.vaadin.ui.GridLayout;
+import com.vaadin.ui.Label;
+import com.vaadin.ui.TextField;
+import com.vaadin.ui.UI;
+import com.vaadin.ui.VerticalLayout;
+
+@SuppressWarnings("serial")
+public class UniformGridLayoutUI extends UI {
+
+    @Override
+    protected void init(VaadinRequest request) {
+        final VerticalLayout layout = new VerticalLayout();
+        layout.setMargin(true);
+        setContent(layout);
+        layout.setSpacing(true);
+        boolean hide = (request.getParameter("collapse") != null);
+        layout.addComponent(createGridWithoutGridBuilder(1, hide));
+        layout.addComponent(createGridWithoutGridBuilder(2, hide));
+        layout.addComponent(createGridWithoutGridBuilder(3, hide));
+    }
+
+    private GridLayout createGridWithoutGridBuilder(int rows, boolean collapse) {
+        GridLayout grid = new GridLayout(30, 3);
+        grid.setWidth("100%");
+        // grid.setMargin(true);
+        // grid.setSpacing(true);
+        grid.setHideEmptyRowsAndColumns(collapse);
+
+        // Row 1
+        Label label1 = new Label("Row1");
+        label1.setWidth(100.0F, Unit.PERCENTAGE);
+        grid.addComponent(label1, 0, 0, 10, 0);
+        grid.setComponentAlignment(label1, Alignment.MIDDLE_LEFT);
+
+        TextField textField1 = new TextField();
+        textField1.setWidth(100.0F, Unit.PERCENTAGE);
+        grid.addComponent(textField1, 12, 0, 14, 0);
+        grid.setComponentAlignment(textField1, Alignment.MIDDLE_LEFT);
+
+        if (rows < 2) {
+            return grid;
+        }
+
+        // Row 2
+        Label label2 = new Label("Row2");
+        label2.setWidth(100.0F, Unit.PERCENTAGE);
+        grid.addComponent(label2, 0, 1, 10, 1);
+        grid.setComponentAlignment(label2, Alignment.MIDDLE_LEFT);
+
+        TextField textField2 = new TextField();
+        textField2.setWidth(100.0F, Unit.PERCENTAGE);
+        grid.addComponent(textField2, 12, 1, 20, 1);
+        grid.setComponentAlignment(textField2, Alignment.MIDDLE_LEFT);
+
+        if (rows < 3) {
+            return grid;
+        }
+
+        // Row 3
+        Label label3 = new Label("Row3");
+        label3.setWidth(100.0F, Unit.PERCENTAGE);
+        grid.addComponent(label3, 0, 2, 10, 2);
+        grid.setComponentAlignment(label3, Alignment.MIDDLE_LEFT);
+
+        TextField textField3 = new TextField();
+        textField3.setWidth(100.0F, Unit.PERCENTAGE);
+        grid.addComponent(textField3, 12, 2, 29, 2);
+        grid.setComponentAlignment(textField3, Alignment.MIDDLE_LEFT);
+
+        return grid;
+    }
+
+}
\ No newline at end of file
diff --git a/uitest/src/com/vaadin/tests/components/gridlayout/UniformGridLayoutUITest.java b/uitest/src/com/vaadin/tests/components/gridlayout/UniformGridLayoutUITest.java
new file mode 100644 (file)
index 0000000..80f85b3
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.tests.components.gridlayout;
+
+import org.junit.Test;
+
+import com.vaadin.tests.tb3.MultiBrowserTest;
+
+public class UniformGridLayoutUITest extends MultiBrowserTest {
+
+    @Test
+    public void noncollapsed() throws Exception {
+        openTestURL();
+        compareScreen("noncollapsed");
+    }
+
+    @Test
+    public void collapsed() throws Exception {
+        openTestURL("collapse");
+        compareScreen("collapsed");
+    }
+}
index 96091bdab502cbbc07988b086593092833edf489..c07deccd68a2480fe136298c27540c0e7b5d1300 100644 (file)
@@ -2,7 +2,6 @@ package com.vaadin.tests.layouts.gridlayout;
 
 import com.vaadin.server.VaadinRequest;
 import com.vaadin.tests.components.AbstractTestUI;
-import com.vaadin.tests.components.TestBase;
 import com.vaadin.ui.Button;
 import com.vaadin.ui.Button.ClickEvent;
 import com.vaadin.ui.CustomComponent;
@@ -28,6 +27,7 @@ public class GridLayoutWidthChange extends AbstractTestUI {
         b.setHeight("200px");
 
         GridLayout layout = new GridLayout(3, 2);
+        layout.setHideEmptyRowsAndColumns(true);
         layout.setWidth("100%");
         layout.addComponent(fields1, 0, 0, 0, 1);
         layout.addComponent(b, 2, 1);
index d79c52ccdaf11700c9987e74ed3bd62be5be5322..c08e5286a670bbc7f8c6ffcfa3ef5c1facd88752 100644 (file)
@@ -38,6 +38,7 @@ public abstract class GridBaseLayoutTestUI extends BaseLayoutTestUI {
      */
     public GridBaseLayoutTestUI() {
         super(GridLayout.class);
+        layout.setHideEmptyRowsAndColumns(true);
     }
 
     @Override