From 6c378ca01dac9fdfe0ead72067463009efd597a4 Mon Sep 17 00:00:00 2001 From: Matti Tahvonen Date: Fri, 24 Oct 2008 05:44:44 +0000 Subject: [PATCH] expandRatio for gridlayout svn changeset:5699/svn branch:trunk --- src/com/itmill/toolkit/ui/GridLayout.java | 112 ++++++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/src/com/itmill/toolkit/ui/GridLayout.java b/src/com/itmill/toolkit/ui/GridLayout.java index 31f7428af1..2e019835b3 100644 --- a/src/com/itmill/toolkit/ui/GridLayout.java +++ b/src/com/itmill/toolkit/ui/GridLayout.java @@ -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 columnExpandRatio = new HashMap(); + private Map rowExpandRatio = new HashMap(); + /** * 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 ratioMap) { + float sum = 0; + for (Iterator> 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(); + } + } -- 2.39.5