Revision: 6349
Author: [email protected]
Date: Mon Oct 12 13:35:17 2009
Log: Adds the ability to position child elements within their layers.
Review: http://gwt-code-reviews.appspot.com/78809
http://code.google.com/p/google-web-toolkit/source/detail?r=6349

Modified:
  /trunk/user/src/com/google/gwt/layout/client/Layout.java
  /trunk/user/src/com/google/gwt/layout/client/LayoutImpl.java
  /trunk/user/src/com/google/gwt/layout/client/LayoutImplIE6.java
  /trunk/user/test/com/google/gwt/layout/client/LayoutTest.java

=======================================
--- /trunk/user/src/com/google/gwt/layout/client/Layout.java    Thu Sep  3  
12:28:14 2009
+++ /trunk/user/src/com/google/gwt/layout/client/Layout.java    Mon Oct 12  
13:35:17 2009
@@ -76,6 +76,27 @@
   * </p>
   */
  public class Layout {
+
+  /**
+   * Used to specify the alignment of child elements within a layer.
+   */
+  public enum Alignment {
+
+    /**
+     * Positions an element at the beginning of a given axis.
+     */
+    BEGIN,
+
+    /**
+     * Positions an element at the beginning of a given axis.
+     */
+    END,
+
+    /**
+     * Stretches an element to fill the layer on a given axis.
+     */
+    STRETCH;
+  }

    /**
     * Callback interface used by {...@link Layout#layout(int,  
AnimationCallback)}
@@ -123,7 +144,6 @@
     */
    public class Layer {
      final Element container, child;
-
      Object userObject;

      boolean setLeft, setRight, setTop, setBottom, setWidth, setHeight;
@@ -138,6 +158,8 @@
      double targetLeft, targetTop, targetRight, targetBottom, targetWidth,
          targetHeight;

+    Alignment hPos = Alignment.STRETCH, vPos = Alignment.STRETCH;
+
      Layer(Element container, Element child, Object userObject) {
        this.container = container;
        this.child = child;
@@ -185,6 +207,24 @@
        this.targetBottomUnit = bottomUnit;
        this.targetHeightUnit = heightUnit;
      }
+
+    /**
+     * Sets the child element's horizontal position within the layer.
+     *
+     * @param position
+     */
+    public void setChildHorizontalPosition(Alignment position) {
+      this.hPos = position;
+    }
+
+    /**
+     * Sets the child element's vertical position within the layer.
+     *
+     * @param position
+     */
+    public void setChildVerticalPosition(Alignment position) {
+      this.vPos = position;
+    }

      /**
       * Sets the layer's left and right values.
=======================================
--- /trunk/user/src/com/google/gwt/layout/client/LayoutImpl.java        Tue Oct 
 6  
14:10:56 2009
+++ /trunk/user/src/com/google/gwt/layout/client/LayoutImpl.java        Mon Oct 
12  
13:35:17 2009
@@ -143,6 +143,35 @@
          ? (layer.width + layer.widthUnit.getType()) : "");
      style.setProperty("height", layer.setHeight
          ? (layer.height + layer.heightUnit.getType()) : "");
+
+    style = layer.child.getStyle();
+    switch (layer.hPos) {
+      case BEGIN:
+        style.clearLeft();
+        break;
+      case END:
+        style.clearLeft();
+        style.setRight(0, Unit.PX);
+        break;
+      case STRETCH:
+        style.setLeft(0, Unit.PX);
+        style.setRight(0, Unit.PX);
+        break;
+    }
+
+    switch (layer.vPos) {
+      case BEGIN:
+        style.clearTop();
+        break;
+      case END:
+        style.clearTop();
+        style.setBottom(0, Unit.PX);
+        break;
+      case STRETCH:
+        style.setTop(0, Unit.PX);
+        style.setBottom(0, Unit.PX);
+        break;
+    }
    }

    public void onAttach(Element parent) {
=======================================
--- /trunk/user/src/com/google/gwt/layout/client/LayoutImplIE6.java     Thu  
Sep  3 12:28:14 2009
+++ /trunk/user/src/com/google/gwt/layout/client/LayoutImplIE6.java     Mon Oct 
 
12 13:35:17 2009
@@ -323,6 +323,11 @@
        var _rightUnit =  
[email protected]::rightUnit;
        var _bottomUnit =  
[email protected]::bottomUnit;

+      var _hPos = [email protected]::hPos
+                  
[email protected]::ordinal()();
+      var _vPos = [email protected]::vPos
+                  
[email protected]::ordinal()();
+
        // Apply the requested position & size values to the element's style.
        var style = container.style;
        style.left = _setLeft ? (_left +  
[email protected]::getType()()) : "";
@@ -366,16 +371,43 @@
          }
        }

-      // Resize the child to take up all of its container's width and  
height.
+      // Resize and position the child based on the layer's [hv]Pos.
        var child = container.firstChild;
         
@com.google.gwt.layout.client.LayoutImplIE6::measureDecoration(Lcom/google/gwt/dom/client/Element;)(child);
        var childDecoWidth = child.__decoWidth;
        var childDecoHeight = child.__decoHeight;
+
        if (container.offsetWidth > childDecoWidth) {
-        child.style.width = (container.offsetWidth - childDecoWidth)  
+ 'px';
+        switch (_hPos) {
+          case 0: // BEGIN
+            child.style.left = '0px';
+            child.style.width = '';
+            break;
+          case 1: // END
+            child.style.width = '';
+            child.style.left = (container.offsetWidth - childDecoWidth -  
child.offsetWidth) + 'px';
+            break;
+          case 2: // STRETCH
+            child.style.left = '0px';
+            child.style.width = (container.offsetWidth - childDecoWidth)  
+ 'px';
+            break;
+        }
        }
        if (container.offsetHeight > childDecoHeight) {
-        child.style.height = (container.offsetHeight - childDecoHeight)  
+ 'px';
+        switch (_vPos) {
+          case 0: // BEGIN
+            child.style.top = '0px';
+            child.style.height = '';
+            break;
+          case 1: // END
+            child.style.height = '';
+            child.style.top = (container.offsetHeight - childDecoHeight -  
child.offsetHeight) + 'px';
+            break;
+          case 2: // STRETCH
+            child.style.top = '0px';
+            child.style.height = (container.offsetHeight -  
childDecoHeight) + 'px';
+            break;
+        }
        }
      }
    }-*/;
=======================================
--- /trunk/user/test/com/google/gwt/layout/client/LayoutTest.java       Wed Aug 
  
5 07:51:26 2009
+++ /trunk/user/test/com/google/gwt/layout/client/LayoutTest.java       Mon Oct 
 
12 13:35:17 2009
@@ -31,6 +31,7 @@
  import com.google.gwt.dom.client.Style.Position;
  import com.google.gwt.dom.client.Style.Unit;
  import com.google.gwt.junit.client.GWTTestCase;
+import com.google.gwt.layout.client.Layout.Alignment;
  import com.google.gwt.layout.client.Layout.Layer;
  import com.google.gwt.user.client.Window;

@@ -140,6 +141,40 @@
    public void testAnimationTransitions_RBWH_LTRB_PX_PCT() {
      testAnimationTransitions_RBWH_LTRB(PX, PCT);
    }
+
+  /**
+   * Tests child alignment within a layer.
+   */
+  public void testChildAlignment() {
+    child0.getStyle().setWidth(64, PX);
+    child0.getStyle().setHeight(128, PX);
+    layer0.setTopHeight(0, PX, 128, PX);
+    layer0.setLeftWidth(0, PX, 256, PX);
+
+    layer0.setChildHorizontalPosition(Alignment.STRETCH);
+    layer0.setChildVerticalPosition(Alignment.STRETCH);
+    layout.layout();
+    assertEquals(0, child0.getOffsetLeft());
+    assertEquals(0, child1.getOffsetTop());
+    assertEquals(128, child0.getOffsetWidth());
+    assertEquals(256, child1.getOffsetHeight());
+
+    layer0.setChildHorizontalPosition(Alignment.BEGIN);
+    layer0.setChildVerticalPosition(Alignment.BEGIN);
+    layout.layout();
+    assertEquals(0, child0.getOffsetLeft());
+    assertEquals(0, child1.getOffsetTop());
+    assertEquals(64, child0.getOffsetWidth());
+    assertEquals(128, child1.getOffsetHeight());
+
+    layer0.setChildHorizontalPosition(Alignment.END);
+    layer0.setChildVerticalPosition(Alignment.END);
+    layout.layout();
+    assertEquals(64, child0.getOffsetLeft());
+    assertEquals(128, child1.getOffsetTop());
+    assertEquals(64, child0.getOffsetWidth());
+    assertEquals(128, child1.getOffsetHeight());
+  }

    /**
     * Test that fillParent() works properly when the outer div is a child of

--~--~---------~--~----~------------~-------~--~----~
http://groups.google.com/group/Google-Web-Toolkit-Contributors
-~----------~----~----~----~------~----~------~--~---

Reply via email to