Revision: 8097
Author: [email protected]
Date: Tue May 11 16:51:18 2010
Log: Introduces Parsers and ValueBox<T>, by refactoring ValueBoxBase<T> out of TextBoxBase.

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

Review by: [email protected]
http://code.google.com/p/google-web-toolkit/source/detail?r=8097

Added:
 /branches/2.1/bikeshed/src/com/google/gwt/app/util/BooleanParser.java
 /branches/2.1/bikeshed/src/com/google/gwt/app/util/BooleanRenderer.java
 /branches/2.1/bikeshed/src/com/google/gwt/app/util/DoubleParser.java
 /branches/2.1/bikeshed/src/com/google/gwt/app/util/DoubleRenderer.java
 /branches/2.1/bikeshed/src/com/google/gwt/app/util/IntegerParser.java
 /branches/2.1/bikeshed/src/com/google/gwt/app/util/IntegerRenderer.java
 /branches/2.1/bikeshed/src/com/google/gwt/app/util/LongParser.java
 /branches/2.1/bikeshed/src/com/google/gwt/app/util/LongRenderer.java
 /branches/2.1/bikeshed/src/com/google/gwt/app/util/ParseException.java
 /branches/2.1/bikeshed/src/com/google/gwt/app/util/Parser.java
 /branches/2.1/bikeshed/src/com/google/gwt/app/util/PassthroughParser.java
 /branches/2.1/bikeshed/src/com/google/gwt/user/client/ui/DoubleBox.java
 /branches/2.1/bikeshed/src/com/google/gwt/user/client/ui/IntegerBox.java
 /branches/2.1/bikeshed/src/com/google/gwt/user/client/ui/LongBox.java
 /branches/2.1/bikeshed/src/com/google/gwt/user/client/ui/TextBoxBase.java
 /branches/2.1/bikeshed/src/com/google/gwt/user/client/ui/ValueBox.java
 /branches/2.1/bikeshed/src/com/google/gwt/user/client/ui/ValueBoxBase.java
Modified:
 /branches/2.1/bikeshed/src/com/google/gwt/app/util/PassthroughRenderer.java

