changeset 1232:46dadf1d60fc

RT-20931: GridPaneDesignInfo.getCellBounds() is not consistent with GridPane.getLayoutBounds().
author Kinsley Wong
date Tue, 05 Jun 2012 14:44:47 -0700
parents aa28336a1e58
children b855ec5afa28
files javafx-ui-common/src/javafx/scene/layout/GridPane.java javafx-ui-common/test/unit/javafx/scene/layout/GridPaneTest.java
diffstat 2 files changed, 141 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/javafx-ui-common/src/javafx/scene/layout/GridPane.java	Tue Jun 05 10:42:59 2012 -0700
+++ b/javafx-ui-common/src/javafx/scene/layout/GridPane.java	Tue Jun 05 14:44:47 2012 -0700
@@ -1589,27 +1589,47 @@
         }
 
         double available = extraHeight; // will be negative in shrinking case
-        while (Math.abs(available) > 1.0 && adjusting.size() > 0) {
-            final double portion = available / adjusting.size(); // negative in shrinking case
-            for (int i = 0; i < adjusting.size(); i++) {
-                final int index = adjusting.get(i);
-                final double limit = (shrinking? rowMinHeight[index] : rowMaxHeight[index])
-                        - rowHeights[index]; // negative in shrinking case
-                final double change = Math.abs(limit) <= Math.abs(portion)? limit : portion;
-                //System.out.println("row "+index+": height="+rowHeights[index]+" extra="+extraHeight+"portion="+portion+" row mpm="+rowMinHeight[index]+"/"+rowPrefHeight[index]+"/"+rowMaxHeight[index]+" limit="+limit+" change="+change);
-                rowHeights[index] += change;
-                available -= change;
-                if (Math.abs(change) < Math.abs(portion)) {
-                    adjusted.add(index);
+        boolean handleRemainder = false;
+        int portion = 0;
+        while (available != 0 && adjusting.size() > 0) {
+            if (!handleRemainder) {
+                portion = (int)available / adjusting.size(); // negative in shrinking case
+            }
+            if (portion != 0) {
+                for (int i = 0; i < adjusting.size(); i++) {
+                    final int index = adjusting.get(i);
+                    final double limit = (shrinking? rowMinHeight[index] : rowMaxHeight[index])
+                            - rowHeights[index]; // negative in shrinking case
+                    final double change = Math.abs(limit) <= Math.abs(portion)? limit : portion;
+                    //System.out.println("row "+index+": height="+rowHeights[index]+" extra="+extraHeight+"portion="+portion+" row mpm="+rowMinHeight[index]+"/"+rowPrefHeight[index]+"/"+rowMaxHeight[index]+" limit="+limit+" change="+change);
+                    rowHeights[index] += change;                
+                    available -= change;
+                    if (Math.abs(change) < Math.abs(portion)) {
+                        adjusted.add(index);
+                    }
+                    if (available == 0) {
+                        break;
+                    }
+                }
+                for (int i = 0; i < adjusted.size(); i++) {
+                    adjusting.remove(adjusted.get(i));
+                }
+                adjusted.clear();
+            } else {
+                // Handle the remainder
+                portion = (int)(available) % adjusting.size();
+                if (portion == 0) {
+                    break;
+                } else {
+                    // We have a remainder evenly distribute it.
+                    portion = shrinking ? -1 : 1;
+                    handleRemainder = true;
                 }
             }
-            for (int i = 0; i < adjusted.size(); i++) {
-                adjusting.remove(adjusted.get(i));
-            }
-            adjusted.clear();
         }
+                        
         for (int i = 0; i < rowHeights.length; i++) {
-            rowHeights[i] = snapSpace(rowHeights[i]);
+            rowHeights[i] = snapSpace(rowHeights[i]);            
         }
         return available; // might be negative in shrinking case
     }
@@ -1662,26 +1682,47 @@
                 adjusting.add(i);
             }
         }
+        
         double available = extraWidth; // will be negative in shrinking case
