KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > web > NavItem


1 /*
2  * Copyright (c) 1998-2000 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  *
28  * $Id: NavItem.java,v 1.2 2004/09/29 00:13:49 cvs Exp $
29  */

30
31 package com.caucho.web;
32
33 import com.caucho.util.Tree;
34
35 import java.util.ArrayList JavaDoc;
36 import java.util.Iterator JavaDoc;
37
38 public class NavItem {
39   Tree tree;
40   String JavaDoc title;
41   String JavaDoc link;
42   String JavaDoc description;
43   String JavaDoc brief;
44   String JavaDoc _product;
45
46   NavItem()
47   {
48   }
49
50   void setTree(Tree tree)
51   {
52     this.tree = tree;
53   }
54
55   /**
56    * Returns the underlying tree.
57    */

58   Tree getTree()
59   {
60     return tree;
61   }
62
63   /**
64    * Returns the parent item.
65    */

66   public NavItem getParent()
67   {
68     if (tree == null)
69       return null;
70     
71     Tree parent = tree.getParent();
72
73     return parent == null ? null : (NavItem) parent.getData();
74   }
75
76   public Iterator JavaDoc children()
77   {
78     return tree.iterator();
79   }
80
81   public String JavaDoc getTitle()
82   {
83     return title;
84   }
85
86   public void setTitle(String JavaDoc title)
87   {
88     this.title = title;
89   }
90
91   public String JavaDoc getProduct()
92   {
93     return _product;
94   }
95
96   public void setProduct(String JavaDoc product)
97   {
98     _product = product;
99   }
100
101   public String JavaDoc getBrief()
102   {
103     return brief;
104   }
105
106   public void setBrief(String JavaDoc brief)
107   {
108     this.brief = brief;
109   }
110
111   public String JavaDoc getLink()
112   {
113     return link;
114   }
115
116   public void setLink(String JavaDoc link)
117   {
118     this.link = link;
119   }
120
121   public String JavaDoc getDescription()
122   {
123     return description;
124   }
125
126   public void setDescription(String JavaDoc description)
127   {
128     this.description = description;
129   }
130
131   /**
132    * Returns the previous sibling.
133    */

134   public NavItem getPrevious()
135   {
136     Tree prevTree = tree.getPrevious();
137     
138     if (prevTree == null)
139       return null;
140     else
141       return (NavItem) prevTree.getData();
142   }
143
144   /**
145    * Returns the previous item in a preorder DFS traversal.
146    */

147   public NavItem getPreviousPreorder()
148   {
149     Tree prevTree = tree.getPreviousPreorder();
150     
151     if (prevTree == null)
152       return null;
153     else
154       return (NavItem) prevTree.getData();
155   }
156
157   /**
158    * Returns the next sibling item.
159    */

160   public NavItem getNext()
161   {
162     Tree nextTree = tree.getNext();
163     
164     if (nextTree == null)
165       return null;
166     else
167       return (NavItem) nextTree.getData();
168   }
169
170   /**
171    * Returns the next item in a preorder DFS traversal.
172    */

173   public NavItem getNextPreorder()
174   {
175     Tree nextTree = tree.getNextPreorder();
176     
177     if (nextTree == null)
178       return null;
179     else
180       return (NavItem) nextTree.getData();
181   }
182
183   /**
184    * Returns the specialized family navigation
185    */

186   public ArrayList JavaDoc familyNavigation()
187   {
188     ArrayList JavaDoc list = new ArrayList JavaDoc();
189
190     familyNavigation(tree, list);
191
192     return list;
193   }
194
195   /**
196    * Specialized to get a family navigation.
197    */

198   private boolean familyNavigation(Tree tree, ArrayList JavaDoc results)
199   {
200     if (tree == null) {
201       return false;
202     }
203
204     boolean hasParent = false;
205     if (tree.getParent() != null) {
206       hasParent = familyNavigation(tree.getParent(), results);
207     }
208
209     Iterator JavaDoc iter = tree.iterator();
210
211     boolean hasChild = false;
212     while (iter.hasNext()) {
213       NavItem child = (NavItem) iter.next();
214
215       if (! hasChild && hasParent)
216         results.add(null);
217       
218       hasChild = true;
219       results.add(child);
220     }
221
222     return hasChild || hasParent;
223   }
224
225   public String JavaDoc toString()
226   {
227     return "[NavItem title='" + title + "' link='" + link + "']";
228   }
229 }
230
Popular Tags