You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lo...@apache.org on 2009/07/21 15:44:17 UTC

svn commit: r796295 - in /myfaces/tobago/trunk: core/src/main/java/org/apache/myfaces/tobago/component/ core/src/main/java/org/apache/myfaces/tobago/layout/ core/src/main/java/org/apache/myfaces/tobago/layout/math/ core/src/test/java/org/apache/myfaces...

Author: lofwyr
Date: Tue Jul 21 13:44:17 2009
New Revision: 796295

URL: http://svn.apache.org/viewvc?rev=796295&view=rev
Log:
TOBAGO-606: Layout-Manager
 - Add handling of sizes that cannot be exactly fulfill the constraints. E.g. split 100px in 3 pices with the same size -> 33.33333333333 is not possible for pixels.
 - split PartitionEquation into 2 classes (PartitionEquation and CombinationEquation). 
      The PartitionEquation handles the sub-layouting, e.g. when putting a layout into a cell. 
      The CombinationEquation handles spans over e.g. 3 cell building a new logical cell.
 - using Measures in the *Equation-classes.
 - fixing spacing errors

Added:
    myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/CombinationEquation.java
      - copied, changed from r795014, myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/PartitionEquation.java
    myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/MathUtils.java
    myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/layout/math/CombinationEquationUnitTest.java
    myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/layout/math/MathUtilsUnitTest.java
    myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/layout/math/PartitionEquationUnitTest.java
    myfaces/tobago/trunk/example/test/src/main/webapp/tc/gridLayout/size-not-exact-4x4.xhtml
      - copied, changed from r795009, myfaces/tobago/trunk/example/test/src/main/webapp/tc/gridLayout/position-4x4-span-steps.xhtml
    myfaces/tobago/trunk/example/test/src/main/webapp/tc/gridLayout/spacing-nested.xhtml
      - copied, changed from r795009, myfaces/tobago/trunk/example/test/src/main/webapp/tc/gridLayout/spacing.xhtml
    myfaces/tobago/trunk/example/test/src/main/webapp/tc/gridLayout/spacing-simple.xhtml
Removed:
    myfaces/tobago/trunk/example/test/src/main/webapp/tc/gridLayout/spacing.xhtml
Modified:
    myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/component/AbstractUIGridLayout.java
    myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/LayoutContext.java
    myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/PixelMeasure.java
    myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/EquationManager.java
    myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/FixedEquation.java
    myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/PartitionEquation.java
    myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/SystemOfEquations.java
    myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/component/GridLayoutManagerUnitTest.java
    myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/layout/math/EquationManagerUnitTest.java
    myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/layout/math/SystemOfEquationsUnitTest.java

Modified: myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/component/AbstractUIGridLayout.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/component/AbstractUIGridLayout.java?rev=796295&r1=796294&r2=796295&view=diff
==============================================================================
--- myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/component/AbstractUIGridLayout.java (original)
+++ myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/component/AbstractUIGridLayout.java Tue Jul 21 13:44:17 2009
@@ -74,11 +74,11 @@
 
     // horizontal
     EquationManager horizontal = layoutContext.getHorizontal();
-    int[] horizontalIndices = horizontal.divide(horizontalIndex, columnTokens.getSize(), getColumnSpacing().getPixel());
+    int[] horizontalIndices = horizontal.divide(horizontalIndex, columnTokens.getSize(), getColumnSpacing());
 
     // vertical
     EquationManager vertical = layoutContext.getVertical();
