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