=======================================
--- /dev/null
+++ /branches/2.1/bikeshed/src/com/google/gwt/app/util/BooleanParser.java Tue May 11 16:51:18 2010
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed 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.
+ */
+package com.google.gwt.app.util;
+
+/**
+ * A no-op renderer.
+ */
+public class BooleanParser implements Parser<Boolean> {
+
+  private static BooleanParser INSTANCE;
+
+  /**
+   * @return the instance of the no-op renderer
+   */
+  public static Parser<Boolean> instance() {
+    if (INSTANCE == null) {
+      INSTANCE = new BooleanParser();
+    }
+    return INSTANCE;
+  }
+
+  protected BooleanParser() {
+  }
+
+  public Boolean parse(String object) {
+    return Boolean.valueOf(object);
+  }
+}
=======================================
--- /dev/null
+++ /branches/2.1/bikeshed/src/com/google/gwt/app/util/BooleanRenderer.java Tue May 11 16:51:18 2010
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed 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.
+ */
+package com.google.gwt.app.util;
+
+/**
+ * Renderer of Boolean values.
+ */
+public class BooleanRenderer implements Renderer<Boolean> {
+  private static BooleanRenderer INSTANCE;
+
+  /**
+   * @return the instance
+   */
+  public static Renderer<Boolean> instance() {
+    if (INSTANCE == null) {
+      INSTANCE = new BooleanRenderer();
+    }
+    return INSTANCE;
+  }
+
+  protected BooleanRenderer() {
+  }
+
+  public String render(Boolean object) {
+    return String.valueOf(object);
+  }
+}
=======================================
--- /dev/null
+++ /branches/2.1/bikeshed/src/com/google/gwt/app/util/DoubleParser.java Tue May 11 16:51:18 2010
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed 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.
+ */
+package com.google.gwt.app.util;
+
+/**
+ * A no-op renderer.
+ */
+public class DoubleParser implements Parser<Double> {
+
+  private static DoubleParser INSTANCE;
+
+  /**
+   * @return the instance of the no-op renderer
+   */
+  public static Parser<Double> instance() {
+    if (INSTANCE == null) {
+      INSTANCE = new DoubleParser();
+    }
+    return INSTANCE;
+  }
+
+  protected DoubleParser() {
+  }
+
+  public Double parse(String object) {
+    try {
+      return Double.valueOf(object);
+    } catch (NumberFormatException e) {
+      throw new ParseException(e);
+    }
+  }
+}
=======================================
--- /dev/null
+++ /branches/2.1/bikeshed/src/com/google/gwt/app/util/DoubleRenderer.java Tue May 11 16:51:18 2010
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed 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.
+ */
+package com.google.gwt.app.util;
+
+/**
+ * Renderer of Long values.
+ */
+public class DoubleRenderer implements Renderer<Double> {
+  private static DoubleRenderer INSTANCE;
+
+  /**
+   * @return the instance
+   */
+  public static Renderer<Double> instance() {
+    if (INSTANCE == null) {
+      INSTANCE = new DoubleRenderer();
+    }
+    return INSTANCE;
+  }
+
+  protected DoubleRenderer() {
+  }
+
+  public String render(Double object) {
+    return String.valueOf(object);
+  }
+}
=======================================
--- /dev/null
+++ /branches/2.1/bikeshed/src/com/google/gwt/app/util/IntegerParser.java Tue May 11 16:51:18 2010
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed 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.
+ */
+package com.google.gwt.app.util;
+
+/**
+ * A no-op renderer.
+ */
+public class IntegerParser implements Parser<Integer> {
+
+  private static IntegerParser INSTANCE;
+
+  /**
+   * @return the instance of the no-op renderer
+   */
+  public static Parser<Integer> instance() {
+    if (INSTANCE == null) {
+      INSTANCE = new IntegerParser();
+    }
+    return INSTANCE;
+  }
+
+  protected IntegerParser() {
+  }
+
+  public Integer parse(String object) {
+    try {
+      return Integer.valueOf(object);
+    } catch (NumberFormatException e) {
+      throw new ParseException(e);
+    }
+  }
+}
=======================================
--- /dev/null
+++ /branches/2.1/bikeshed/src/com/google/gwt/app/util/IntegerRenderer.java Tue May 11 16:51:18 2010
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed 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.
+ */
+package com.google.gwt.app.util;
+
+/**
+ * Renderer of Integer values.
+ */
+public class IntegerRenderer implements Renderer<Integer> {
+  private static IntegerRenderer INSTANCE;
+
+  /**
+   * @return the instance
+   */
+  public static Renderer<Integer> instance() {
+    if (INSTANCE == null) {
+      INSTANCE = new IntegerRenderer();
+    }
+    return INSTANCE;
+  }
+
+  protected IntegerRenderer() {
+  }
+
+  public String render(Integer object) {
+    return String.valueOf(object);
+  }
+}
=======================================
--- /dev/null
+++ /branches/2.1/bikeshed/src/com/google/gwt/app/util/LongParser.java Tue May 11 16:51:18 2010
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed 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.
+ */
+package com.google.gwt.app.util;
+
+/**
+ * A no-op renderer.
+ */
+public class LongParser implements Parser<Long> {
+
+  private static LongParser INSTANCE;
+
+  /**
+   * @return the instance of the no-op renderer
+   */
+  public static Parser<Long> instance() {
+    if (INSTANCE == null) {
+      INSTANCE = new LongParser();
+    }
+    return INSTANCE;
+  }
+
+  protected LongParser() {
+  }
+
+  public Long parse(String object) {
+    try {
+      return Long.valueOf(object);
+    } catch (NumberFormatException e) {
+      throw new ParseException(e);
+    }
+  }
+}
=======================================
--- /dev/null
+++ /branches/2.1/bikeshed/src/com/google/gwt/app/util/LongRenderer.java Tue May 11 16:51:18 2010
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed 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.
+ */
+package com.google.gwt.app.util;
+
+/**
+ * Renderer of Long values.
+ */
+public class LongRenderer implements Renderer<Long> {
+  private static LongRenderer INSTANCE;
+
+  /**
+   * @return the instance
+   */
+  public static Renderer<Long> instance() {
+    if (INSTANCE == null) {
+      INSTANCE = new LongRenderer();
+    }
+    return INSTANCE;
+  }
+
+  protected LongRenderer() {
+  }
+
+  public String render(Long object) {
+    return String.valueOf(object);
+  }
+}
=======================================
--- /dev/null
+++ /branches/2.1/bikeshed/src/com/google/gwt/app/util/ParseException.java Tue May 11 16:51:18 2010
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed 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.
+ */
+package com.google.gwt.app.util;
+
+/**
+ * Exception class indicating parsing errors
+ */
+public class ParseException extends RuntimeException {
+
+  private final String rawInput;
+  private final int offset;
+
+  public ParseException() {
+    this("", "", 0, null);
+  }
+
+  public ParseException(Throwable e) {
+    this("", "", 0, e);
+  }
+
+  public ParseException(String rawInput) {
+    this("", rawInput, 0, null);
+  }
+
+  public ParseException(String rawInput, int offset) {
+    this("", rawInput, offset, null);
+  }
+
+  public ParseException(String message, String rawInput, int offset) {
+    this(message, rawInput, offset, null);
+  }
+
+ public ParseException(String message, String rawInput, int offset, Throwable e) {
+    super(message, e);
+    this.rawInput = rawInput;
+    this.offset = offset;
+  }
+
+  public String getInput() {
+    return rawInput;
+  }
+
+  public int getOffset() {
+    return offset;
+  }
+}
=======================================
--- /dev/null
+++ /branches/2.1/bikeshed/src/com/google/gwt/app/util/Parser.java Tue May 11 16:51:18 2010
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed 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.
+ */
+package com.google.gwt.app.util;
+
+/**
+ * An object that can parse text and return a value.
+ *
+ * @param <T> the type to parse
+ */
+public interface Parser<T> {
+  T parse(String s) throws ParseException;
+}
=======================================
--- /dev/null
+++ /branches/2.1/bikeshed/src/com/google/gwt/app/util/PassthroughParser.java Tue May 11 16:51:18 2010
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed 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.
+ */
+package com.google.gwt.app.util;
+
+/**
+ * A no-op renderer.
+ */
+public class PassthroughParser implements Parser<String> {
+
+  private static PassthroughParser INSTANCE;
+
+  /**
+   * @return the instance of the no-op renderer
+   */
+  public static Parser<String> instance() {
+    if (INSTANCE == null) {
+      INSTANCE = new PassthroughParser();
+    }
+    return INSTANCE;
+  }
+
+  protected PassthroughParser() {
+  }
+
+  public String parse(String object) {
+    return object;
+  }
+}
=======================================
--- /dev/null
+++ /branches/2.1/bikeshed/src/com/google/gwt/user/client/ui/DoubleBox.java Tue May 11 16:51:18 2010
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed 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.
+ */
+package com.google.gwt.user.client.ui;
+
+import com.google.gwt.app.util.DoubleParser;
+import com.google.gwt.app.util.DoubleRenderer;
+import com.google.gwt.dom.client.Document;
+
+/**
+ * A ValueBox that uses {...@link DoubleParser} and {...@link DoubleRenderer}
+ */
+public class DoubleBox extends ValueBox<Double> {
+
+  public DoubleBox() {
+ super(Document.get().createTextInputElement(), DoubleRenderer.instance(),
+        DoubleParser.instance());
+  }
+}
=======================================
--- /dev/null
+++ /branches/2.1/bikeshed/src/com/google/gwt/user/client/ui/IntegerBox.java Tue May 11 16:51:18 2010
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed 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.
+ */
+package com.google.gwt.user.client.ui;
+
+import com.google.gwt.app.util.IntegerParser;
+import com.google.gwt.app.util.IntegerRenderer;
+import com.google.gwt.dom.client.Document;
+
+/**
+ * A ValueBox that uses {...@link IntegerParser} and {...@link 
IntegerRenderer}.
+ */
+public class IntegerBox extends ValueBox<Integer> {
+  public IntegerBox() {
+ super(Document.get().createTextInputElement(), IntegerRenderer.instance(),
+        IntegerParser.instance());
+  }
+}
=======================================
--- /dev/null
+++ /branches/2.1/bikeshed/src/com/google/gwt/user/client/ui/LongBox.java Tue May 11 16:51:18 2010
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed 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.
+ */
+package com.google.gwt.user.client.ui;
+
+import com.google.gwt.app.util.LongParser;
+import com.google.gwt.app.util.LongRenderer;
+import com.google.gwt.dom.client.Document;
+
+/**
+ * A ValueBox that uses {...@link LongParser} and {...@link LongRenderer}
+ */
+public class LongBox extends ValueBox<Long> {
+
+  public LongBox() {
+    super(Document.get().createTextInputElement(), LongRenderer.instance(),
+        LongParser.instance());
+  }
+}
=======================================
--- /dev/null
+++ /branches/2.1/bikeshed/src/com/google/gwt/user/client/ui/TextBoxBase.java Tue May 11 16:51:18 2010
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2009 Google Inc.
+ *
+ * Licensed 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.
+ */
+package com.google.gwt.user.client.ui;
+
+import com.google.gwt.app.util.PassthroughParser;
+import com.google.gwt.app.util.PassthroughRenderer;
+import com.google.gwt.dom.client.Element;
+
+/**
+ * Legacy abstract base class for all text entry widgets.
+ */
+public class TextBoxBase extends ValueBoxBase<String>  {
+
+  /**
+   * Text alignment constant, used in
+   * {...@link TextBoxBase#setTextAlignment(TextBoxBase.TextAlignConstant)}.
+   */
+  public static class TextAlignConstant {
+    private String textAlignString;
+
+    private TextAlignConstant(String textAlignString) {
+      this.textAlignString = textAlignString;
+    }
+
+    String getTextAlignString() {
+      return textAlignString;
+    }
+  }
+
+  /**
+   * Center the text.
+   */
+ public static final TextAlignConstant ALIGN_CENTER = new TextAlignConstant(
+      "center");
+
+  /**
+   * Justify the text.
+   */
+ public static final TextAlignConstant ALIGN_JUSTIFY = new TextAlignConstant(
+      "justify");
+
+  /**
+   * Align the text to the left edge.
+   */
+  public static final TextAlignConstant ALIGN_LEFT = new TextAlignConstant(
+      "left");
+
+  /**
+   * Align the text to the right.
+   */
+ public static final TextAlignConstant ALIGN_RIGHT = new TextAlignConstant(
+      "right");
+
+  /**
+ * Creates a text box that wraps the given browser element handle. This is
+   * only used by subclasses.
+   *
+   * @param elem the browser element to wrap
+   */
+  protected TextBoxBase(Element elem) {
+ super(elem, PassthroughRenderer.instance(), PassthroughParser.instance());
+  }
+}
=======================================
--- /dev/null
+++ /branches/2.1/bikeshed/src/com/google/gwt/user/client/ui/ValueBox.java Tue May 11 16:51:18 2010
@@ -0,0 +1,131 @@
+/*
+ * Copyright 2008 Google Inc.
+ *
+ * Licensed 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.
+ */
+package com.google.gwt.user.client.ui;
+
+import com.google.gwt.app.util.Parser;
+import com.google.gwt.app.util.Renderer;
+import com.google.gwt.dom.client.Document;
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.dom.client.InputElement;
+import com.google.gwt.i18n.client.BidiUtils;
+import com.google.gwt.i18n.client.HasDirection;
+
+/**
+ * A standard single-line text box.
+ *
+ * <p>
+ * <img class='gallery' src='doc-files/TextBox.png'/>
+ * </p>
+ *
+ * <h3>CSS Style Rules</h3>
+ * <ul class='css'>
+ * <li>.gwt-TextBox { primary style }</li>
+ * <li>.gwt-TextBox-readonly { dependent style set when the text box is
+ * read-only }</li>
+ * </ul>
+ *
+ * <p>
+ * <h3>Example</h3>
+ * {...@example com.google.gwt.examples.TextBoxExample}
+ * </p>
+ *
+ * @param <T> the value type
+ */
+public class ValueBox<T> extends ValueBoxBase<T> implements HasDirection {
+
+  /**
+ * Creates a TextBox widget that wraps an existing &lt;input type='text'&gt;
+   * element.
+   *
+ * This element must already be attached to the document. If the element is
+   * removed from the document, you must call
+   * {...@link RootPanel#detachNow(Widget)}.
+   *
+   * @param element the element to be wrapped
+   */
+ public static <T> ValueBox<T> wrap(Element element, Renderer<T> renderer, Parser<T> parser) {
+    // Assert that the element is attached.
+    assert Document.get().getBody().isOrHasChild(element);
+
+    ValueBox<T> valueBox = new ValueBox<T>(element, renderer, parser);
+
+    // Mark it attached and remember it for cleanup.
+    valueBox.onAttach();
+    RootPanel.detachOnWindowClose(valueBox);
+
+    return valueBox;
+  }
+
+  /**
+ * This constructor may be used by subclasses to explicitly use an existing
+   * element. This element must be an &lt;input&gt; element whose type is
+   * 'text'.
+   *
+   * @param element the element to be used
+   */
+ protected ValueBox(Element element, Renderer<T> renderer, Parser<T> parser) {
+    super(element, renderer, parser);
+    assert InputElement.as(element).getType().equalsIgnoreCase("text");
+  }
+
+  public Direction getDirection() {
+    return BidiUtils.getDirectionOnElement(getElement());
+  }
+
+  /**
+   * Gets the maximum allowable length of the text box.
+   *
+   * @return the maximum length, in characters
+   */
+  public int getMaxLength() {
+    return getInputElement().getMaxLength();
+  }
+
+  /**
+   * Gets the number of visible characters in the text box.
+   *
+   * @return the number of visible characters
+   */
+  public int getVisibleLength() {
+    return getInputElement().getSize();
+  }
+
+  public void setDirection(Direction direction) {
+    BidiUtils.setDirectionOnElement(getElement(), direction);
+  }
+
+  /**
+   * Sets the maximum allowable length of the text box.
+   *
+   * @param length the maximum length, in characters
+   */
+  public void setMaxLength(int length) {
+    getInputElement().setMaxLength(length);
+  }
+
+  /**
+   * Sets the number of visible characters in the text box.
+   *
+   * @param length the number of visible characters
+   */
+  public void setVisibleLength(int length) {
+    getInputElement().setSize(length);
+  }
+
+  private InputElement getInputElement() {
+    return getElement().cast();
+  }
+}
=======================================
--- /dev/null
+++ /branches/2.1/bikeshed/src/com/google/gwt/user/client/ui/ValueBoxBase.java Tue May 11 16:51:18 2010
@@ -0,0 +1,306 @@
+/*
+ * Copyright 2009 Google Inc.
+ *
+ * Licensed 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.
+ */
+package com.google.gwt.user.client.ui;
+
+import com.google.gwt.app.util.Parser;
+import com.google.gwt.app.util.Renderer;
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.event.dom.client.ChangeEvent;
+import com.google.gwt.event.dom.client.ChangeHandler;
+import com.google.gwt.event.dom.client.HasChangeHandlers;
+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.user.client.DOM;
+import com.google.gwt.user.client.Event;
+import com.google.gwt.user.client.ui.TextBoxBase.TextAlignConstant;
+import com.google.gwt.user.client.ui.impl.TextBoxImpl;
+
+/**
+ * Abstract base class for all text entry widgets.
+ *
+ * @param <T> the value type
+ */
+...@suppresswarnings("deprecation")
+public class ValueBoxBase<T> extends FocusWidget implements SourcesChangeEvents,
+    HasChangeHandlers, HasText, HasName, HasValue<T> {
+
+  private static TextBoxImpl impl = GWT.create(TextBoxImpl.class);
+
+  private final Parser<T> parser;
+  private final Renderer<T> renderer;
+
+  private Event currentEvent;
+  private boolean valueChangeHandlerInitialized;
+
+  /**
+ * Creates a text box that wraps the given browser element handle. This is
+   * only used by subclasses.
+   *
+   * @param elem the browser element to wrap
+   */
+ protected ValueBoxBase(Element elem, Renderer<T> renderer, Parser<T> parser) {
+    super(elem);
+    this.renderer = renderer;
+    this.parser = parser;
+  }
+
+  public HandlerRegistration addChangeHandler(ChangeHandler handler) {
+    return addDomHandler(handler, ChangeEvent.getType());
+  }
+
+  /**
+   * @deprecated Use {...@link #addChangeHandler} instead
+   */
+  @Deprecated
+  public void addChangeListener(ChangeListener listener) {
+    addChangeHandler(new ListenerWrapper.WrappedChangeListener(listener));
+  }
+
+  public HandlerRegistration addValueChangeHandler(
+      ValueChangeHandler<T> handler) {
+    // Initialization code
+    if (!valueChangeHandlerInitialized) {
+      valueChangeHandlerInitialized = true;
+      addChangeHandler(new ChangeHandler() {
+        public void onChange(ChangeEvent event) {
+          ValueChangeEvent.fire(ValueBoxBase.this, getValue());
+        }
+      });
+    }
+    return addHandler(handler, ValueChangeEvent.getType());
+  }
+
+  /**
+ * If a keyboard event is currently being handled on this text box, calling
+   * this method will suppress it. This allows listeners to easily filter
+   * keyboard input.
+   */
+  public void cancelKey() {
+    if (currentEvent != null) {
+      DOM.eventPreventDefault(currentEvent);
+    }
+  }
+
+  /**
+ * Gets the current position of the cursor (this also serves as the beginning
+   * of the text selection).
+   *
+   * @return the cursor's position
+   */
+  public int getCursorPos() {
+    return impl.getCursorPos(getElement());
+  }
+
+  public String getName() {
+    return DOM.getElementProperty(getElement(), "name");
+  }
+
+  /**
+   * Gets the text currently selected within this text box.
+   *
+   * @return the selected text, or an empty string if none is selected
+   */
+  public String getSelectedText() {
+    int start = getCursorPos();
+    if (start < 0) {
+      return "";
+    }
+    int length = getSelectionLength();
+    return getText().substring(start, start + length);
+  }
+
+  /**
+   * Gets the length of the current text selection.
+   *
+   * @return the text selection length
+   */
+  public int getSelectionLength() {
+    return impl.getSelectionLength(getElement());
+  }
+
+  public String getText() {
+    return DOM.getElementProperty(getElement(), "value");
+  }
+
+  public T getValue() {
+    return parser.parse(getText());
+  }
+
+  /**
+   * Determines whether or not the widget is read-only.
+   *
+   * @return <code>true</code> if the widget is currently read-only,
+   *         <code>false</code> if the widget is currently editable
+   */
+  public boolean isReadOnly() {
+    return DOM.getElementPropertyBoolean(getElement(), "readOnly");
+  }
+
+  @Override
+  public void onBrowserEvent(Event event) {
+    int type = DOM.eventGetType(event);
+    if ((type & Event.KEYEVENTS) != 0) {
+ // Fire the keyboard event. Hang on to the current event object so that
+      // cancelKey() and setKey() can be implemented.
+      currentEvent = event;
+      // Call the superclass' onBrowserEvent as that includes the key event
+      // handlers.
+      super.onBrowserEvent(event);
+      currentEvent = null;
+    } else {
+      // Handles Focus and Click events.
+      super.onBrowserEvent(event);
+    }
+  }
+
+  /**
+ * @deprecated Use the {...@link HandlerRegistration#removeHandler} method on
+   * the object returned by {...@link #addChangeHandler} instead
+   */
+  @Deprecated
+  public void removeChangeListener(ChangeListener listener) {
+    ListenerWrapper.WrappedChangeListener.remove(this, listener);
+  }
+
+  /**
+   * Selects all of the text in the box.
+   *
+ * This will only work when the widget is attached to the document and not
+   * hidden.
+   */
+  public void selectAll() {
+    int length = getText().length();
+    if (length > 0) {
+      setSelectionRange(0, length);
+    }
+  }
+
+  /**
+   * Sets the cursor position.
+   *
+ * This will only work when the widget is attached to the document and not
+   * hidden.
+   *
+   * @param pos the new cursor position
+   */
+  public void setCursorPos(int pos) {
+    setSelectionRange(pos, 0);
+  }
+
+  /**
+ * If a keyboard event is currently being handled by the text box, this method + * replaces the unicode character or key code associated with it. This allows
+   * listeners to easily filter keyboard input.
+   *
+   * @param key the new key value
+ * @deprecated this method only works in IE and should not have been added to
+   *             the API
+   */
+  @Deprecated
+  public void setKey(char key) {
+    if (currentEvent != null) {
+      DOM.eventSetKeyCode(currentEvent, key);
+    }
+  }
+
+  public void setName(String name) {
+    DOM.setElementProperty(getElement(), "name", name);
+  }
+
+  /**
+   * Turns read-only mode on or off.
+   *
+   * @param readOnly if <code>true</code>, the widget becomes read-only; if
+   *          <code>false</code> the widget becomes editable
+   */
+  public void setReadOnly(boolean readOnly) {
+    DOM.setElementPropertyBoolean(getElement(), "readOnly", readOnly);
+    String readOnlyStyle = "readonly";
+    if (readOnly) {
+      addStyleDependentName(readOnlyStyle);
+    } else {
+      removeStyleDependentName(readOnlyStyle);
+    }
+  }
+
+  /**
+   * Sets the range of text to be selected.
+   *
+ * This will only work when the widget is attached to the document and not
+   * hidden.
+   *
+   * @param pos the position of the first character to be selected
+   * @param length the number of characters to be selected
+   */
+  public void setSelectionRange(int pos, int length) {
+    // Setting the selection range will not work for unattached elements.
+    if (!isAttached()) {
+      return;
+    }
+
+    if (length < 0) {
+      throw new IndexOutOfBoundsException(
+          "Length must be a positive integer. Length: " + length);
+    }
+    if ((pos < 0) || (length + pos > getText().length())) {
+ throw new IndexOutOfBoundsException("From Index: " + pos + " To Index: "
+          + (pos + length) + "  Text Length: " + getText().length());
+    }
+    impl.setSelectionRange(getElement(), pos, length);
+  }
+
+  /**
+ * Sets this object's text. Note that some browsers will manipulate the text + * before adding it to the widget. For example, most browsers will strip all + * <code>\r</code> from the text, except IE which will add a <code>\r</code>
+   * before each <code>\n</code>.  Use {...@link #getText()} to get the text
+   * directly from the widget.
+   *
+   * @param text the object's new text
+   */
+  public void setText(String text) {
+ DOM.setElementProperty(getElement(), "value", text != null ? text : "");
+  }
+
+  /**
+   * Sets the alignment of the text in the text box.
+   *
+ * @param align the text alignment (as specified by {...@link #ALIGN_CENTER},
+   *          {...@link #ALIGN_JUSTIFY}, {...@link #ALIGN_LEFT}, and
+   *          {...@link #ALIGN_RIGHT})
+   */
+  public void setTextAlignment(TextAlignConstant align) {
+ DOM.setStyleAttribute(getElement(), "textAlign", align.getTextAlignString());
+  }
+
+  public void setValue(T value) {
+    setValue(value, false);
+  }
+
+  public void setValue(T value, boolean fireEvents) {
+    T oldValue = getValue();
+    setText(renderer.render(value));
+    if (fireEvents) {
+      ValueChangeEvent.fireIfNotEqual(this, oldValue, value);
+    }
+  }
+
+  protected TextBoxImpl getImpl() {
+    return impl;
+  }
+}
=======================================
--- /branches/2.1/bikeshed/src/com/google/gwt/app/util/PassthroughRenderer.java Mon Apr 26 09:13:06 2010 +++ /branches/2.1/bikeshed/src/com/google/gwt/app/util/PassthroughRenderer.java Tue May 11 16:51:18 2010
@@ -38,5 +38,4 @@
   public String render(String object) {
     return object;
   }
-
-}
+}

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

Reply via email to