001: /*
002: * Javu WingS - Lightweight Java Component Set
003: * Copyright (c) 2005-2007 Krzysztof A. Sadlocha
004: * e-mail: ksadlocha@programics.com
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: package com.javujavu.javux.wings.tree;
022:
023: import java.util.Vector;
024:
025: /**
026: * This class represents a single tree node of <code>WingTree</code>
027: * <br><br>
028: * <b>This class is thread safe.</b>
029: * but it is not necessary because
030: * synchronization is provided by WingTree
031: */
032: public class TreeNode {
033: protected TreeNode parent;
034: protected Vector childs;
035: protected boolean expanded;
036:
037: protected Object item;
038:
039: public TreeNode(Object item) {
040: this .item = item;
041: }
042:
043: public TreeNode getParent() {
044: return parent;
045: }
046:
047: public void setItem(Object item) {
048: this .item = item;
049: }
050:
051: public Object getItem() {
052: return item;
053: }
054:
055: public boolean isExpanded() {
056: return expanded;
057: }
058:
059: public void setExpanded(boolean expanded) {
060: this .expanded = expanded;
061: }
062:
063: public boolean isLeaf() {
064: return getChildCount() == 0;
065: }
066:
067: public synchronized int getChildCount() {
068: return (childs != null) ? childs.size() : 0;
069: }
070:
071: public int getLevel() {
072: int level = 0;
073: for (TreeNode n = parent; n != null; n = n.parent)
074: level++;
075: return level;
076: }
077:
078: public boolean isChildOf(TreeNode n) {
079: for (TreeNode p = parent; p != null; p = p.parent)
080: if (p == n)
081: return true;
082: return false;
083: }
084:
085: public synchronized void insert(TreeNode n, int index) {
086: if (childs == null)
087: childs = new Vector();
088: int size = childs.size();
089: if (index < 0 || index > size)
090: index = size;
091: childs.insertElementAt(n, index);
092: n.parent = this ;
093: }
094:
095: public synchronized void remove(TreeNode n) {
096: if (n.parent == this ) {
097: childs.removeElement(n);
098: n.parent = null;
099: }
100: }
101:
102: public synchronized TreeNode childAt(int index) {
103: if (index < 0 || index > getChildCount())
104: return null;
105: return (TreeNode) childs.elementAt(index);
106: }
107:
108: public synchronized int indexOf(TreeNode n) {
109: return (childs != null) ? childs.indexOf(n) : -1;
110: }
111:
112: }
|