001: /* Checkbox.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Thu Jun 16 23:45:45 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.UiException;
025: import org.zkoss.zk.ui.WrongValueException;
026: import org.zkoss.zk.ui.ext.client.Checkable;
027: import org.zkoss.zk.ui.event.Events;
028:
029: import org.zkoss.zul.impl.LabelImageElement;
030:
031: /**
032: * A checkbox.
033: *
034: * <p>Event:
035: * <ol>
036: * <li>org.zkoss.zk.ui.event.CheckEvent is sent when a checkbox
037: * is checked or unchecked by user.</li>
038: * </ol>
039: *
040: * @author tomyeh
041: */
042: public class Checkbox extends LabelImageElement {
043: /** The name. */
044: private String _name;
045: private int _tabindex = -1;
046: private boolean _checked;
047: private boolean _disabled;
048: private boolean _readonly;
049:
050: public Checkbox() {
051: }
052:
053: public Checkbox(String label) {
054: setLabel(label);
055: }
056:
057: public Checkbox(String label, String image) {
058: setLabel(label);
059: setImage(image);
060: }
061:
062: /** Returns whether it is disabled.
063: * <p>Default: false.
064: */
065: public boolean isDisabled() {
066: return _disabled;
067: }
068:
069: /** Sets whether it is disabled.
070: */
071: public void setDisabled(boolean disabled) {
072: if (_disabled != disabled) {
073: _disabled = disabled;
074: smartUpdate("disabled", _disabled);
075: }
076: }
077:
078: /** Returns whether it is checked.
079: * <p>Default: false.
080: */
081: public boolean isChecked() {
082: return _checked;
083: }
084:
085: /** Sets whether it is checked.
086: */
087: public void setChecked(boolean checked) {
088: if (_checked != checked) {
089: _checked = checked;
090: smartUpdate("checked", _checked);
091: }
092: }
093:
094: /** Returns whether it is readonly.
095: * <p>Default: false.
096: * <p>This method has no real effect.
097: * See <a href="http://www.w3.org/TR/html4/interact/forms.html">w3.org</a>
098: * @deprecated As of release 2.4.1, since this method has no real effect.
099: */
100: public boolean isReadonly() {
101: return _readonly;
102: }
103:
104: /** Sets whether it is readonly.
105: * <p>This method has no real effect.
106: * See <a href="http://www.w3.org/TR/html4/interact/forms.html">w3.org</a>
107: * @deprecated As of release 2.4.1, since this method has no real effect.
108: */
109: public void setReadonly(boolean readonly) {
110: _readonly = readonly;
111: }
112:
113: /** Returns the name of this component.
114: * <p>Default: null.
115: * <p>Don't use this method if your application is purely based
116: * on ZK's event-driven model.
117: * <p>The name is used only to work with "legacy" Web application that
118: * handles user's request by servlets.
119: * It works only with HTTP/HTML-based browsers. It doesn't work
120: * with other kind of clients.
121: */
122: public String getName() {
123: return _name;
124: }
125:
126: /** Sets the name of this component.
127: * <p>Don't use this method if your application is purely based
128: * on ZK's event-driven model.
129: * <p>The name is used only to work with "legacy" Web application that
130: * handles user's request by servlets.
131: * It works only with HTTP/HTML-based browsers. It doesn't work
132: * with other kind of clients.
133: *
134: * @param name the name of this component.
135: */
136: public void setName(String name) {
137: if (name != null && name.length() == 0)
138: name = null;
139: if (!Objects.equals(_name, name)) {
140: _name = name;
141: smartUpdate("name", _name);
142: }
143: }
144:
145: /** Returns the tab order of this component.
146: * <p>Default: -1 (means the same as browser's default).
147: */
148: public int getTabindex() {
149: return _tabindex;
150: }
151:
152: /** Sets the tab order of this component.
153: */
154: public void setTabindex(int tabindex) throws WrongValueException {
155: if (_tabindex != tabindex) {
156: _tabindex = tabindex;
157: if (tabindex < 0)
158: smartUpdate("tabindex", null);
159: else
160: smartUpdate("tabindex", Integer.toString(_tabindex));
161: }
162: }
163:
164: /** Returns the attributes used by the embedded HTML LABEL tag.
165: * It returns text-relevant styles only.
166: * <p>Used only by component developer.
167: */
168: public String getLabelAttrs() {
169: final String style = HTMLs.getTextRelevantStyle(getRealStyle());
170: return style.length() > 0 ? " style=\"" + style + '"' : "";
171: }
172:
173: //-- super --//
174: /** Appends interior attributes for generating the HTML checkbox tag
175: * (the name, disabled and other attribute).
176: * <p>Used only by component developers.
177: */
178: public String getInnerAttrs() {
179: final StringBuffer sb = new StringBuffer(64).append(super
180: .getInnerAttrs());
181:
182: HTMLs.appendAttribute(sb, "name", getName());
183: if (isDisabled())
184: HTMLs.appendAttribute(sb, "disabled", "disabled");
185: if (isChecked())
186: HTMLs.appendAttribute(sb, "checked", "checked");
187: if (_tabindex >= 0)
188: HTMLs.appendAttribute(sb, "tabindex", _tabindex);
189: return sb.toString();
190: }
191:
192: /** Appends exterior attributes for generating the HTML span tag
193: * (the event relevant attribute).
194: * <p>Used only by component developers.
195: */
196: public String getOuterAttrs() {
197: final StringBuffer sb = new StringBuffer(64).append(super
198: .getOuterAttrs());
199:
200: appendAsapAttr(sb, Events.ON_FOCUS);
201: appendAsapAttr(sb, Events.ON_BLUR);
202: appendAsapAttr(sb, Events.ON_CHECK);
203: appendAsapAttr(sb, Events.ON_RIGHT_CLICK);
204: appendAsapAttr(sb, Events.ON_DOUBLE_CLICK);
205: //no z.lfclk since it is handled by widget.js
206:
207: return sb.toString();
208: }
209:
210: //-- ComponentCtrl --//
211: protected Object newExtraCtrl() {
212: return new ExtraCtrl();
213: }
214:
215: /** A utility class to implement {@link #getExtraCtrl}.
216: * It is used only by component developers.
217: */
218: protected class ExtraCtrl extends LabelImageElement.ExtraCtrl
219: implements Checkable {
220: //-- Checkable --//
221: public void setCheckedByClient(boolean checked) {
222: _checked = checked;
223: }
224: }
225: }
|