001: /* Menuitem.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Thu Sep 22 10:58:23 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:
023: import org.zkoss.xml.HTMLs;
024: import org.zkoss.zk.ui.Component;
025: import org.zkoss.zk.ui.UiException;
026: import org.zkoss.zk.ui.WrongValueException;
027: import org.zkoss.zk.ui.ext.client.Checkable;
028:
029: import org.zkoss.zul.impl.LabelImageElement;
030:
031: /**
032: * sA single choice in a {@link Menupopup} element.
033: * It acts much like a button but it is rendered on a menu.
034: *
035: * @author tomyeh
036: */
037: public class Menuitem extends LabelImageElement {
038: private String _value = "";
039: private String _href, _target;
040: private boolean _autocheck, _checked;
041: private boolean _disabled = false;
042:
043: public Menuitem() {
044: }
045:
046: public Menuitem(String label) {
047: setLabel(label);
048: }
049:
050: public Menuitem(String label, String src) {
051: setLabel(label);
052: setImage(src);
053: }
054:
055: public String getSclass() {
056: String scls = super .getSclass();
057: if (isDisabled())
058: return scls != null && scls.length() > 0 ? scls + " disd"
059: : "disd";
060: return scls;
061: }
062:
063: /**
064: * Sets whether it is disabled.
065: * @since 3.0.1
066: */
067: public void setDisabled(boolean disabled) {
068: if (_disabled != disabled) {
069: _disabled = disabled;
070: invalidate();
071: }
072: }
073:
074: /** Returns whether it is disabled.
075: * <p>Default: false.
076: * @since 3.0.1
077: */
078: public boolean isDisabled() {
079: return _disabled;
080: }
081:
082: /** Returns the value.
083: * <p>Default: "".
084: */
085: public String getValue() {
086: return _value;
087: }
088:
089: /** Sets the value.
090: */
091: public void setValue(String value) {
092: if (value == null)
093: value = "";
094: _value = value; //no need to update client
095: }
096:
097: /** Returns whether it is checked.
098: * <p>Default: false.
099: */
100: public boolean isChecked() {
101: return _checked;
102: }
103:
104: /** Sets whether it is checked.
105: */
106: public void setChecked(boolean checked) {
107: if (_checked != checked) {
108: _checked = checked;
109:
110: final Component parent = getParent();
111: if (parent instanceof Menupopup)
112: parent.invalidate();
113: //CONSIDER: to use smartUpdate instead of invalidate
114: //FUTURE: to support checked for top-level menuitems
115: }
116: }
117:
118: /** Returns whether the menuitem check mark will update each time
119: * the menu item is selected
120: * <p>Default: false.
121: */
122: public boolean isAutocheck() {
123: return _autocheck;
124: }
125:
126: /** Sets whether the menuitem check mark will update each time
127: * the menu item is selected
128: */
129: public void setAutocheck(boolean autocheck) {
130: if (_autocheck != autocheck) {
131: _autocheck = autocheck;
132: invalidate();
133: }
134: }
135:
136: /** Returns the href.
137: * <p>Default: null. If null, the button has no function unless you
138: * specify the onClick handler.
139: */
140: public String getHref() {
141: return _href;
142: }
143:
144: /** Sets the href.
145: */
146: public void setHref(String href) throws WrongValueException {
147: if (href != null && href.length() == 0)
148: href = null;
149: if (!Objects.equals(_href, href)) {
150: _href = href;
151: invalidate();
152: }
153: }
154:
155: /** Returns the target frame or window.
156: *
157: * <p>Note: it is useful only if href ({@link #setHref}) is specified
158: * (i.e., use the onClick listener).
159: *
160: * <p>Default: null.
161: */
162: public String getTarget() {
163: return _target;
164: }
165:
166: /** Sets the target frame or window.
167: * @param target the name of the frame or window to hyperlink.
168: */
169: public void setTarget(String target) {
170: if (target != null && target.length() == 0)
171: target = null;
172:
173: if (!Objects.equals(_target, target)) {
174: _target = target;
175: smartUpdate("target", _target);
176: }
177: }
178:
179: /** Returns whether this is an top-level menu, i.e., not owning
180: * by another {@link Menupopup}.
181: */
182: public boolean isTopmost() {
183: return !(getParent() instanceof Menupopup);
184: }
185:
186: //-- Super --//
187: public String getOuterAttrs() {
188: final String attrs = super .getOuterAttrs();
189: boolean topmost = isTopmost();
190: if (!topmost && !_autocheck && !_disabled)
191: return attrs;
192:
193: final StringBuffer sb = new StringBuffer(64).append(attrs);
194: if (topmost)
195: sb.append(" z.top=\"true\"");
196: HTMLs.appendAttribute(sb, "z.disd", isDisabled());
197: if (_autocheck) {
198: sb.append(" z.autock=\"true\"");
199: if (_checked)
200: sb.append(" z.checked=\"true\"");
201: }
202: return sb.toString();
203: }
204:
205: protected String getRealStyle() {
206: final String style = super .getRealStyle();
207: return isTopmost() ? style
208: + "padding-left:4px;padding-right:4px;" : style;
209: }
210:
211: //-- Component --//
212: public void setParent(Component parent) {
213: if (parent != null && !(parent instanceof Menupopup)
214: && !(parent instanceof Menubar))
215: throw new UiException("Unsupported parent for menuitem: "
216: + parent);
217: super .setParent(parent);
218: }
219:
220: /** Not childable. */
221: public boolean isChildable() {
222: return false;
223: }
224:
225: //-- ComponentCtrl --//
226: protected Object newExtraCtrl() {
227: return new ExtraCtrl();
228: }
229:
230: /** A utility class to implement {@link #getExtraCtrl}.
231: * It is used only by component developers.
232: */
233: protected class ExtraCtrl extends LabelImageElement.ExtraCtrl
234: implements Checkable {
235: //-- Checkable --//
236: public void setCheckedByClient(boolean checked) {
237: setChecked(checked);
238: }
239: }
240: }
|