001: /* Button.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Wed Jun 8 10:31:02 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.Desktop;
025: import org.zkoss.zk.ui.UiException;
026: import org.zkoss.zk.ui.WrongValueException;
027: import org.zkoss.zk.ui.event.Events;
028:
029: import org.zkoss.zul.impl.LabelImageElement;
030:
031: /**
032: * A button.
033: *
034: * @author tomyeh
035: */
036: public class Button extends LabelImageElement {
037: private String _orient = "horizontal", _dir = "normal";
038: private String _href, _target;
039: private int _tabindex = -1;
040: private boolean _disabled;
041: private boolean _readonly;
042:
043: public Button() {
044: }
045:
046: public Button(String label) {
047: setLabel(label);
048: }
049:
050: public Button(String label, String image) {
051: setLabel(label);
052: setImage(image);
053: }
054:
055: /** Returns whether it is disabled.
056: * <p>Default: false.
057: */
058: public boolean isDisabled() {
059: return _disabled;
060: }
061:
062: /** Sets whether it is disabled.
063: */
064: public void setDisabled(boolean disabled) {
065: if (_disabled != disabled) {
066: _disabled = disabled;
067: smartUpdate("disabled", _disabled);
068: }
069: }
070:
071: /** Returns the direction.
072: * <p>Default: "normal".
073: */
074: public String getDir() {
075: return _dir;
076: }
077:
078: /** Sets the direction.
079: * @param dir either "normal" or "reverse".
080: */
081: public void setDir(String dir) throws WrongValueException {
082: if (!"normal".equals(dir) && !"reverse".equals(dir))
083: throw new WrongValueException(dir);
084:
085: if (!Objects.equals(_dir, dir)) {
086: _dir = dir;
087: invalidate();
088: }
089: }
090:
091: /** Returns the orient.
092: * <p>Default: "horizontal".
093: */
094: public String getOrient() {
095: return _orient;
096: }
097:
098: /** Sets the orient.
099: * @param orient either "horizontal" or "vertical".
100: */
101: public void setOrient(String orient) throws WrongValueException {
102: if (!"horizontal".equals(orient) && !"vertical".equals(orient))
103: throw new WrongValueException(orient);
104:
105: if (!Objects.equals(_orient, orient)) {
106: _orient = orient;
107: invalidate();
108: }
109: }
110:
111: /** Returns the href that the browser shall jump to, if an user clicks
112: * this button.
113: * <p>Default: null. If null, the button has no function unless you
114: * specify the onClick event listener.
115: * <p>If it is not null, the onClick event won't be sent.
116: */
117: public String getHref() {
118: return _href;
119: }
120:
121: /** Sets the href.
122: */
123: public void setHref(String href) {
124: if (href != null && href.length() == 0)
125: href = null;
126: if (!Objects.equals(_href, href)) {
127: _href = href;
128: smartUpdateDeferred("z.href", new EncodedHref()); //Bug 1850895
129: }
130: }
131:
132: /** Returns the target frame or window.
133: *
134: * <p>Note: it is useful only if href ({@link #setHref}) is specified
135: * (i.e., use the onClick listener).
136: *
137: * <p>Default: null.
138: */
139: public String getTarget() {
140: return _target;
141: }
142:
143: /** Sets the target frame or window.
144: * @param target the name of the frame or window to hyperlink.
145: */
146: public void setTarget(String target) {
147: if (target != null && target.length() == 0)
148: target = null;
149:
150: if (!Objects.equals(_target, target)) {
151: _target = target;
152: smartUpdate("z.target", _target);
153: }
154: }
155:
156: /** Returns the tab order of this component.
157: * <p>Default: -1 (means the same as browser's default).
158: */
159: public int getTabindex() {
160: return _tabindex;
161: }
162:
163: /** Sets the tab order of this component.
164: */
165: public void setTabindex(int tabindex) throws WrongValueException {
166: if (_tabindex != tabindex) {
167: _tabindex = tabindex;
168: if (tabindex < 0)
169: smartUpdate("tabindex", null);
170: else
171: smartUpdate("tabindex", Integer.toString(_tabindex));
172: }
173: }
174:
175: /** Returns whether it is readonly.
176: * <p>Default: false.
177: * <p>This method has no real effect.
178: * See <a href="http://www.w3.org/TR/html4/interact/forms.html">w3.org</a>
179: * @deprecated As of release 2.4.1, since this method has no real effect.
180: */
181: public boolean isReadonly() {
182: return _readonly;
183: }
184:
185: /** Sets whether it is readonly.
186: * <p>This method has no real effect.
187: * See <a href="http://www.w3.org/TR/html4/interact/forms.html">w3.org</a>
188: * @deprecated As of release 2.4.1, since this method has no real effect.
189: */
190: public void setReadonly(boolean readonly) {
191: _readonly = readonly;
192: }
193:
194: private String getEncodedHref() {
195: final Desktop dt = getDesktop();
196: return _href != null && dt != null ? dt.getExecution()
197: .encodeURL(_href) : null;
198: //if desktop is null, it doesn't belong to any execution
199: }
200:
201: //-- super --//
202: public String getOuterAttrs() {
203: final StringBuffer sb = new StringBuffer(64).append(super
204: .getOuterAttrs());
205: HTMLs.appendAttribute(sb, "z.href", getEncodedHref());
206: HTMLs.appendAttribute(sb, "z.target", getTarget());
207:
208: appendAsapAttr(sb, Events.ON_FOCUS);
209: appendAsapAttr(sb, Events.ON_BLUR);
210: appendAsapAttr(sb, Events.ON_RIGHT_CLICK);
211: appendAsapAttr(sb, Events.ON_DOUBLE_CLICK);
212: //no z.lfclk since it is handled by widget.js
213:
214: if (isDisabled())
215: HTMLs.appendAttribute(sb, "disabled", "disabled");
216: if (_tabindex >= 0)
217: HTMLs.appendAttribute(sb, "tabindex", _tabindex);
218: return sb.toString();
219: }
220:
221: //Component//
222: /** No child is allowed.
223: */
224: public boolean isChildable() {
225: return false;
226: }
227:
228: private class EncodedHref implements
229: org.zkoss.zk.ui.util.DeferredValue {
230: public String getValue() {
231: return getEncodedHref();
232: }
233: }
234: }
|