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 2010/11/05 22:36:33 UTC

svn commit: r1031815 - in /myfaces/tobago/trunk: tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/ tobago-core/src/main/java/org/apache/myfaces/tobago/internal/layout/ tobago-theme/tobago-theme-scarborough/src/main/java/org/apache...

Author: lofwyr
Date: Fri Nov  5 21:36:32 2010
New Revision: 1031815

URL: http://svn.apache.org/viewvc?rev=1031815&view=rev
Log:
TOBAGO-606: Layout Manager
  - make it possible to have "*" inside of "auto"
  - less logging

Modified:
    myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUIGridLayout.java
    myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/layout/BankHead.java
    myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/layout/Interval.java
    myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/layout/LayoutContext.java
    myfaces/tobago/trunk/tobago-theme/tobago-theme-scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/PageRenderer.java

Modified: myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUIGridLayout.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUIGridLayout.java?rev=1031815&r1=1031814&r2=1031815&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUIGridLayout.java (original)
+++ myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUIGridLayout.java Fri Nov  5 21:36:32 2010
@@ -17,6 +17,7 @@ package org.apache.myfaces.tobago.intern
  * limitations under the License.
  */
 
+import org.apache.commons.lang.StringUtils;
 import org.apache.myfaces.tobago.component.SupportsMarkup;
 import org.apache.myfaces.tobago.internal.layout.BankHead;
 import org.apache.myfaces.tobago.internal.layout.Cell;
@@ -41,7 +42,6 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import javax.faces.context.FacesContext;
-import java.util.Arrays;
 import java.util.List;
 
 public abstract class AbstractUIGridLayout extends AbstractUILayoutBase implements LayoutManager, SupportsMarkup {
@@ -75,6 +75,7 @@ public abstract class AbstractUIGridLayo
     BankHead[] heads = grid.getBankHeads(orientation);
     BankHead[] heads2 = grid.getBankHeads(orientation.other());
 
+/*
     if (auto) {
       for (int i = 0; i < heads.length; i++) {
         if (heads[i].getToken() instanceof RelativeLayoutToken) {
@@ -87,6 +88,7 @@ public abstract class AbstractUIGridLayo
         }
       }
     }
+*/
 
     for (int i = 0; i < heads.length; i++) {
       boolean neitherRendered = true;
@@ -154,51 +156,59 @@ public abstract class AbstractUIGridLayo
         }
       }
 
-      if (intervalList.size() >= 1) {
-        intervalList.evaluate();
-      }
+      intervalList.evaluate();
       if (token instanceof AutoLayoutToken || token instanceof RelativeLayoutToken) {
-        if (intervalList.size() >= 1) {
-          heads[i].setMinimum(intervalList.getMinimum());
-        }
+        heads[i].setIntervalList(intervalList);
       }
       if (token instanceof AutoLayoutToken) {
-        if (intervalList.size() >= 1) {
-          heads[i].setCurrent(intervalList.getCurrent());
-        } else {
-          heads[i].setCurrent(Measure.ZERO);
-// todo: what when we cannot find a good value for "auto"?
-        }
+        heads[i].setCurrent(intervalList.getCurrent());
       }
       i++;
     }
 
+    IntervalList relatives = new IntervalList();
+    for (BankHead head : heads) {
+      LayoutToken token = head.getToken();
+      if (token instanceof RelativeLayoutToken) {
+        final int factor = ((RelativeLayoutToken) token).getFactor();
+        for (Interval interval : head.getIntervalList()) {
+          relatives.add(new Interval(interval, factor));
+        }
+      }
+    }
+    relatives.evaluate();
+
     // set the size if all sizes of the grid are set
     Measure sum = Measure.ZERO;
     for (BankHead head : heads) {
-      Measure size = head.getCurrent();
+      Measure size;
+      final LayoutToken token = head.getToken();
+      if (token instanceof RelativeLayoutToken) {
+        final int factor = ((RelativeLayoutToken) token).getFactor();
+        size = relatives.getCurrent().multiply(factor);
+      } else {
+        size = head.getCurrent();
+      }
       if (size == null) {
-        sum = null; // set to invalid
-        break;
+        LOG.error("May not happen!", new Exception());
       }
       sum = sum.add(size);
     }
