KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map JavaDoc;
24
25 import javax.swing.tree.TreeNode JavaDoc;
26
27 public class ClientTreeRenderer
28 extends TreeRenderer {
29
30   // constructors /////////////////////////////////////////////////////////////
31

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

34   // classes //////////////////////////////////////////////////////////////////
35

36   // methods //////////////////////////////////////////////////////////////////
37

38   /**
39    * Renders the header of the tree: a <tt>table</tt> tag.
40    */

41   protected void renderHeader(
42     StringBuffer JavaDoc buf )
43   throws
44     TreeException {
45
46     buf.append( "<table>\n" );
47   }
48
49   /**
50    * Renders the footer of the tree.
51    */

52   protected void renderFooter(
53     StringBuffer JavaDoc buf )
54   throws
55     TreeException {
56
57     buf.append( "</table>\n" );
58   }
59
60
61   protected void renderScript(
62     StringBuffer JavaDoc buf ) {
63
64     String JavaDoc openedHandleImage = getOpenedHandleImage();
65     String JavaDoc closedHandleImage = getClosedHandleImage();
66
67     buf.append(
68       "<script> " +
69       " " +
70       " function getNextTr( el ) { " +
71       " for ( var s = el.nextSibling; s != null; s = s.nextSibling ) " +
72       " if ( s.nodeName.toLowerCase() == \"tr\" ) " +
73       " return s; " +
74       " return null; " +
75       " } " +
76       " " +
77       " function getFirstNode( el ) { " +
78       " for ( var i = 0; i < el.childNodes.length; i++ ) " +
79       " if ( el.childNodes[ i ] && " +
80       " ( el.childNodes[ i ].nodeType == 1 ) ) " +
81       " return el.childNodes[ i ]; " +
82       " return null; " +
83       " } " +
84       " " +
85       " function getFirstText( el ) { " +
86       " for ( var i = 0; i < el.childNodes.length; i++ ) " +
87       " if ( el.childNodes[ i ] && " +
88       " ( el.childNodes[ i ].nodeType == 3 ) ) " +
89       " return el.childNodes[ i ]; " +
90       " return null; " +
91       " } " +
92       " " +
93       " function toggleNode( el ) { " +
94       " var r = getNextTr( el.parentNode.parentNode ); " +
95       " var n = getFirstNode( el ); " +
96       " var t = getFirstText( el ); " +
97       " " +
98       " if ( r.style.display != \"none\" ) { " +
99       " r.style.display = \"none\"; " +
100       " " +
101       " if ( ( n == null ) && ( t != null ) ) " +
102       " t.nodeValue = \"+\"; " +
103       " else " +
104       " n.src = \"" + closedHandleImage + "\"; " +
105       " } " +
106       " else { " +
107       " r.style.display = \"\"; " +
108       " " +
109       " if ( ( n == null ) && ( t != null ) ) " +
110       " t.nodeValue=\"-\"; " +
111       " else " +
112       " n.src = \"" + openedHandleImage + "\"; " +
113       " } " +
114       " } " +
115       "</script>\n" );
116   }
117
118   protected void renderNode(
119     StringBuffer JavaDoc buf,
120     FoldingTreeNode node ) {
121
122     String JavaDoc s = null;
123     Map JavaDoc iconImages = getIconImages();
124
125     //
126
// render node
127
//
128
buf.append( "<tr>" );
129
130     if ( node.isLeaf() ) {
131       if ( iconImages == null ) {
132         buf.append( "<td>&nbsp;</td>" );
133       }
134       else {
135         s = ( String JavaDoc )iconImages.get( SPACER_ICON );
136
137         if ( s == null )
138           buf.append( "<td>&nbsp;</td>" );
139         else
140           buf.append(
141             "<td><img SRC=\"" + s + "\"></img></td>" );
142       }
143     }
144     else {
145
146       //
147
// render handle
148
//
149
buf.append(
150         "<td><a onclick=\"toggleNode(this)\" HREF=\"javascript:;\">" );
151
152       if ( node.getOpened() ) {
153         if ( getOpenedHandleImage() == null )
154           buf.append( "-" );
155         else
156           buf.append(
157             "<img border=\"0\" SRC=\"" + getOpenedHandleImage() + "\"></img>" );
158       }
159       else {
160         if ( getClosedHandleImage() == null )
161           buf.append( "+" );
162         else
163           buf.append(
164             "<img border=\"0\" SRC=\"" + getClosedHandleImage() + "\"></img>" );
165       }
166
167       buf.append( "</a></td>" );
168     }
169
170     //
171
// render icon
172
//
173
if ( iconImages != null ) {
174
175       s = ( String JavaDoc )iconImages.get( node.getIconHint() );
176
177       if ( s == null )
178         s = ( String JavaDoc )iconImages.get( DEFAULT_ICON );
179
180       if ( s == null ) {
181         buf.append( "<td>&nbsp;</td>" );
182       }
183       else {
184         if ( node.getUrl() == null )
185           buf.append( "<td><img SRC=\"" + s + "\"></td>" );
186         else
187           buf.append(
188             "<td><a HREF=\"" + node.getUrl() + "\"><img border=\"0\" SRC=\"" +
189             s + "\"></a></td>" );
190       }
191     }
192
193     //
194
// render label
195
//
196
if ( node.getUrl() == null )
197       buf.append( "<td width=\"100%\">" + node.getLabel() + "</td>" );
198     else
199       buf.append(
200         "<td width=\"100%\"><a HREF=\"" + node.getUrl() + "\">" +
201         node.getLabel() + "</a></td>" );
202
203     buf.append( "</tr>\n" );
204
205     if ( !node.isLeaf() ) {
206
207       //
208
// render children
209
//
210
buf.append( "<tr style=\"display:" );
211
212       if ( node.getOpened() )
213         buf.append( "normal" );
214       else
215         buf.append( "none" );
216
217       buf.append( "\"><td>&nbsp;</td>" );
218
219       buf.append( "<td colspan=\"2\"><table>" );
220
221       for ( int i = 0; i < node.getChildCount(); i++ )
222         renderNode( buf, ( FoldingTreeNode )node.getChildAt( i ) );
223
224       buf.append( "</table></td></tr>\n" );
225     }
226   }
227
228   public String JavaDoc renderTree(
229     FoldingTreeNode root )
230   throws
231     TreeException {
232
233     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
234
235     if ( !scriptRendered_ ) {
236       scriptRendered_ = true;
237       renderScript( buf );
238     }
239
240     renderHeader( buf );
241
242     if ( isRootHidden() ) {
243       for ( int i = 0; i < root.getChildCount(); i++ ) {
244         renderNode( buf, ( FoldingTreeNode )root.getChildAt( i ) );
245       }
246     }
247     else {
248       renderNode( buf, root );
249     }
250
251     renderFooter( buf );
252
253     return buf.toString();
254   }
255
256   // properties ///////////////////////////////////////////////////////////////
257

258   // attributes ///////////////////////////////////////////////////////////////
259

260   private boolean scriptRendered_ = false;
261 }
262
Popular Tags