⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tree.java

📁 ZK 基础介绍 功能操作 模块 结合数据库操作
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* 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.Arrays;import java.util.List;import java.util.Set;import java.util.LinkedHashSet;import java.util.Iterator;import java.util.ListIterator;import java.util.Collection;import java.util.Collections;import java.util.AbstractCollection;import java.util.ArrayList;import org.zkoss.lang.Exceptions;import org.zkoss.lang.Objects;import org.zkoss.util.logging.Log;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.client.InnerWidth;import org.zkoss.zk.ui.event.Event;import org.zkoss.zk.ui.event.EventListener;import org.zkoss.zk.ui.event.Events;//import org.zkoss.zul.Listbox.Renderer;import org.zkoss.zul.event.ListDataEvent;import org.zkoss.zul.event.TreeDataEvent;import org.zkoss.zul.event.TreeDataListener;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 Treefoot _treefoot;	private transient Treechildren _treechildren;	/** A list of selected items. */	private transient Set _selItems;	/** The first selected item. */	private transient Treeitem _sel;	private transient Collection _heads;	private int _rows = 0;	/** The name. */	private String _name;	/** The style class prefix for generating icons. */	private String _iconScls = "tree";	/** # of items per page. */	private int _pgsz = 10;	private boolean _multiple, _checkmark;	private boolean _vflex;	/** disable smartUpdate; usually caused by the client. */	private transient boolean _noSmartUpdate;	private String _innerWidth = "100%";	public Tree() {		init();		setSclass("tree");	}	private void init() {		_selItems = new LinkedHashSet(5);		_heads = new AbstractCollection() {			public int size() {				int sz = getChildren().size();				if (_treechildren != null) --sz;				if (_treefoot != null) --sz;				return sz;			}			public Iterator iterator() {				return new Iter();			}		};	}	/** Returns the treecols that this tree owns (might null).	 */	public Treecols getTreecols() {		return _treecols;	}	/** Returns the treefoot that this tree owns (might null).	 */	public Treefoot getTreefoot() {		return _treefoot;	}	/** Returns the treechildren that this tree owns (might null).	 */	public Treechildren getTreechildren() {		return _treechildren;	}	/** Returns a collection of heads, including {@link #getTreecols}	 * and auxiliary heads ({@link Auxhead}) (never null).	 *	 * @since 3.0.0	 */	public Collection getHeads() {		return _heads;	}	/** 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));		}	}	/** Returns the name of this component.	 * <p>Default: null.	 * <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.	 * <p>Don't use this method if your application is purely based	 * on ZK's event-driven model.	 */	public String getName() {		return _name;	}	/** Sets the name of this component.	 * <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.	 * <p>Don't use this method if your application is purely based	 * on ZK's event-driven model.	 *	 * @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)) {			_name = name;			if (_name != null) smartUpdate("z.name", _name);			else invalidate(); //1) generate _value; 2) add submit listener		}	}	/** 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);		}	}	/**	 * Sets the inner width of this component.	 * The inner width is the width of the inner table.	 * By default, it is 100%. That is, it is the same as the width	 * of this component. However, it is changed when the user	 * is sizing the column's width.	 *	 * <p>Application developers rarely call this method, unless	 * they want to preserve the widths of sizable columns	 * changed by the user.	 * To preserve the widths, the developer have to store the widths of	 * all columns and the inner width ({@link #getInnerWidth}),	 * and then restore them when re-creating this component.	 *	 * @param innerWidth the inner width. If null, "100%" is assumed.	 * @since 3.0.0	 */	public void setInnerWidth(String innerWidth) {		if (innerWidth == null) innerWidth = "100%";		if (!_innerWidth.equals(innerWidth)) {			_innerWidth = innerWidth;			smartUpdate("z.innerWidth", innerWidth);		}	}	/**	 * Returns the inner width of this component.	 * The inner width is the width of the inner table.	 * <p>Default: "100%"	 * @see #setInnerWidth	 * @since 3.0.0	 */	public String getInnerWidth() {		return _innerWidth;	}		/** 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();				for (Iterator it = _selItems.iterator(); it.hasNext();) {					final Treeitem ti = (Treeitem)it.next();					if (ti != item) {						ti.setSelectedDirectly(false);						it.remove();					}				}				//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);			}			_selItems.clear();			_sel = null;			smartUpdate("select", "");		}	}	/** Selects all items.	 */	public void selectAll() {		if (!_multiple)			throw new UiException("Appliable only to the multiple seltype: "+this);		//we don't invoke getItemCount first because it is slow!		boolean changed = false, first = true;		for (Iterator it = getItems().iterator(); it.hasNext();) {			final Treeitem item = (Treeitem)it.next();			if (!item.isSelected()) {				_selItems.add(item);				item.setSelectedDirectly(true);				changed = true;			}			if (first) {				_sel = item;				first = false;			}		}		smartUpdate("selectAll", "true");	}	/** Returns the selected item.	 */	public Treeitem getSelectedItem() {		return _sel;	}	/**  Deselects all of the currently selected items and selects	 * the given item.	 * <p>It is the same as {@link #selectItem}.	 */	public void setSelectedItem(Treeitem item) {		selectItem(item);	}	/** Returns all selected items.	 */	public Set getSelectedItems() {		return Collections.unmodifiableSet(_selItems);	}	/** Returns the number of items being selected.	 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -