001: /* Comboitem.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Thu Dec 15 17:33:35 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 java.util.Iterator;
022:
023: import org.zkoss.lang.Objects;
024:
025: import org.zkoss.xml.HTMLs;
026: import org.zkoss.zk.ui.Component;
027: import org.zkoss.zk.ui.UiException;
028: import org.zkoss.zk.ui.event.Event;
029: import org.zkoss.zk.ui.event.Events;
030: import org.zkoss.zul.impl.LabelImageElement;
031:
032: /**
033: * An item of a combo box.
034: *
035: * <p>Non-XUL extension. Refer to {@link Combobox}.
036: *
037: * <p>Default {@link #getSclass}: item.
038: *
039: * @author tomyeh
040: * @see Combobox
041: */
042: public class Comboitem extends LabelImageElement {
043: private String _desc = "";
044: private Object _value;
045: private String _content = "";
046: private boolean _disabled = false;
047:
048: public Comboitem() {
049: setSclass("item");
050: }
051:
052: public Comboitem(String label) {
053: this ();
054: setLabel(label);
055: }
056:
057: public Comboitem(String label, String image) {
058: this ();
059: setLabel(label);
060: setImage(image);
061: }
062:
063: public String getSclass() {
064: String scls = super .getSclass();
065: if (isDisabled())
066: return scls.length() > 0 ? scls + " disd" : "disd";
067: return scls;
068: }
069:
070: public void setLabel(String label) {
071: final String old = getLabel();
072: if (!Objects.equals(old, label)) {
073: final Combobox cb = (Combobox) getParent();
074: final boolean reIndex = cb != null
075: && cb.getSelectedItem() == this ;
076:
077: super .setLabel(label);
078:
079: if (reIndex) {
080: final Constraint constr = cb.getConstraint();
081: if (constr != null
082: && constr instanceof SimpleConstraint
083: && (((SimpleConstraint) constr).getFlags() & SimpleConstraint.STRICT) != 0) {
084: cb.setValue(label);
085: } else {
086: cb.reIndex();
087: }
088: }
089: }
090: }
091:
092: /**
093: * Sets whether it is disabled.
094: * @since 3.0.1
095: */
096: public void setDisabled(boolean disabled) {
097: if (_disabled != disabled) {
098: _disabled = disabled;
099: invalidate();
100: }
101: }
102:
103: /** Returns whether it is disabled.
104: * <p>Default: false.
105: * @since 3.0.1
106: */
107: public boolean isDisabled() {
108: return _disabled;
109: }
110:
111: /** Returns the description (never null).
112: * The description is used to provide extra information such that
113: * users is easy to make a selection.
114: * <p>Default: "".
115: */
116: public String getDescription() {
117: return _desc;
118: }
119:
120: /** Sets the description.
121: */
122: public void setDescription(String desc) {
123: if (desc == null)
124: desc = "";
125: if (!_desc.equals(desc)) {
126: _desc = desc;
127: invalidate();
128: }
129: }
130:
131: /** Returns the embedded content (i.e., HTML tags) that is
132: * shown as part of the description.
133: *
134: * <p>It is useful to show the description in more versatile way.
135: *
136: * <p>Default: empty ("").
137: *
138: * @see #getDescription
139: * @since 3.0.0
140: */
141: public String getContent() {
142: return _content;
143: }
144:
145: /** Sets the embedded content (i.e., HTML tags) that is
146: * shown as part of the description.
147: *
148: * <p>It is useful to show the description in more versatile way.
149: *
150: * @see #setDescription
151: * @since 3.0.0
152: */
153: public void setContent(String content) {
154: if (content == null)
155: content = "";
156: if (!Objects.equals(_content, content)) {
157: _content = content;
158: invalidate();
159: }
160: }
161:
162: /** Returns the value associated with this combo item.
163: * The value is application dependent. It can be anything.
164: *
165: * <p>It is usually used with {@link Combobox#getSelectedItem}.
166: * For example,
167: * <code>combobox.getSelectedItem().getValue()</code>
168: *
169: * @see Combobox#getSelectedItem
170: * @see #setValue
171: * @since 2.4.0
172: */
173: public Object getValue() {
174: return _value;
175: }
176:
177: /** Associate the value with this combo item.
178: * The value is application dependent. It can be anything.
179: *
180: * @see Combobox#getSelectedItem
181: * @see #getValue
182: * @since 2.4.0
183: */
184: public void setValue(Object value) {
185: _value = value;
186: }
187:
188: //-- super --//
189: public void setParent(Component parent) {
190: if (parent != null && !(parent instanceof Combobox))
191: throw new UiException("Comboitem's parent must be Combobox");
192:
193: final Combobox old = (Combobox) getParent();
194: final boolean reIndex = parent != old && old != null
195: && old.getSelectedItem() == this ;
196:
197: super .setParent(parent);
198:
199: if (reIndex)
200: postOnReIndex(old);
201: }
202:
203: /** re-index later */
204: private void postOnReIndex(Combobox old) {
205: Events.postEvent("onReIndex", this , old);
206: }
207:
208: public void onReIndex(Event evt) {
209: final Combobox cb = (Combobox) evt.getData();
210: if (cb != null)
211: cb.reIndex();
212: }
213:
214: /** No child is allowed. */
215: public boolean isChildable() {
216: return false;
217: }
218:
219: public String getOuterAttrs() {
220: final StringBuffer sb = new StringBuffer(60).append(super
221: .getOuterAttrs());
222: HTMLs.appendAttribute(sb, "z.disd", isDisabled());
223: return sb.toString();
224: }
225: }
|