KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > meshcms > webui > FolderXTree


1 /*
2  * MeshCMS - A simple CMS based on SiteMesh
3  * Copyright (C) 2004-2007 Luciano Vernaschi
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * You can contact the author at http://www.cromoteca.com
20  * and at info@cromoteca.com
21  */

22
23 package org.meshcms.webui;
24
25 import java.io.*;
26 import org.meshcms.core.*;
27 import org.meshcms.util.*;
28
29 /**
30  * Used internally to create the JavaScript code needed by xTree to display
31  * the folder tree. xTree has been created by <a HREF="http://webfx.eae.net/">WebFX</a>.
32  */

33 public class FolderXTree extends DirectoryParser {
34   WebSite webSite;
35   UserInfo userInfo;
36   Writer writer;
37   String JavaDoc thumbsParam;
38   String JavaDoc rootName;
39
40   /**
41    * Creates a new instance.
42    *
43    * @param writer used to write down the needed JavaScript code.
44    */

45   public FolderXTree(WebSite webSite, UserInfo userInfo, Writer writer,
46       String JavaDoc thumbsParam, String JavaDoc rootName) {
47     this.webSite = webSite;
48     this.userInfo = userInfo;
49     this.writer = writer;
50     this.thumbsParam = thumbsParam;
51     this.rootName = rootName;
52     setRecursive(true);
53     setSorted(true);
54     setInitialDir(webSite.getRootFile());
55     setDaemon(true);
56     setName("XTree builder");
57   }
58
59   protected boolean preProcess() {
60     try {
61       writer.write("var folder0 = new WebFXTree('" + rootName + "');\n");
62       writer.write("folder0.setBehavior('explorer');\n");
63       writer.write("folder0.action='showlist.jsp?folder=" + thumbsParam + "';\n");
64       writer.write("folder0.target='listframe';\n");
65       writer.write("folder0.icon='images/world.gif';\n");
66       writer.write("folder0.openIcon='images/world.gif';\n");
67     } catch (IOException ex) {
68       return false;
69     }
70
71     return true;
72   }
73
74   protected void processFile(File file, Path path) {
75     // nothing to do here
76
}
77
78   protected boolean preProcessDirectory(File file, Path path) {
79     if (path.equals(webSite.getRepositoryPath())) {
80       return false;
81     }
82
83     boolean include = true;
84
85     try {
86       DirectoryInfo di = getDirectoryInfo(webSite, userInfo, path);
87
88       if (di.include) {
89         int code = WebUtils.getMenuCode(path);
90           writer.write("\nvar folder" + code + " = new WebFXTreeItem(\"" +
91               path.getLastElement() + "\");\n");
92           writer.write("folder" + WebUtils.getMenuCode(path.getParent()) +
93               ".add(folder" + code + ");\n");
94           writer.write("folder" + code + ".action=\"showlist.jsp?folder=" +
95               path + thumbsParam + "\";\n");
96           writer.write("folder" + code + ".target='listframe';\n");
97         if (di.iconName != null) {
98           writer.write("folder" + code + ".icon='images/" + di.iconName +
99               ".gif';\n");
100           writer.write("folder" + code + ".openIcon='images/" + di.iconName +
101               "open.gif';\n");
102         }
103       }
104     } catch (IOException ex) {
105       return false;
106     }
107
108     return include;
109   }
110
111   /**
112    * @return info about a directory, based on path and permissions.
113    */

114   public static DirectoryInfo getDirectoryInfo(WebSite webSite,
115       UserInfo userInfo, Path dirPath) {
116     DirectoryInfo di = new DirectoryInfo();
117
118     if (dirPath.isContainedIn(webSite.getVirtualSitesPath())) {
119       if (userInfo.canDo(UserInfo.CAN_DO_ADMINTASKS)) {
120         di.include = true;
121
122         if (dirPath.getElementCount() <
123             webSite.getVirtualSitesPath().getElementCount() + 2) {
124           di.iconName = "sitefolder";
125         } else if (webSite instanceof MainWebSite) {
126           Path siteRoot =
127               dirPath.getPartial(webSite.getVirtualSitesPath().getElementCount() + 1);
128           VirtualWebSite vws =
129               ((MainWebSite) webSite).getVirtualSite(siteRoot.getLastElement());
130           return getDirectoryInfo(vws, userInfo, dirPath.getRelativeTo(siteRoot));
131         }
132       } else {
133         di.include = false;
134       }
135     } else if (webSite.isSystem(dirPath)) {
136       di.include = false;
137       /* di.iconName = "systemfolder";
138       di.include = userInfo.canDo(UserInfo.CAN_DO_ADMINTASKS); */

139     } else if (dirPath.isContainedIn(webSite.getGeneratedFilesPath())) {
140       di.include = false;
141     } else if (dirPath.isContainedIn(webSite.getCMSPath())) {
142       di.iconName = "cmsfolder";
143     }
144
145     return di;
146   }
147
148   /**
149    * Encapsulates inclusion flag and icon for a directory.
150    */

151   public static class DirectoryInfo {
152     public boolean include = true;
153     public String JavaDoc iconName = null;
154   }
155 }
156
Popular Tags