KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openedit > links > webtree > LinkNode


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 org.openedit.links.webtree;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import javax.naming.LinkException JavaDoc;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.openedit.links.Link;
25
26 import com.openedit.util.PathUtilities;
27 import com.openedit.webui.tree.DefaultWebTreeNode;
28
29
30 /**
31  * This class represents a node in a {@link LinkTreeModel}.
32  *
33  * @author Matt Avery, mavery@einnovation.com
34  */

35 public class LinkNode extends DefaultWebTreeNode implements Comparable JavaDoc
36 {
37     //public static final DefaultWebTreeNode ERROR_NODE = new DefaultWebTreeNode( "Error accessing tree node." );
38
protected Set JavaDoc fieldIgnoreTypes;
39     protected Link fieldLink;
40
41     private static final Log log = LogFactory.getLog(LinkNode.class);
42     /**
43      * Create a new <code>PageTreeNode</code>.
44      *
45      * @param inFile The file that this node represents
46      * @param inPath The path to the file, relative to the site context root
47      * @throws LinkException
48      */

49     public LinkNode( Link inLink )
50     {
51         super( inLink.getId(), inLink.getText() );
52         fieldLink = inLink;
53         setLeaf(!getLink().hasChildren());
54     }
55     
56     /**
57      * Get the first child of this node with the given name.
58      *
59      * @param inName The node name
60      *
61      * @return The node, or <code>null</code> if no such child could be found
62      */

63     public LinkNode getChild(String JavaDoc inName)
64     {
65         for (Iterator JavaDoc iter = getChildren().iterator(); iter.hasNext();)
66         {
67             LinkNode child = (LinkNode) iter.next();
68
69             if ((child.getName() != null) && child.getName().equals(inName))
70             {
71                 return child;
72             }
73         }
74
75         return null;
76     }
77
78     /* (non-Javadoc)
79      * @see DefaultWebTreeNode#getChildren()
80      */

81     public List JavaDoc getChildren()
82     {
83         if (fieldChildren == null)
84         {
85             fieldChildren = new ArrayList JavaDoc();
86             reloadChildren();
87         }
88
89         return fieldChildren;
90     }
91
92     /* (non-Javadoc)
93      * @see java.lang.Comparable#compareTo(Object)
94      */

95     public int compareTo(Object JavaDoc o)
96     {
97         if (o == null)
98         {
99             return 1;
100         }
101
102         if (o instanceof LinkNode)
103         {
104             LinkNode node = (LinkNode) o;
105
106             return getLink().getPath().compareTo(node.getLink().getPath());
107         }
108         else
109         {
110             return 0;
111         }
112     }
113
114     /**
115      * Find the descendant of this node with the given path.
116      *
117      * @param inPath The path to find
118      *
119      * @return The node at the given path, or <code>null</code> if it could not be found
120      */

121     public LinkNode findNode(String JavaDoc inPath)
122     {
123         // Quick initial checks...
124
if (!inPath.startsWith("/"))
125         {
126             inPath = "/" + inPath;
127         }
128
129         if (inPath.equals("") || inPath.equals("/"))
130         {
131             return this;
132         }
133
134         int beforeSlashIndex = 0;
135
136         if (inPath.startsWith("/"))
137         {
138             beforeSlashIndex = 1;
139         }
140
141         int nextSlashIndex = inPath.indexOf('/', beforeSlashIndex);
142
143         if (nextSlashIndex < 0)
144         {
145             nextSlashIndex = inPath.length();
146         }
147
148         String JavaDoc childName = inPath.substring(beforeSlashIndex, nextSlashIndex);
149
150         LinkNode child = getChild(childName);
151
152         if (child == null)
153         {
154             return null;
155         }
156         else
157         {
158             return child.findNode(inPath.substring(nextSlashIndex));
159         }
160     }
161
162     /**
163      * Reload the children of this page tree node.
164      */

165     public void reloadChildren()
166     {
167         getChildren().clear();
168
169         if (getLink().hasChildren())
170         {
171             List JavaDoc childItems = getLink().getChildren();
172             for ( Iterator JavaDoc iterator = childItems.iterator(); iterator.hasNext(); )
173             {
174                 Link childItem = (Link) iterator.next();
175
176                 String JavaDoc name = childItem.getPath();
177                 boolean okToAdd = true;
178
179                 if( getIgnoreTypes() != null)
180                 {
181                     //we want to ignore some files in this directory ie. CVS
182
for (Iterator JavaDoc iter = getIgnoreTypes().iterator(); iter.hasNext();)
183                     {
184                         String JavaDoc key = (String JavaDoc) iter.next();
185     
186                         if (PathUtilities.match(name, key))
187                         {
188                             okToAdd = false;
189                             break;
190                         }
191                     }
192                 }
193                 if (okToAdd)
194                 {
195
196                     LinkNode child = createNode( childItem );
197                     child.setParent(this);
198
199                     getChildren().add(child);
200                 }
201             }
202
203             // Make sure the files appear in lexicographically increasing
204
// order, with all the directories appearing before all the files.
205
}
206     }
207
208     protected void setIgnoreTypes(Set JavaDoc inIgnoreTypes)
209     {
210         fieldIgnoreTypes = inIgnoreTypes;
211     }
212
213     protected Set JavaDoc getIgnoreTypes()
214     {
215         return fieldIgnoreTypes;
216     }
217
218     /**
219      * Method createPageTreeNode.
220      *
221      * @param childFile
222      * @param path
223      *
224      * @return PageTreeNode
225      */

226     protected LinkNode createNode(Link childItem)
227     {
228         LinkNode node = new LinkNode( childItem);
229         node.setIgnoreTypes(getIgnoreTypes());
230         return node;
231     }
232
233     public Link getLink()
234     {
235         return fieldLink;
236     }
237     public void setLink( Link Link )
238     {
239         fieldLink = Link;
240     }
241     public String JavaDoc getURL()
242     {
243         return getLink().getId();
244     }
245
246 }
247
Popular Tags