📄 tree.java
字号:
/* Tree.java{{IS_NOTE Purpose: Description: History: Wed Jul 6 18:51:33 2005, Created by tomyeh}}IS_NOTECopyright (C) 2005 Potix Corporation. All Rights Reserved.{{IS_RIGHT This program is distributed under GPL Version 2.0 in the hope that it will be useful, but WITHOUT ANY WARRANTY.}}IS_RIGHT*/package org.zkoss.zul;import java.util.List;import java.util.Set;import java.util.LinkedHashSet;import java.util.Iterator;import java.util.Collection;import java.util.Collections;import java.util.ArrayList;import org.zkoss.lang.Objects;import org.zkoss.xml.HTMLs;import org.zkoss.zk.ui.Component;import org.zkoss.zk.ui.UiException;import org.zkoss.zk.ui.WrongValueException;import org.zkoss.zk.ui.ext.client.Selectable;import org.zkoss.zk.ui.ext.render.ChildChangedAware;import org.zkoss.zk.ui.event.Events;import org.zkoss.zul.impl.XulElement;/** * A container which can be used to hold a tabular * or hierarchical set of rows of elements. * * <p>Event: * <ol> * <li>org.zkoss.zk.ui.event.SelectEvent is sent when user changes * the selection.</li> * </ol> * * <p>Default {@link #getSclass}: tree. * * @author tomyeh */public class Tree extends XulElement { private transient Treecols _treecols; private transient Treechildren _treechildren; /** A list of selected items. */ private transient Set _selItems; /** The first selected item. */ private transient Treeitem _sel; private int _rows = 0; /** The name. */ private String _name; private boolean _multiple, _checkmark; private boolean _vflex; /** disable smartUpdate; usually caused by the client. */ private transient boolean _noSmartUpdate; public Tree() { init(); setSclass("tree"); } private void init() { _selItems = new LinkedHashSet(5); } /** Returns the treecols that this tree owns (might null). */ public Treecols getTreecols() { return _treecols; } /** Returns the treechildren that this tree owns (might null). */ public Treechildren getTreechildren() { return _treechildren; } /** Returns the rows. Zero means no limitation. * <p>Default: 0. */ public int getRows() { return _rows; } /** Sets the rows. * <p>Note: if both {@link #setHeight} is specified with non-empty, * {@link #setRows} is ignored */ public void setRows(int rows) throws WrongValueException { if (rows < 0) throw new WrongValueException("Illegal rows: "+rows); if (_rows != rows) { _rows = rows; smartUpdate("z.size", Integer.toString(_rows)); initAtClient(); //Don't use smartUpdate because client has to extra job //besides maintaining HTML DOM } } /** Returns the name of this component. * <p>Default: null. * <p>Don't use this method if your application is purely based * on ZK's event-driven model. * <p>The name is used only to work with "legacy" Web application that * handles user's request by servlets. * It works only with HTTP/HTML-based browsers. It doesn't work * with other kind of clients. */ public String getName() { return _name; } /** Sets the name of this component. * <p>Don't use this method if your application is purely based * on ZK's event-driven model. * <p>The name is used only to work with "legacy" Web application that * handles user's request by servlets. * It works only with HTTP/HTML-based browsers. It doesn't work * with other kind of clients. * * @param name the name of this component. */ public void setName(String name) { if (name != null && name.length() == 0) name = null; if (!Objects.equals(_name, name)) { if (_name != null) smartUpdate("z.name", _name); else invalidate(); //1) generate _value; 2) add submit listener _name = name; } } /** Returns whether the check mark shall be displayed in front * of each item. * <p>Default: false. */ public final boolean isCheckmark() { return _checkmark; } /** Sets whether the check mark shall be displayed in front * of each item. * <p>The check mark is a checkbox if {@link #isMultiple} returns * true. It is a radio button if {@link #isMultiple} returns false. */ public void setCheckmark(boolean checkmark) { if (_checkmark != checkmark) { _checkmark = checkmark; invalidate(); } } /** Returns whether to grow and shrink vertical to fit their given space, * so called vertial flexibility. * * <p>Note: this attribute is ignored if {@link #setRows} is specified * * <p>Default: false. */ public final boolean isVflex() { return _vflex; } /** Sets whether to grow and shrink vertical to fit their given space, * so called vertial flexibility. * * <p>Note: this attribute is ignored if {@link #setRows} is specified */ public void setVflex(boolean vflex) { if (_vflex != vflex) { _vflex = vflex; smartUpdate("z.flex", _vflex); } } /** Returns the seltype. * <p>Default: "single". */ public String getSeltype() { return _multiple ? "multiple": "single"; } /** Sets the seltype. * Currently, only "single" is supported. */ public void setSeltype(String seltype) throws WrongValueException { if ("single".equals(seltype)) setMultiple(false); else if ("multiple".equals(seltype)) setMultiple(true); else throw new WrongValueException("Unknown seltype: "+seltype); } /** Returns whether multiple selections are allowed. * <p>Default: false. */ public boolean isMultiple() { return _multiple; } /** Sets whether multiple selections are allowed. */ public void setMultiple(boolean multiple) { if (_multiple != multiple) { _multiple = multiple; if (!_multiple && _selItems.size() > 1) { final Treeitem item = getSelectedItem(); _selItems.clear(); if (item != null) _selItems.add(item); //No need to update z.selId because z.multiple will do the job } if (isCheckmark()) invalidate(); //change check mark else smartUpdate("z.multiple", _multiple); } } /** Returns the ID of the selected item (it is stored as the z.selId * attribute of the tree). */ private String getSelectedId() { //NOTE: Treerow's uuid; not Treeitem's final Treerow tr = _sel != null ? _sel.getTreerow(): null; return tr != null ? tr.getUuid(): "zk_n_a"; } /** Returns a readonly list of all descending {@link Treeitem} * (children's children and so on). * * <p>Note: the performance of the size method of returned collection * is no good. */ public Collection getItems() { return _treechildren != null ? _treechildren.getItems(): Collections.EMPTY_LIST; } /** Returns the number of child {@link Treeitem}. * The same as {@link #getItems}.size(). * <p>Note: the performance of this method is no good. */ public int getItemCount() { return _treechildren != null ? _treechildren.getItemCount(): 0; } /** Deselects all of the currently selected items and selects * the given item. * <p>It is the same as {@link #setSelectedItem}. * @param item the item to select. If null, all items are deselected. */ public void selectItem(Treeitem item) { if (item == null) { clearSelection(); } else { if (item.getTree() != this) throw new UiException("Not a child: "+item); if (_sel != item || (_multiple && _selItems.size() > 1)) { for (Iterator it = _selItems.iterator(); it.hasNext();) { final Treeitem ti = (Treeitem)it.next(); ti.setSelectedDirectly(false); } _selItems.clear(); _sel = item; item.setSelectedDirectly(true); _selItems.add(item); final Treerow tr = item.getTreerow(); if (tr != null) smartUpdate("select", tr.getUuid()); } } } /** Selects the given item, without deselecting any other items * that are already selected.. */ public void addItemToSelection(Treeitem item) { if (item.getTree() != this) throw new UiException("Not a child: "+item); if (!item.isSelected()) { if (!_multiple) { selectItem(item); } else { item.setSelectedDirectly(true); _selItems.add(item); smartUpdateSelection(); if (fixSelected()) smartUpdate("z.selId", getSelectedId()); } } } /** Deselects the given item without deselecting other items. */ public void removeItemFromSelection(Treeitem item) { if (item.getTree() != this) throw new UiException("Not a child: "+item); if (item.isSelected()) { if (!_multiple) { clearSelection(); } else { item.setSelectedDirectly(false); _selItems.remove(item); smartUpdateSelection(); if (fixSelected()) smartUpdate("z.selId", getSelectedId()); //No need to use response because such info is carried on tags } } } /** Note: we have to update all selection at once, since addItemToSelection * and removeItemFromSelection might be called interchangeably. */ private void smartUpdateSelection() { final StringBuffer sb = new StringBuffer(80); for (Iterator it = _selItems.iterator(); it.hasNext();) { final Treeitem item = (Treeitem)it.next(); final Treerow tr = item.getTreerow(); if (tr != null) { if (sb.length() > 0) sb.append(','); sb.append(tr.getUuid()); } } smartUpdate("chgSel", sb.toString()); } /** If the specified item is selected, it is deselected. * If it is not selected, it is selected. Other items in the tree * that are selected are not affected, and retain their selected state. */ public void toggleItemSelection(Treeitem item) { if (item.isSelected()) removeItemFromSelection(item); else addItemToSelection(item); } /** Clears the selection. */ public void clearSelection() { if (!_selItems.isEmpty()) { for (Iterator it = _selItems.iterator(); it.hasNext();) { final Treeitem item = (Treeitem)it.next(); item.setSelectedDirectly(false); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -