18 package org.zkforge.ckez;
24 import org.zkoss.lang.Strings;
25 import org.zkoss.zk.ui.Component;
26 import org.zkoss.zk.ui.UiException;
27 import org.zkoss.zk.ui.event.Event;
28 import org.zkoss.zk.ui.event.EventListener;
29 import org.zkoss.zk.ui.util.GenericForwardComposer;
30 import org.zkoss.zul.*;
34 private static final String[] EXCLUDE_FOLDERS = {
"WEB-INF",
"META-INF"};
35 private static final String[] EXCLUDE_FILES = {};
36 private static final String[] IMAGES = {
"gif",
"jpg",
"jpeg",
"png"};
37 private static final String[] FILES = {
"htm",
"html",
"php",
"php3",
38 "php5",
"phtml",
"asp",
"aspx",
39 "ascx",
"jsp",
"cfm",
"cfc",
"pl",
40 "bat",
"exe",
"dll",
"reg",
"cgi",
"asmx"};
41 private static final String[] FLASH = {
"swf"};
42 private static final String[] MEDIA = {
"swf",
"fla",
"jpg",
"gif",
"jpeg",
"png",
"avi",
"mpg",
"mpeg"};
44 private String apName =
"";
45 private String type =
"";
46 private Map fileFilterMap;
50 private Toolbarbutton selBtn;
54 super.doAfterCompose(comp);
56 type = ((String[])param.get(
"Type"))[0];
57 fileFilterMap = initFileFilterMap();
58 String url = getFolderUrl();
60 if (Strings.isBlank(url))
return;
61 if (url.startsWith(
"./"))
62 url = url.substring(1);
64 if (!url.startsWith(
"/"))
66 Map rootFolderMap =
new TreeMap();
67 Map map =
new TreeMap();
68 rootFolderMap.put(url, map);
70 parseFolders(url, map);
78 private String getFolderUrl() {
79 String uuid = ((String[])param.get(
"CKEditor"))[0];
80 uuid = uuid.substring(0, uuid.lastIndexOf(
"-cnt"));
83 if (
"Images".equals(type))
84 url = (String) session.getAttribute(
"filebrowserImageBrowseUrl"+ uuid);
85 else if (
"Flash".equals(type))
86 url = (String) session.getAttribute(
"filebrowserFlashBrowseUrl"+ uuid);
89 url = (String) session.getAttribute(
"filebrowserBrowseUrl"+ uuid);
91 String updateURI = application.getUpdateURI();
92 apName = updateURI.substring(0, updateURI.indexOf(
"/zkau"));
93 int index = url.indexOf(apName);
94 if (index == 0 && apName.length() > 0)
95 index = url.indexOf(
"/", 1);
97 url = index < 0 ? url: url.substring(index);
99 index = url.lastIndexOf(
";jsessionid");
101 url = url.substring(0, index);
103 if (application.getResourcePaths(url) ==
null)
104 throw new UiException(
"Folder not found: " + url);
109 private List initTreeModel(Map parentFolderMap, List list) {
110 for (Iterator it = parentFolderMap.entrySet().iterator(); it.hasNext();) {
111 Map.Entry entry = (Map.Entry)it.next();
112 Object key = entry.getKey();
113 Object value = entry.getValue();
115 if (value instanceof Map) {
116 Map map = (Map) value;
117 if (map.size() == 0)
continue;
118 ArrayList al =
new ArrayList();
120 initTreeModel(map, al);
126 private Map parseFolders(String path, Map parentFolderMap) {
128 Iterator it = application.getResourcePaths(path).iterator();
129 while (it.hasNext()) {
130 String pagePath = String.valueOf(it.next());
131 if (pagePath.endsWith(
"/")) {
132 String folderName = pagePath.substring(0, pagePath.length() - 1);
133 folderName = folderName.substring(folderName.lastIndexOf(
"/") + 1);
134 if (shallShowFile(folderName))
135 parentFolderMap.put(folderName, parseFolders(pagePath,
new TreeMap()));
137 String fileName = pagePath.substring(pagePath.lastIndexOf(
"/") + 1);
138 String extension = fileName.substring(fileName.lastIndexOf(
".") + 1);
139 if (shallShowFile(fileName) || shallShowFile(extension))
140 parentFolderMap.put(fileName, pagePath);
143 return parentFolderMap;
147 private boolean shallShowFile(String folderName) {
148 String value = (String) fileFilterMap.get(folderName);
149 return new Boolean(value ==
null ?
"true" : value).booleanValue();
153 private Map initFileFilterMap() {
154 Map fileFilterMap =
new HashMap();
156 for (
int i = 0, j = EXCLUDE_FOLDERS.length; i < j; i++)
157 fileFilterMap.put(EXCLUDE_FOLDERS[i],
"false");
158 for (
int i = 0, j = EXCLUDE_FILES.length; i < j; i++)
159 fileFilterMap.put(EXCLUDE_FILES[i],
"false");
162 if (type.equals(
"Flash"))
163 for (
int i = 0, j = FLASH.length; i < j; i++)
164 fileFilterMap.put(FLASH[i],
"true");
165 else if (type.equals(
"Images"))
166 for (
int i = 0, j = IMAGES.length; i < j; i++)
167 fileFilterMap.put(IMAGES[i],
"true");
168 else if (type.equals(
"Files")) {
169 for (
int i = 0, j = FLASH.length; i < j; i++)
170 fileFilterMap.put(FLASH[i],
"true");
171 for (
int i = 0, j = IMAGES.length; i < j; i++)
172 fileFilterMap.put(IMAGES[i],
"true");
173 for (
int i = 0, j = FILES.length; i < j; i++)
174 fileFilterMap.put(FILES[i],
"true");
177 return fileFilterMap;
182 cntDiv.getChildren().clear();
183 Treeitem item = tree.getSelectedItem();
184 Map map = (Map)item.getValue();
190 private void showImages(Map map){
191 for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
192 Map.Entry me = (Map.Entry) it.next();
193 Object value = me.getValue();
194 if (value instanceof Map)
continue;
195 String path = String.valueOf(value);
197 if (path.endsWith(
"swf"))
198 swfPath =
"~./ckez/img/flashIcon.jpg";
199 Toolbarbutton tb =
new Toolbarbutton(String.valueOf(me.getKey()),
"".equals(swfPath)? path: swfPath);
200 tb.addEventListener(
"onClick",
new EventListener() {
202 public void onEvent(Event event)
throws Exception {
204 selBtn.setSclass(
null);
205 selBtn = (Toolbarbutton) event.getTarget();
206 selBtn.setSclass(
"sel");
209 int CKEditorFuncNum = 1;
210 CKEditorFuncNum =
new Integer(((String[])param.get(
"CKEditorFuncNum"))[0]).intValue();
211 String script =
"window.opener.CKEDITOR.tools.callFunction("+CKEditorFuncNum+
", '" + apName + path +
"'); window.close(); ";
212 tb.setWidgetListener(
"onDoubleClick",script);
214 cntDiv.appendChild(tb);
void doAfterCompose(Component comp)