Revision: 9884
Author:   jlaba...@google.com
Date:     Wed Mar 23 11:53:24 2011
Log: Making rendered Cells in CellWidgets fill the root element of the widget so that setting the height/width of the Widget applies to the Cell. Also making TextButton implement HasText so it can be used in UiBinder files with a default string.

Review at http://gwt-code-reviews.appspot.com/1383807

Review by: p...@google.com
http://code.google.com/p/google-web-toolkit/source/detail?r=9884

Modified:
 /trunk/user/src/com/google/gwt/user/cellview/client/CellWidget.java
 /trunk/user/src/com/google/gwt/widget/client/TextButton.java
 /trunk/user/test/com/google/gwt/user/cellview/client/CellWidgetTest.java

=======================================
--- /trunk/user/src/com/google/gwt/user/cellview/client/CellWidget.java Mon Dec 13 11:55:24 2010 +++ /trunk/user/src/com/google/gwt/user/cellview/client/CellWidget.java Wed Mar 23 11:53:24 2011
@@ -20,6 +20,7 @@
 import com.google.gwt.cell.client.ValueUpdater;
 import com.google.gwt.dom.client.Document;
 import com.google.gwt.dom.client.Element;
+import com.google.gwt.dom.client.Style.Unit;
 import com.google.gwt.event.logical.shared.ValueChangeEvent;
 import com.google.gwt.event.logical.shared.ValueChangeHandler;
 import com.google.gwt.event.shared.HandlerRegistration;
@@ -36,8 +37,7 @@
  *
  * @param <C> the type that the Cell represents
  */
