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 */ 016package com.github.gwtbootstrap.client.ui; 017 018import com.github.gwtbootstrap.client.ui.base.TextBoxBase; 019import com.google.gwt.dom.client.Document; 020import com.google.gwt.dom.client.Element; 021import com.google.gwt.dom.client.InputElement; 022import com.google.gwt.user.client.ui.RootPanel; 023import com.google.gwt.user.client.ui.Widget; 024 025/** 026 * A TextBox for Bootstrap form. 027 * 028 * @since 2.0.4.0 029 * 030 * @author Carlos Alexandro Becker 031 * @author ohashi keisuke 032 * 033 */ 034public class TextBox extends TextBoxBase { 035 036 /** 037 * Creates a TextBox widget that wraps an existing <input type='text'> 038 * element. 039 * 040 * This element must already be attached to the document. If the element is 041 * removed from the document, you must call 042 * {@link RootPanel#detachNow(Widget)}. 043 * 044 * @param element 045 * the element to be wrapped 046 */ 047 public static TextBox wrap(Element element) { 048 // Assert that the element is attached. 049 assert Document.get().getBody().isOrHasChild(element); 050 051 TextBox textBox = new TextBox(element); 052 053 // Mark it attached and remember it for cleanup. 054 textBox.onAttach(); 055 RootPanel.detachOnWindowClose(textBox); 056 057 return textBox; 058 } 059 060 /** 061 * Creates an empty text box. 062 */ 063 public TextBox() { 064 this(Document.get().createTextInputElement(), "gwt-TextBox"); 065 } 066 067 /** 068 * This constructor may be used by subclasses to explicitly use an existing 069 * element. This element must be an <input> element whose type is 070 * 'text'. 071 * 072 * @param element 073 * the element to be used 074 */ 075 protected TextBox(Element element) { 076 super(element); 077 assert InputElement.as(element).getType().equalsIgnoreCase("text"); 078 } 079 080 TextBox(Element element, 081 String styleName) { 082 super(element); 083 if (styleName != null) { 084 setStyleName(styleName); 085 } 086 } 087 088 /** 089 * Gets the maximum allowable length of the text box. 090 * 091 * @return the maximum length, in characters 092 */ 093 public int getMaxLength() { 094 return getInputElement().getMaxLength(); 095 } 096 097 /** 098 * Gets the number of visible characters in the text box. 099 * 100 * @return the number of visible characters 101 */ 102 public int getVisibleLength() { 103 return getInputElement().getSize(); 104 } 105 106 /** 107 * Sets the maximum allowable length of the text box. 108 * 109 * @param length 110 * the maximum length, in characters 111 */ 112 public void setMaxLength(int length) { 113 getInputElement().setMaxLength(length); 114 } 115 116 /** 117 * Sets the number of visible characters in the text box. 118 * 119 * @param length 120 * the number of visible characters 121 */ 122 public void setVisibleLength(int length) { 123 getInputElement().setSize(length); 124 } 125 126 private InputElement getInputElement() { 127 return getElement().cast(); 128 } 129 130}