KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > webui > tree > DefaultWebTreeModel


1 /*
2 Copyright (c) 2003 eInnovation Inc. All rights reserved
3
4 This library is free software; you can redistribute it and/or modify it under the terms
5 of the GNU Lesser General Public License as published by the Free Software Foundation;
6 either version 2.1 of the License, or (at your option) any later version.
7
8 This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the GNU Lesser General Public License for more details.
11 */

12
13 package com.openedit.webui.tree;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.List JavaDoc;
17
18 import com.openedit.util.PathUtilities;
19
20 /**
21  * This is a default implementation of a {@link WebTreeModel}, which assumes that all its nodes are
22  * <code>{@link DefaultWebTreeNode}</code>s.
23  *
24  * @author Eric Galluzzo
25  */

26 public class DefaultWebTreeModel implements WebTreeModel
27 {
28     protected DefaultWebTreeNode fieldRoot;
29
30     /**
31      * Construct a tree model with no root (i.e. no tree).
32      */

33     public DefaultWebTreeModel()
34     {
35     }
36
37     /**
38      * Construct a tree model with the given root node.
39      *
40      * @param inRoot DOCUMENT ME!
41      */

42     public DefaultWebTreeModel(DefaultWebTreeNode inRoot)
43     {
44         fieldRoot = inRoot;
45     }
46
47     /* (non-Javadoc)
48      * @see TreeModel#getChild(Object, int)
49      */

50     public Object JavaDoc getChild(Object JavaDoc parent, int index)
51     {
52         return ((DefaultWebTreeNode) parent).getChild(index);
53     }
54     public List JavaDoc getChildrenInRows(Object JavaDoc inParent, int inColCount)
55     {
56         //Now break up the page into rows by dividing the count they wanted
57
List JavaDoc children = getChildren(inParent);
58         double rowscount = (double)children.size() / (double)inColCount;
59         
60         List JavaDoc rows = new ArrayList JavaDoc();
61         for (int i = 0; i < rowscount; i++)
62         {
63             int start = i*inColCount;
64             int end = i*inColCount + inColCount;
65             List JavaDoc sublist = children.subList(start,Math.min( children.size(),end ));
66             rows.add(sublist);
67         }
68         return rows;
69
70     }
71     public List JavaDoc getChildren(Object JavaDoc parent)
72     {
73         return ((DefaultWebTreeNode) parent).getChildren();
74     }
75
76     /* (non-Javadoc)
77      * @see TreeModel#getChildCount(Object)
78      */

79     public int getChildCount(Object JavaDoc parent)
80     {
81         return ((DefaultWebTreeNode) parent).getChildCount();
82     }
83
84     /* (non-Javadoc)
85      * @see TreeModel#getIndexOfChild(Object, Object)
86      */

87     public int getIndexOfChild(Object JavaDoc parent, Object JavaDoc child)
88     {
89         return ((DefaultWebTreeNode) parent).getIndexOfChild((DefaultWebTreeNode) child);
90     }
91
92     /* (non-Javadoc)
93      * @see TreeModel#isLeaf(Object)
94      */

95     public boolean isLeaf(Object JavaDoc node)
96     {
97         return ((DefaultWebTreeNode) node).isLeaf();
98     }
99
100     /**
101      * Set the root node of this tree model.
102      *
103      * @param inRoot DOCUMENT ME!
104      */

105     public void setRoot(DefaultWebTreeNode inRoot)
106     {
107         fieldRoot = inRoot;
108     }
109
110     /* (non-Javadoc)
111      * @see TreeModel#getRoot()
112      */

113     public Object JavaDoc getRoot()
114     {
115         return fieldRoot;
116     }
117     
118     public String JavaDoc getId( Object JavaDoc inNode )
119     {
120         return String.valueOf( ( (DefaultWebTreeNode) inNode ).getID() );
121     }
122
123     public Object JavaDoc getParent(Object JavaDoc inNode)
124     {
125         DefaultWebTreeNode child = (DefaultWebTreeNode) inNode;
126         return child.getParent();
127     }
128
129     public Object JavaDoc getChildById(String JavaDoc inId)
130     {
131         return findNodeById(getRoot(), inId);
132     }
133     
134     public Object JavaDoc findNodeById(Object JavaDoc inRoot, String JavaDoc inId)
135     {
136         String JavaDoc test = getId(inRoot);
137         if ( test.equals(inId) )
138         {
139             return inRoot;
140         }
141         int count = getChildCount(inRoot);
142         for (int i = 0; i < count; i++)
143         {
144             Object JavaDoc child = getChild(inRoot,i);
145             child = findNodeById(child,inId);
146             if ( child != null)
147             {
148                 return child;
149             }
150         }
151         return null;
152     }
153
154     
155 }
156
Popular Tags