001: /* Listitem.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Jun 15, 2007 10:17:32 AM, Created by henrichen
010: }}IS_NOTE
011:
012: Copyright (C) 2007 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:
020: package org.zkoss.mil;
021:
022: import org.zkoss.lang.Objects;
023: import org.zkoss.lang.Strings;
024: import org.zkoss.xml.HTMLs;
025: import org.zkoss.zk.ui.Component;
026: import org.zkoss.zk.ui.Desktop;
027: import org.zkoss.zk.ui.UiException;
028:
029: /**
030: * The Listitem that can be located under Listbox only.
031: *
032: * @author henrichen
033: */
034: public class Listitem extends MilComponent {
035: private static final long serialVersionUID = 200706151103L;
036:
037: private boolean _selected;
038: private String _label;
039: private Object _value;
040: private String _src;
041: /** The image. If not null, _src is generated automatically. */
042: private org.zkoss.image.Image _image;
043: /** Count the version of {@link #_image}. */
044: private int _imgver;
045:
046: public Listitem() {
047: }
048:
049: public Listitem(String label) {
050: setLabel(label);
051: }
052:
053: public Listitem(String label, Object value) {
054: setLabel(label);
055: setValue(value);
056: }
057:
058: /** Returns the list box that it belongs to.
059: */
060: public Listbox getListbox() {
061: return (Listbox) getParent();
062: }
063:
064: /**
065: * Get the label of this Item.
066: *
067: * @return the label of this Item.
068: */
069: public String getLabel() {
070: return _label;
071: }
072:
073: /**
074: * Set the new label of this Item.
075: * @param label the new label of this Item.
076: */
077: public void setLabel(String label) {
078: if (label != null && label.length() == 0) {
079: label = null;
080: }
081: if (!Objects.equals(_label, label)) {
082: _label = label;
083: smartUpdate("lb", encodeString(label));
084: }
085: }
086:
087: /** Sets whether it is selected.
088: */
089: public void setSelected(boolean selected) {
090: if (_selected != selected) {
091: final Listbox listbox = (Listbox) getParent();
092: if (listbox != null) {
093: //Note: we don't update it here but let its parent does the job
094: listbox.toggleItemSelection(this );
095: } else {
096: _selected = selected;
097: }
098: }
099: }
100:
101: public boolean isSelected() {
102: return _selected;
103: }
104:
105: /*package*/void setSelectedDirectly(boolean b) {
106: _selected = b;
107: }
108:
109: public void setValue(Object value) {
110: _value = value;
111: }
112:
113: public Object getValue() {
114: return _value;
115: }
116:
117: /** Returns the image source of this Listitem, or null if no image.
118: */
119: public String getSrc() {
120: return _src;
121: }
122:
123: /** Sets theimage soruce of this Listitem.
124: * @param src the image source
125: */
126: public void setSrc(String src) {
127: setImage(src);
128: }
129:
130: /** Returns the image source of this Listitem, or null if no image.
131: */
132: public String getImage() {
133: return _src;
134: }
135:
136: /** Sets the image source of this Listitem.
137: * @param image the image source
138: */
139: public void setImage(String image) {
140: if (image != null && image.length() == 0)
141: image = null;
142:
143: if (!Objects.equals(_src, image)) {
144: _src = image;
145: if (_image == null) {
146: smartUpdate("im", getEncodedSrc());
147: }
148: //_src is meaningful only if _image is null
149: }
150: }
151:
152: /** Returns the encoded src ({@link #getSrc}).
153: */
154: private String getEncodedSrc() {
155: final Desktop dt = getDesktop(); //it might not belong to any desktop
156: return _image != null ? getContentSrc() : //already encoded
157: dt != null ? dt.getExecution().encodeURL(
158: _src != null ? _src : "~./img/spacer.gif") : "";
159: }
160:
161: /** Sets the content directly.
162: * Default: null.
163: *
164: * @param image the image to display. If not null, it has higher
165: * priority than {@link #getSrc}.
166: */
167: public void setImageContent(org.zkoss.image.Image image) {
168: if (image != _image) {
169: _image = image;
170: if (_image != null)
171: ++_imgver; //enforce browser to reload image
172: smartUpdate("im", getEncodedSrc());
173: }
174: }
175:
176: /** Returns the content set by {@link #setImageContent}.
177: * <p>Note: it won't fetch what is set thru by {@link #setSrc}.
178: * It simply returns what is passed to {@link #setImageContent}.
179: */
180: public org.zkoss.image.Image getImageContent() {
181: return _image;
182: }
183:
184: /** Returns the encoded URL for the current image content.
185: * Don't call this method unless _image is not null;
186: *
187: * <p>Used only for component template, not for application developers.
188: */
189: private String getContentSrc() {
190: final Desktop desktop = getDesktop();
191: if (desktop == null)
192: return ""; //no avail at client
193:
194: final StringBuffer sb = new StringBuffer(64).append('/');
195: Strings.encode(sb, _imgver);
196: final String name = _image.getName();
197: final String format = _image.getFormat();
198: if (name != null || format != null) {
199: sb.append('/');
200: boolean bExtRequired = true;
201: if (name != null && name.length() != 0) {
202: sb.append(name);
203: bExtRequired = name.lastIndexOf('.') < 0;
204: } else {
205: sb.append(getId());
206: }
207: if (bExtRequired && format != null)
208: sb.append('.').append(format);
209: }
210: return desktop.getDynamicMediaURI(this , sb.toString()); //already encoded
211: }
212:
213: //--Component--//
214: public void setParent(Component parent) {
215: if (parent != null
216: && !(parent instanceof org.zkoss.mil.Listbox)) {
217: throw new UiException("Unsupported parent for Listitem: "
218: + this + ", parent: " + parent);
219: }
220: super .setParent(parent);
221: }
222:
223: /** Not childable. */
224: public boolean isChildable() {
225: return false;
226: }
227:
228: public String getInnerAttrs() {
229: final StringBuffer sb = new StringBuffer(64);
230: if (getLabel() != null) {
231: HTMLs.appendAttribute(sb, "lb", getLabel());
232: }
233: if (getImage() != null) {
234: HTMLs.appendAttribute(sb, "im", getEncodedSrc());
235: }
236: return sb.toString();
237: }
238: }
|