001: /*
002: Copyright (c) 2003 eInnovation Inc. All rights reserved
003:
004: This library is free software; you can redistribute it and/or modify it under the terms
005: of the GNU Lesser General Public License as published by the Free Software Foundation;
006: either version 2.1 of the License, or (at your option) any later version.
007:
008: This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
009: without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: See the GNU Lesser General Public License for more details.
011: */
012:
013: package com.openedit.webui.tree;
014:
015: import java.util.ArrayList;
016: import java.util.List;
017:
018: import com.openedit.util.strainer.Filter;
019:
020: /**
021: * This class represents a node in a <code>{@link DefaultWebTreeModel}</code>. It has the
022: * attributes most people will need: name, URL, and icon URL. Most of the methods in
023: * <code>DefaultWebTreeModel</code> delegate to this class.
024: *
025: * @author Eric Galluzzo
026: */
027: public class DefaultWebTreeNode {
028: protected static int staticNextID = 0;
029: protected List fieldChildren;
030: protected String fieldIconURL;
031: protected String fieldName;
032: protected boolean fieldLeaf;
033: protected String fieldID;
034: protected Filter fieldFilter;
035: protected DefaultWebTreeNode fieldParent;
036: protected String fieldIconSet;
037:
038: /**
039: * Create a tree node with the given name, without an icon or link.
040: *
041: * @param inName DOCUMENT ME!
042: */
043: public DefaultWebTreeNode(String inName) {
044: setName(inName);
045: fieldID = String.valueOf(staticNextID++);
046: }
047:
048: public DefaultWebTreeNode(String inId, String inName) {
049: setName(inName);
050: fieldID = inId;
051: }
052:
053: /**
054: * Get the child at the given position in this node.
055: *
056: * @param inIndex DOCUMENT ME!
057: *
058: * @return
059: */
060: public DefaultWebTreeNode getChild(int inIndex) {
061: return (DefaultWebTreeNode) getChildren().get(inIndex);
062: }
063:
064: /**
065: * Get the number of children that this node has.
066: *
067: * @return
068: */
069: public int getChildCount() {
070: return getChildren().size();
071: }
072:
073: /**
074: * Gets the children.
075: *
076: * @return Returns a List
077: */
078: public List getChildren() {
079: if (fieldChildren == null) {
080: fieldChildren = new ArrayList();
081: }
082:
083: return fieldChildren;
084: }
085:
086: /**
087: * Gets the ID of this node, which is unique within the VM for the life of the VM (unless you
088: * do weird things with classloaders).
089: *
090: * @return Returns an int
091: */
092: public String getID() {
093: return fieldID;
094: }
095:
096: /**
097: * Sets the icon URL.
098: *
099: * @param iconURL The icon URL to set
100: */
101: public void setIconURL(String iconURL) {
102: fieldIconURL = iconURL;
103: }
104:
105: /**
106: * Gets the icon URL.
107: *
108: * @return Returns a String
109: */
110: public String getIconURL() {
111: return fieldIconURL;
112: }
113:
114: /**
115: * Get the index of the given child in this node
116: *
117: * @param inChild DOCUMENT ME!
118: *
119: * @return The index, or -1 if the child could not be found
120: */
121: public int getIndexOfChild(DefaultWebTreeNode inChild) {
122: return getChildren().indexOf(inChild);
123: }
124:
125: /**
126: * Set whether this node is a leaf.
127: *
128: * @param inLeaf DOCUMENT ME!
129: */
130: public void setLeaf(boolean inLeaf) {
131: fieldLeaf = inLeaf;
132: }
133:
134: /**
135: * Determine whether this node is a leaf node.
136: *
137: * @return
138: */
139: public boolean isLeaf() {
140: return fieldLeaf;
141: }
142:
143: /**
144: * Sets the name.
145: *
146: * @param name The name to set
147: */
148: public void setName(String name) {
149: fieldName = name;
150: }
151:
152: /**
153: * Gets the name.
154: *
155: * @return Returns a String
156: */
157: public String getName() {
158: return fieldName;
159: }
160:
161: /**
162: * Gets the URL.
163: *
164: * @return Returns a String
165: */
166: public String getURL() {
167: if (getParent() != null) {
168: String p = getParent().getURL();
169: if (p.endsWith("/")) {
170: return p + getName();
171: } else {
172: return p + "/" + getName();
173: }
174: } else {
175: return getName(); //the root does not need a special URL since it is part of the base path
176: }
177: }
178:
179: /**
180: * Add the given child to this node's children.
181: *
182: * @param inNode DOCUMENT ME!
183: */
184: public void addChild(DefaultWebTreeNode inNode) {
185: getChildren().add(inNode);
186: inNode.setParent(this );
187: }
188:
189: /**
190: * DOCME
191: */
192: public void reloadChildren() {
193: //nothing to do?
194: }
195:
196: public DefaultWebTreeNode getParent() {
197: return fieldParent;
198: }
199:
200: public void setParent(DefaultWebTreeNode inParent) {
201: fieldParent = inParent;
202: }
203:
204: public Filter getFilter() {
205: return fieldFilter;
206: }
207:
208: public void setFilter(Filter inFilter) {
209: fieldFilter = inFilter;
210: }
211:
212: public boolean hasLoadedChildren() {
213: return fieldChildren != null;
214: }
215:
216: public String getIconSet() {
217: return fieldIconSet;
218: }
219:
220: public void setIconSet(String inIconSet) {
221: fieldIconSet = inIconSet;
222: }
223: }
|