Revision: 9773
Author: [email protected]
Date: Sun Feb 27 23:58:39 2011
Log: Enhancing GWT's CheckBox and RadioButton with bidi support.

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

http://code.google.com/p/google-web-toolkit/source/detail?r=9773

Modified:
 /trunk/user/src/com/google/gwt/user/client/ui/CheckBox.java
 /trunk/user/src/com/google/gwt/user/client/ui/RadioButton.java

=======================================
--- /trunk/user/src/com/google/gwt/user/client/ui/CheckBox.java Mon Nov 29 10:45:36 2010 +++ /trunk/user/src/com/google/gwt/user/client/ui/CheckBox.java Sun Feb 27 23:58:39 2011
@@ -26,6 +26,9 @@
 import com.google.gwt.event.logical.shared.ValueChangeEvent;
 import com.google.gwt.event.logical.shared.ValueChangeHandler;
 import com.google.gwt.event.shared.HandlerRegistration;
+import com.google.gwt.i18n.client.HasDirection.Direction;
+import com.google.gwt.i18n.shared.DirectionEstimator;
+import com.google.gwt.i18n.shared.HasDirectionEstimator;
 import com.google.gwt.safehtml.shared.SafeHtml;
 import com.google.gwt.user.client.DOM;
 import com.google.gwt.user.client.Element;
@@ -56,7 +59,13 @@
  * </p>
  */
