001: /* Treecol.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Wed Jul 6 18:55:59 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.lang.Objects;
025:
026: import org.zkoss.zk.ui.Component;
027: import org.zkoss.zk.ui.UiException;
028:
029: import org.zkoss.zul.impl.HeaderElement;
030:
031: /**
032: * A treecol.
033: *
034: * @author tomyeh
035: */
036: public class Treecol extends HeaderElement {
037: private int _maxlength;
038:
039: public Treecol() {
040: }
041:
042: public Treecol(String label) {
043: setLabel(label);
044: }
045:
046: public Treecol(String label, String src) {
047: setLabel(label);
048: setSrc(src);
049: }
050:
051: /** Returns the tree that it belongs to.
052: */
053: public Tree getTree() {
054: final Component comp = getParent();
055: return comp != null ? (Tree) comp.getParent() : null;
056: }
057:
058: /** Returns the maximal length of each item's label.
059: */
060: public int getMaxlength() {
061: return _maxlength;
062: }
063:
064: /** Sets the maximal length of each item's label.
065: */
066: public void setMaxlength(int maxlength) {
067: if (maxlength < 0)
068: maxlength = 0;
069: if (_maxlength != maxlength) {
070: _maxlength = maxlength;
071: invalidateCells();
072: }
073: }
074:
075: /** Returns the column index, starting from 0.
076: */
077: public int getColumnIndex() {
078: int j = 0;
079: for (Iterator it = getParent().getChildren().iterator(); it
080: .hasNext(); ++j)
081: if (it.next() == this )
082: break;
083: return j;
084: }
085:
086: /** Invalidates the relevant cells. */
087: private void invalidateCells() {
088: final Tree tree = getTree();
089: if (tree != null)
090: invalidateCells(tree.getTreechildren(), getColumnIndex());
091: }
092:
093: private static void invalidateCells(Treechildren tch, int jcol) {
094: if (tch == null)
095: return;
096:
097: for (Iterator it = tch.getChildren().iterator(); it.hasNext();) {
098: final Treeitem ti = (Treeitem) it.next();
099: final Treerow tr = ti.getTreerow();
100: if (tr != null) {
101: final List chs = tr.getChildren();
102: if (jcol < chs.size())
103: ((Component) chs.get(jcol)).invalidate();
104: }
105:
106: invalidateCells(ti.getTreechildren(), jcol); //recursive
107: }
108: }
109:
110: //-- super --//
111: public String getOuterAttrs() {
112: final String attrs = super .getOuterAttrs();
113: final String clkattrs = getAllOnClickAttrs(false);
114: return clkattrs == null ? attrs : attrs + clkattrs;
115: }
116:
117: /** Invalidates the whole tree. */
118: protected void invalidateWhole() {
119: final Tree tree = getTree();
120: if (tree != null)
121: tree.invalidate();
122: }
123:
124: //-- Component --//
125: public void setParent(Component parent) {
126: if (parent != null && !(parent instanceof Treecols))
127: throw new UiException("Wrong parent: " + parent);
128: super.setParent(parent);
129: }
130: }
|