]> source.dussan.org Git - vaadin-framework.git/commitdiff
expandRatio for gridlayout
authorMatti Tahvonen <matti.tahvonen@itmill.com>
Fri, 24 Oct 2008 05:44:44 +0000 (05:44 +0000)
committerMatti Tahvonen <matti.tahvonen@itmill.com>
Fri, 24 Oct 2008 05:44:44 +0000 (05:44 +0000)
svn changeset:5699/svn branch:trunk

src/com/itmill/toolkit/ui/GridLayout.java

index 31f7428af13f8dc64da408b4835703115fc67067..2e019835b3115db16aaf5377633fe21608e4ca41 100644 (file)
@@ -9,6 +9,7 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.Map;
+import java.util.Map.Entry;
 
 import com.itmill.toolkit.terminal.PaintException;
 import com.itmill.toolkit.terminal.PaintTarget;
@@ -87,6 +88,9 @@ public class GridLayout extends AbstractLayout implements
      */
     private boolean structuralChange = false;
 
+    private Map<Integer, Float> columnExpandRatio = new HashMap<Integer, Float>();
+    private Map<Integer, Float> rowExpandRatio = new HashMap<Integer, Float>();
+
     /**
      * Constructor for grid of given size (number of cells). Note that grid's
      * final size depends on the items that are added into the grid. Grid grows
@@ -402,12 +406,58 @@ public class GridLayout extends AbstractLayout implements
         int emptyCells = 0;
 
         final String[] alignmentsArray = new String[components.size()];
+        final Integer[] columnExpandRatioArray = new Integer[cols];
+        final Integer[] rowExpandRatioArray = new Integer[rows];
+
+        boolean equallyDividedCols = false;
+        int realColExpandRatioSum = 0;
+        float colSum = getExpandRatioSum(columnExpandRatio);
+        if (colSum == 0 && components.size() > 0) {
+            // no columns has been expanded, all cols have same expand
+            // rate
+            equallyDividedCols = true;
+            float equalSize = 1 / (float) cols;
+            int myRatio = Math.round(equalSize * 1000);
+            for (int i = 0; i < cols; i++) {
+                columnExpandRatioArray[i] = myRatio;
+            }
+            realColExpandRatioSum = myRatio * cols;
+        } else {
+            for (int i = 0; i < cols; i++) {
+                int myRatio = Math
+                        .round((getColumnExpandRatio(i) / colSum) * 1000);
+                columnExpandRatioArray[i] = myRatio;
+                realColExpandRatioSum += myRatio;
+            }
+        }
+
+        boolean equallyDividedRows = false;
+        int realRowExpandRatioSum = 0;
+        float rowSum = getExpandRatioSum(rowExpandRatio);
+        if (rowSum == 0 && components.size() > 0) {
+            // no rows have been expanded
+            equallyDividedRows = true;
+            float equalSize = 1 / (float) rows;
+            int myRatio = Math.round(equalSize * 1000);
+            for (int i = 0; i < rows; i++) {
+                rowExpandRatioArray[i] = myRatio;
+            }
+            realRowExpandRatioSum = myRatio * rows;
+        }
+
         int index = 0;
 
         // Iterates every applicable row
         for (int cury = 0; cury < rows; cury++) {
             target.startTag("gr");
 
+            if (!equallyDividedRows) {
+                int myRatio = Math
+                        .round((getRowExpandRatio(cury) / rowSum) * 1000);
+                rowExpandRatioArray[index] = myRatio;
+                realRowExpandRatioSum += myRatio;
+
+            }
             // Iterates every applicable column
             for (int curx = 0; curx < cols; curx++) {
 
@@ -533,11 +583,31 @@ public class GridLayout extends AbstractLayout implements
 
         // Last row handled
 
+        // correct possible rounding error
+        if (rowExpandRatioArray.length > 0) {
+            rowExpandRatioArray[0] -= realRowExpandRatioSum - 1000;
+        }
+        if (columnExpandRatioArray.length > 0) {
+            columnExpandRatioArray[0] -= realColExpandRatioSum - 1000;
+        }
+
+        target.addAttribute("colExpand", columnExpandRatioArray);
+        target.addAttribute("rowExpand", rowExpandRatioArray);
+
         // Add child component alignment info to layout tag
         target.addAttribute("alignments", alignmentsArray);
 
     }
 
+    private float getExpandRatioSum(Map<Integer, Float> ratioMap) {
+        float sum = 0;
+        for (Iterator<Entry<Integer, Float>> iterator = ratioMap.entrySet()
+                .iterator(); iterator.hasNext();) {
+            sum += iterator.next().getValue();
+        }
+        return sum;
+    }
+
     /*
      * (non-Javadoc)
      * 
@@ -1085,4 +1155,46 @@ public class GridLayout extends AbstractLayout implements
 
     }
 
+    /**
+     * TODO
+     * 
+     * @param columnIndex
+     * @param ratio
+     */
+    public void setColumnExpandRatio(int columnIndex, float ratio) {
+        columnExpandRatio.put(columnIndex, ratio);
+    }
+
+    /**
+     * TODO
+     * 
+     * @param columnIndex
+     * @return
+     */
+    public float getColumnExpandRatio(int columnIndex) {
+        Float r = columnExpandRatio.get(columnIndex);
+        return r == null ? 0 : r.floatValue();
+    }
+
+    /**
+     * TODO
+     * 
+     * @param rowIndex
+     * @param ratio
+     */
+    public void setRowExpandRatio(int rowIndex, float ratio) {
+        rowExpandRatio.put(rowIndex, ratio);
+    }
+
+    /**
+     * TODO
+     * 
+     * @param rowIndex
+     * @return
+     */
+    public float getRowExpandRatio(int rowIndex) {
+        Float r = rowExpandRatio.get(rowIndex);
+        return r == null ? 0 : r.floatValue();
+    }
+
 }