001: /* Toolbarbutton.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Thu Jun 23 11:33: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.WrongValueException;
025: import org.zkoss.zk.ui.event.Events;
026:
027: import org.zkoss.zul.impl.LabelImageElement;
028:
029: /**
030: * A tool button.
031: *
032: * <p>The default CSS class is "button".
033: *
034: * <p>Non-xul extension: Toolbarbutton supports {@link #getHref}. If {@link #getHref}
035: * is not null, the onClick handler is ignored and this element is degenerated
036: * to HTML's A tag.
037: *
038: * @author tomyeh
039: */
040: public class Toolbarbutton extends LabelImageElement {
041: private String _orient = "horizontal", _dir = "normal";
042: private String _href, _target;
043: private int _tabindex = -1;
044: private boolean _disabled = false;
045:
046: public Toolbarbutton() {
047: }
048:
049: public Toolbarbutton(String label) {
050: setLabel(label);
051: }
052:
053: public Toolbarbutton(String label, String image) {
054: setLabel(label);
055: setImage(image);
056: }
057:
058: public String getSclass() {
059: String scls = super .getSclass();
060: if (isDisabled())
061: return scls != null && scls.length() > 0 ? scls + " disd"
062: : "disd";
063: return scls;
064: }
065:
066: /**
067: * Sets whether it is disabled.
068: * @since 3.0.1
069: */
070: public void setDisabled(boolean disabled) {
071: if (_disabled != disabled) {
072: _disabled = disabled;
073: invalidate();
074: }
075: }
076:
077: /** Returns whether it is disabled.
078: * <p>Default: false.
079: * @since 3.0.1
080: */
081: public boolean isDisabled() {
082: return _disabled;
083: }
084:
085: /** Returns the direction.
086: * <p>Default: "normal".
087: */
088: public String getDir() {
089: return _dir;
090: }
091:
092: /** Sets the direction.
093: * @param dir either "normal" or "reverse".
094: */
095: public void setDir(String dir) throws WrongValueException {
096: if (!"normal".equals(dir) && !"reverse".equals(dir))
097: throw new WrongValueException(dir);
098:
099: if (!Objects.equals(_dir, dir)) {
100: _dir = dir;
101: invalidate();
102: }
103: }
104:
105: /** Returns the href.
106: * <p>Default: null. If null, the button has no function unless you
107: * specify the onClick handler.
108: */
109: public String getHref() {
110: return _href;
111: }
112:
113: /** Sets the href.
114: */
115: public void setHref(String href) throws WrongValueException {
116: if (href != null && href.length() == 0)
117: href = null;
118: if (!Objects.equals(_href, href)) {
119: _href = href;
120: invalidate();
121: }
122: }
123:
124: /** Returns the orient.
125: * <p>Default: "horizontal".
126: */
127: public String getOrient() {
128: return _orient;
129: }
130:
131: /** Sets the orient.
132: * @param orient either "horizontal" or "vertical".
133: */
134: public void setOrient(String orient) throws WrongValueException {
135: if (!"horizontal".equals(orient) && !"vertical".equals(orient))
136: throw new WrongValueException("orient cannot be " + orient);
137:
138: if (!Objects.equals(_orient, orient)) {
139: _orient = orient;
140: invalidate();
141: }
142: }
143:
144: /** Returns the target frame or window.
145: *
146: * <p>Note: it is useful only if href ({@link #setHref}) is specified
147: * (i.e., use the onClick listener).
148: *
149: * <p>Default: null.
150: */
151: public String getTarget() {
152: return _target;
153: }
154:
155: /** Sets the target frame or window.
156: * @param target the name of the frame or window to hyperlink.
157: */
158: public void setTarget(String target) {
159: if (target != null && target.length() == 0)
160: target = null;
161:
162: if (!Objects.equals(_target, target)) {
163: _target = target;
164: smartUpdate("target", _target);
165: }
166: }
167:
168: /** Returns the tab order of this component.
169: * <p>Default: -1 (means the same as browser's default).
170: */
171: public int getTabindex() {
172: return _tabindex;
173: }
174:
175: /** Sets the tab order of this component.
176: */
177: public void setTabindex(int tabindex) throws WrongValueException {
178: if (_tabindex != tabindex) {
179: _tabindex = tabindex;
180: if (tabindex < 0)
181: smartUpdate("tabindex", null);
182: else
183: smartUpdate("tabindex", Integer.toString(_tabindex));
184: }
185: }
186:
187: //-- super --//
188: public String getOuterAttrs() {
189: final StringBuffer sb = new StringBuffer(64).append(super
190: .getOuterAttrs());
191:
192: appendAsapAttr(sb, Events.ON_FOCUS);
193: appendAsapAttr(sb, Events.ON_BLUR);
194: appendAsapAttr(sb, Events.ON_RIGHT_CLICK);
195: //no z.dbclk to avoid confusing
196: //no z.lfclk since it is handled by widget.js
197:
198: if (_href == null) {
199: sb.append(" href=\"javascript:;\"");
200: } else {
201: sb.append(" href=\"").append(
202: getDesktop().getExecution().encodeURL(_href))
203: .append('"');
204: //When hyper to other page, we always show progress dlg
205: }
206: HTMLs.appendAttribute(sb, "z.disd", isDisabled());
207: HTMLs.appendAttribute(sb, "target", _target);
208:
209: if (_tabindex >= 0)
210: HTMLs.appendAttribute(sb, "tabindex", _tabindex);
211: return sb.toString();
212: }
213:
214: //Component//
215: /** No child is allowed.
216: */
217: public boolean isChildable() {
218: return false;
219: }
220: }
|