-    int[] verticalIndices = vertical.divide(verticalIndex, rowTokens.getSize(), getRowSpacing().getPixel());
+    int[] verticalIndices = vertical.divide(verticalIndex, rowTokens.getSize(), getRowSpacing());
 
     List<LayoutComponent> components = container.getComponents();
     for (LayoutComponent c : components) {
@@ -98,12 +98,12 @@
 
           // horizontal
           int hIndex
-              = horizontal.addComponent(horizontalIndices[i], cell.getColumnSpan(), getColumnSpacing().getPixel());
+              = horizontal.addComponent(horizontalIndices[i], cell.getColumnSpan(), getColumnSpacing());
           cell.getComponent().setHorizontalIndex(hIndex);
 
           // vertical
           int vIndex
-              = vertical.addComponent(verticalIndices[j], cell.getRowSpan(), getRowSpacing().getPixel());
+              = vertical.addComponent(verticalIndices[j], cell.getRowSpan(), getRowSpacing());
           cell.getComponent().setVerticalIndex(vIndex);
 
           if (component instanceof LayoutContainer) {
@@ -139,8 +139,8 @@
           int horizontalIndex = cell.getComponent().getHorizontalIndex();
           int verticalIndex = cell.getComponent().getVerticalIndex();
 
-          PixelMeasure width = new PixelMeasure(horizontal.getResult()[horizontalIndex]);
-          PixelMeasure height = new PixelMeasure(vertical.getResult()[verticalIndex]);
+          PixelMeasure width = (PixelMeasure) horizontal.getResult()[horizontalIndex];
+          PixelMeasure height = (PixelMeasure) vertical.getResult()[verticalIndex];
 
           component.setWidth(width);
           component.setHeight(height);
@@ -208,11 +208,11 @@
     for (int i = 0; i < columnTokens.getSize(); i++) {
       LayoutToken layoutToken = columnTokens.get(i);
       if (layoutToken instanceof PixelLayoutToken) {
-        int pixel = ((PixelLayoutToken) layoutToken).getPixel();
+        Measure pixel = new PixelMeasure(((PixelLayoutToken) layoutToken).getPixel());
         layoutContext.getHorizontal().setFixedLength(i + horizontalIndexOffset, pixel);
       }
       if (layoutToken instanceof FixedLayoutToken) {
-        int pixel = 100;
+        Measure pixel = new PixelMeasure(100);
         LOG.warn("auto/fixed is not implemented yet and was set to 100px");
         layoutContext.getHorizontal().setFixedLength(i + horizontalIndexOffset, pixel);
       }
@@ -221,11 +221,12 @@
     for (int i = 0; i < rowTokens.getSize(); i++) {
       LayoutToken layoutToken = rowTokens.get(i);
       if (layoutToken instanceof PixelLayoutToken) {
-        int pixel = ((PixelLayoutToken) layoutToken).getPixel();
+         // XXX PixelLayoutToken might be removed/changed
+        Measure pixel = new PixelMeasure(((PixelLayoutToken) layoutToken).getPixel());
         layoutContext.getVertical().setFixedLength(i + verticalIndexOffset, pixel);
       }
       if (layoutToken instanceof FixedLayoutToken) {
-        int pixel = 25;
+        Measure pixel = new PixelMeasure(25);
         LOG.warn("auto/fixed is not implemented yet and was set to 25px");
         layoutContext.getVertical().setFixedLength(i + verticalIndexOffset, pixel);
       }

Modified: myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/LayoutContext.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/LayoutContext.java?rev=796295&r1=796294&r2=796295&view=diff
==============================================================================
--- myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/LayoutContext.java (original)
+++ myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/LayoutContext.java Tue Jul 21 13:44:17 2009
@@ -45,10 +45,10 @@
     vertical.addComponentRoot();
 
     if (container.getWidth() != null) {
-      horizontal.setFixedLength(0, container.getWidth().getPixel());
+      horizontal.setFixedLength(0, container.getWidth());
     }
     if (container.getHeight() != null) {
-      vertical.setFixedLength(0, container.getHeight().getPixel());
+      vertical.setFixedLength(0, container.getHeight());
     }
 
     container.getLayoutManager().collect(this, container, 0, 0);

Modified: myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/PixelMeasure.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/PixelMeasure.java?rev=796295&r1=796294&r2=796295&view=diff
==============================================================================
--- myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/PixelMeasure.java (original)
+++ myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/PixelMeasure.java Tue Jul 21 13:44:17 2009
@@ -29,16 +29,14 @@
 
   private static final PixelMeasure NULL = new PixelMeasure(0);
 
+  public static final Measure ZERO = new PixelMeasure(0);
+
   private final int pixel;
 
   public PixelMeasure(int pixel) {
     this.pixel = pixel;
   }
 
-  public PixelMeasure(double value) {
-    this((int) (value + 0.5));
-  }
-
   public Measure add(Measure m) {
     return new PixelMeasure(pixel + m.getPixel());
   }
@@ -61,4 +59,26 @@
     return pixel + "px";
   }
 
+  @Override
+  public boolean equals(Object o) {
+    if (this == o) {
+      return true;
+    }
+    if (o == null || getClass() != o.getClass()) {
+      return false;
+    }
+
+    PixelMeasure that = (PixelMeasure) o;
+
+    if (pixel != that.pixel) {
+      return false;
+    }
+
+    return true;
+  }
+
+  @Override
+  public int hashCode() {
+    return pixel;
+  }
 }

Copied: myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/CombinationEquation.java (from r795014, myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/PartitionEquation.java)
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/CombinationEquation.java?p2=myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/CombinationEquation.java&p1=myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/PartitionEquation.java&r1=795014&r2=796295&rev=796295&view=diff
==============================================================================
--- myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/PartitionEquation.java (original)
+++ myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/CombinationEquation.java Tue Jul 21 13:44:17 2009
@@ -17,64 +17,75 @@
  * limitations under the License.
  */
 
+import org.apache.myfaces.tobago.layout.Measure;
+
 /**
  * This equation describes the partition of one column (or row) into some other columns (or rows).
  * E. g.: column2 = column4 + column5 + column6 -> 0 0 -1 0 1 1 1 0 ...
  * In this example are: begin=4, end=6, parent=2, span=1
  * Because of the algorithm we have indices without gap.
  */
-public final class PartitionEquation implements Equation {
+public final class CombinationEquation implements Equation {
 
-  private int begin;
-  private int end;
+  private int newIndex;
   private int parent;
   private int span;
-  private double innerSpacing;
-  private double outerSpacing;
+  private Measure spacing;
 
   /**
-   * @param begin        lowest index
-   * @param end          one more than the largest index
-   * @param parent       parent index
-   * @param span         number of parent cells
-   * @param innerSpacing space between two cells between begin and end
-   * @param outerSpacing space between two cells inside the span
+   * @param newIndex new index
+   * @param parent   parent index
+   * @param span     number of parent cells
+   * @param spacing  space between two cells inside the span
    */
-  public PartitionEquation(int begin, int end, int parent, int span, double innerSpacing, double outerSpacing) {
-    this.begin = begin;
-    this.end = end;
+  public CombinationEquation(int newIndex, int parent, int span, Measure spacing) {
+    this.newIndex = newIndex;
     this.parent = parent;
     this.span = span;
-    this.innerSpacing = innerSpacing;
-    this.outerSpacing = outerSpacing;
+    this.spacing = spacing;
   }
 
   public void fillRow(double[] row) {
-    assert begin >= 0 && end > 0 && parent >= 0 && span > 0;
-    assert begin < end;
-    assert parent + span <= begin || parent >= end;
+    assert newIndex >= 0 && parent >= 0 && span > 0;
+    assert parent + span <= newIndex || parent > newIndex;
 
     int i = 0;
-    for (; i < begin; i++) {
+    for (; i < newIndex; i++) {
       row[i] = 0.0;
     }
-    for (; i < end; i++) {
+    for (; i < newIndex + 1; i++) {
       row[i] = 1.0;
     }
     for (; i < row.length - 1; i++) {
       row[i] = 0.0;
     }
     // the last variable contains a constant, this is here the sum of spaces between cells.
-    row[row.length - 1] =  (span - 1) * outerSpacing - (end - begin - 1) * innerSpacing;
+    row[row.length - 1] =  (span - 1) * spacing.getPixel();
 
     for (i = parent; i < parent + span; i++) {
       row[i] = -1.0;
     }
   }
 
+  public int getNewIndex() {
+    return newIndex;
+  }
+
+  public int getParent() {
+    return parent;
+  }
+
+  public int getSpan() {
+    return span;
+  }
+
+  public Measure getSpacing() {
+    return spacing;
+  }
+
   @Override
   public String toString() {
-    StringBuilder builder = new StringBuilder("PartitionEquation: ");
+    StringBuilder builder = new StringBuilder("CombinationEquation: ");
     // cells from parent
     builder.append(" x_");
     builder.append(parent);
@@ -86,34 +97,20 @@
       builder.append(parent + span - 1);
     }
     // plus spacing
-    if (span >= 2) {
-      builder.append(" + ");
+    if (spacing.getPixel() > 0) {
       if (span >= 2) {
-        builder.append(span - 1);
-        builder.append(" * ");
+        builder.append(" + ");
+        if (span > 2) {
+          builder.append(span - 1);
+          builder.append(" * ");
+        }
+        builder.append(spacing);
       }
-      builder.append(outerSpacing);
     }
     // sub cells
     builder.append(" = ");
     builder.append("x_");
-    builder.append(begin);
-    if (end - begin > 2) {
-      builder.append(" + ...");
-    }
-    if (end - begin >= 2) {
-      builder.append(" + x_");
-      builder.append(end - 1);
-    }
-    // plus spacing
-    if (end - begin >= 2) {
-      builder.append(" + ");
-      if (end - begin > 2) {
-        builder.append(end - begin - 1);
-        builder.append(" * ");
-      }
-      builder.append(innerSpacing);
-    }
+    builder.append(newIndex);
 
     return builder.toString();
   }

Modified: myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/EquationManager.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/EquationManager.java?rev=796295&r1=796294&r2=796295&view=diff
==============================================================================
--- myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/EquationManager.java (original)
+++ myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/EquationManager.java Tue Jul 21 13:44:17 2009
@@ -19,6 +19,7 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.myfaces.tobago.layout.Measure;
 
 /**
  * Manages the relation between the Tree of LayoutManagers and the Linear System of Equations
@@ -28,44 +29,38 @@
   private static final Log LOG = LogFactory.getLog(EquationManager.class);
 
   private SystemOfEquations equations;
-  private double[] result;
+  private Measure[] result;
 
   public int addComponentRoot() {
     equations = new SystemOfEquations(1);
     return 0;
   }
 
-  public void setFixedLength(int index, int length) {
+  public void setFixedLength(int index, Measure length) {
     equations.addEqualsEquation(new FixedEquation(index, length));
     LOG.info(equations);
   }
 
-  public int[] divide(int index, int number, int spacing) {
-    int[] newIndices = addSubTree(index, number, 1, spacing, 0);
-    LOG.info(equations);
-    return newIndices;
-  }
+  public int[] divide(int index, int number, Measure spacing) {
+
+    assert number > 0;
 
-  public int addComponent(int index, int span, int spacing) {
-    int[] newIndices = addSubTree(index, 1, span, 0, spacing);
+    int[] newIndices = equations.addVariables(number);
+    equations.addEqualsEquation(new PartitionEquation(
+        newIndices[0], number, index, spacing));
     LOG.info(equations);
-    return newIndices[0];
+    return newIndices;
   }
 
-  private int[] addSubTree(int index, int number, int span, double innerSpacing, double outerSpacing) {
+  public int addComponent(int index, int span, Measure spacing) {
 
-    assert number > 0;
     assert span > 0;
 
-    int[] newIndices = equations.addVariables(number);
-    equations.addEqualsEquation(new PartitionEquation(
-        newIndices[0],
-        newIndices[newIndices.length - 1] + 1,
-        index,
-        span,
-        innerSpacing,
-        outerSpacing));
-    return newIndices;
+    int[] newIndices = equations.addVariables(1);
+    equations.addEqualsEquation(new CombinationEquation(
+        newIndices[0], index, span, spacing));
+    LOG.info(equations);
+    return newIndices[0];
   }
 
   public void setProportion(int index1, int index2, int factor1, int factor2) {
@@ -77,20 +72,13 @@
 
     LOG.info("solve:\n" + equations);
 
-    equations.prepare();
-    equations.gauss();
-    equations.reduce();
-    result = equations.result();
+    result = equations.solve();
   }
 
-  public double[] getResult() {
+  public Measure[] getResult() {
     return result;
   }
 
-  public int getNumberOfVariable() {
-    return equations.getNumberOfVariables();
-  }
-
   @Override
   public String toString() {
     return "EquationManager: " + equations.toString();

Modified: myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/FixedEquation.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/FixedEquation.java?rev=796295&r1=796294&r2=796295&view=diff
==============================================================================
--- myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/FixedEquation.java (original)
+++ myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/FixedEquation.java Tue Jul 21 13:44:17 2009
@@ -17,14 +17,16 @@
  * limitations under the License.
  */
 
+import org.apache.myfaces.tobago.layout.Measure;
+
 import java.util.Arrays;
 
 public final class FixedEquation implements Equation {
 
   private int index;
-  private double result;
+  private Measure result;
 
-  public FixedEquation(int index, double result) {
+  public FixedEquation(int index, Measure result) {
     this.index = index;
     this.result = result;
   }
@@ -32,7 +34,7 @@
   public void fillRow(double[] row) {
     Arrays.fill(row, 0.0);
     row[index] = 1.0;
-    row[row.length - 1] = result;
+    row[row.length - 1] = result.getPixel();
   }
 
   @Override

Added: myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/MathUtils.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/MathUtils.java?rev=796295&view=auto
==============================================================================
--- myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/MathUtils.java (added)
+++ myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/MathUtils.java Tue Jul 21 13:44:17 2009
@@ -0,0 +1,107 @@
+package org.apache.myfaces.tobago.layout.math;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+public class MathUtils {
+
+  /**
+   * Values smaller than this EPSILON should be treated as zero.
+   */
+  public static final double EPSILON = 0.0000001;
+
+  /**
+   * Adjusts the list of double values to rounded values with the same sum.
+   * E. g. 2.3, 2.4, 2.5, 2.8 -> 2.0, 2.0, 3.0, 3.0
+   *
+   * @param list
+   */
+  static void adjustRemainders(double[] list) {
+    double bias = 0.0;
+    for (double v : list) {
+      double lastBias;
+      if (bias >= 0.0) {
+        lastBias = findAndAdjustMaxRemainder(list);
+      } else {
+        lastBias = findAndAdjustMinRemainder(list);
+      }
+      if (isZero(lastBias)) {
+        break;
+      }
+      bias += lastBias;
+    }
+
+    assert isZero(bias);
+  }
+
+  static double findAndAdjustMaxRemainder(double[] list) {
+    double max = 0.0;
+    Integer indexOfMax = null;
+    for (int i = 0; i < list.length; i++) {
+      double remainder = remainder(list[i]);
+      if (remainder > max + EPSILON) {
+        max = remainder;
+        indexOfMax = i;
+      }
+    }
+    if (indexOfMax != null) {
+      list[indexOfMax] += 1.0 - max;
+      return max - 1.0;
+    }
+    return 0.0;
+  }
+
+  static double findAndAdjustMinRemainder(double[] list) {
+    double min = 1.0;
+    Integer indexOfMin = null;
+    for (int i = 0; i < list.length; i++) {
+      double remainder = remainder(list[i]);
+      if (remainder == 0) { // is zero
+        continue;
+      }
+      if (remainder < min - EPSILON) {
+        min = remainder;
+        indexOfMin = i;
+      }
+    }
+    if (indexOfMin != null) {
+      list[indexOfMin] -= min;
+      return min;
+    }
+    return 0.0;
+  }
+
+  static double remainder(double v) {
+    return v - Math.floor(v);
+  }
+
+  static boolean isZero(double factor) {
+    return Math.abs(factor) < EPSILON;
+  }
+
+  static boolean isNotZero(double factor) {
+    return Math.abs(factor) >= EPSILON;
+  }
+
+  static boolean isInteger(double value) {
+    return isZero(value - Math.round(value));
+  }
+
+  static boolean isNotInteger(double value) {
+    return isNotZero(value - Math.round(value));
+  }
+}

Modified: myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/PartitionEquation.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/PartitionEquation.java?rev=796295&r1=796294&r2=796295&view=diff
==============================================================================
--- myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/PartitionEquation.java (original)
+++ myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/PartitionEquation.java Tue Jul 21 13:44:17 2009
@@ -17,6 +17,8 @@
  * limitations under the License.
  */
 
+import org.apache.myfaces.tobago.layout.Measure;
+
 /**
  * This equation describes the partition of one column (or row) into some other columns (or rows).
  * E. g.: column2 = column4 + column5 + column6 -> 0 0 -1 0 1 1 1 0 ...
@@ -26,93 +28,80 @@
 public final class PartitionEquation implements Equation {
 
   private int begin;
-  private int end;
+  private int count;
   private int parent;
-  private int span;
-  private double innerSpacing;
-  private double outerSpacing;
+  private Measure spacing;
 
   /**
-   * @param begin        lowest index
-   * @param end          one more than the largest index
-   * @param parent       parent index
-   * @param span         number of parent cells
-   * @param innerSpacing space between two cells between begin and end
-   * @param outerSpacing space between two cells inside the span
+   * @param begin    lowest index
+   * @param count    number of cells of the partition
+   * @param parent   parent index
+   * @param spacing  space between two cells of the partition
    */
-  public PartitionEquation(int begin, int end, int parent, int span, double innerSpacing, double outerSpacing) {
+  public PartitionEquation(int begin, int count, int parent, Measure spacing) {
     this.begin = begin;
-    this.end = end;
+    this.count = count;
     this.parent = parent;
-    this.span = span;
-    this.innerSpacing = innerSpacing;
-    this.outerSpacing = outerSpacing;
+    this.spacing = spacing;
+
+    assert begin >= 0 && count > 0 && parent >= 0;
+    assert parent <= begin;
   }
 
   public void fillRow(double[] row) {
-    assert begin >= 0 && end > 0 && parent >= 0 && span > 0;
-    assert begin < end;
-    assert parent + span <= begin || parent >= end;
-
     int i = 0;
     for (; i < begin; i++) {
       row[i] = 0.0;
     }
-    for (; i < end; i++) {
+    for (; i < begin + count; i++) {
       row[i] = 1.0;
     }
     for (; i < row.length - 1; i++) {
       row[i] = 0.0;
     }
     // the last variable contains a constant, this is here the sum of spaces between cells.
-    row[row.length - 1] =  (span - 1) * outerSpacing - (end - begin - 1) * innerSpacing;
+    row[row.length - 1] = -(count - 1) * spacing.getPixel();
 
-    for (i = parent; i < parent + span; i++) {
+    for (i = parent; i < parent + 1; i++) {
       row[i] = -1.0;
     }
   }
 
+  public int getBegin() {
+    return begin;
+  }
+
+  public int getCount() {
+    return count;
+  }
+
   @Override
   public String toString() {
     StringBuilder builder = new StringBuilder("PartitionEquation: ");
     // cells from parent
     builder.append(" x_");
     builder.append(parent);
-    if (span > 2) {
-      builder.append(" + ...");
-    }
-    if (span >= 2) {
-      builder.append(" + x_");
-      builder.append(parent + span - 1);
-    }
-    // plus spacing
-    if (span >= 2) {
-      builder.append(" + ");
-      if (span >= 2) {
-        builder.append(span - 1);
-        builder.append(" * ");
-      }
-      builder.append(outerSpacing);
-    }
     // sub cells
     builder.append(" = ");
     builder.append("x_");
     builder.append(begin);
-    if (end - begin > 2) {
+    if (count > 2) {
       builder.append(" + ...");
     }
-    if (end - begin >= 2) {
+    if (count >= 2) {
       builder.append(" + x_");
-      builder.append(end - 1);
+      builder.append(begin + count - 1);
     }
     // plus spacing
-    if (end - begin >= 2) {
-      builder.append(" + ");
-      if (end - begin > 2) {
-        builder.append(end - begin - 1);
-        builder.append(" * ");
+    if (spacing.getPixel() > 0) {
+      if (count >= 2) {
+        builder.append(" + ");
+        if (count > 2) {
+          builder.append(count - 1);
+          builder.append(" * ");
+        }
+        builder.append(spacing);
       }
-      builder.append(innerSpacing);
     }
 
     return builder.toString();

Modified: myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/SystemOfEquations.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/SystemOfEquations.java?rev=796295&r1=796294&r2=796295&view=diff
==============================================================================
--- myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/SystemOfEquations.java (original)
+++ myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/layout/math/SystemOfEquations.java Tue Jul 21 13:44:17 2009
@@ -19,6 +19,8 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.myfaces.tobago.layout.Measure;
+import org.apache.myfaces.tobago.layout.PixelMeasure;
 
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -83,11 +85,6 @@
 
   private static final Log LOG = LogFactory.getLog(SystemOfEquations.class);
 
-  /**
-   * Values smaller than this EPSILON should be treated as zero.
-   */
-  public static final double EPSILON = 0.0000001;
-
   private int numberOfVariables;
   private List<Equation> equations = new ArrayList<Equation>();
   private double[][] data;
@@ -110,6 +107,12 @@
     equations.add(equation);
   }
 
+  public void addEqualsEquation(CombinationEquation equation) {
+    assert step == Step.NEW;
+
+    equations.add(equation);
+  }
+
   public void addEqualsEquation(ProportionEquation equation) {
     assert step == Step.NEW;
 
@@ -128,7 +131,14 @@
     return indices;
   }
 
-  public void prepare() {
+  public Measure[] solve() {
+    prepare();
+    gauss();
+    reduce();
+    return result();
+  }
+
+  private void prepare() {
     assert step == Step.NEW;
     step = step.next();
 //    data = new double[equations.size() + equalEquations.size()][];
@@ -156,7 +166,7 @@
     step = step.next();
   }
 
-  public void gauss() {
+  private void gauss() {
 
     LOG.info(this);
 
@@ -169,7 +179,7 @@
       // normalize row
       LOG.info(this);
       double factor = data[j][j];
-      if (isZero(factor)) {
+      if (MathUtils.isZero(factor)) {
         int nonZeroIndex = findNonZero(j);
         if (nonZeroIndex != -1) {
           swapRow(j, nonZeroIndex);
@@ -197,7 +207,7 @@
     step = step.next();
   }
 
-  public void reduce() {
+  private void reduce() {
     assert step == Step.TRIANGULAR;
     step = step.next();
 
@@ -215,17 +225,64 @@
     step = step.next();
   }
 
-  public double[] result() {
+  private Measure[] result() {
     assert step == Step.DIAGONAL;
 
     LOG.info(this);
-    double[] result = new double[numberOfVariables];
+
+    round();
+
+    Measure[] result = new Measure[numberOfVariables];
     for (int i = 0; i < numberOfVariables; i++) {
-      result[i] = data[i][numberOfVariables];
+      assert MathUtils.isInteger(data[i][numberOfVariables]);
+      result[i] = new PixelMeasure((int) Math.round(data[i][numberOfVariables]));
     }
+
+    LOG.info("after adjust remainders:  " + Arrays.toString(result));
+
     return result;
   }
 
+  private void round() {
+
+    for (Equation equation : equations) {
+      if (equation instanceof PartitionEquation) {
+        PartitionEquation partition = (PartitionEquation) equation;
+
+        int begin = partition.getBegin();
+        int count = partition.getCount();
+        double[] temp = new double[count];
+        // copy from data to temporary array
+        for (int i = 0; i < count; i++) {
+          temp[i] = data[i + begin][numberOfVariables];
+        }
+        // processing
+        MathUtils.adjustRemainders(temp);
+        // write back to data
+        for (int i = 0; i < count; i++) {
+          data[i + begin][numberOfVariables] = temp[i];
+        }
+      }
+      if (equation instanceof CombinationEquation) {
+        CombinationEquation combination = (CombinationEquation) equation;
+        combination.getParent();
+        int parent = combination.getParent();
+        int span = combination.getSpan();
+        double sum = (span - 1) * combination.getSpacing().getPixel();
+        for (int i = 0; i < span; i++) {
+          double value = data[i + parent][numberOfVariables];
+          assert MathUtils.isInteger(value);
+          sum += value;
+        }
+        int index = combination.getNewIndex();
+        LOG.info("Change value for index=" + index + " from "
+            + (data[index][numberOfVariables]) + " -> " + Math.round(sum));
+        assert Math.abs(data[index][numberOfVariables] - Math.round(sum)) < 1;
+        data[index][numberOfVariables] = Math.round(sum);
+      }
+    }
+  }
+
   private void swapRow(int j, int k) {
     double[] temp = data[j];
     data[j] = data[k];
@@ -234,7 +291,7 @@
 
   private int findNonZero(int j) {
     for (int k = j + 1; k < data.length; k++) {
-      if (isNotZero(data[k][j])) {
+      if (MathUtils.isNotZero(data[k][j])) {
         return k;
       }
     }
@@ -250,7 +307,7 @@
     for (int j = data.length - 1; j >= 0; j--) {
       boolean allZero = true;
       for (double value : data[j]) {
-        if (isNotZero(value)) {
+        if (MathUtils.isNotZero(value)) {
           allZero = false;
           break;
         }
@@ -267,31 +324,18 @@
     for (int i = 0; i < numberOfVariables + 1; i++) {
       data[j][i] = data[j][i] / denominator;
     }
-    // try to fix float values
-//    double b = data[j][numberOfVariables];
-//    if (isNotZero(b - Math.round(b))) {
-//      data[j][numberOfVariables] = Math.round(b);
-//      LOG.warn("Fixing float result from " + b + " to " + data[j][numberOfVariables]);
-//    }
   }
 
   private void substractMultipleOfRowJToRowK(int j, int k) {
     double factor = data[k][j];
-    if (isNotZero(factor)) {
+    if (MathUtils.isNotZero(factor)) {
       for (int i = 0; i < numberOfVariables + 1; i++) {
         data[k][i] -= data[j][i] * factor;
       }
+//      fixResultOfRow(k);
     }
   }
 
-  private boolean isZero(double factor) {
-    return Math.abs(factor) < EPSILON;
-  }
-
-  private boolean isNotZero(double factor) {
-    return Math.abs(factor) >= EPSILON;
-  }
-
   /**
    * Determines if the row j has only null entries, accept the last one.
    *
@@ -300,7 +344,7 @@
    */
   private boolean rowNull(int j) {
     for (int i = 0; i < numberOfVariables; i++) {
-      if (isNotZero(data[j][i])) {
+      if (MathUtils.isNotZero(data[j][i])) {
         return false;
       }
     }

Modified: myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/component/GridLayoutManagerUnitTest.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/component/GridLayoutManagerUnitTest.java?rev=796295&r1=796294&r2=796295&view=diff
==============================================================================
--- myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/component/GridLayoutManagerUnitTest.java (original)
+++ myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/component/GridLayoutManagerUnitTest.java Tue Jul 21 13:44:17 2009
@@ -20,10 +20,10 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.myfaces.tobago.layout.LayoutContext;
+import org.apache.myfaces.tobago.layout.Measure;
 import org.apache.myfaces.tobago.layout.MockComponent;
 import org.apache.myfaces.tobago.layout.MockContainer;
 import org.apache.myfaces.tobago.layout.PixelMeasure;
-import org.apache.myfaces.tobago.layout.math.SystemOfEquations;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -62,13 +62,16 @@
     LayoutContext layoutContext = new LayoutContext(container);
     layoutContext.layout();
 
-    double[] result = layoutContext.getHorizontal().getResult();
+    Measure[] result = layoutContext.getHorizontal().getResult();
     LOG.info("result: " + Arrays.toString(result));
-    Assert.assertArrayEquals(new double[]{300, 100, 200, 100, 200}, result, 0.000001);
+    Assert.assertArrayEquals(new Measure[]{
+        new PixelMeasure(300), new PixelMeasure(100), new PixelMeasure(200),
+        new PixelMeasure(100), new PixelMeasure(200)}, result);
 
     result = layoutContext.getVertical().getResult();
     LOG.info("result: " + Arrays.toString(result));
-    Assert.assertArrayEquals(new double[]{20, 20, 20, 20}, result, 0.000001);
+    Assert.assertArrayEquals(new Measure[]{
+        new PixelMeasure(20), new PixelMeasure(20), new PixelMeasure(20), new PixelMeasure(20)}, result);
 
     Assert.assertEquals("width of container", 300, container.getWidth().getPixel());
     Assert.assertEquals("width of component 1", 100, c1.getWidth().getPixel());
@@ -105,13 +108,16 @@
     LayoutContext layoutContext = new LayoutContext(container);
     layoutContext.layout();
 
-    double[] result = layoutContext.getHorizontal().getResult();
+    Measure[] result = layoutContext.getHorizontal().getResult();
     LOG.info("result: " + Arrays.toString(result));
-    Assert.assertArrayEquals(new double[]{300, 100, 100, 100, 100, 200}, result, 0.000001);
+    Assert.assertArrayEquals(new Measure[]{
+        new PixelMeasure(300), new PixelMeasure(100), new PixelMeasure(100), new PixelMeasure(100),
+        new PixelMeasure(100), new PixelMeasure(200)}, result);
 
     result = layoutContext.getVertical().getResult();
     LOG.info("result: " + Arrays.toString(result));
-    Assert.assertArrayEquals(new double[]{20, 20, 20, 20}, result, 0.000001);
+    Assert.assertArrayEquals(new Measure[]{
+        new PixelMeasure(20), new PixelMeasure(20), new PixelMeasure(20), new PixelMeasure(20)}, result);
 
     Assert.assertEquals("width of container", 300, container.getWidth().getPixel());
     Assert.assertEquals("width of component", 100, c.getWidth().getPixel());
@@ -170,18 +176,24 @@
     LayoutContext layoutContext = new LayoutContext(container);
     layoutContext.layout();
 
-    double[] result = layoutContext.getHorizontal().getResult();
+    Measure[] result = layoutContext.getHorizontal().getResult();
     LOG.info("result: " + Arrays.toString(result));
-    Assert.assertArrayEquals(new double[]{
-        800, 100, 200, 500, 100, 700, 100, 200, 500, 350, 150, 350, 150, 350, 150
-    }, result, 0.000001);
+    Assert.assertArrayEquals(new Measure[]{
+        new PixelMeasure(800), new PixelMeasure(100), new PixelMeasure(200), new PixelMeasure(500),
+        new PixelMeasure(100), new PixelMeasure(700), new PixelMeasure(100), new PixelMeasure(200),
+        new PixelMeasure(500), new PixelMeasure(350), new PixelMeasure(150), new PixelMeasure(350),
+        new PixelMeasure(150), new PixelMeasure(350), new PixelMeasure(150)
+    }, result);
 
 
     result = layoutContext.getVertical().getResult();
     LOG.info("result: " + Arrays.toString(result));
-    Assert.assertArrayEquals(new double[]{
-        800, 200, 600, 200, 200, 600, 600, 600, 300, 300, 300, 300, 300, 300
-    }, result, 0.000001);
+    Assert.assertArrayEquals(new Measure[]{
+        new PixelMeasure(800), new PixelMeasure(200), new PixelMeasure(600), new PixelMeasure(200),
+        new PixelMeasure(200), new PixelMeasure(600), new PixelMeasure(600), new PixelMeasure(600),
+        new PixelMeasure(300), new PixelMeasure(300), new PixelMeasure(300), new PixelMeasure(300),
+        new PixelMeasure(300), new PixelMeasure(300)
+    }, result);
 
     Assert.assertEquals("width of container", 800, container.getWidth().getPixel());
     Assert.assertEquals("width of span", 700, span.getWidth().getPixel());
@@ -235,16 +247,21 @@
 
     LOG.info(((UIGridLayout) container.getLayoutManager()).getGrid());
 
-    double[] result = layoutContext.getHorizontal().getResult();
+    Measure[] result = layoutContext.getHorizontal().getResult();
     LOG.info("result: " + Arrays.toString(result));
-    Assert.assertArrayEquals(new double[]{
-        900, 300, 300, 300, 300, 600, 200, 200, 200, 600, 200, 200, 200, 300
-    }, result, 0.000001);
-
+    Assert.assertArrayEquals(new Measure[]{
+        new PixelMeasure(900), new PixelMeasure(300), new PixelMeasure(300), new PixelMeasure(300),
+        new PixelMeasure(300), new PixelMeasure(600), new PixelMeasure(200), new PixelMeasure(200),
+        new PixelMeasure(200), new PixelMeasure(600), new PixelMeasure(200), new PixelMeasure(200),
+        new PixelMeasure(200), new PixelMeasure(300)
+    }, result);
 
     result = layoutContext.getVertical().getResult();
     LOG.info("result: " + Arrays.toString(result));
-    Assert.assertArrayEquals(new double[]{200, 100, 100, 100, 100, 100, 100, 100, 100}, result, 0.000001);
+    Assert.assertArrayEquals(new Measure[]{
+        new PixelMeasure(200), new PixelMeasure(100), new PixelMeasure(100), new PixelMeasure(100),
+        new PixelMeasure(100), new PixelMeasure(100), new PixelMeasure(100), new PixelMeasure(100),
+        new PixelMeasure(100)}, result);
 
     Assert.assertEquals("width of container", 900, container.getWidth().getPixel());
     Assert.assertEquals("width of span 1", 600, span1.getWidth().getPixel());
@@ -289,14 +306,18 @@
     LayoutContext layoutContext = new LayoutContext(container);
     layoutContext.layout();
 
-    double[] result = layoutContext.getHorizontal().getResult();
+    Measure[] result = layoutContext.getHorizontal().getResult();
     LOG.info("result: " + Arrays.toString(result));
-    Assert.assertArrayEquals(new double[]{800.0, 400.0, 400.0, 400.0, 400.0, 400.0}, result, SystemOfEquations.EPSILON);
+    Assert.assertArrayEquals(new Measure[]{
+        new PixelMeasure(800), new PixelMeasure(400), new PixelMeasure(400), new PixelMeasure(400),
+        new PixelMeasure(400), new PixelMeasure(400)}, result);
 
 
     result = layoutContext.getVertical().getResult();
     LOG.info("result: " + Arrays.toString(result));
-    Assert.assertArrayEquals(new double[]{800.0, 400.0, 400.0, 800.0, 400.0, 400.0}, result, SystemOfEquations.EPSILON);
+    Assert.assertArrayEquals(new Measure[]{
+        new PixelMeasure(800), new PixelMeasure(400), new PixelMeasure(400), new PixelMeasure(800),
+        new PixelMeasure(400), new PixelMeasure(400)}, result);
 
     Assert.assertEquals("width of container", 800, container.getWidth().getPixel());
     Assert.assertEquals("height of container", 800, container.getHeight().getPixel());

Added: myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/layout/math/CombinationEquationUnitTest.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/layout/math/CombinationEquationUnitTest.java?rev=796295&view=auto
==============================================================================
--- myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/layout/math/CombinationEquationUnitTest.java (added)
+++ myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/layout/math/CombinationEquationUnitTest.java Tue Jul 21 13:44:17 2009
@@ -0,0 +1,62 @@
+package org.apache.myfaces.tobago.layout.math;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+import org.apache.myfaces.tobago.layout.PixelMeasure;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class CombinationEquationUnitTest {
+
+  @Test
+  public void testToString() {
+    Assert.assertEquals("CombinationEquation:  x_0 = x_4",
+        new CombinationEquation(4, 0, 1, PixelMeasure.ZERO).toString());
+    Assert.assertEquals("CombinationEquation:  x_0 + x_1 = x_4",
+        new CombinationEquation(4, 0, 2, PixelMeasure.ZERO).toString());
+    Assert.assertEquals("CombinationEquation:  x_0 + ... + x_2 = x_4",
+        new CombinationEquation(4, 0, 3, PixelMeasure.ZERO).toString());
+
+    Assert.assertEquals("CombinationEquation:  x_0 = x_4",
+        new CombinationEquation(4, 0, 1, new PixelMeasure(5)).toString());
+    Assert.assertEquals("CombinationEquation:  x_0 + x_1 + 5px = x_4",
+        new CombinationEquation(4, 0, 2, new PixelMeasure(5)).toString());
+    Assert.assertEquals("CombinationEquation:  x_0 + ... + x_2 + 2 * 5px = x_4",
+        new CombinationEquation(4, 0, 3, new PixelMeasure(5)).toString());
+  }
+
+  @Test
+  public void testFillRow() {
+    double[] row = new double[8];
+
+    new CombinationEquation(4, 0, 1, PixelMeasure.ZERO).fillRow(row);
+    Assert.assertArrayEquals(new double[] {-1, 0, 0, 0, 1, 0, 0, 0}, row, MathUtils.EPSILON);
+    new CombinationEquation(4, 0, 2, PixelMeasure.ZERO).fillRow(row);
+    Assert.assertArrayEquals(new double[] {-1, -1, 0, 0, 1, 0, 0, 0}, row, MathUtils.EPSILON);
+    new CombinationEquation(4, 0, 3, PixelMeasure.ZERO).fillRow(row);
+    Assert.assertArrayEquals(new double[] {-1, -1, -1, 0, 1, 0, 0, 0}, row, MathUtils.EPSILON);
+
+    new CombinationEquation(4, 0, 1, new PixelMeasure(5)).fillRow(row);
+    Assert.assertArrayEquals(new double[] {-1, 0, 0, 0, 1, 0, 0, 0}, row, MathUtils.EPSILON);
+    new CombinationEquation(4, 0, 2, new PixelMeasure(5)).fillRow(row);
+    Assert.assertArrayEquals(new double[] {-1, -1, 0, 0, 1, 0, 0, 5}, row, MathUtils.EPSILON);
+    new CombinationEquation(4, 0, 3, new PixelMeasure(5)).fillRow(row);
+    Assert.assertArrayEquals(new double[] {-1, -1, -1, 0, 1, 0, 0, 10}, row, MathUtils.EPSILON);
+  }
+
+}

Modified: myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/layout/math/EquationManagerUnitTest.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/layout/math/EquationManagerUnitTest.java?rev=796295&r1=796294&r2=796295&view=diff
==============================================================================
--- myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/layout/math/EquationManagerUnitTest.java (original)
+++ myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/layout/math/EquationManagerUnitTest.java Tue Jul 21 13:44:17 2009
@@ -19,6 +19,8 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.myfaces.tobago.layout.Measure;
+import org.apache.myfaces.tobago.layout.PixelMeasure;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -253,52 +255,52 @@
 
     index = equationManager.addComponentRoot();
     Assert.assertEquals(0, index);
-    equationManager.setFixedLength(index, 630);        // the first (current) index has a fix size of 600
-    indices = equationManager.divide(index, 4, 0);                     // this index is divided into 4 parts
+    equationManager.setFixedLength(index, new PixelMeasure(630));        // the first (current) index has a fix size of 600
+    indices = equationManager.divide(index, 4, PixelMeasure.ZERO);                     // this index is divided into 4 parts
     Assert.assertArrayEquals(new int[]{1, 2, 3, 4}, indices);
     equationManager.setProportion(indices[2], indices[3], 1, 2);     // the value on index 2 has factor 1,
     //                                                the value on position 3 has factor 2
     {
-      equationManager.setFixedLength(indices[0], 100);        // the first one has a fix size of 100
-      index = equationManager.addComponent(indices[0], 1, 0);
+      equationManager.setFixedLength(indices[0], new PixelMeasure(100));        // the first one has a fix size of 100
+      index = equationManager.addComponent(indices[0], 1, PixelMeasure.ZERO);
       Assert.assertEquals(5, index);
     }
     {
-      equationManager.setFixedLength(indices[1], 200);        // the second one has a fix size of 200
-      index = equationManager.addComponent(indices[1], 1, 0);
+      equationManager.setFixedLength(indices[1], new PixelMeasure(200));        // the second one has a fix size of 200
+      index = equationManager.addComponent(indices[1], 1, PixelMeasure.ZERO);
       Assert.assertEquals(6, index);
 
-      index = equationManager.addComponent(indices[1], 1, 0);
+      index = equationManager.addComponent(indices[1], 1, PixelMeasure.ZERO);
       Assert.assertEquals(7, index);
-      int[] i7 = equationManager.divide(7, 2, 0);
+      int[] i7 = equationManager.divide(7, 2, PixelMeasure.ZERO);
       Assert.assertArrayEquals(new int[]{8, 9}, i7);
       equationManager.setProportion(i7[0], i7[1], 1, 2);
 
-      index = equationManager.addComponent(indices[1], 1, 0);
+      index = equationManager.addComponent(indices[1], 1, PixelMeasure.ZERO);
       Assert.assertEquals(10, index);
-      int[] i10 = equationManager.divide(10, 2, 0);
+      int[] i10 = equationManager.divide(10, 2, PixelMeasure.ZERO);
       Assert.assertArrayEquals(new int[]{11, 12}, i10);
       equationManager.setProportion(i10[0], i10[1], 4, 1);
     }
     {
-      index = equationManager.addComponent(indices[2], 1, 0);
+      index = equationManager.addComponent(indices[2], 1, PixelMeasure.ZERO);
       Assert.assertEquals(13, index);
     }
     {
-      index = equationManager.addComponent(indices[3], 1, 0);
+      index = equationManager.addComponent(indices[3], 1, PixelMeasure.ZERO);
       Assert.assertEquals(14, index);
-      int[] i14 = equationManager.divide(14, 2, 0);
+      int[] i14 = equationManager.divide(14, 2, PixelMeasure.ZERO);
       Assert.assertArrayEquals(new int[]{15, 16}, i14);
-      equationManager.setFixedLength(i14[0], 130);        // the second one has a fix size of 200
+      equationManager.setFixedLength(i14[0], new PixelMeasure(130));        // the second one has a fix size of 200
     }
     {
-      int iSpan2 = equationManager.addComponent(indices[2], 2, 0);
+      int iSpan2 = equationManager.addComponent(indices[2], 2, PixelMeasure.ZERO);
       Assert.assertEquals(17, iSpan2);
     }
     {
-      int iSpan4 = equationManager.addComponent(indices[0], 4, 0);
+      int iSpan4 = equationManager.addComponent(indices[0], 4, PixelMeasure.ZERO);
       Assert.assertEquals(18, iSpan4);
-      int[] i18 = equationManager.divide(18, 6, 0);
+      int[] i18 = equationManager.divide(18, 6, PixelMeasure.ZERO);
       Assert.assertArrayEquals(new int[]{19, 20, 21, 22, 23, 24}, i18);
       equationManager.setProportion(i18[0], i18[1], 1, 2);
       equationManager.setProportion(i18[0], i18[2], 1, 3);
@@ -307,11 +309,11 @@
       equationManager.setProportion(i18[0], i18[5], 1, 6);
     }
     {
-      int i19_1 = equationManager.addComponent(19, 6, 0);
+      int i19_1 = equationManager.addComponent(19, 6, PixelMeasure.ZERO);
       Assert.assertEquals(25, i19_1);
-      int i19_2 = equationManager.addComponent(19, 3, 0);
+      int i19_2 = equationManager.addComponent(19, 3, PixelMeasure.ZERO);
       Assert.assertEquals(26, i19_2);
-      int i22 = equationManager.addComponent(22, 3, 0);
+      int i22 = equationManager.addComponent(22, 3, PixelMeasure.ZERO);
       Assert.assertEquals(27, i22);
     }
 
@@ -320,38 +322,37 @@
     // solve
 
     equationManager.solve();
-    double[] result = equationManager.getResult();
+    Measure[] result = equationManager.getResult();
     LOG.info("result: " + Arrays.toString(result));
-    Assert.assertArrayEquals(new double[]{
-       630,        // x_0
-       100,        // x_1
-       200,        // x_2
-       110,        // x_3
-       220,        // x_4
-       100,        // x_5
-       200,        // x_6
-       200,        // x_7
-       66.666667,  // x_8
-       133.333333, // x_9
-       200,        // x_10
-       160,        // x_11
-       40,         // x_12
-       110,        // x_13
-       220,        // x_14
-       130,        // x_15
-       90,         // x_16
-       330,        // x_17
-       630,        // x_18
-       30,         // x_19
-       60,         // x_20
-       90,         // x_21
-       120,        // x_22
-       150,        // x_23
-       180,        // x_24
-       630,        // x_25
-       180,        // x_26
-       450,        // x_27
-    }, result, 0.000001);
-
+    Assert.assertArrayEquals(new Measure[]{
+        new PixelMeasure(630),        // x_0
+        new PixelMeasure(100),        // x_1
+        new PixelMeasure(200),        // x_2
+        new PixelMeasure(110),        // x_3
+        new PixelMeasure(220),        // x_4
+        new PixelMeasure(100),        // x_5
+        new PixelMeasure(200),        // x_6
+        new PixelMeasure(200),        // x_7
+        new PixelMeasure(67),         // x_8
+        new PixelMeasure(133),        // x_9
+        new PixelMeasure(200),        // x_10
+        new PixelMeasure(160),        // x_11
+        new PixelMeasure(40),         // x_12
+        new PixelMeasure(110),        // x_13
+        new PixelMeasure(220),        // x_14
+        new PixelMeasure(130),        // x_15
+        new PixelMeasure(90),         // x_16
+        new PixelMeasure(330),        // x_17
+        new PixelMeasure(630),        // x_18
+        new PixelMeasure(30),         // x_19
+        new PixelMeasure(60),         // x_20
+        new PixelMeasure(90),         // x_21
+        new PixelMeasure(120),        // x_22
+        new PixelMeasure(150),        // x_23
+        new PixelMeasure(180),        // x_24
+        new PixelMeasure(630),        // x_25
+        new PixelMeasure(180),        // x_26
+        new PixelMeasure(450),        // x_27
+    }, result);
   }
 }

Added: myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/layout/math/MathUtilsUnitTest.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/layout/math/MathUtilsUnitTest.java?rev=796295&view=auto
==============================================================================
--- myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/layout/math/MathUtilsUnitTest.java (added)
+++ myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/layout/math/MathUtilsUnitTest.java Tue Jul 21 13:44:17 2009
@@ -0,0 +1,87 @@
+package org.apache.myfaces.tobago.layout.math;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class MathUtilsUnitTest {
+
+  @Test
+  public void testAdjust() {
+    double[] d = {6.3, 7.9, 8.7, 9.2, 10.3, 11.6};
+    MathUtils.adjustRemainders(d);
+    Assert.assertArrayEquals("mixed", new double[]{6, 8, 9, 9, 10, 12}, d, MathUtils.EPSILON);
+  }
+
+  @Test
+  public void testAdjust999() {
+    double[] d = {9.9, 9.9, 9.9, 9.9, 9.9, 9.9, 9.9, 9.9, 9.9, 9.9};
+    MathUtils.adjustRemainders(d);
+    Assert.assertArrayEquals(
+        "9.9, ...", new double[]{10, 9, 10, 10, 10, 10, 10, 10, 10, 10}, d, MathUtils.EPSILON);
+  }
+
+  @Test
+  public void testAdjust111() {
+    double[] d = {1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1};
+    MathUtils.adjustRemainders(d);
+    Assert.assertArrayEquals(
+        "1.1, ...", new double[]{2, 1, 1, 1, 1, 1, 1, 1, 1, 1}, d, MathUtils.EPSILON);
+  }
+
+  @Test
+  public void testAdjust133() {
+    double[] d = {1, 1, 1, 1.333333333, 1.333333333, 1.333333333, 1, 1, 1};
+    MathUtils.adjustRemainders(d);
+    Assert.assertArrayEquals(
+        "1, ..., 1.333...", new double[]{1, 1, 1, 2, 1, 1, 1, 1, 1}, d, MathUtils.EPSILON);
+  }
+
+  @Test
+  public void testIsZero() {
+    Assert.assertTrue(MathUtils.isZero(0));
+    Assert.assertFalse(MathUtils.isZero(1.0/1000.0));
+    Assert.assertTrue(MathUtils.isZero(1.0/1000000000.0));
+    Assert.assertTrue(MathUtils.isZero(-1.0/1000000000.0));
+  }
+
+  @Test
+  public void testIsNotZero() {
+    Assert.assertFalse(MathUtils.isNotZero(0));
+    Assert.assertTrue(MathUtils.isNotZero(1.0/1000.0));
+    Assert.assertFalse(MathUtils.isNotZero(1.0/1000000000.0));
+    Assert.assertFalse(MathUtils.isNotZero(-1.0/1000000000.0));
+  }
+
+  @Test
+  public void testIsInteger() {
+    Assert.assertTrue(MathUtils.isInteger(1.0));
+    Assert.assertTrue(MathUtils.isInteger(0.0));
+    Assert.assertTrue(MathUtils.isInteger(1.0/1000000000.0));
+    Assert.assertFalse(MathUtils.isInteger(1.0/1000.0));
+  }
+
+  @Test
+  public void testIsNotInteger() {
+    Assert.assertFalse(MathUtils.isNotInteger(1.0));
+    Assert.assertFalse(MathUtils.isNotInteger(0.0));
+    Assert.assertFalse(MathUtils.isNotInteger(1.0/1000000000.0));
+    Assert.assertTrue(MathUtils.isNotInteger(1.0/1000.0));
+  }
+}

Added: myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/layout/math/PartitionEquationUnitTest.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/layout/math/PartitionEquationUnitTest.java?rev=796295&view=auto
==============================================================================
--- myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/layout/math/PartitionEquationUnitTest.java (added)
+++ myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/layout/math/PartitionEquationUnitTest.java Tue Jul 21 13:44:17 2009
@@ -0,0 +1,61 @@
+package org.apache.myfaces.tobago.layout.math;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+import org.apache.myfaces.tobago.layout.PixelMeasure;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class PartitionEquationUnitTest {
+
+  @Test
+  public void testToString() {
+    Assert.assertEquals("PartitionEquation:  x_0 = x_4",
+        new PartitionEquation(4, 1, 0, PixelMeasure.ZERO).toString());
+    Assert.assertEquals("PartitionEquation:  x_0 = x_4 + x_5",
+        new PartitionEquation(4, 2, 0, PixelMeasure.ZERO).toString());
+    Assert.assertEquals("PartitionEquation:  x_0 = x_4 + ... + x_6",
+        new PartitionEquation(4, 3, 0, PixelMeasure.ZERO).toString());
+
+    Assert.assertEquals("PartitionEquation:  x_0 = x_4",
+        new PartitionEquation(4, 1, 0, new PixelMeasure(5)).toString());
+    Assert.assertEquals("PartitionEquation:  x_0 = x_4 + x_5 + 5px",
+        new PartitionEquation(4, 2, 0, new PixelMeasure(5)).toString());
+    Assert.assertEquals("PartitionEquation:  x_0 = x_4 + ... + x_6 + 2 * 5px",
+        new PartitionEquation(4, 3, 0, new PixelMeasure(5)).toString());
+  }
+
+  @Test
+  public void testFillRow() {
+    double[] row = new double[8];
+
+    new PartitionEquation(4, 1, 0, PixelMeasure.ZERO).fillRow(row);
+    Assert.assertArrayEquals(new double[] {-1, 0, 0, 0, 1, 0, 0, 0}, row, MathUtils.EPSILON);
+    new PartitionEquation(4, 2, 0, PixelMeasure.ZERO).fillRow(row);
+    Assert.assertArrayEquals(new double[] {-1, 0, 0, 0, 1, 1, 0, 0}, row, MathUtils.EPSILON);
+    new PartitionEquation(4, 3, 0, PixelMeasure.ZERO).fillRow(row);
+    Assert.assertArrayEquals(new double[] {-1, 0, 0, 0, 1, 1, 1, 0}, row, MathUtils.EPSILON);
+
+    new PartitionEquation(4, 1, 0, new PixelMeasure(5)).fillRow(row);
+    Assert.assertArrayEquals(new double[] {-1, 0, 0, 0, 1, 0, 0, 0}, row, MathUtils.EPSILON);
+    new PartitionEquation(4, 2, 0, new PixelMeasure(5)).fillRow(row);
+    Assert.assertArrayEquals(new double[] {-1, 0, 0, 0, 1, 1, 0, -5}, row, MathUtils.EPSILON);
+    new PartitionEquation(4, 3, 0, new PixelMeasure(5)).fillRow(row);
+    Assert.assertArrayEquals(new double[] {-1, 0, 0, 0, 1, 1, 1, -10}, row, MathUtils.EPSILON);
+  }
+}
\ No newline at end of file

Modified: myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/layout/math/SystemOfEquationsUnitTest.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/layout/math/SystemOfEquationsUnitTest.java?rev=796295&r1=796294&r2=796295&view=diff
==============================================================================
--- myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/layout/math/SystemOfEquationsUnitTest.java (original)
+++ myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/layout/math/SystemOfEquationsUnitTest.java Tue Jul 21 13:44:17 2009
@@ -19,6 +19,8 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.myfaces.tobago.layout.Measure;
+import org.apache.myfaces.tobago.layout.PixelMeasure;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -35,20 +37,18 @@
     long begin = System.nanoTime();
 
     SystemOfEquations system = new SystemOfEquations(3);
-    system.addEqualsEquation(new FixedEquation(0, 1005));
-    system.addEqualsEquation(new PartitionEquation(1, 3, 0, 1, 5, 0));
+    system.addEqualsEquation(new FixedEquation(0, new PixelMeasure(1005)));
+    system.addEqualsEquation(new PartitionEquation(1, 2, 0, new PixelMeasure(5)));
     system.addEqualsEquation(new ProportionEquation(1, 2, 2, 3));
-    system.prepare();
-    system.gauss();
-    system.reduce();
-    double[] result = system.result();
+    Measure[] result = system.solve();
 
     long end = System.nanoTime();
 
     LOG.info("result: " + Arrays.toString(result));
     LOG.info("Duration: " + new DecimalFormat().format(end - begin) + " ns");
 
-    Assert.assertArrayEquals(new double[]{1005, 400, 600}, result, SystemOfEquations.EPSILON);
+    Assert.assertArrayEquals(new Measure[]{
+        new PixelMeasure(1005), new PixelMeasure(400), new PixelMeasure(600)}, result);
   }
 
   @Test
@@ -57,22 +57,21 @@
     long begin = System.nanoTime();
 
     SystemOfEquations system = new SystemOfEquations(5);
-    system.addEqualsEquation(new FixedEquation(0, 1015));
-    system.addEqualsEquation(new PartitionEquation(1, 5, 0, 1, 5, 0));
+    system.addEqualsEquation(new FixedEquation(0, new PixelMeasure(1015)));
+    system.addEqualsEquation(new PartitionEquation(1, 4, 0, new PixelMeasure(5)));
     system.addEqualsEquation(new ProportionEquation(1, 2, 1, 2));
     system.addEqualsEquation(new ProportionEquation(2, 3, 2, 3));
     system.addEqualsEquation(new ProportionEquation(3, 4, 3, 4));
-    system.prepare();
-    system.gauss();
-    system.reduce();
-    double[] result = system.result();
+    Measure[] result = system.solve();
 
     long end = System.nanoTime();
 
     LOG.info("result: " + Arrays.toString(result));
     LOG.info("Duration: " + new DecimalFormat().format(end - begin) + " ns");
 
-    Assert.assertArrayEquals(new double[]{1015, 100, 200, 300, 400}, result, SystemOfEquations.EPSILON);
+    Assert.assertArrayEquals(new Measure[]{
+        new PixelMeasure(1015), new PixelMeasure(100), new PixelMeasure(200), new PixelMeasure(300),
+        new PixelMeasure(400)}, result);
   }
 
   @Test
@@ -81,8 +80,8 @@
     long begin = System.nanoTime();
 
     SystemOfEquations system = new SystemOfEquations(11);
-    system.addEqualsEquation(new FixedEquation(0, 1100));
-    system.addEqualsEquation(new PartitionEquation(1, 11, 0, 1, 0, 0));
+    system.addEqualsEquation(new FixedEquation(0, new PixelMeasure(1100)));
+    system.addEqualsEquation(new PartitionEquation(1, 10, 0, PixelMeasure.ZERO));
     system.addEqualsEquation(new ProportionEquation(1, 2, 1, 2));
     system.addEqualsEquation(new ProportionEquation(2, 3, 2, 3));
     system.addEqualsEquation(new ProportionEquation(3, 4, 3, 4));
@@ -92,18 +91,17 @@
     system.addEqualsEquation(new ProportionEquation(7, 8, 7, 8));
     system.addEqualsEquation(new ProportionEquation(8, 9, 8, 9));
     system.addEqualsEquation(new ProportionEquation(9, 10, 9, 10));
-
-    system.prepare();
-    system.gauss();
-    system.reduce();
-    double[] result = system.result();
+    Measure[] result = system.solve();
 
     long end = System.nanoTime();
 
     LOG.info("result: " + Arrays.toString(result));
     LOG.info("Duration: " + new DecimalFormat().format(end - begin) + " ns");
 
-    Assert.assertArrayEquals(new double[]{1100, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200}, result, SystemOfEquations.EPSILON);
+    Assert.assertArrayEquals(new Measure[]{
+        new PixelMeasure(1100), new PixelMeasure(20), new PixelMeasure(40), new PixelMeasure(60),
+        new PixelMeasure(80), new PixelMeasure(100), new PixelMeasure(120), new PixelMeasure(140),
+        new PixelMeasure(160), new PixelMeasure(180), new PixelMeasure(200)}, result);
   }
 
   @Test
@@ -115,32 +113,28 @@
     int sum = n * (n + 1) / 2;
 
     SystemOfEquations system = new SystemOfEquations(n + 1);
-    system.addEqualsEquation(new FixedEquation(0, sum));
-    system.addEqualsEquation(new PartitionEquation(1, n + 1, 0, 1, 0, 0));
+    system.addEqualsEquation(new FixedEquation(0, new PixelMeasure(sum)));
+    system.addEqualsEquation(new PartitionEquation(1, n, 0, PixelMeasure.ZERO));
     for (int i = 1; i < n; i++) {
       system.addEqualsEquation(new ProportionEquation(i, i + 1, (double) i, (double) i + 1));
     }
-
-    system.prepare();
-    system.gauss();
-    system.reduce();
-    double[] result = system.result();
+    Measure[] result = system.solve();
 
     long end = System.nanoTime();
 
     LOG.info("result: " + Arrays.toString(result));
     LOG.info("Duration: " + new DecimalFormat().format(end - begin) + " ns");
-    double[] expected = new double[n + 1];
-    expected[0] = sum;
+    Measure[] expected = new Measure[n + 1];
+    expected[0] = new PixelMeasure(sum);
     for (int i = 0; i < n; i++) {
-      expected[i + 1] = i + 1;
+      expected[i + 1] = new PixelMeasure(i + 1);
     }
-    Assert.assertArrayEquals(expected, result, SystemOfEquations.EPSILON);
+    Assert.assertArrayEquals(expected, result);
   }
 
   /**
    * <pre>
-   * |               1000px              |
+   * |               900px              |
    * |     *     |     *     |     *     |
    * |           |   *   |   *   |   *   |
    * </pre>
@@ -150,25 +144,25 @@
 
     long begin = System.nanoTime();
 
-    SystemOfEquations system = new SystemOfEquations(7);
-    system.addEqualsEquation(new FixedEquation(0, 900));
-    system.addEqualsEquation(new PartitionEquation(1, 4, 0, 1, 0, 0));
-    system.addEqualsEquation(new PartitionEquation(4, 7, 2, 2, 0, 0));
+    SystemOfEquations system = new SystemOfEquations(8);
+    system.addEqualsEquation(new FixedEquation(0, new PixelMeasure(900)));
+    system.addEqualsEquation(new PartitionEquation(1, 3, 0, PixelMeasure.ZERO));
+    system.addEqualsEquation(new CombinationEquation(4, 2, 2, PixelMeasure.ZERO));
+    system.addEqualsEquation(new PartitionEquation(5, 3, 4, PixelMeasure.ZERO));
     system.addEqualsEquation(new ProportionEquation(1, 2, 1, 1));
     system.addEqualsEquation(new ProportionEquation(1, 3, 1, 1));
-    system.addEqualsEquation(new ProportionEquation(4, 5, 1, 1));
-    system.addEqualsEquation(new ProportionEquation(4, 6, 1, 1));
-    system.prepare();
-    system.gauss();
-    system.reduce();
-    double[] result = system.result();
+    system.addEqualsEquation(new ProportionEquation(5, 6, 1, 1));
+    system.addEqualsEquation(new ProportionEquation(5, 7, 1, 1));
+    Measure[] result = system.solve();
 
     long end = System.nanoTime();
 
     LOG.info("result: " + Arrays.toString(result));
     LOG.info("Duration: " + new DecimalFormat().format(end - begin) + " ns");
 
-    Assert.assertArrayEquals(new double[]{900, 300, 300, 300, 200, 200, 200}, result, SystemOfEquations.EPSILON);
+    Assert.assertArrayEquals(new Measure[]{
+        new PixelMeasure(900), new PixelMeasure(300), new PixelMeasure(300), new PixelMeasure(300),
+        new PixelMeasure(600), new PixelMeasure(200), new PixelMeasure(200), new PixelMeasure(200)}, result);
   }
 
   @Test
@@ -194,25 +188,25 @@
 
     long begin = System.nanoTime();
 
-    SystemOfEquations system = new SystemOfEquations(7);
+    SystemOfEquations system = new SystemOfEquations(8);
 //    system.addEqualsEquation(new FixedEquation(0, 900));
-    system.addEqualsEquation(new PartitionEquation(1, 4, 0, 1, 0, 0));
-    system.addEqualsEquation(new PartitionEquation(4, 7, 2, 2, 0, 0));
+    system.addEqualsEquation(new PartitionEquation(1, 3, 0, PixelMeasure.ZERO));
+    system.addEqualsEquation(new CombinationEquation(4, 2, 2, PixelMeasure.ZERO));
+    system.addEqualsEquation(new PartitionEquation(5, 3, 4, PixelMeasure.ZERO));
     system.addEqualsEquation(new ProportionEquation(1, 2, 1, 1));
     system.addEqualsEquation(new ProportionEquation(1, 3, 1, 1));
-    system.addEqualsEquation(new ProportionEquation(4, 5, 1, 1));
-    system.addEqualsEquation(new ProportionEquation(4, 6, 1, 1));
-    system.prepare();
-    system.gauss();
-    system.reduce();
-    double[] result = system.result();
+    system.addEqualsEquation(new ProportionEquation(5, 6, 1, 1));
+    system.addEqualsEquation(new ProportionEquation(5, 7, 1, 1));
+    Measure[] result = system.solve();
 
     long end = System.nanoTime();
 
     LOG.info("result: " + Arrays.toString(result));
     LOG.info("Duration: " + new DecimalFormat().format(end - begin) + " ns");
 
-    Assert.assertArrayEquals(new double[]{450, 150, 150, 150, 100, 100, 100}, result, SystemOfEquations.EPSILON);
+    Assert.assertArrayEquals(new Measure[]{
+        new PixelMeasure(450), new PixelMeasure(150), new PixelMeasure(150), new PixelMeasure(150),
+        new PixelMeasure(300), new PixelMeasure(100), new PixelMeasure(100), new PixelMeasure(100)}, result);
   }
 
   /**
@@ -227,23 +221,21 @@
     long begin = System.nanoTime();
 
     SystemOfEquations system = new SystemOfEquations(4);
-    system.addEqualsEquation(new FixedEquation(0, 100));
-    system.addEqualsEquation(new PartitionEquation(1, 3, 0, 1, 0, 0));
-    system.addEqualsEquation(new FixedEquation(1, 50));
-    system.addEqualsEquation(new FixedEquation(2, 50));
-    system.addEqualsEquation(new PartitionEquation(3, 4, 2, 1, 0, 0));
-    system.addEqualsEquation(new FixedEquation(3, 50));
-    system.prepare();
-    system.gauss();
-    system.reduce();
-    double[] result = system.result();
+    system.addEqualsEquation(new FixedEquation(0, new PixelMeasure(100)));
+    system.addEqualsEquation(new PartitionEquation(1, 2, 0, PixelMeasure.ZERO));
+    system.addEqualsEquation(new FixedEquation(1, new PixelMeasure(50)));
+    system.addEqualsEquation(new FixedEquation(2, new PixelMeasure(50)));
+    system.addEqualsEquation(new PartitionEquation(3, 1, 2, PixelMeasure.ZERO));
+    system.addEqualsEquation(new FixedEquation(3, new PixelMeasure(50)));
+    Measure[] result = system.solve();
 
     long end = System.nanoTime();
 
     LOG.info("result: " + Arrays.toString(result));
     LOG.info("Duration: " + new DecimalFormat().format(end - begin) + " ns");
 
-    Assert.assertArrayEquals(new double[]{100, 50, 50, 50}, result, SystemOfEquations.EPSILON);
+    Assert.assertArrayEquals(new Measure[]{
+        new PixelMeasure(100), new PixelMeasure(50), new PixelMeasure(50), new PixelMeasure(50)}, result);
   }
 
   /**
@@ -258,27 +250,72 @@
 
     long begin = System.nanoTime();
 
-    SystemOfEquations system = new SystemOfEquations(7);
+    SystemOfEquations system = new SystemOfEquations(8);
 //    system.addEqualsEquation(new FixedEquation(0, 900));
-    system.addEqualsEquation(new PartitionEquation(1, 4, 0, 1, 0, 0));
-    system.addEqualsEquation(new PartitionEquation(4, 7, 2, 2, 0, 0));
+    system.addEqualsEquation(new PartitionEquation(1, 3, 0, PixelMeasure.ZERO));
+    system.addEqualsEquation(new CombinationEquation(4, 2, 2, PixelMeasure.ZERO));
+    system.addEqualsEquation(new PartitionEquation(5, 3, 4, PixelMeasure.ZERO));
     system.addEqualsEquation(new ProportionEquation(1, 2, 1, 1));
     system.addEqualsEquation(new ProportionEquation(1, 3, 1, 1));
-    system.addEqualsEquation(new ProportionEquation(4, 5, 1, 1));
-    system.addEqualsEquation(new ProportionEquation(4, 6, 1, 1));
-    system.prepare();
-    system.gauss();
-    system.reduce();
-    double[] result = system.result();
+    system.addEqualsEquation(new ProportionEquation(5, 6, 1, 1));
+    system.addEqualsEquation(new ProportionEquation(5, 7, 1, 1));
+    Measure[] result = system.solve();
+
+    long end = System.nanoTime();
+
+    LOG.info("result: " + Arrays.toString(result));
+    LOG.info("Duration: " + new DecimalFormat().format(end - begin) + " ns");
+
+    Assert.assertArrayEquals(new Measure[]{
+        new PixelMeasure(450), new PixelMeasure(150), new PixelMeasure(150), new PixelMeasure(150),
+        new PixelMeasure(300), new PixelMeasure(100), new PixelMeasure(100), new PixelMeasure(100)}, result);
+  }
+
+  @Test
+  public void testRound() {
+
+    long begin = System.nanoTime();
+
+    SystemOfEquations system = new SystemOfEquations(4);
+    system.addEqualsEquation(new FixedEquation(0, new PixelMeasure(1001)));
+    system.addEqualsEquation(new PartitionEquation(1, 3, 0, PixelMeasure.ZERO));
+    system.addEqualsEquation(new ProportionEquation(1, 2, 1, 1));
+    system.addEqualsEquation(new ProportionEquation(2, 3, 1, 1));
+    Measure[] result = system.solve();
 
     long end = System.nanoTime();
 
     LOG.info("result: " + Arrays.toString(result));
     LOG.info("Duration: " + new DecimalFormat().format(end - begin) + " ns");
 
-    Assert.assertArrayEquals(new double[]{450, 150, 150, 150, 100, 100, 100}, result, SystemOfEquations.EPSILON);
+    Assert.assertArrayEquals(new Measure[]{
+        new PixelMeasure(1001), new PixelMeasure(334), new PixelMeasure(333), new PixelMeasure(334)}, result);
   }
 
+  @Test
+  public void testRoundWithCombination() {
+
+    long begin = System.nanoTime();
+
+    SystemOfEquations system = new SystemOfEquations(7);
+    system.addEqualsEquation(new FixedEquation(0, new PixelMeasure(310)));
+    system.addEqualsEquation(new PartitionEquation(1, 2, 0, new PixelMeasure(5)));
+    system.addEqualsEquation(new ProportionEquation(1, 2, 1, 1));
+    system.addEqualsEquation(new CombinationEquation(3, 1, 1, PixelMeasure.ZERO));
+    system.addEqualsEquation(new CombinationEquation(4, 1, 1, PixelMeasure.ZERO));
+    system.addEqualsEquation(new CombinationEquation(5, 2, 1, PixelMeasure.ZERO));
+    system.addEqualsEquation(new CombinationEquation(6, 2, 1, PixelMeasure.ZERO));
+    Measure[] result = system.solve();
+
+    long end = System.nanoTime();
+
+    LOG.info("result: " + Arrays.toString(result));
+    LOG.info("Duration: " + new DecimalFormat().format(end - begin) + " ns");
+
+    Assert.assertArrayEquals(new Measure[]{
+        new PixelMeasure(310), new PixelMeasure(153), new PixelMeasure(152),
+        new PixelMeasure(153), new PixelMeasure(153), new PixelMeasure(152), new PixelMeasure(152)}, result);
+  }
 
   /**
    * todo later: inequations

Copied: myfaces/tobago/trunk/example/test/src/main/webapp/tc/gridLayout/size-not-exact-4x4.xhtml (from r795009, myfaces/tobago/trunk/example/test/src/main/webapp/tc/gridLayout/position-4x4-span-steps.xhtml)
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/example/test/src/main/webapp/tc/gridLayout/size-not-exact-4x4.xhtml?p2=myfaces/tobago/trunk/example/test/src/main/webapp/tc/gridLayout/size-not-exact-4x4.xhtml&p1=myfaces/tobago/trunk/example/test/src/main/webapp/tc/gridLayout/position-4x4-span-steps.xhtml&r1=795009&r2=796295&rev=796295&view=diff
==============================================================================
--- myfaces/tobago/trunk/example/test/src/main/webapp/tc/gridLayout/position-4x4-span-steps.xhtml (original)
+++ myfaces/tobago/trunk/example/test/src/main/webapp/tc/gridLayout/size-not-exact-4x4.xhtml Tue Jul 21 13:44:17 2009
@@ -11,7 +11,8 @@
     <f:facet name="layout">
       <tc:gridLayout columns="6*;9*;13*;2*" rows="4*;11*;10*;5*"/>
     </f:facet>
-    <tc:gridLayoutConstraint width="315px" height="315px"/>
+    <!-- 211 (prime number) + 15px = 226px -->
+    <tc:gridLayoutConstraint width="226px" height="226px"/>
 
     <tc:in id="in-0" value="0"/>
     <tc:in id="in-1" value="1">
@@ -38,17 +39,16 @@
 
     <tc:script file="script/test-utils.js"/>
 
-    <!--todo-->
-    <tc:script onload="checkLayout('page:in-0', 0, 0, 60, 40);"/>
-    <tc:script onload="checkLayout('page:in-1', 65, 0, 90, 155);"/>
-    <tc:script onload="checkLayout('page:in-2', 160, 0, 155, 40);"/>
-    <tc:script onload="checkLayout('page:in-3', 0, 45, 60, 110);"/>
-    <tc:script onload="checkLayout('page:in-4', 160, 45, 130, 215);"/>
-    <tc:script onload="checkLayout('page:in-5', 295, 45, 20, 110);"/>
-    <tc:script onload="checkLayout('page:in-6', 0, 160, 155, 100);"/>
-    <tc:script onload="checkLayout('page:in-7', 295, 160, 20, 155);"/>
-    <tc:script onload="checkLayout('page:in-8', 0, 265, 60, 50);"/>
-    <tc:script onload="checkLayout('page:in-9', 65, 265, 225, 50);"/>
+    <tc:script onload="checkLayout('page:in-0', 0, 0, 42, 28);"/>
+    <tc:script onload="checkLayout('page:in-1', 47, 0, 63, 111);"/>
+    <tc:script onload="checkLayout('page:in-2', 115, 0, 111, 28);"/>
+    <tc:script onload="checkLayout('page:in-3', 0, 33, 42, 78);"/>
+    <tc:script onload="checkLayout('page:in-4', 115, 33, 92, 153);"/>
+    <tc:script onload="checkLayout('page:in-5', 212, 33, 14, 78);"/>
+    <tc:script onload="checkLayout('page:in-6', 0, 116, 110, 70);"/>
+    <tc:script onload="checkLayout('page:in-7', 212, 116, 14, 110);"/>
+    <tc:script onload="checkLayout('page:in-8', 0, 191, 42, 35);"/>
+    <tc:script onload="checkLayout('page:in-9', 47, 191, 160, 35);"/>
 
   </tc:page>
 </f:view>

Copied: myfaces/tobago/trunk/example/test/src/main/webapp/tc/gridLayout/spacing-nested.xhtml (from r795009, myfaces/tobago/trunk/example/test/src/main/webapp/tc/gridLayout/spacing.xhtml)
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/example/test/src/main/webapp/tc/gridLayout/spacing-nested.xhtml?p2=myfaces/tobago/trunk/example/test/src/main/webapp/tc/gridLayout/spacing-nested.xhtml&p1=myfaces/tobago/trunk/example/test/src/main/webapp/tc/gridLayout/spacing.xhtml&r1=795009&r2=796295&rev=796295&view=diff
==============================================================================
--- myfaces/tobago/trunk/example/test/src/main/webapp/tc/gridLayout/spacing.xhtml (original)
+++ myfaces/tobago/trunk/example/test/src/main/webapp/tc/gridLayout/spacing-nested.xhtml Tue Jul 21 13:44:17 2009
@@ -97,12 +97,15 @@
     <tc:script onload="checkLayout('page:b_S', 465, 210, 100, 100);"/>
     <tc:script onload="checkLayout('page:b_SE', 570, 210, 100, 100);"/>
 
-    <tc:script onload="checkLayout('page:c_NW', 0, 360, 155, 152);"/>
+    <tc:script onload="checkLayout('page:c_NW', 0, 360, 155, 153);"/>
     <tc:script onload="checkLayout('page:c_NE', 155, 360, 155, 153);"/>
-    <tc:script onload="checkLayout('page:c_SW', 0, 517, 155, 152);"/>
-    <tc:script onload="checkLayout('page:c_SE', 155, 517, 155, 153);"/>
+    <tc:script onload="checkLayout('page:c_SW', 0, 518, 155, 152);"/>
+    <tc:script onload="checkLayout('page:c_SE', 155, 518, 155, 152);"/>
 
-    <!--todo: test the other controls ...-->
+    <tc:script onload="checkLayout('page:d_NW', 360, 360, 153, 155);"/>
+    <tc:script onload="checkLayout('page:d_NE', 518, 360, 152, 155);"/>
+    <tc:script onload="checkLayout('page:d_SW', 360, 515, 153, 155);"/>
+    <tc:script onload="checkLayout('page:d_SE', 518, 515, 152, 155);"/>
 
   </tc:page>
 </f:view>

Added: myfaces/tobago/trunk/example/test/src/main/webapp/tc/gridLayout/spacing-simple.xhtml
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/example/test/src/main/webapp/tc/gridLayout/spacing-simple.xhtml?rev=796295&view=auto
==============================================================================
--- myfaces/tobago/trunk/example/test/src/main/webapp/tc/gridLayout/spacing-simple.xhtml (added)
+++ myfaces/tobago/trunk/example/test/src/main/webapp/tc/gridLayout/spacing-simple.xhtml Tue Jul 21 13:44:17 2009
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<f:view
+    xmlns:jsp="http://java.sun.com/JSP/Page"
+    xmlns:tc="http://myfaces.apache.org/tobago/component"
+    xmlns:tx="http://myfaces.apache.org/tobago/extension"
+    xmlns:ui="http://java.sun.com/jsf/facelets"
+    xmlns:f="http://java.sun.com/jsf/core">
+
+  <!--
+  Test page for spacing:
+  1. spacing attributes
+  2. theme defaults
+  3. only columnSpacing
+  4. only rowSpacing
+  -->
+
+  <tc:page id="page" label="Spacing Test">
+    <tc:gridLayoutConstraint width="310px" height="310px"/>
+
+    <f:facet name="layout">
+      <tc:gridLayout columns="*;*" rows="*;*" columnSpacing="0px"/>
+    </f:facet>
+
+    <tc:in id="c_NW" value="NW"/>
+    <tc:in id="c_NE" value="NE"/>
+    <tc:in id="c_SW" value="SW"/>
+    <tc:in id="c_SE" value="SE"/>
+
+    <tc:script file="script/test-utils.js"/>
+
+    <tc:script onload="checkLayout('page:c_NW', 0, 0, 155, 153);"/>
+    <tc:script onload="checkLayout('page:c_NE', 155, 0, 155, 153);"/>
+    <tc:script onload="checkLayout('page:c_SW', 0, 158, 155, 152);"/>
+    <tc:script onload="checkLayout('page:c_SE', 155, 158, 155, 152);"/>
+
+  </tc:page>
+</f:view>