-public class CellWidget<C> extends Widget implements HasKeyProvider<C>,
-    HasValue<C> {
+public class CellWidget<C> extends Widget implements HasKeyProvider<C>, HasValue<C> {

   /**
* Create the default element used to wrap the Cell. The default element is a
@@ -128,8 +128,7 @@
    * @param keyProvider the key provider used to get keys from values
    * @param elem the browser element to use
    */
-  protected CellWidget(Cell<C> cell, C initialValue,
-      ProvidesKey<C> keyProvider, Element elem) {
+ protected CellWidget(Cell<C> cell, C initialValue, ProvidesKey<C> keyProvider, Element elem) {
     this.cell = cell;
     this.keyProvider = keyProvider;
     setElement(elem);
@@ -166,8 +165,7 @@
     // Forward the event to the cell.
     String eventType = event.getType();
     if (cell.getConsumedEvents().contains(eventType)) {
-      cell.onBrowserEvent(createContext(), getElement(), value, event,
-          valueUpdater);
+ cell.onBrowserEvent(createContext(), getElement(), value, event, valueUpdater);
     }
   }

@@ -178,6 +176,16 @@
     SafeHtmlBuilder sb = new SafeHtmlBuilder();
     cell.render(createContext(), value, sb);
     getElement().setInnerHTML(sb.toSafeHtml().asString());
+
+    /*
+ * The rendered Cell should fill the root element so height and width styles
+     * applied to the widget also apply to the Cell.
+     */
+    Element child = getElement().getFirstChildElement();
+    if (child != null) {
+      child.getStyle().setHeight(100, Unit.PCT);
+      child.getStyle().setWidth(100, Unit.PCT);
+    }
   }

   /**
@@ -240,7 +248,6 @@
    * @return the key
    */
   private Object getKey(C value) {
-    return (keyProvider == null || value == null) ? value
-        : keyProvider.getKey(value);
+ return (keyProvider == null || value == null) ? value : keyProvider.getKey(value);
   }
 }
=======================================
--- /trunk/user/src/com/google/gwt/widget/client/TextButton.java Wed Mar 23 08:29:27 2011 +++ /trunk/user/src/com/google/gwt/widget/client/TextButton.java Wed Mar 23 11:53:24 2011
@@ -17,11 +17,12 @@

 import com.google.gwt.cell.client.TextButtonCell;
 import com.google.gwt.cell.client.TextButtonCell.Appearance;
+import com.google.gwt.user.client.ui.HasText;

 /**
  * A button that displays text and an optional icon.
  */
-public class TextButton extends ButtonBase<String> {
+public class TextButton extends ButtonBase<String> implements HasText {

   /**
* Construct a new {@link TextButton} using the default {@link Appearance}.
@@ -71,4 +72,12 @@
   protected TextButton(TextButtonCell cell, String text) {
     super(cell, text);
   }
-}
+
+  public String getText() {
+    return getValue();
+  }
+
+  public void setText(String text) {
+    setValue(text);
+  }
+}
=======================================
--- /trunk/user/test/com/google/gwt/user/cellview/client/CellWidgetTest.java Mon Dec 13 11:55:24 2010 +++ /trunk/user/test/com/google/gwt/user/cellview/client/CellWidgetTest.java Wed Mar 23 11:53:24 2011
@@ -16,10 +16,12 @@
 package com.google.gwt.user.cellview.client;

 import com.google.gwt.cell.client.AbstractCell;
+import com.google.gwt.cell.client.TextButtonCell;
 import com.google.gwt.cell.client.ValueUpdater;
 import com.google.gwt.dom.client.Document;
 import com.google.gwt.dom.client.Element;
 import com.google.gwt.dom.client.NativeEvent;
+import com.google.gwt.dom.client.Style;
 import com.google.gwt.event.logical.shared.ValueChangeEvent;
 import com.google.gwt.event.logical.shared.ValueChangeHandler;
 import com.google.gwt.junit.client.GWTTestCase;
@@ -55,8 +57,8 @@
     }

     @Override
- public void onBrowserEvent(Context context, Element parent, String value,
-        NativeEvent event, ValueUpdater<String> valueUpdater) {
+ public void onBrowserEvent(Context context, Element parent, String value, NativeEvent event,
+        ValueUpdater<String> valueUpdater) {
       lastEventValue = value;
       lastEventKey = context.getKey();
       if (valueUpdater != null) {
@@ -77,8 +79,7 @@
    *
    * @param <C> the data type
    */
-  private static class MockValueChangeHandler<C> implements
-      ValueChangeHandler<C> {
+ private static class MockValueChangeHandler<C> implements ValueChangeHandler<C> {

     private boolean onValueChangeCalled = false;
     private C lastValue;
@@ -125,8 +126,7 @@
       }
     };
     CustomCell cell = new CustomCell();
-    final CellWidget<String> cw = new CellWidget<String>(cell, "test",
-        keyProvider);
+ final CellWidget<String> cw = new CellWidget<String>(cell, "test", keyProvider);
     assertEquals("test", cw.getValue());
     assertEquals(keyProvider, cw.getKeyProvider());

@@ -153,7 +153,28 @@
     handler.assertLastValue("newValue");
   }

-  public void testRedraw() {
+  /**
+   * Test that a cell that defines an HTML elment can be rendered.
+   */
+  public void testRedrawWithInnerChild() {
+    CellWidget<String> cw = new CellWidget<String>(new TextButtonCell());
+
+    // Set value without redrawing.
+    cw.setValue("test123", false, false);
+    assertEquals("", cw.getElement().getInnerText());
+
+    // Redraw.
+    cw.redraw();
+    assertTrue(cw.getElement().getInnerText().contains("test123"));
+ Style firstChildStyle = cw.getElement().getFirstChildElement().getStyle();
+    assertTrue(firstChildStyle.getHeight().matches("100(.0)?%"));
+    assertTrue(firstChildStyle.getWidth().matches("100(.0)?%"));
+  }
+
+  /**
+   * Test that a cell that defines no HTML elments can be rendered.
+   */
+  public void testRedrawWithoutInnerChild() {
     CellWidget<String> cw = new CellWidget<String>(new CustomCell());

     // Set value without redrawing.

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

Reply via email to