001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/jsf/tags/sakai_2-4-1/widgets/src/java/org/sakaiproject/jsf/model/TreeLevel.java $
003: * $Id: TreeLevel.java 9278 2006-05-10 23:29:21Z ray@media.berkeley.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.jsf.model;
021:
022: import java.util.ArrayList;
023: import java.util.List;
024: import java.util.*;
025:
026: public class TreeLevel {
027: private TreeLevel parent = null;
028: private ArrayList childList;
029: private int index;
030: boolean child;
031:
032: /**
033: * "Parent" constructor. This is a top level and is its own parent and is a
034: * child of none.
035: */
036: public TreeLevel(int index) {
037: this .childList = new ArrayList();
038: this .parent = this ;
039: this .index = index;
040: this .child = false;
041: }
042:
043: /**
044: * No-arg constructor. "Parent" constructor equivalent, with index set to 0.
045: */
046: public TreeLevel() {
047: this .childList = new ArrayList();
048: this .parent = this ;
049: this .index = 0;
050: this .child = false;
051: }
052:
053: /**
054: * "Child" constructor. Create a child of "parent".
055: * @param parent TreeLevel
056: */
057: public TreeLevel(TreeLevel parent) {
058: this .childList = new ArrayList();
059: this .parent = parent;
060: this .index = parent.addChild(this );
061: this .child = true;
062: }
063:
064: /**
065: * Get parent, or self if no parent.
066: * @return TreeLevel
067: */
068: public TreeLevel getParent() {
069: return parent;
070: }
071:
072: /**
073: * Get list of all child TreeLevels
074: * @return List
075: */
076: public List getChildren() {
077: return childList;
078: }
079:
080: /**
081: * True if this has a parent.
082: * @return boolean
083: */
084: public boolean isChild() {
085: return child;
086: }
087:
088: /**
089: * String representing 0-indexed parent-child relationships.
090: * Example: 18_9_4 is the fourth child of the nineth child of 18
091: * @return String
092: */
093: public String toString() {
094: // recurse for children, done if parent
095: if (this .isChild()) {
096: return this .getParent().toString() + "_" + index;
097: } else {
098: return "" + index;
099: }
100: }
101:
102: /**
103: * Utility reciprocal method to add a child when it chooses this as its parent.
104: * @param child TreeLevel
105: * @return int
106: */
107: protected int addChild(TreeLevel child) {
108: childList.add(child);
109: return childList.size();
110: }
111:
112: /**
113: * test code, demonstrates usage.
114: * @param args String[]
115: */
116: public static void main(String[] args) {
117: System.out.println("testing flat");
118: ArrayList treeList = new ArrayList();
119: for (int i = 0; i < 10; i++) {
120: treeList.add(new TreeLevel(i));
121: }
122: for (Iterator iter = treeList.iterator(); iter.hasNext();) {
123: Object item = (Object) iter.next();
124: System.out.println("LEVEL: " + item);
125: }
126: System.out.println("testing hierarchy");
127: treeList = new ArrayList();
128: TreeLevel t0 = new TreeLevel(0);
129: treeList.add(t0);
130:
131: for (int i = 0; i < 10; i++) {
132: TreeLevel t1 = new TreeLevel(t0);
133: treeList.add(t1);
134: for (int j = 0; j < 5; j++) {
135: TreeLevel t2 = new TreeLevel(t1);
136: treeList.add(t2);
137: }
138: }
139:
140: for (Iterator iter = treeList.iterator(); iter.hasNext();) {
141: Object item = (Object) iter.next();
142: System.out.println("LEVEL: " + item);
143: }
144: }
145:
146: }
|