-    if (sum != null) {
-      // adding the space between the cells
-      sum = sum.add(LayoutUtils.getOffsetBegin(orientation, getLayoutContainer()));
-      sum = sum.add(getMarginBegin(orientation));
-      sum = sum.add(computeSpacing(orientation, 0, heads.length));
-      sum = sum.add(getMarginEnd(orientation));
-      sum = sum.add(LayoutUtils.getOffsetEnd(orientation, getLayoutContainer()));
-      LayoutUtils.setCurrentSize(orientation, getLayoutContainer(), sum);
-    }
+
+    // adding the space between the cells
+    sum = sum.add(LayoutUtils.getOffsetBegin(orientation, getLayoutContainer()));
+    sum = sum.add(getMarginBegin(orientation));
+    sum = sum.add(computeSpacing(orientation, 0, heads.length));
+    sum = sum.add(getMarginEnd(orientation));
+    sum = sum.add(LayoutUtils.getOffsetEnd(orientation, getLayoutContainer()));
+    LayoutUtils.setCurrentSize(orientation, getLayoutContainer(), sum);
   }
 
   public void mainProcessing(Orientation orientation) {
 
     final BankHead[] heads = grid.getBankHeads(orientation);
     final BankHead[] heads2 = grid.getBankHeads(orientation.other());
-    
+
     // find *
     FactorList factorList = new FactorList();
     for (BankHead head : heads) {
@@ -228,7 +238,7 @@ public abstract class AbstractUIGridLayo
         for (BankHead head : heads) {
           if (head.getToken() instanceof RelativeLayoutToken && head.isRendered()) {
             // respect the minimum
-            heads[i].setCurrent(Measure.max(partition.get(j), heads[i].getMinimum()));
+            heads[i].setCurrent(Measure.max(partition.get(j), heads[i].getIntervalList().getMinimum()));
             j++;
           }
           i++;
@@ -270,7 +280,7 @@ public abstract class AbstractUIGridLayo
 
     final BankHead[] heads = grid.getBankHeads(orientation);
     final BankHead[] heads2 = grid.getBankHeads(orientation.other());
-    
+
     // call manage sizes for all sub-layout-managers
     for (int i = 0; i < heads.length; i++) {
       for (int j = 0; j < heads2.length; j++) {
@@ -374,14 +384,39 @@ public abstract class AbstractUIGridLayo
     return false;
   }
 
+  public String toString(int depth) {
+    StringBuilder builder = new StringBuilder();
+    builder.append(getClass().getSimpleName()).append("#");
+    builder.append(getClientId(FacesContext.getCurrentInstance()));
+    builder.append("\n");
+    if (grid != null) {
+      builder.append(StringUtils.repeat("  ", depth + 4));
+      builder.append("horiz.: ");
+      BankHead[] heads = grid.getBankHeads(Orientation.HORIZONTAL);
+      for (int i = 0; i < heads.length; i++) {
+        if (i != 0) {
+          builder.append(StringUtils.repeat("  ", depth + 4 + 4));
+        }
+        builder.append(heads[i]);
+        builder.append("\n");
+      }
+      builder.append(StringUtils.repeat("  ", depth + 4));
+      builder.append("verti.: ");
+      heads = grid.getBankHeads(Orientation.VERTICAL);
+      for (int i = 0; i < heads.length; i++) {
+        if (i != 0) {
+          builder.append(StringUtils.repeat("  ", depth + 4 + 4));
+        }
+        builder.append(heads[i]);
+        builder.append("\n");
+      }
+    }
+    builder.setLength(builder.length() - 1);
+    return builder.toString();
+  }
+
   @Override
   public String toString() {
-    return getClass().getSimpleName()
-        + "#"
-        + getClientId(FacesContext.getCurrentInstance())
-        + (grid != null
-        ? "(" + Arrays.toString(grid.getBankHeads(Orientation.HORIZONTAL)) 
-        + ", " + Arrays.toString(grid.getBankHeads(Orientation.VERTICAL)) + ")"
-        : "");
+    return toString(0);
   }
 }

Modified: myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/layout/BankHead.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/layout/BankHead.java?rev=1031815&r1=1031814&r2=1031815&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/layout/BankHead.java (original)
+++ myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/layout/BankHead.java Fri Nov  5 21:36:32 2010
@@ -27,8 +27,11 @@ import org.apache.myfaces.tobago.layout.
 public class BankHead {
 
   private LayoutToken token;
-  private Measure minimum;
+
+  private IntervalList intervalList;
+
   private Measure current;
+
   private boolean rendered;
 
   public BankHead(LayoutToken token) {
@@ -44,12 +47,12 @@ public class BankHead {
     this.token = token;
   }
 
-  public Measure getMinimum() {
-    return minimum;
+  public IntervalList getIntervalList() {
+    return intervalList;
   }
 
-  public void setMinimum(Measure minimum) {
-    this.minimum = minimum;
+  public void setIntervalList(IntervalList intervalList) {
+    this.intervalList = intervalList;
   }
 
   public Measure getCurrent() {
@@ -72,7 +75,7 @@ public class BankHead {
   public String toString() {
     return "BankHead{"
         + token
-        + "," + minimum
+        + "," + intervalList
         + "," + current
         + "," + rendered
         + '}';

Modified: myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/layout/Interval.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/layout/Interval.java?rev=1031815&r1=1031814&r2=1031815&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/layout/Interval.java (original)
+++ myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/layout/Interval.java Fri Nov  5 21:36:32 2010
@@ -59,6 +59,15 @@ public class Interval {
     this.current = current;
   }
 
+  // XXX what about rounding??? may use multiply instead of divide
+  public Interval(Interval interval, int divider) {
+    this.minimum = interval.minimum != null
+        ? interval.minimum.divide(divider) : null; // XXX may add one for rounding up.
+    this.preferred = interval.preferred != null ? interval.preferred.divide(divider) : null;
+    this.maximum = interval.maximum != null ? interval.maximum.divide(divider) : null;
+    this.current = interval.current != null ? interval.current.divide(divider) : null;
+  }
+
   public Measure getMinimum() {
     return minimum;
   }

Modified: myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/layout/LayoutContext.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/layout/LayoutContext.java?rev=1031815&r1=1031814&r2=1031815&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/layout/LayoutContext.java (original)
+++ myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/layout/LayoutContext.java Fri Nov  5 21:36:32 2010
@@ -17,16 +17,18 @@ package org.apache.myfaces.tobago.intern
  * limitations under the License.
  */
 
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.apache.commons.lang.StringUtils;
 import org.apache.myfaces.tobago.internal.component.AbstractUIGridLayout;
 import org.apache.myfaces.tobago.layout.LayoutBase;
 import org.apache.myfaces.tobago.layout.LayoutContainer;
 import org.apache.myfaces.tobago.layout.LayoutManager;
 import org.apache.myfaces.tobago.layout.Orientation;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import javax.faces.component.UIComponent;
 import javax.faces.context.FacesContext;
+import java.text.DecimalFormat;
 
 /*
 An algorithm for laying out ...
@@ -64,6 +66,11 @@ public class LayoutContext {
 
   public void layout() {
 
+    long begin = 0;
+    if (LOG.isDebugEnabled()) {
+      begin = System.nanoTime();
+    }
+
     LayoutManager layoutManager = container.getLayoutManager();
     layoutManager.init();
     layoutManager.fixRelativeInsideAuto(Orientation.HORIZONTAL, false);
@@ -75,6 +82,10 @@ public class LayoutContext {
     layoutManager.postProcessing(Orientation.HORIZONTAL);
     layoutManager.postProcessing(Orientation.VERTICAL);
 
+    if (LOG.isDebugEnabled()) {
+        LOG.debug("Laying out takes: {} ns", new DecimalFormat("#,##0").format(System.nanoTime() - begin));
+    }
+
     log();
   }
 
@@ -88,9 +99,7 @@ public class LayoutContext {
 
   private void log(StringBuffer buffer, UIComponent component, int depth) {
     FacesContext facesContext = FacesContext.getCurrentInstance();
-    for (int i = 0; i < depth; i++) {
-      buffer.append("  ");
-    }
+    buffer.append(StringUtils.repeat("  ", depth));
     buffer.append(component.getClass().getSimpleName());
     buffer.append("#");
     buffer.append(component.getClientId(facesContext));
@@ -104,8 +113,10 @@ public class LayoutContext {
     if (component instanceof LayoutContainer) {
       LayoutManager layoutManager = ((LayoutContainer) component).getLayoutManager();
       if (layoutManager instanceof AbstractUIGridLayout) {
-        buffer.append(" ");
-        buffer.append(layoutManager.toString());
+        buffer.append("\n");
+        buffer.append(StringUtils.repeat("  ", depth + 4));
+        buffer.append("layout: ");
+        buffer.append(((AbstractUIGridLayout) layoutManager).toString(depth));
       }
     }
     buffer.append("\n");

Modified: myfaces/tobago/trunk/tobago-theme/tobago-theme-scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/PageRenderer.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-theme/tobago-theme-scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/PageRenderer.java?rev=1031815&r1=1031814&r2=1031815&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-theme/tobago-theme-scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/PageRenderer.java (original)
+++ myfaces/tobago/trunk/tobago-theme/tobago-theme-scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/PageRenderer.java Fri Nov  5 21:36:32 2010
@@ -63,7 +63,6 @@ import javax.faces.context.ExternalConte
 import javax.faces.context.FacesContext;
 import javax.faces.context.ResponseWriter;
 import java.io.IOException;
-import java.text.DecimalFormat;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Iterator;
@@ -135,20 +134,10 @@ public class PageRenderer extends PageRe
       facesContext = new TobagoFacesContext(facesContextOrg);
     }
 
-// LAYOUT Begin
     boolean developmentMode = TobagoConfig.getInstance(facesContext).getProjectStage() == ProjectStage.Development;
 
-    long begin = 0;
-    if (developmentMode) {
-      begin = System.nanoTime();
-    }
     LayoutContext layoutContext = new LayoutContext(page);
     layoutContext.layout();
-    if (developmentMode) {
-        LOG.info("Laying out takes: {} ns", new DecimalFormat("#,##0").format(System.nanoTime() - begin));
-    }
-
-// LAYOUT End
 
     RenderUtils.prepareRendererAll(facesContext, page);