001: /* Listcell.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Fri Aug 5 13:06:17 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.List;
022: import java.util.Iterator;
023:
024: import org.zkoss.xml.HTMLs;
025:
026: import org.zkoss.zk.ui.Component;
027: import org.zkoss.zk.ui.UiException;
028:
029: import org.zkoss.zul.impl.LabelImageElement;
030:
031: /**
032: * A list cell.
033: *
034: * @author tomyeh
035: */
036: public class Listcell extends LabelImageElement {
037: private Object _value;
038: private int _span = 1;
039:
040: public Listcell() {
041: }
042:
043: public Listcell(String label) {
044: setLabel(label);
045: }
046:
047: public Listcell(String label, String src) {
048: setLabel(label);
049: setImage(src);
050: }
051:
052: /** Returns the list box that it belongs to.
053: */
054: public Listbox getListbox() {
055: final Component comp = getParent();
056: return comp != null ? (Listbox) comp.getParent() : null;
057: }
058:
059: /** Returns the list item that it belongs to.
060: * @deprecated As of release 2.4.1, due to confusion
061: */
062: public Listitem getListitem() {
063: return (Listitem) getParent();
064: }
065:
066: /** Returns the list header that is in the same column as
067: * this cell, or null if not available.
068: */
069: public Listheader getListheader() {
070: final Listbox listbox = getListbox();
071: if (listbox != null) {
072: final Listhead lcs = listbox.getListhead();
073: if (lcs != null) {
074: final int j = getColumnIndex();
075: final List lcschs = lcs.getChildren();
076: if (j < lcschs.size())
077: return (Listheader) lcschs.get(j);
078: }
079: }
080: return null;
081: }
082:
083: /** Returns the column index of this cell, starting from 0.
084: */
085: public int getColumnIndex() {
086: int j = 0;
087: for (Iterator it = getParent().getChildren().iterator(); it
088: .hasNext(); ++j)
089: if (it.next() == this )
090: break;
091: return j;
092: }
093:
094: /** Returns the maximal length for this cell.
095: * If listbox's mold is "select", it is the same as
096: * {@link Listbox#getMaxlength}
097: * If not, it is the same as the correponding {@link #getListheader}'s
098: * {@link Listheader#getMaxlength}.
099: *
100: * <p>Note: {@link Listitem#getMaxlength} is the same as {@link Listbox#getMaxlength}.
101: */
102: public int getMaxlength() {
103: final Listbox listbox = getListbox();
104: if (listbox == null)
105: return 0;
106: if (listbox.inSelectMold())
107: return listbox.getMaxlength();
108: final Listheader lc = getListheader();
109: return lc != null ? lc.getMaxlength() : 0;
110: }
111:
112: /** Returns the value.
113: * <p>Default: null.
114: * <p>Note: the value is application dependent, you can place
115: * whatever value you want.
116: */
117: public Object getValue() {
118: return _value;
119: }
120:
121: /** Sets the value.
122: * @param value the value.
123: * <p>Note: the value is application dependent, you can place
124: * whatever value you want.
125: */
126: public void setValue(Object value) {
127: _value = value;
128: }
129:
130: /** Returns number of columns to span this cell.
131: * Default: 1.
132: */
133: public int getSpan() {
134: return _span;
135: }
136:
137: /** Sets the number of columns to span this cell.
138: * <p>It is the same as the colspan attribute of HTML TD tag.
139: */
140: public void setSpan(int span) {
141: if (_span != span) {
142: _span = span;
143: smartUpdate("colspan", Integer.toString(_span));
144: }
145: }
146:
147: //-- super --//
148: public void setWidth(String width) {
149: throw new UnsupportedOperationException(
150: "Set listheader's width instead");
151: }
152:
153: //-- Internal use only --//
154: /** Returns the prefix of the first column (in HTML tags), null if this
155: * is not first column. Called only by listcell.dsp.
156: */
157: public String getColumnHtmlPrefix() {
158: final Listitem item = getListitem();
159: final Listbox listbox = getListbox();
160: if (listbox != null && item.getFirstChild() == this ) {
161: final StringBuffer sb = new StringBuffer(64);
162: if (listbox.isCheckmark()) {
163: sb.append("<input type=\"").append(
164: listbox.isMultiple() ? "checkbox" : "radio")
165: .append('"');
166: if (item.isDisabled())
167: sb.append(" disabled=\"disabled\"");
168: if (item.isSelected())
169: sb.append(" checked=\"checked\"");
170:
171: sb.append(" id=\"").append(item.getUuid()).append(
172: "!cm\" z.type=\"Lcfc\"/>");
173: return sb.toString();
174: } else if (isFocusRequired(listbox, item)) {
175: sb.append("<a href=\"javascript:;\" id=\"").append(
176: item.getUuid()).append(
177: "!sel\" z.type=\"Lcfc\"> </a>");
178: return sb.toString();
179: }
180: }
181:
182: //To make the listbox's height more correct, we have to generate
183: //for empty cell. Otherwise, IE will make the height too small
184: final boolean empty = getImage() == null
185: && getLabel().length() == 0 && getChildren().isEmpty();
186: return empty ? " " : null;
187:
188: }
189:
190: /** Returns the postfix of the first column (in HTML tags), null if this
191: * is not first column. Called only by listcell.jsp.
192: */
193: public String getColumnHtmlPostfix() {
194: return null;
195: }
196:
197: /** Returns whether this cell requires focus.
198: */
199: private boolean isFocusRequired(Listbox listbox, Component parent) {
200: final Listitem sel = listbox.getSelectedItem();
201: return parent == sel
202: || (sel == null && ((Listitem) parent).getIndex() == 0);
203: }
204:
205: //-- super --//
206: public String getOuterAttrs() {
207: final String attrs = super .getOuterAttrs();
208:
209: final Listheader header = getListheader();
210: final String clkattrs = getAllOnClickAttrs(false);
211: if (header == null && clkattrs == null && _span == 1)
212: return attrs;
213:
214: final StringBuffer sb = new StringBuffer(64).append(attrs);
215: if (header != null)
216: sb.append(header.getColAttrs());
217: if (clkattrs != null)
218: sb.append(clkattrs);
219: if (_span != 1)
220: HTMLs.appendAttribute(sb, "colspan", _span);
221:
222: return sb.toString();
223: }
224:
225: /** Returns the attributes used by the embedded HTML LABEL tag.
226: * It returns text-relevant styles only.
227: * <p>Used only by component developer.
228: */
229: public String getLabelAttrs() {
230: final String style = HTMLs.getTextRelevantStyle(getRealStyle());
231: return style.length() > 0 ? " style=\"" + style + '"' : "";
232: }
233:
234: //-- Component --//
235: public void setParent(Component parent) {
236: if (parent != null && !(parent instanceof Listitem))
237: throw new UiException("Wrong parent: " + parent);
238: super .setParent(parent);
239: }
240:
241: public void invalidate() {
242: final Listbox listbox = getListbox();
243: if (listbox != null && listbox.inSelectMold()) {
244: getParent().invalidate();
245: //if HTML select, the cell doesn't exists in client
246: } else {
247: super.invalidate();
248: }
249: }
250: }
|