001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.forms.element;
025:
026: import java.io.PrintWriter;
027:
028: import org.riotfamily.common.markup.DocumentWriter;
029: import org.riotfamily.common.markup.Html;
030: import org.riotfamily.forms.DHTMLElement;
031: import org.riotfamily.forms.resource.FormResource;
032: import org.riotfamily.forms.resource.ResourceElement;
033: import org.riotfamily.forms.resource.Resources;
034: import org.riotfamily.forms.resource.ScriptResource;
035:
036: /**
037: * A textarea widget.
038: */
039: public class Textarea extends AbstractTextElement implements
040: ResourceElement, DHTMLElement {
041:
042: private static FormResource RESOURCE = new ScriptResource(
043: "riot-js/textarea.js", "RiotTextArea", Resources.RIOT_UTIL);
044:
045: private Integer rows = null;
046:
047: private Integer cols = null;
048:
049: public void setCols(Integer cols) {
050: this .cols = cols;
051: }
052:
053: public void setRows(Integer rows) {
054: this .rows = rows;
055: }
056:
057: public void renderInternal(PrintWriter writer) {
058: DocumentWriter doc = new DocumentWriter(writer);
059: if (getMaxLength() == null && rows != null) {
060: // If no init script is rendered we surround the textarea with a
061: // div so that we work around the IE 100% width bug via CSS:
062: // http://fplanque.net/2003/Articles/iecsstextarea/
063: doc.start(Html.DIV).attribute(Html.COMMON_CLASS,
064: "textarea-wrapper");
065: }
066: doc.start(Html.TEXTAREA).attribute(Html.COMMON_CLASS,
067: getStyleClass()).attribute(Html.COMMON_ID, getId())
068: .attribute(Html.INPUT_NAME, getParamName());
069:
070: if (rows != null) {
071: doc.attribute(Html.TEXTAREA_ROWS, rows.intValue());
072: }
073: if (cols != null) {
074: doc.attribute(Html.TEXTAREA_COLS, cols.intValue());
075: }
076:
077: doc.body(getText()).closeAll();
078: }
079:
080: public FormResource getResource() {
081: return RESOURCE;
082: }
083:
084: public String getInitScript() {
085: if (getMaxLength() != null || rows == null) {
086: StringBuffer sb = new StringBuffer();
087: sb.append("new RiotTextArea('").append(getId())
088: .append("')");
089: if (getMaxLength() != null) {
090: sb.append(".setMaxLength(").append(getMaxLength())
091: .append(')');
092: }
093: if (rows == null) {
094: sb.append(".autoResize()");
095: }
096: sb.append(';');
097: return sb.toString();
098: }
099: return null;
100: }
101:
102: }
|