KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > tree > ServerTreeRenderer


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.tree;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import javax.swing.tree.TreeNode JavaDoc;
29
30 /**
31  * <p>
32  * A <tt>TreeRenderer</tt> for trees whose state is maintained on the server.
33  * The renderer only renders those nodes that are visible, and a request must
34  * be made to open or close a node. A mechanism must exist on the server
35  * side to handle those requests. While more elaborate to implement, this
36  * approach makes it possible maintain a tree's state across requests.
37  * </p>
38  */

39 public class ServerTreeRenderer
40 extends TreeRenderer {
41
42   // constructors /////////////////////////////////////////////////////////////
43

44   // constants ////////////////////////////////////////////////////////////////
45

46   // classes //////////////////////////////////////////////////////////////////
47

48   // methods //////////////////////////////////////////////////////////////////
49

50   /**
51    *
52    */

53   public String JavaDoc renderTree(
54     FoldingTreeNode root )
55   throws
56     TreeException {
57
58     if ( openUrl_ == null )
59       throw new TreeException( "openUrl has not been set." );
60
61     if ( closeUrl_ == null )
62       throw new TreeException( "closeUrl has not been set." );
63
64     //
65
// set defaults
66
//
67
if ( getClosedHandleImage() == null )
68       closedHandleImageHtml_ = "+";
69     else
70       closedHandleImageHtml_ =
71         "<img border=\"0\" SRC=\"" + getClosedHandleImage() + "\">";
72
73     if ( getOpenedHandleImage() == null )
74       openedHandleImageHtml_ = "-";
75     else
76       openedHandleImageHtml_ =
77         "<img border=\"0\" SRC=\"" + getOpenedHandleImage() + "\">";
78
79     //
80
// get visible nodes
81
//
82
List JavaDoc nodes = new ArrayList JavaDoc();
83     getVisibleNodes( nodes, root );
84
85     //
86
// get maximum visible level
87
//
88
int maxVisibleLevel = getMaxLevel( nodes );
89
90     //
91
// render the tree
92
//
93
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
94     renderHeader( buf );
95     
96     for ( Iterator JavaDoc iter = nodes.iterator(); iter.hasNext(); ) {
97       FoldingTreeNode node = ( FoldingTreeNode )iter.next();
98       renderNode( buf, maxVisibleLevel, node );
99     }
100
101     renderFooter( buf );
102
103     //
104
// return the html
105
//
106
return buf.toString();
107   }
108
109   /**
110    * Transforms the tree into a list of nodes that should be displayed.
111    */

112   protected void getVisibleNodes(
113     List JavaDoc rows,
114     FoldingTreeNode node ) {
115
116     if ( !node.isRoot() || !isRootHidden() )
117       rows.add( node );
118
119     if ( !node.isLeaf() && node.getOpened() )
120       for ( int i = 0; i < node.getChildCount(); i++ )
121         getVisibleNodes( rows, ( FoldingTreeNode )node.getChildAt( i ) );
122   }
123
124   /**
125    * Returns the level of the deepest level node in <tt>nodes</tt>.
126    */

127   protected int getMaxLevel(
128     List JavaDoc nodes ) {
129
130     int level = 0;
131     for ( Iterator JavaDoc iter = nodes.iterator(); iter.hasNext(); ) {
132       FoldingTreeNode node = ( FoldingTreeNode )iter.next();
133
134       int tmp = node.getLevel();
135
136       if ( tmp > level )
137         level = tmp;
138     }
139
140     return level;
141   }
142
143   /**
144    * Renders the header of the tree: a <tt>table</tt> tag.
145    */

146   protected void renderHeader(
147     StringBuffer JavaDoc buf )
148   throws
149     TreeException {
150
151     buf.append( "<table>\n" );
152   }
153
154   /**
155    * Renders the footer of the tree.
156    */

157   protected void renderFooter(
158     StringBuffer JavaDoc buf )
159   throws
160     TreeException {
161
162     buf.append( "</table>\n" );
163   }
164
165   /**
166    * Renders a node.
167    */

168   protected void renderNode(
169     StringBuffer JavaDoc buf,
170     int maxVisibleLevel,
171     FoldingTreeNode node ) {
172
173     //
174
// if the root is hidden, it throws off most of these calculations by one;
175
// find this offset peppered through the code below
176
//
177
int hiddenRootOffset = 0;
178     if ( isRootHidden() )
179       hiddenRootOffset = 1;
180
181     //
182
// scratch strings
183
//
184
String JavaDoc s = null;
185     String JavaDoc t = null;
186
187     buf.append( "<tr>" );
188
189     //
190
// insert an empty cell for each level deep the node is
191
//
192
for ( int i = hiddenRootOffset; i < node.getLevel(); i++ )
193       buf.append( "<td>&nbsp;</td>" );
194
195     //
196
// render open/close link; leaf?
197
//
198
if ( node.isLeaf() ) {
199       buf.append(
200         "<td>&nbsp;</td>" );
201     }
202     else {
203       //
204
// opened?
205
//
206
if ( !node.getOpened() )
207         buf.append(
208           "<td><a HREF=\"" + openUrl_ +
209           node.hashCode() + "\">" + closedHandleImageHtml_ + "</a></td>" );
210
211       //
212
// closed
213
//
214
else
215         buf.append(
216           "<td><a HREF=\"" + closeUrl_ +
217           node.hashCode() + "\">" + openedHandleImageHtml_ + "</a></td>" );
218     }
219
220     //
221
// render icon
222
//
223
Map JavaDoc iconImages = getIconImages();
224
225     if ( iconImages != null ) {
226
227       s = ( String JavaDoc )iconImages.get( node.getIconHint() );
228
229       if ( s == null )
230         s = ( String JavaDoc )iconImages.get( DEFAULT_ICON );
231
232       if ( s == null ) {
233         buf.append( "<td>&nbsp;</td>" );
234       }
235       else {
236         if ( node.getUrl() == null )
237           buf.append( "<td><img SRC=\"" + s + "\"></td>" );
238         else
239           buf.append(
240             "<td><a HREF=\"" + node.getUrl() + "\"><img border=\"0\" SRC=\"" +
241             s + "\"></a></td>" );
242       }
243     }
244
245     //
246
// render label
247
//
248
if ( node.getUrl() == null )
249       buf.append(
250         "<td width=\"100%\" colspan=\"" +
251         ( maxVisibleLevel - node.getLevel() + 1 ) + "\">" +
252         node.getLabel() + "</td>" );
253     else
254       buf.append(
255         "<td width=\"100%\" colspan=\"" +
256         ( maxVisibleLevel - node.getLevel() + 1 ) +
257         "\"><a HREF=\"" + node.getUrl() + "\">" + node.getLabel() +
258         "</a></td>" );
259
260     buf.append( "</tr>\n" );
261   }
262
263   // properties ///////////////////////////////////////////////////////////////
264

265   /**
266    * Sets the url to open a node; the hash code of the node to be opened will
267    * be appended to the end of this url.
268    */

269   public void setOpenUrl( String JavaDoc openUrl ) {
270     openUrl_ = openUrl;
271   }
272
273   /**
274    * Sets the url to close a node; the hash code of the node to be closed will
275    * be appended to the end of this url.
276    */

277   public void setCloseUrl( String JavaDoc closeUrl ) {
278     closeUrl_ = closeUrl;
279   }
280
281   // attributes ///////////////////////////////////////////////////////////////
282

283   protected String JavaDoc openUrl_ = null;
284   protected String JavaDoc closeUrl_ = null;
285
286   private String JavaDoc openedHandleImageHtml_ = null;
287   private String JavaDoc closedHandleImageHtml_ = null;
288 }
289
Popular Tags