KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Created on Dec 22, 2004
3  */

4 package com.openedit.webui.tree;
5
6 import java.util.List JavaDoc;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10
11 import com.openedit.OpenEditException;
12 import com.openedit.WebPageRequest;
13 import com.openedit.modules.BaseModule;
14 import com.openedit.page.PageAction;
15 import com.openedit.page.manage.PageManager;
16 import com.openedit.users.User;
17
18 /**
19  * @author Matthew Avery, mavery@einnovation.com
20  */

21 public class TreeModule extends BaseModule
22 {
23     private static final Log log = LogFactory.getLog(TreeModule.class);
24     public static final String JavaDoc PARAMETER_KEY = "treeName";
25     protected PageManager fieldPageManager;
26     protected String JavaDoc fieldTreeName;
27     
28     public void toggleTreeNode( WebPageRequest inRequest ) throws OpenEditException
29     {
30         WebTree tree = (WebTree) getTree(inRequest);
31         Object JavaDoc childNode = findNode(inRequest, tree);
32         if( childNode != null)
33         {
34             if( tree.getTreeRenderer().hasBeenExpanded(childNode) )
35             {
36                 tree.getTreeRenderer().collapseNode(childNode);
37             }
38             else
39             {
40                 tree.getTreeRenderer().expandNode(childNode);
41             }
42         }
43     }
44     
45     public void expandTreeNode( WebPageRequest inRequest ) throws OpenEditException
46     {
47         WebTree tree = (WebTree) getTree(inRequest);
48         if( tree != null)
49         {
50             Object JavaDoc childNode = findNode(inRequest, tree);
51     
52             if (childNode == null)
53             {
54                 log.error("Most specify nodeID or nodePath for expansion");
55                 return;
56             }
57     
58             tree.getTreeRenderer().expandNode(childNode);
59         }
60     }
61     public void collapseTreeNode( WebPageRequest inRequest ) throws OpenEditException
62     {
63         WebTree tree = getTree(inRequest);
64         if( tree== null)
65         {
66             return;
67         }
68         Object JavaDoc childNode = findNode(inRequest, tree);
69         //log.info("Collapse" + inRequest.getPathUrl() + " " + childNode);
70

71         if (childNode == null)
72         {
73             log.error("Most specify nodeID or nodePath for expansion");
74             return;
75         }
76
77         //WebTree tree = PageTree.getPageTree( getSiteContext() );
78
tree.getTreeRenderer().collapseNode(childNode);
79     }
80     protected Object JavaDoc findNode(WebPageRequest inRequest, WebTree tree)
81     {
82         String JavaDoc id = inRequest.getRequestParameter("nodeID");
83         if (id != null)
84         {
85             Object JavaDoc childNode = tree.getModel().getChildById(id);
86             if ( childNode != null)
87             {
88                 return childNode;
89             }
90         }
91
92         // This assumes that we're using DefaultWebTreeNodes.
93
String JavaDoc path = inRequest.getRequestParameter("nodePath");
94         if (path != null)
95         {
96             Object JavaDoc childNode = getNode((DefaultWebTreeNode) tree.getModel().getRoot(), path.split("/"));
97             return childNode;
98         }
99         return null;
100     }
101     
102     public void reloadTree( WebPageRequest inRequest ) throws OpenEditException
103     {
104         String JavaDoc name = findValue("tree-name", inRequest);
105         
106         //WebTree webTree = (WebTree) inRequest.getSessionValue(treeName + inRequest.getUser());
107

108         inRequest.removeSessionValue(name + inRequest.getUser());
109         inRequest.redirect(inRequest.getPath());
110     }
111     
112     /**
113      * This method initializes the WebTree from the action config.
114      *
115      * @param inRequest
116      * @throws OpenEditException
117      */

118     public WebTree getTree( WebPageRequest inRequest ) throws OpenEditException
119     {
120         String JavaDoc treeid = inRequest.getRequestParameter("treeid");
121         String JavaDoc name = findValue("tree-name",inRequest);
122         if( name == null)
123         {
124             name = findValue("WebTreeName",inRequest); //legacy
125
}
126         if( treeid == null)
127         {
128             treeid = name + "_" + inRequest.getUserName();
129         }
130         WebTree webTree = (WebTree) inRequest.getSessionValue(treeid);
131
132         if (webTree == null && name != null )
133         {
134             //The root is applicable to our model only
135
PageAction inAction = inRequest.getCurrentAction();
136             String JavaDoc root = null;
137             if( inAction.getConfig() != null)
138             {
139                 root = inAction.getConfig().getChildValue( "root"); //This might not be set
140
}
141             RepositoryTreeModel model = null;
142
143             if ((root == null) || root.equals("/"))
144             {
145                 // TODO: This is a shortcut to migrate to the new core.
146
// Instead, we need to implement a PageTreeModel that can accept a Page
147
// or perhaps a ContentItem.
148
model = new RepositoryTreeModel( getPageManager().getRepository() );
149             }
150             else
151             {
152                 
153                 model = new RepositoryTreeModel(getPageManager().getRepository(), root );
154             }
155             getPageManager().addPageAccessListener(model);
156             
157             String JavaDoc ignore = (String JavaDoc) inAction.getConfig().getChildValue("excludes");
158             if (ignore != null)
159             {
160                 String JavaDoc[] types = ignore.split(",");
161                 for (int i = 0; i < types.length; i++)
162                 {
163                     model.ignore(types[i].trim());
164                 }
165             }
166
167             webTree = new WebTree(model);
168             webTree.setName(name);
169             webTree.setId(treeid);
170             //setup the renderer
171
WebTreeNodeTreeRenderer renderero = new WebTreeNodeTreeRenderer(webTree);
172             String JavaDoc prefix = inAction.getConfig().getChildValue( "url-prefix");
173             if (prefix != null)
174             {
175                 renderero.setUrlPrefix(prefix);
176             }
177             String JavaDoc friendly = (String JavaDoc) inAction.getConfig().getChildValue("friendlyNames");
178             if (friendly != null)
179             {
180                 renderero.setFriendlyNames(Boolean.valueOf(friendly).booleanValue());
181             }
182             String JavaDoc home = (String JavaDoc) inRequest.getPageValue( "home" );
183             renderero.setHome(home);
184             
185             webTree.setTreeRenderer(renderero);
186             
187             inRequest.putSessionValue(treeid, webTree);
188         }
189         if( webTree != null)
190         {
191             inRequest.putPageValue("pageManager",getPageManager());
192             inRequest.putPageValue(name , webTree);
193         }
194         return webTree;
195     }
196
197     /*
198      * Uses a string path name
199      */

200     protected DefaultWebTreeNode getNode(DefaultWebTreeNode inRoot, String JavaDoc[] inPath)
201     {
202         DefaultWebTreeNode currentPath = inRoot;
203         
204         for (int i = 0; i < inPath.length; i++)
205         {
206             List JavaDoc children = currentPath.getChildren();
207     
208             for (int j = 0; j < children.size(); j++)
209             {
210                 DefaultWebTreeNode child = (DefaultWebTreeNode) children.get(j);
211     
212                 if (inPath[i].equals(child.getName()))
213                 {
214                     currentPath = child;
215                 }
216             }
217         }
218     
219         return currentPath;
220     }
221
222
223     public PageManager getPageManager()
224     {
225         return fieldPageManager;
226     }
227     public void setPageManager( PageManager pageManager )
228     {
229         fieldPageManager = pageManager;
230     }
231 }
232
Popular Tags