-        while (Math.abs(available) > 1.0 && adjusting.size() > 0) {            
-            final double portion = available / adjusting.size(); // negative in shrinking case
-            for (int i = 0; i < adjusting.size(); i++) {
-                final int index = adjusting.get(i);
-                final double limit = (shrinking? columnMinWidth[index] : columnMaxWidth[index])
-                        - columnWidths[index]; // negative in shrinking case
-                final double change = Math.abs(limit) <= Math.abs(portion)? limit : portion;
-                columnWidths[index] += change;
-                //if (node.id.startsWith("debug.")) println("{if (shrinking) "vshrink" else "vgrow"}: {node.id} portion({portion})=available({available})/({sizeof adjusting}) change={change}");
-                available -= change;
-                if (Math.abs(change) < Math.abs(portion)) {
-                    adjusted.add(index);
+        boolean handleRemainder = false;
+        int portion = 0;
+        while (available != 0 && adjusting.size() > 0) {            
+            if (!handleRemainder) {
+                portion = (int)available / adjusting.size(); // negative in shrinking case
+            }
+            if (portion != 0) {
+                for (int i = 0; i < adjusting.size(); i++) {
+                    final int index = adjusting.get(i);
+                    final double limit = (shrinking? columnMinWidth[index] : columnMaxWidth[index])
+                            - columnWidths[index]; // negative in shrinking case
+                    final double change = Math.abs(limit) <= Math.abs(portion)? limit : portion;
+                    columnWidths[index] += change;                
+                    //if (node.id.startsWith("debug.")) println("{if (shrinking) "vshrink" else "vgrow"}: {node.id} portion({portion})=available({available})/({sizeof adjusting}) change={change}");
+                    available -= change;
+                    if (Math.abs(change) < Math.abs(portion)) {
+                        adjusted.add(index);
+                    }
+                    if (available == 0) {                        
+                        break;
+                    }                    
+                }
+                for (int i = 0; i < adjusted.size(); i++) {
+                    adjusting.remove(adjusted.get(i));
+                }
+                adjusted.clear();
+            } else {
+                // Handle the remainder
+                portion = (int)(available) % adjusting.size();
+                if (portion == 0) {
+                    break;
+                } else {
+                    // We have a remainder evenly distribute it.
+                    portion = shrinking ? -1 : 1;
+                    handleRemainder = true;
                 }
             }
-            for (int i = 0; i < adjusted.size(); i++) {
-                adjusting.remove(adjusted.get(i));
-            }
-            adjusted.clear();
         }
+               
         for (int i = 0; i < columnWidths.length; i++) {
             columnWidths[i] = snapSpace(columnWidths[i]);
         }
--- a/javafx-ui-common/test/unit/javafx/scene/layout/GridPaneTest.java	Tue Jun 05 10:42:59 2012 -0700
+++ b/javafx-ui-common/test/unit/javafx/scene/layout/GridPaneTest.java	Tue Jun 05 14:44:47 2012 -0700
@@ -1782,7 +1782,7 @@
         assertEquals(100, child1_0.getLayoutBounds().getHeight(), 1e-100);
         assertEquals(366, child2_0.getLayoutX(), 1e-100);
         assertEquals(0, child2_0.getLayoutY(), 1e-100);
-        assertEquals(233, child2_0.getLayoutBounds().getWidth(), 1e-100);
+        assertEquals(234, child2_0.getLayoutBounds().getWidth(), 1e-100);
         assertEquals(129, child2_0.getLayoutBounds().getHeight(), 1e-100);
 
         assertEquals(0, child0_1.getLayoutX(), 1e-100);
@@ -1795,7 +1795,7 @@
         assertEquals(300, child1_1.getLayoutBounds().getHeight(), 1e-100);
         assertEquals(366, child2_1.getLayoutX(), 1e-100);
         assertEquals(129, child2_1.getLayoutY(), 1e-100);
-        assertEquals(233, child2_1.getLayoutBounds().getWidth(), 1e-100);
+        assertEquals(234, child2_1.getLayoutBounds().getWidth(), 1e-100);
         assertEquals(300, child2_1.getLayoutBounds().getHeight(), 1e-100);
     }
 
@@ -2239,4 +2239,69 @@
         assertEquals(100, cw[0], 1e-100);
         assertEquals(50, cw[1], 1e-100);
     }
+    
+    @Test public void test_RT20931_cellHeightTotalShouldEqualColumnHeight() {
+        ColumnConstraints c1 = new ColumnConstraints();
+        c1.setMinWidth(10);
+        c1.setHgrow(Priority.SOMETIMES);
+        
+        ColumnConstraints c2 = new ColumnConstraints();
+        c2.setMinWidth(10);
+        c2.setHgrow(Priority.SOMETIMES);
+                
+        RowConstraints r1 = new RowConstraints();
+        r1.setMinHeight(10);
+        r1.setVgrow(Priority.SOMETIMES);
+        
+        RowConstraints r2 = new RowConstraints();
+        r2.setMinHeight(10);
+        r2.setVgrow(Priority.SOMETIMES);
+        
+        RowConstraints r3 = new RowConstraints();
+        r3.setMinHeight(10);
+        r3.setVgrow(Priority.SOMETIMES);        
+        
+        gridpane.getColumnConstraints().addAll(c1, c2);
+        gridpane.getRowConstraints().addAll(r1, r2, r3);
+        
+        gridpane.resize(150, 73);
+        gridpane.layout();        
+        double[] rh = gridpane.getRowHeights();
+
+        assertEquals(25, rh[0], 1e-100);
+        assertEquals(24, rh[1], 1e-100);
+        assertEquals(24, rh[2], 1e-100);
+    }
+    
+    @Test public void test_RT20931_cellWidthTotalShouldEqualRowWidth() {
+        ColumnConstraints c1 = new ColumnConstraints();
+        c1.setMinWidth(10);
+        c1.setHgrow(Priority.SOMETIMES);
+        
+        ColumnConstraints c2 = new ColumnConstraints();
+        c2.setMinWidth(10);
+        c2.setHgrow(Priority.SOMETIMES);
+                
+        RowConstraints r1 = new RowConstraints();
+        r1.setMinHeight(10);
+        r1.setVgrow(Priority.SOMETIMES);
+        
+        RowConstraints r2 = new RowConstraints();
+        r2.setMinHeight(10);
+        r2.setVgrow(Priority.SOMETIMES);
+        
+        RowConstraints r3 = new RowConstraints();
+        r3.setMinHeight(10);
+        r3.setVgrow(Priority.SOMETIMES);        
+        
+        gridpane.getColumnConstraints().addAll(c1, c2);
+        gridpane.getRowConstraints().addAll(r1, r2, r3);
+        
+        gridpane.resize(151, 73);
+        gridpane.layout();        
+        double[] cw = gridpane.getColumnWidths();
+
+        assertEquals(76, cw[0], 1e-100);
+        assertEquals(75, cw[1], 1e-100);
+    }    
 }