001 /*
002 * Copyright 2012 GWT-Bootstrap
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package com.github.gwtbootstrap.client.ui;
017
018 import com.github.gwtbootstrap.client.ui.base.ValueBoxBase;
019 import com.google.gwt.dom.client.Document;
020 import com.google.gwt.dom.client.Element;
021 import com.google.gwt.dom.client.InputElement;
022 import com.google.gwt.i18n.client.LocaleInfo;
023 import com.google.gwt.text.shared.Parser;
024 import com.google.gwt.text.shared.Renderer;
025 import com.google.gwt.user.client.ui.RootPanel;
026 import com.google.gwt.user.client.ui.Widget;
027
028 /**
029 * A text box cant parse its displayed value.Support Bootstarp style.
030 *
031 * It's a same as GWT's {@link com.google.gwt.user.client.ui.ValueBox}.
032 * But support Bootstarp styles.
033 *
034 * @param <T> the value type
035 * @see com.google.gwt.user.client.ui.ValueBox
036 * @since 2.0.4.0
037 * @author ohashi keisuke
038 */
039 public class ValueBox<T> extends ValueBoxBase<T> {
040
041 /**
042 * Creates a ValueBox widget that wraps an existing <input
043 * type='text'> element.
044 *
045 * This element must already be attached to the document. If the element is
046 * removed from the document, you must call
047 * {@link RootPanel#detachNow(Widget)}.
048 *
049 * @param element
050 * the element to be wrapped
051 */
052 public static <T> ValueBox<T> wrap(Element element, Renderer<T> renderer, Parser<T> parser) {
053 // Assert that the element is attached.
054 assert Document.get().getBody().isOrHasChild(element);
055
056 ValueBox<T> valueBox = new ValueBox<T>(element, renderer, parser);
057
058 // Mark it attached and remember it for cleanup.
059 valueBox.onAttach();
060 RootPanel.detachOnWindowClose(valueBox);
061
062 return valueBox;
063 }
064
065 /**
066 * This constructor may be used by subclasses to explicitly use an existing
067 * element. This element must be an <input> element whose type is
068 * 'text'.
069 *
070 * @param element
071 * the element to be used
072 */
073 protected ValueBox(Element element,
074 Renderer<T> renderer,
075 Parser<T> parser) {
076 super(element, renderer, parser);
077 // BiDi input is not expected - disable direction estimation.
078 setDirectionEstimator(false);
079 if (LocaleInfo.getCurrentLocale().isRTL()) {
080 setDirection(Direction.LTR);
081 }
082 assert InputElement.as(element).getType().equalsIgnoreCase("text");
083 }
084
085 /**
086 * Gets the maximum allowable length.
087 *
088 * @return the maximum length, in characters
089 */
090 public int getMaxLength() {
091 return getInputElement().getMaxLength();
092 }
093
094 /**
095 * Gets the number of visible characters.
096 *
097 * @return the number of visible characters
098 */
099 public int getVisibleLength() {
100 return getInputElement().getSize();
101 }
102
103 /**
104 * Sets the maximum allowable length.
105 *
106 * @param length
107 * the maximum length, in characters
108 */
109 public void setMaxLength(int length) {
110 getInputElement().setMaxLength(length);
111 }
112
113 /**
114 * Sets the number of visible characters.
115 *
116 * @param length
117 * the number of visible characters
118 */
119 public void setVisibleLength(int length) {
120 getInputElement().setSize(length);
121 }
122
123 private InputElement getInputElement() {
124 return getElement().cast();
125 }
126
127 }