001: /* Textbox.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Tue Jun 14 15:48:28 2005, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2005 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.zul;
020:
021: import org.zkoss.lang.Objects;
022: import org.zkoss.xml.HTMLs;
023:
024: import org.zkoss.zk.ui.WrongValueException;
025:
026: import org.zkoss.zul.impl.InputElement;
027:
028: /**
029: * A textbox.
030: *
031: * <p>See <a href="package-summary.html">Specification</a>.</p>
032: *
033: * @author tomyeh
034: */
035: public class Textbox extends InputElement {
036: private String _type = "text";
037: private int _rows = 1;
038: private boolean _multiline;
039:
040: public Textbox() {
041: setValueDirectly("");
042: }
043:
044: public Textbox(String value) throws WrongValueException {
045: setValue(value);
046: }
047:
048: /** Returns the value.
049: * The same as {@link #getText}.
050: * <p>Default: "".
051: * @exception WrongValueException if user entered a wrong value
052: */
053: public String getValue() throws WrongValueException {
054: return getText();
055: }
056:
057: /** Sets the value.
058: *
059: * @param value the value; If null, it is considered as empty.
060: * @exception WrongValueException if value is wrong
061: */
062: public void setValue(String value) throws WrongValueException {
063: setText(value);
064: }
065:
066: //-- super --//
067: /** Coerces the value passed to {@link #setValue}.
068: *
069: * <p>Default: convert null to an empty string.
070: */
071: protected Object coerceFromString(String value)
072: throws WrongValueException {
073: return value != null ? value : "";
074: }
075:
076: /** Coerces the value passed to {@link #setValue}.
077: *
078: * <p>Default: convert null to an empty string.
079: */
080: protected String coerceToString(Object value) {
081: return value != null ? (String) value : "";
082: }
083:
084: /** Returns the type.
085: * <p>Default: text.
086: */
087: public String getType() {
088: return _type;
089: }
090:
091: /** Sets the type.
092: * @param type the type. Acceptable values are "text" and "password".
093: * Unlike XUL, "timed" is redudant because it is enabled as long as
094: * onChanging is added.
095: */
096: public void setType(String type) throws WrongValueException {
097: if (!"text".equals(type) && !"password".equals(type))
098: throw new WrongValueException("Illegal type: " + type);
099:
100: if (!_type.equals(type)) {
101: _type = type;
102: invalidate();
103: }
104: }
105:
106: /** Returns the rows.
107: * <p>Default: 1.
108: */
109: public int getRows() {
110: return _rows;
111: }
112:
113: /** Sets the rows.
114: */
115: public void setRows(int rows) throws WrongValueException {
116: if (rows <= 0)
117: throw new WrongValueException("Illegal rows: " + rows);
118:
119: if (_rows != rows) {
120: _rows = rows;
121: if (_rows > 1)
122: setMultiline(true); //auto-enable
123:
124: smartUpdate("rows", Integer.toString(_rows));
125: }
126: }
127:
128: /** Returns whether it is multiline.
129: * <p>Default: false.
130: */
131: public boolean isMultiline() {
132: return _multiline;
133: }
134:
135: /** Sets whether it is multiline.
136: */
137: public void setMultiline(boolean multiline) {
138: if (_multiline != multiline) {
139: _multiline = multiline;
140: invalidate(); //different element
141: }
142: }
143:
144: //-- super --//
145: public String getInnerAttrs() {
146: final String attrs = super .getInnerAttrs();
147: if (!_multiline)
148: return attrs;
149:
150: final StringBuffer sb = new StringBuffer(64).append(attrs);
151: HTMLs.appendAttribute(sb, "rows", _rows);
152: return sb.toString();
153: }
154:
155: public String getOuterAttrs() {
156: final String attrs = super .getOuterAttrs();
157: return _multiline ? attrs + " z.skipOK=\"true\"" : attrs;
158: }
159: }
|