KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > webui > tree > BaseTreeRenderer


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 /*
14  * Created on May 29, 2003
15  */

16 package com.openedit.webui.tree;
17
18 import java.util.HashSet JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import com.openedit.util.PathUtilities;
22
23 /**
24  * This is the base implemation of a JavaScript renderer for output to the web.
25  *
26  * @author Matt Avery
27  */

28 public abstract class BaseTreeRenderer implements TreeRenderer
29 {
30     protected WebTree fieldWebTree;
31     protected String JavaDoc fieldHome;
32
33     protected String JavaDoc fieldImageDir = "/openedit/images/tree";
34     protected Set JavaDoc fieldExpandedNodes;
35     protected Object JavaDoc fieldSelectedNode;
36     
37     protected String JavaDoc fieldUrlPrefix = "";
38     protected String JavaDoc fieldUrlPostfix = "";
39     protected boolean fieldFoldersLinked;
40     
41     public BaseTreeRenderer(WebTree inWebTree)
42     {
43         fieldWebTree = inWebTree;
44     }
45     public BaseTreeRenderer()
46     {
47         
48     }
49     public String JavaDoc getHome()
50     {
51         if ( fieldHome == null )
52         {
53             fieldHome = "";
54         }
55         return fieldHome;
56     }
57
58     public void setHome(String JavaDoc inHome)
59     {
60         fieldHome = inHome;
61     }
62
63     public String JavaDoc getExpandNodeCommand()
64     {
65         return "WebTree.expandTreeNode";
66     }
67
68     public String JavaDoc getImageDir()
69     {
70         return fieldImageDir;
71     }
72     public void setImageDir(String JavaDoc inImageDir)
73     {
74         fieldImageDir = inImageDir;
75     }
76     public void setWebTree(WebTree tree)
77     {
78         fieldWebTree = tree;
79     }
80
81     public WebTree getWebTree()
82     {
83         return fieldWebTree;
84     }
85     protected Set JavaDoc getExpandedNodes()
86     {
87         if (fieldExpandedNodes == null)
88         {
89             fieldExpandedNodes = new HashSet JavaDoc();
90         }
91
92         return fieldExpandedNodes;
93     }
94
95     public void expandNode(Object JavaDoc inNode)
96     {
97         String JavaDoc path = toUrl(inNode);
98         if ( !getExpandedNodes().contains(path))
99         {
100             getExpandedNodes().add(path);
101             //get all the parents somehow
102
Object JavaDoc parent = getWebTree().getModel().getParent( inNode);
103             if ( parent != null)
104             {
105                 expandNode(parent);
106             }
107         }
108     }
109     
110     /* (non-javadoc)
111      * @see com.openedit.webui.tree.TreeRenderer#collapseNode(java.lang.Object)
112      */

113     public void collapseNode(Object JavaDoc inNode)
114     {
115         String JavaDoc path = toUrl(inNode);
116
117         getExpandedNodes().remove(path);
118     }
119
120     public boolean hasBeenExpanded(Object JavaDoc inNode)
121     {
122         String JavaDoc path = toUrl(inNode);
123
124         return getExpandedNodes().contains(path);
125     }
126
127     protected String JavaDoc quotes(String JavaDoc inString)
128     {
129         return "\"" + inString + "\"";
130     }
131     
132     public String JavaDoc renderAsString()
133     {
134         StringBuffer JavaDoc js = new StringBuffer JavaDoc( "var tree = new Tree( \n" );
135         
136         // Ensure the root node is always expanded.
137
expandNode( getWebTree().getModel().getRoot() );
138
139         js.append( renderNewFolder( getWebTree().getModel().getRoot()));
140         js.append( ", " );
141         js.append( quotes(getWebTree().getId()) );
142         js.append( ", \"" );
143         js.append( getHome() );
144         js.append( getImageDir() + "/\" );\n");
145         if ( getUrlPrefix() != null)
146         {
147             js.append( "tree.setViewerUrl( '" + getHome() + getUrlPrefix() + "' );\n" );
148         }
149         if ( getUrlPostfix() != null)
150         {
151             js.append( "tree.setViewerUrlEnding( '" + getUrlPostfix() + "' );\n" );
152         }
153         js.append( "tree.showFolderData= " + isFoldersLinked() + ";\n");
154         if ( getSelectedNode() != null)
155         {
156             js.append("tree.setSelectedNodeById(" );
157             js.append(quotes(getWebTree().getModel().getId( getSelectedNode() ) ));
158             js.append(");\n");
159         }
160         return js.toString();
161     }
162     public void setSelectedNodeByUrl(String JavaDoc inPath)
163     {
164         if ( inPath == null)
165         {
166             return;
167         }
168
169         Object JavaDoc node = null;
170         
171         String JavaDoc root = toUrl( getWebTree().getModel().getRoot() );
172         if ( PathUtilities.match(root, inPath ) )
173         {
174             node = getWebTree().getModel().getRoot();
175         }
176         else
177         {
178             node = findNodeByUrl(getWebTree().getModel().getRoot(), inPath);
179         }
180         setSelectedNode(node);
181         if ( node != null)
182         {
183             //expand the parent
184
node = getWebTree().getModel().getParent(node);
185             if ( node != null)
186             {
187                 expandNode(node);
188             }
189         }
190     }
191
192     public Object JavaDoc findNodeByUrl(Object JavaDoc inRoot, String JavaDoc inUrl)
193     {
194         int count = getWebTree().getModel().getChildCount(inRoot);
195         for (int i = 0; i < count; i++)
196         {
197             Object JavaDoc child = getWebTree().getModel().getChild(inRoot,i);
198             
199             String JavaDoc test = toUrl( child );
200             if ( PathUtilities.match(test,inUrl ) )
201             {
202                 return child;
203             }
204
205             //this test must be a directory
206
if( !inUrl.startsWith(test))
207             {
208                 continue;
209             }
210             child = findNodeByUrl(child,inUrl);
211             if ( child != null)
212             {
213                 return child;
214             }
215         }
216         return null;
217     }
218     
219     public void setSelectedNodeById(String JavaDoc inId)
220     {
221         if ( inId == null)
222         {
223             return;
224         }
225         Object JavaDoc node = getWebTree().getModel().getChildById(inId);
226         setSelectedNode(node);
227         if ( node != null)
228         {
229             //expand the parent
230
node = getWebTree().getModel().getParent(node);
231             if ( node != null)
232             {
233                 expandNode(node);
234             }
235         }
236
237     }
238
239     
240     protected String JavaDoc renderNewFolder(Object JavaDoc inNode)
241     {
242         StringBuffer JavaDoc js = new StringBuffer JavaDoc(
243                 "new Node( " + quotes( toName( inNode ) ) + ", " + quotes( toUrl( inNode ) ) + ", ");
244
245         js.append(quotes(getWebTree().getModel().getId(inNode)));
246
247         if ( getWebTree().getModel().isLeaf( inNode ) )
248         {
249             js.append(",null");
250         }
251         else
252         {
253             js.append(renderChildren(inNode));
254         }
255
256         js.append( "," + getWebTree().getModel().isLeaf( inNode ) + " )");
257
258         return js.toString();
259     }
260
261     protected String JavaDoc renderChildren(Object JavaDoc inNode)
262     {
263         // Use null for our folder icon, and the JavaScript tree will substitute either folder.gif or
264
// folderopen.gif, depending on whether the node is open or closed.
265
StringBuffer JavaDoc js = new StringBuffer JavaDoc();
266         
267         if ( hasBeenExpanded( inNode ) )
268         {
269 // String collapseUrl = quotes("?" + "WebTreeName=" + getWebTree().getName() + "&oe-action=" +
270
// getCollapseNodeCommand() + "&nodeID=" + getWebTree().getModel().getId(inNode));
271
// js.append(collapseUrl +", new Array(\n");
272

273             js.append(", new Array(\n");
274
275             int c = getWebTree().getModel().getChildCount( inNode );
276             for (int i = 0; i < c; i++ )
277             {
278                 if ( i > 0 )
279                 {
280                     js.append(",\n");
281                 }
282                 Object JavaDoc childNode = getWebTree().getModel().getChild( inNode, i );
283                 js.append(renderNewFolder(childNode));
284             }
285
286             js.append(" ) ");
287         }
288         else
289         {
290 // String expandUrl = quotes("?" + "WebTreeName=" + getWebTree().getName() + "&oe-action=" +
291
// getExpandNodeCommand() + "&nodeID=" + getWebTree().getModel().getId(inNode));
292

293             // Might need to make this configurable later on.
294
// Different controllers may want to use different commands here.
295
js.append(",null " );
296             
297         // js.append(
298
// quotes("?" + "WebTreeName=" + getWebTree().getName() + "&oe-action=" +
299
// getExpandNodeCommand() + "&nodeID=" + getWebTree().getModel().getId(inNode)) + ", null, ");
300

301         }
302
303         return js.toString();
304     }
305     
306     /**
307      * The name is used on the tree display
308      * @param inNode
309      * @return
310      */

311     public abstract String JavaDoc toName(Object JavaDoc inNode);
312     
313     /**
314      * The url will be triggered when a node is clicked
315      * @param inNode
316      * @return
317      */

318     public abstract String JavaDoc toUrl(Object JavaDoc inNode);
319
320     public String JavaDoc getCollapseNodeCommand()
321     {
322         return "WebTree.collapseTreeNode";
323     }
324
325     public String JavaDoc getUrlPrefix()
326     {
327         return fieldUrlPrefix;
328     }
329     public void setUrlPrefix(String JavaDoc inUrlPrefix)
330     {
331         fieldUrlPrefix = inUrlPrefix;
332     }
333     public boolean isFoldersLinked()
334     {
335         return fieldFoldersLinked;
336     }
337     public void setFoldersLinked(boolean inFoldersLinked)
338     {
339         fieldFoldersLinked = inFoldersLinked;
340     }
341
342     public Object JavaDoc getSelectedNode()
343     {
344         return fieldSelectedNode;
345     }
346     public void setSelectedNode(Object JavaDoc inSelectedNode)
347     {
348         fieldSelectedNode = inSelectedNode;
349     }
350     public String JavaDoc getUrlPostfix()
351     {
352         return fieldUrlPostfix;
353     }
354     public void setUrlPostfix(String JavaDoc inUrlPostfix)
355     {
356         fieldUrlPostfix = inUrlPostfix;
357     }
358 }
359
Popular Tags