001: /* Treerow.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Wed Jul 6 18:56:22 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.Component;
025: import org.zkoss.zk.ui.UiException;
026: import org.zkoss.zk.ui.event.Events;
027:
028: import org.zkoss.zul.impl.XulElement;
029:
030: /**
031: * A treerow.
032: *
033: * @author tomyeh
034: */
035: public class Treerow extends XulElement {
036: /** Returns the {@link Tree} instance containing this element.
037: */
038: public Tree getTree() {
039: for (Component p = this ; (p = p.getParent()) != null;)
040: if (p instanceof Tree)
041: return (Tree) p;
042: return null;
043: }
044:
045: /** Returns the level this cell is. The root is level 0.
046: */
047: public int getLevel() {
048: final Component parent = getParent();
049: return parent != null ? ((Treeitem) parent).getLevel() : 0;
050: }
051:
052: /** Returns the parent {@link Treeitem}.
053: * @deprecated As of release 2.4.1, due to confusing
054: */
055: public Treeitem getTreeitem() {
056: return (Treeitem) getParent();
057: }
058:
059: /** Returns the {@link Treechildren} associated with this
060: * {@link Treerow}.
061: * In other words, it is {@link Treeitem#getTreechildren} of
062: * {@link #getParent}.
063: * @since 2.4.1
064: * @see Treechildren#getLinkedTreerow
065: */
066: public Treechildren getLinkedTreechildren() {
067: final Component parent = getParent();
068: return parent != null ? ((Treeitem) parent).getTreechildren()
069: : null;
070: }
071:
072: //-- super --//
073: /** Returns the style class.
074: * Note: 1) if not set (or setSclass(null), "item" is assumed;
075: * 2) if selected, it appends " seld" to super's getSclass().
076: */
077: public String getSclass() {
078: String scls = super .getSclass();
079: if (scls == null)
080: scls = "item";
081: final Treeitem ti = getTreeitem();
082: if (ti != null) {
083: if (ti.isDisabled())
084: return scls.length() > 0 ? scls + " disd" : "disd";
085: else if (ti.isSelected())
086: return scls.length() > 0 ? scls + " seld" : "seld";
087: }
088: return scls;
089: }
090:
091: /** Alwasys throws UnsupportedOperationException since developers shall
092: * use {@link Treeitem#setContext} instead.
093: */
094: public void setContext(String context) {
095: throw new UnsupportedOperationException("Use treeitem instead");
096: }
097:
098: /** Alwasys throws UnsupportedOperationException since developers shall
099: * use {@link Treeitem#setPopup} instead.
100: */
101: public void setPopup(String popup) {
102: throw new UnsupportedOperationException("Use treeitem instead");
103: }
104:
105: /** Alwasys throws UnsupportedOperationException since developers shall
106: * use {@link Treeitem#setTooltip} instead.
107: */
108: public void setTooltip(String tooltip) {
109: throw new UnsupportedOperationException("Use treeitem instead");
110: }
111:
112: /** Returns the same as {@link Treeitem#getContext}.
113: */
114: public String getContext() {
115: final Treeitem ti = getTreeitem();
116: return ti != null ? ti.getContext() : null;
117: }
118:
119: /** Returns the same as {@link Treeitem#getPopup}.
120: */
121: public String getPopup() {
122: final Treeitem ti = getTreeitem();
123: return ti != null ? ti.getPopup() : null;
124: }
125:
126: /** Returns the same as {@link Treeitem#getTooltip}.
127: */
128: public String getTooltip() {
129: final Treeitem ti = getTreeitem();
130: return ti != null ? ti.getTooltip() : null;
131: }
132:
133: /** Returns the same as {@link Treeitem#getTooltiptext}
134: */
135: public String getTooltiptext() {
136: final Treeitem ti = getTreeitem();
137: return ti != null ? ti.getTooltiptext() : null;
138: }
139:
140: protected boolean isAsapRequired(String evtnm) {
141: if (!Events.ON_OPEN.equals(evtnm))
142: return super .isAsapRequired(evtnm);
143: final Treeitem ti = getTreeitem();
144: return ti != null && ti.isAsapRequired(evtnm);
145: }
146:
147: /** Appends attributes for generating the real checkbox HTML tags
148: * (name="val"); Used only by component developers.
149: */
150: public String getOuterAttrs() {
151: final String attrs = super .getOuterAttrs();
152: final Treeitem item = getTreeitem();
153: if (item == null)
154: return attrs;
155:
156: final StringBuffer sb = new StringBuffer(80).append(attrs);
157:
158: final Tree tree = getTree();
159: if (tree != null && tree.getName() != null)
160: HTMLs.appendAttribute(sb, "z.value", Objects.toString(item
161: .getValue()));
162: HTMLs.appendAttribute(sb, "z.pitem", item.getUuid());
163: HTMLs.appendAttribute(sb, "z.sel", item.isSelected());
164: HTMLs.appendAttribute(sb, "z.disd", item.isDisabled());
165: HTMLs.appendAttribute(sb, "z.rid", tree.getUuid());
166: if (item.isContainer())
167: HTMLs.appendAttribute(sb, "z.open", item.isOpen());
168:
169: final Component gp = item.getParent(); //Treechildren
170: if (gp != null) {
171: HTMLs.appendAttribute(sb, "z.ptch", gp.getUuid());
172: Component gpitem = gp.getParent();
173: if (gpitem instanceof Treeitem)
174: HTMLs.appendAttribute(sb, "z.gpitem", gpitem.getUuid());
175: }
176:
177: final Treechildren tcsib = getLinkedTreechildren();
178: if (tcsib != null) {
179: HTMLs.appendAttribute(sb, "z.tchsib", tcsib.getUuid());
180:
181: final int pgcnt = tcsib.getPageCount();
182: if (pgcnt > 1) {
183: HTMLs.appendAttribute(sb, "z.pgc", pgcnt);
184: HTMLs.appendAttribute(sb, "z.pgi", tcsib
185: .getActivePage());
186: HTMLs
187: .appendAttribute(sb, "z.pgsz", tcsib
188: .getPageSize());
189: }
190: }
191:
192: //TODO AREA JEFF ADDED
193: //Modified for load-on-demand
194: if (getTree().getModel() != null)
195: sb.append("z.onopen=\"true\"");
196: else
197: appendAsapAttr(sb, Events.ON_OPEN);
198: //it calls isAsapRequired, so it also tested Treeitem for onOpen
199: //TODO AREA JEFF ADDED END
200:
201: final String clkattrs = getAllOnClickAttrs(false);
202: if (clkattrs != null)
203: sb.append(clkattrs);
204: return sb.toString();
205: }
206:
207: //-- Component --//
208: /** Returns whether this is visible.
209: * whether all its ancestors is open.
210: */
211: public boolean isVisible() {
212: if (!super .isVisible())
213: return false;
214: Component comp = getParent();
215: if (!(comp instanceof Treeitem))
216: return true;
217: if (!comp.isVisible())
218: return false;
219:
220: comp = comp.getParent();
221: return !(comp instanceof Treechildren)
222: || ((Treechildren) comp).isVisible(); //recursive
223: }
224:
225: public void setParent(Component parent) {
226: if (parent != null && !(parent instanceof Treeitem))
227: throw new UiException("Wrong parent: " + parent);
228: super .setParent(parent);
229: }
230:
231: public boolean insertBefore(Component child, Component insertBefore) {
232: if (!(child instanceof Treecell))
233: throw new UiException("Unsupported child for tree row: "
234: + child);
235: return super.insertBefore(child, insertBefore);
236: }
237: }
|