001    package org.gwtbootstrap3.client.ui;
002    
003    /*
004     * #%L
005     * GwtBootstrap3
006     * %%
007     * Copyright (C) 2013 GwtBootstrap3
008     * %%
009     * Licensed under the Apache License, Version 2.0 (the "License");
010     * you may not use this file except in compliance with the License.
011     * You may obtain a copy of the License at
012     * 
013     *      http://www.apache.org/licenses/LICENSE-2.0
014     * 
015     * Unless required by applicable law or agreed to in writing, software
016     * distributed under the License is distributed on an "AS IS" BASIS,
017     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018     * See the License for the specific language governing permissions and
019     * limitations under the License.
020     * #L%
021     */
022    
023    import org.gwtbootstrap3.client.ui.base.TextBoxBase;
024    import org.gwtbootstrap3.client.ui.constants.Styles;
025    import org.gwtbootstrap3.client.ui.gwt.Widget;
026    
027    import com.google.gwt.dom.client.Document;
028    import com.google.gwt.dom.client.Element;
029    import com.google.gwt.dom.client.TextAreaElement;
030    import com.google.gwt.user.client.ui.RootPanel;
031    
032    public class TextArea extends TextBoxBase {
033    
034        /**
035         * Creates a TextArea widget that wraps an existing <textarea>
036         * element.
037         * <p/>
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 the element to be wrapped
043         * @return TextArea
044         */
045        public static TextArea wrap(final Element element) {
046            // Assert that the element is attached.
047            assert Document.get().getBody().isOrHasChild(element);
048    
049            final 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(Styles.FORM_CONTROL);
064        }
065    
066        /**
067         * This constructor may be used by subclasses to explicitly use an existing
068         * element. This element must be a &lt;textarea&gt; element.
069         *
070         * @param element the element to be used
071         */
072        protected TextArea(final Element element) {
073            super(element.<Element>cast());
074            TextAreaElement.as(element);
075            element.addClassName(Styles.FORM_CONTROL);
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        /**
089         * {@inheritDoc}
090         */
091        @Override
092        public int getCursorPos() {
093            return getImpl().getTextAreaCursorPos(getElement());
094        }
095    
096        /**
097         * {@inheritDoc}
098         */
099        @Override
100        public int getSelectionLength() {
101            return getImpl().getTextAreaSelectionLength(getElement());
102        }
103    
104        /**
105         * Gets the number of text lines that are visible.
106         *
107         * @return the number of visible lines
108         */
109        public int getVisibleLines() {
110            return getTextAreaElement().getRows();
111        }
112    
113        /**
114         * Sets the requested width of the text box (this is not an exact value, as
115         * not all characters are created equal).
116         *
117         * @param width the requested width, in characters
118         */
119        public void setCharacterWidth(final int width) {
120            getTextAreaElement().setCols(width);
121        }
122    
123        /**
124         * Sets the number of text lines that are visible.
125         *
126         * @param lines the number of visible lines
127         */
128        public void setVisibleLines(final int lines) {
129            getTextAreaElement().setRows(lines);
130        }
131    
132        /**
133         * Get the TextAreaElement for the widget
134         *
135         * @return TextAreaElement element of the widget
136         */
137        private TextAreaElement getTextAreaElement() {
138            return getElement().cast();
139        }
140    
141        /**
142         * Clear the value
143         */
144        public void clear() {
145            super.setValue(null);
146        }
147    }