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.github.gwtbootstrap.client.ui.constants.ResizeType; 020import com.google.gwt.dom.client.Document; 021import com.google.gwt.dom.client.Element; 022import com.google.gwt.dom.client.TextAreaElement; 023import com.google.gwt.user.client.ui.RootPanel; 024import com.google.gwt.user.client.ui.Widget; 025 026/** 027 * A TextArea for Bootstrap form. 028 * 029 * @since 2.0.4.0 030 * @author ohashi keisuke 031 */ 032public class TextArea extends TextBoxBase { 033 034 /** 035 * Creates a TextArea widget that wraps an existing <textarea> 036 * element. 037 * 038 * This element must already be attached to the document. If the element is 039 * removed from the document, you must call 040 * {@link RootPanel#detachNow(Widget)}. 041 * 042 * @param element 043 * the element to be wrapped 044 */ 045 public static TextArea wrap(Element element) { 046 // Assert that the element is attached. 047 assert Document.get().getBody().isOrHasChild(element); 048 049 TextArea textArea = new TextArea(element); 050 051 // Mark it attached and remember it for cleanup. 052 textArea.onAttach(); 053 RootPanel.detachOnWindowClose(textArea); 054 055 return textArea; 056 } 057 058 /** 059 * Creates an empty text area. 060 */ 061 public TextArea() { 062 super(Document.get().createTextAreaElement()); 063 setStyleName("gwt-TextArea"); 064 } 065 066 /** 067 * This constructor may be used by subclasses to explicitly use an existing 068 * element. This element must be a <textarea> element. 069 * 070 * @param element 071 * the element to be used 072 */ 073 protected TextArea(Element element) { 074 super(element.<Element> cast()); 075 TextAreaElement.as(element); 076 } 077 078 /** 079 * Gets the requested width of the text box (this is not an exact value, as 080 * not all characters are created equal). 081 * 082 * @return the requested width, in characters 083 */ 084 public int getCharacterWidth() { 085 return getTextAreaElement().getCols(); 086 } 087 088 @Override 089 public int getCursorPos() { 090 return getImpl().getTextAreaCursorPos(getElement()); 091 } 092 093 @Override 094 public int getSelectionLength() { 095 return getImpl().getTextAreaSelectionLength(getElement()); 096 } 097 098 /** 099 * Gets the number of text lines that are visible. 100 * 101 * @return the number of visible lines 102 */ 103 public int getVisibleLines() { 104 return getTextAreaElement().getRows(); 105 } 106 107 /** 108 * Sets the requested width of the text box (this is not an exact value, as 109 * not all characters are created equal). 110 * 111 * @param width 112 * the requested width, in characters 113 */ 114 public void setCharacterWidth(int width) { 115 getTextAreaElement().setCols(width); 116 } 117 118 /** 119 * Sets the number of text lines that are visible. 120 * 121 * @param lines 122 * the number of visible lines 123 */ 124 public void setVisibleLines(int lines) { 125 getTextAreaElement().setRows(lines); 126 } 127 128 /** 129 * Sets type of resizing policy 130 * 131 * @param resize type of resizing policy 132 */ 133 public void setResize(ResizeType resize) { 134 getTextAreaElement().getStyle().setProperty("resize", resize.get()); 135 } 136 137 private TextAreaElement getTextAreaElement() { 138 return getElement().cast(); 139 } 140 141}