package org.gwtbootstrap3.client.ui; /* * #%L * GwtBootstrap3 * %% * Copyright (C) 2013 GwtBootstrap3 * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * #L% */ import org.gwtbootstrap3.client.ui.base.TextBoxBase; import org.gwtbootstrap3.client.ui.constants.Styles; import org.gwtbootstrap3.client.ui.gwt.Widget; import com.google.gwt.dom.client.Document; import com.google.gwt.dom.client.Element; import com.google.gwt.dom.client.TextAreaElement; import com.google.gwt.user.client.ui.RootPanel; public class TextArea extends TextBoxBase { /** * Creates a TextArea widget that wraps an existing <textarea> * element. * <p/> * This element must already be attached to the document. If the element is * removed from the document, you must call * {@link RootPanel#detachNow(Widget)}. * * @param element the element to be wrapped * @return TextArea */ public static TextArea wrap(final Element element) { // Assert that the element is attached. assert Document.get().getBody().isOrHasChild(element); final TextArea textArea = new TextArea(element); // Mark it attached and remember it for cleanup. textArea.onAttach(); RootPanel.detachOnWindowClose(textArea); return textArea; } /** * Creates an empty text area. */ public TextArea() { super(Document.get().createTextAreaElement()); setStyleName(Styles.FORM_CONTROL); } /** * This constructor may be used by subclasses to explicitly use an existing * element. This element must be a <textarea> element. * * @param element the element to be used */ protected TextArea(final Element element) { super(element.<Element>cast()); TextAreaElement.as(element); element.addClassName(Styles.FORM_CONTROL); } /** * Gets the requested width of the text box (this is not an exact value, as * not all characters are created equal). * * @return the requested width, in characters */ public int getCharacterWidth() { return getTextAreaElement().getCols(); } /** * {@inheritDoc} */ @Override public int getCursorPos() { return getImpl().getTextAreaCursorPos(getElement()); } /** * {@inheritDoc} */ @Override public int getSelectionLength() { return getImpl().getTextAreaSelectionLength(getElement()); } /** * Gets the number of text lines that are visible. * * @return the number of visible lines */ public int getVisibleLines() { return getTextAreaElement().getRows(); } /** * Sets the requested width of the text box (this is not an exact value, as * not all characters are created equal). * * @param width the requested width, in characters */ public void setCharacterWidth(final int width) { getTextAreaElement().setCols(width); } /** * Sets the number of text lines that are visible. * * @param lines the number of visible lines */ public void setVisibleLines(final int lines) { getTextAreaElement().setRows(lines); } /** * Get the TextAreaElement for the widget * * @return TextAreaElement element of the widget */ private TextAreaElement getTextAreaElement() { return getElement().cast(); } /** * Clear the value */ public void clear() { super.setValue(null); } }