public class CheckBox extends ButtonBase implements HasName, HasValue<Boolean>,
-    HasWordWrap, IsEditor<LeafValueEditor<Boolean>> {
+    HasWordWrap, HasDirectionalSafeHtml, HasDirectionEstimator,
+    IsEditor<LeafValueEditor<Boolean>> {
+
+  public static final DirectionEstimator DEFAULT_DIRECTION_ESTIMATOR =
+    DirectionalTextHelper.DEFAULT_DIRECTION_ESTIMATOR;
+
+  final DirectionalTextHelper directionalTextHelper;
   InputElement inputElem;
   LabelElement labelElem;
   private LeafValueEditor<Boolean> editor;
@@ -83,11 +92,63 @@
    * Creates a check box with the specified text label.
    *
    * @param label the check box's label
+ * @param dir the text's direction. Note that {@code DEFAULT} means direction
+   *          should be inherited from the widget's parent element.
+   */
+  public CheckBox(SafeHtml label, Direction dir) {
+    this();
+    setHTML(label, dir);
+  }
+
+  /**
+   * Creates a check box with the specified text label.
+   *
+   * @param label the check box's label
+ * @param directionEstimator A DirectionEstimator object used for automatic
+   *          direction adjustment. For convenience,
+   *          {@link #DEFAULT_DIRECTION_ESTIMATOR} can be used.
+   */
+  public CheckBox(SafeHtml label, DirectionEstimator directionEstimator) {
+    this();
+    setDirectionEstimator(directionEstimator);
+    setHTML(label.asString());
+  }
+
+  /**
+   * Creates a check box with the specified text label.
+   *
+   * @param label the check box's label
    */
   public CheckBox(String label) {
     this();
     setText(label);
   }
+
+  /**
+   * Creates a check box with the specified text label.
+   *
+   * @param label the check box's label
+ * @param dir the text's direction. Note that {@code DEFAULT} means direction
+   *          should be inherited from the widget's parent element.
+   */
+  public CheckBox(String label, Direction dir) {
+    this();
+    setText(label, dir);
+  }
+
+  /**
+ * Creates a label with the specified text and a default direction estimator.
+   *
+   * @param label the check box's label
+ * @param directionEstimator A DirectionEstimator object used for automatic
+   *          direction adjustment. For convenience,
+   *          {@link #DEFAULT_DIRECTION_ESTIMATOR} can be used.
+   */
+  public CheckBox(String label, DirectionEstimator directionEstimator) {
+    this();
+    setDirectionEstimator(directionEstimator);
+    setText(label);
+  }

   /**
    * Creates a check box with the specified text label.
@@ -106,6 +167,7 @@

   protected CheckBox(Element elem) {
     super(DOM.createSpan());
+
     inputElem = InputElement.as(elem);
     labelElem = Document.get().createLabelElement();

@@ -116,6 +178,8 @@
     inputElem.setPropertyString("id", uid);
     labelElem.setHtmlFor(uid);

+    directionalTextHelper = new DirectionalTextHelper(labelElem, true);
+
// Accessibility: setting tab index to be 0 by default, ensuring element
     // appears in tab sequence. FocusWidget's setElement method already
     // calls setTabIndex, which is overridden below. However, at the time
@@ -140,6 +204,10 @@
     }
     return editor;
   }
+
+  public DirectionEstimator getDirectionEstimator() {
+    return directionalTextHelper.getDirectionEstimator();
+  }

   /**
* Returns the value property of the input element that backs this widget.
@@ -156,7 +224,7 @@

   @Override
   public String getHTML() {
-    return labelElem.getInnerHTML();
+    return directionalTextHelper.getTextOrHtml(true);
   }

   public String getName() {
@@ -170,7 +238,11 @@

   @Override
   public String getText() {
-    return labelElem.getInnerText();
+    return directionalTextHelper.getTextOrHtml(false);
+  }
+
+  public Direction getTextDirection() {
+    return directionalTextHelper.getTextDirection();
   }

   /**
@@ -228,6 +300,28 @@
   public void setChecked(boolean checked) {
     setValue(checked);
   }
+
+  /**
+   * {@inheritDoc}
+   * <p>
+   * See note at {@link #setDirectionEstimator(DirectionEstimator)}.
+   */
+  public void setDirectionEstimator(boolean enabled) {
+    directionalTextHelper.setDirectionEstimator(enabled);
+  }
+
+  /**
+   * {@inheritDoc}
+   * <p>
+ * Note: DirectionEstimator should be set before the label has any content;
+   * it's highly recommended to set it using a constructor. Reason: if the
+   * label already has non-empty content, this will update its direction
+ * according to the new estimator's result. This may cause flicker, and thus
+   * should be avoided.
+   */
+ public void setDirectionEstimator(DirectionEstimator directionEstimator) {
+    directionalTextHelper.setDirectionEstimator(directionEstimator);
+  }

   @Override
   public void setEnabled(boolean enabled) {
@@ -262,10 +356,14 @@
   public void setFormValue(String value) {
     inputElem.setAttribute("value", value);
   }
+
+  public void setHTML(SafeHtml html, Direction dir) {
+    directionalTextHelper.setTextOrHtml(html.asString(), dir, true);
+  }

   @Override
   public void setHTML(String html) {
-    labelElem.setInnerHTML(html);
+    directionalTextHelper.setTextOrHtml(html, true);
   }

   public void setName(String name) {
@@ -285,7 +383,11 @@

   @Override
   public void setText(String text) {
-    labelElem.setInnerText(text);
+    directionalTextHelper.setTextOrHtml(text, false);
+  }
+
+  public void setText(String text, Direction dir) {
+    directionalTextHelper.setTextOrHtml(text, dir, false);
   }

   /**
=======================================
--- /trunk/user/src/com/google/gwt/user/client/ui/RadioButton.java Tue Sep 21 07:53:19 2010 +++ /trunk/user/src/com/google/gwt/user/client/ui/RadioButton.java Sun Feb 27 23:58:39 2011
@@ -18,6 +18,8 @@
 import com.google.gwt.dom.client.Element;
 import com.google.gwt.dom.client.EventTarget;
 import com.google.gwt.event.logical.shared.ValueChangeEvent;
+import com.google.gwt.i18n.client.HasDirection.Direction;
+import com.google.gwt.i18n.shared.DirectionEstimator;
 import com.google.gwt.safehtml.shared.SafeHtml;
 import com.google.gwt.uibinder.client.UiConstructor;
 import com.google.gwt.user.client.DOM;
@@ -47,6 +49,9 @@
  */
 public class RadioButton extends CheckBox {

+  public static final DirectionEstimator DEFAULT_DIRECTION_ESTIMATOR =
+    DirectionalTextHelper.DEFAULT_DIRECTION_ESTIMATOR;
+
   private Boolean oldValue;

   /**
@@ -63,7 +68,7 @@
   public RadioButton(String name) {
     super(DOM.createInputRadio(name));
     setStyleName("gwt-RadioButton");
-
+
     sinkEvents(Event.ONCLICK);
     sinkEvents(Event.ONMOUSEUP);
     sinkEvents(Event.ONBLUR);
@@ -84,6 +89,35 @@
   public RadioButton(String name, SafeHtml label) {
     this(name, label.asString(), true);
   }
+
+  /**
+   * @see #RadioButton(String, SafeHtml)
+   *
+   * @param name the group name with which to associate the radio button
+   * @param label this radio button's html label
+ * @param dir the text's direction. Note that {@code DEFAULT} means direction
+   *          should be inherited from the widget's parent element.
+   */
+  public RadioButton(String name, SafeHtml label, Direction dir) {
+    this(name);
+    setHTML(label, dir);
+  }
+
+  /**
+   * @see #RadioButton(String, SafeHtml)
+   *
+   * @param name the group name with which to associate the radio button
+   * @param label this radio button's html label
+ * @param directionEstimator A DirectionEstimator object used for automatic
+   *          direction adjustment. For convenience,
+   *          {@link #DEFAULT_DIRECTION_ESTIMATOR} can be used.
+   */
+  public RadioButton(String name, SafeHtml label,
+      DirectionEstimator directionEstimator) {
+    this(name);
+    setDirectionEstimator(directionEstimator);
+    setHTML(label.asString());
+  }

   /**
* Creates a new radio associated with a particular group, and initialized
@@ -100,6 +134,35 @@
     this(name);
     setText(label);
   }
+
+  /**
+   * @see #RadioButton(String, SafeHtml)
+   *
+   * @param name the group name with which to associate the radio button
+   * @param label this radio button's label
+ * @param dir the text's direction. Note that {@code DEFAULT} means direction
+   *          should be inherited from the widget's parent element.
+   */
+  public RadioButton(String name, String label, Direction dir) {
+    this(name);
+    setText(label, dir);
+  }
+
+  /**
+   * @see #RadioButton(String, SafeHtml)
+   *
+   * @param name the group name with which to associate the radio button
+   * @param label this radio button's label
+ * @param directionEstimator A DirectionEstimator object used for automatic
+   *          direction adjustment. For convenience,
+   *          {@link #DEFAULT_DIRECTION_ESTIMATOR} can be used.
+   */
+  public RadioButton(String name, String label,
+      DirectionEstimator directionEstimator) {
+    this(name);
+    setDirectionEstimator(directionEstimator);
+    setText(label);
+  }

   /**
    * Creates a new radio button associated with a particular group, and

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

Reply via email to