KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > shim > FoldingNavRenderer


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.shim;
22
23 import org.apache.commons.lang.StringUtils;
24
25 /**
26  * Renders a folding navigation.
27  */

28 public class FoldingNavRenderer {
29
30   // constructors /////////////////////////////////////////////////////////////
31

32   // constants ////////////////////////////////////////////////////////////////
33

34   public static final String JavaDoc URL_KEY = "{url}";
35   public static final String JavaDoc TITLE_KEY = "{title}";
36   public static final String JavaDoc LEVEL_KEY = "{level}";
37
38   // classes //////////////////////////////////////////////////////////////////
39

40   // methods //////////////////////////////////////////////////////////////////
41

42   /**
43    * Appends this nav's header to <tt>buf</tt>.
44    */

45   protected void renderHeader(
46     StringBuffer JavaDoc buf ) {
47
48     buf.append( getHeader() );
49   }
50
51   /**
52    * Appends a link to <tt>buf</tt>, using the template set by {@link #setLink
53    * setLink()} and replacing placeholders ({@link #URL_KEY URL_KEY}, {@link
54    * #TITLE_KEY TITLE_KEY}, {@link #LEVEL_KEY LEVEL_KEY}) with their
55    * appropriate values.
56    */

57   protected void renderLink(
58     StringBuffer JavaDoc buf,
59     Link link,
60     int level ) {
61
62     String JavaDoc s = null;
63
64     if ( link == cur_ )
65       s = getCurLink();
66     else
67       s = getLink();
68
69     //
70
// don't try to remove site context from path in edit mode
71
//
72
s = StringUtils.replace(
73       s, URL_KEY, ShimUtils.getLinkUrl( link ) );
74
75     s = StringUtils.replace( s, TITLE_KEY, link.getTitle() );
76     s = StringUtils.replace( s, LEVEL_KEY, "" + level );
77
78     buf.append( s );
79   }
80
81   /**
82    * Appends this nav's footer to <tt>buf</tt>.
83    */

84   protected void renderFooter(
85     StringBuffer JavaDoc buf ) {
86
87     buf.append( getFooter() );
88   }
89
90   protected void traverse(
91     StringBuffer JavaDoc buf,
92     Link link,
93     int level ) {
94
95     if ( link.getHidden() )
96       return;
97
98     if ( link == cur_ ) {
99       if ( link.getLevel() >= ( start_ - 1 ) )
100         renderLink( buf, link, level );
101
102       for ( int i = 0; i < link.getChildCount(); i++ )
103         traverse( buf, ( Link )link.getChildAt( i ), level + 1 );
104     }
105
106     else if ( link.isNodeDescendant( cur_ ) ) {
107       if ( link.getLevel() >= ( start_ - 1 ) ) {
108         renderLink( buf, link, level );
109         level++;
110       }
111
112       for ( int i = 0; i < link.getChildCount(); i++ )
113         traverse( buf, ( Link )link.getChildAt( i ), level );
114     }
115
116     else if (
117       ( ( link.getLevel() - cur_.getLevel() ) < context_ ) &&
118       ( ( link.getLevel() - cur_.getLevel() ) >= 0 ) ) {
119       if ( link.getLevel() >= ( start_ - 1 ) )
120         renderLink( buf, link, level );
121       
122       for ( int i = 0; i < link.getChildCount(); i++ )
123         traverse( buf, ( Link )link.getChildAt( i ), level + 1 );
124     }
125
126     else if (
127       link.isNodeAncestor( cur_ ) &&
128       ( ( link.getLevel() - cur_.getLevel() ) <= depth_ ) ) {
129
130       if ( link.getLevel() >= ( start_ - 1 ) )
131         renderLink( buf, link, level );
132       
133       for ( int i = 0; i < link.getChildCount(); i++ )
134         traverse( buf, ( Link )link.getChildAt( i ), level + 1 );
135     }
136
137     else if ( level <= top_ ) {
138       if ( link.getLevel() >= ( start_ - 1 ) ) {
139         renderLink( buf, link, level );
140         level++;
141       }
142       
143       if ( level <= top_ )
144         for ( int i = 0; i < link.getChildCount(); i++ )
145           traverse( buf, ( Link )link.getChildAt( i ), level );
146     }
147   }
148
149   /**
150    * Appends this nav's footer to <tt>buf</tt>.
151    */

152   protected void renderNav(
153     StringBuffer JavaDoc buf ) {
154
155     renderHeader( buf );
156
157     traverse( buf, root_, 1 );
158     
159     renderFooter( buf );
160   }
161
162   /**
163    * Returns the HTML for the nav.
164    */

165   public String JavaDoc getNavHtml() {
166     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
167     renderNav( buf );
168     return buf.toString();
169   }
170
171   // properties ///////////////////////////////////////////////////////////////
172

173   public Link getRoot() {
174     return root_;
175   }
176
177   public void setRoot(
178     Link root ) {
179     root_ = root;
180   }
181
182   public Link getCur() {
183     return cur_;
184   }
185
186   public void setCur(
187     Link cur ) {
188     cur_ = cur;
189   }
190
191   public int getStart() {
192     return start_;
193   }
194
195   public void setStart(
196     int start ) {
197     start_ = start;
198   }
199
200   public int getContext() {
201     return context_;
202   }
203
204   public void setContext(
205     int context ) {
206     context_ = context;
207   }
208
209   public int getDepth() {
210     return depth_;
211   }
212
213   public void setDepth(
214     int depth ) {
215     depth_ = depth;
216   }
217
218   public int getTop() {
219     return top_;
220   }
221
222   public void setTop(
223     int top ) {
224     top_ = top;
225   }
226
227   public String JavaDoc getHeader() {
228     return header_;
229   }
230
231   public void setHeader(
232     String JavaDoc header ) {
233     header_ = header;
234   }
235
236   public String JavaDoc getLink() {
237     return link_;
238   }
239
240   public void setLink(
241     String JavaDoc link ) {
242     link_ = link;
243   }
244
245   public String JavaDoc getCurLink() {
246     return curLink_;
247   }
248
249   public void setCurLink(
250     String JavaDoc curLink ) {
251     curLink_ = curLink;
252   }
253
254   public String JavaDoc getFooter() {
255     return footer_;
256   }
257
258   public void setFooter(
259     String JavaDoc footer ) {
260     footer_ = footer;
261   }
262
263   public boolean getIsEditMode() {
264     return isEditMode_;
265   }
266
267   public void setIsEditMode(
268     boolean isEditMode ) {
269     isEditMode_ = isEditMode;
270   }
271
272   // attributes ///////////////////////////////////////////////////////////////
273

274   private Link root_ = null;
275   private Link cur_ = null;
276   private int start_ = 1;
277   private int context_ = 1;
278   private int depth_ = 1;
279   private int top_ = 1;
280   private String JavaDoc header_ = "";
281   private String JavaDoc link_ = "";
282   private String JavaDoc curLink_ = "";
283   private String JavaDoc footer_ = "";
284   private boolean isEditMode_ = false;
285 }
286
Popular Tags