BrightSide Workbench Full Report + Source Code
FilebrowserController_506.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (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 Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.zkforge.ckez;
23 import java.util.*;
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.*;
31 
32 public class FilebrowserController_506 extends GenericForwardComposer {
33 
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"};
43 
44  private String apName = "";
45  private String type = "";
46  private Map fileFilterMap;
47 
48  private Tree tree;
49  private Div cntDiv;
50  private Toolbarbutton selBtn;
51 
52  @Override
53  public void doAfterCompose(Component comp) throws Exception {
54  super.doAfterCompose(comp);
55 
56  type = ((String[])param.get("Type"))[0];
57  fileFilterMap = initFileFilterMap();
58  String url = getFolderUrl();
59 
60  if (Strings.isBlank(url)) return;
61  if (url.startsWith("./"))
62  url = url.substring(1);
63 
64  if (!url.startsWith("/"))
65  url = "/" + url;
66  Map rootFolderMap = new TreeMap();
67  Map map = new TreeMap();
68  rootFolderMap.put(url, map);
69 
70  parseFolders(url, map);
71 
72 // tree.setItemRenderer(new ExplorerTreeitemRenderer());
73 // tree.setModel(new SimpleTreeModel(new SimpleTreeNode("ROOT",initTreeModel(rootFolderMap, new ArrayList()))));
74 
75  showImages(map);
76  }
77 
78  private String getFolderUrl() {
79  String uuid = ((String[])param.get("CKEditor"))[0];
80  uuid = uuid.substring(0, uuid.lastIndexOf("-cnt"));
81 
82  String url = null;
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);
87 
88  if (url == null)
89  url = (String) session.getAttribute("filebrowserBrowseUrl"+ uuid);
90 
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);
96 
97  url = index < 0 ? url: url.substring(index);
98 
99  index = url.lastIndexOf(";jsessionid");
100  if (index > 0)
101  url = url.substring(0, index);
102 
103  if (application.getResourcePaths(url) == null)
104  throw new UiException("Folder not found: " + url);
105 
106  return url;
107  }
108 
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();
114 
115  if (value instanceof Map) {
116  Map map = (Map) value;
117  if (map.size() == 0) continue;
118  ArrayList al = new ArrayList();
119 // list.add(new SimpleTreeNode(entry, al));
120  initTreeModel(map, al);
121  }
122  }
123  return list;
124  }
125 
126  private Map parseFolders(String path, Map parentFolderMap) {
127 
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()));
136  } else {
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);
141  }
142  }
143  return parentFolderMap;
144  }
145 
146 
147  private boolean shallShowFile(String folderName) {
148  String value = (String) fileFilterMap.get(folderName);
149  return new Boolean(value == null ? "true" : value).booleanValue();
150  }
151 
152 
153  private Map initFileFilterMap() {
154  Map fileFilterMap = new HashMap();
155 
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");
160 
161 
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");
175  }
176 
177  return fileFilterMap;
178  }
179 
180 
181  public void onSelect$tree(){
182  cntDiv.getChildren().clear();
183  Treeitem item = tree.getSelectedItem();
184  Map map = (Map)item.getValue();
185 
186  showImages(map);
187  }
188 
189 
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);
196  String swfPath = "";
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() {
201  @Override
202  public void onEvent(Event event) throws Exception {
203  if (selBtn !=null)
204  selBtn.setSclass(null);
205  selBtn = (Toolbarbutton) event.getTarget();
206  selBtn.setSclass("sel");
207  }
208  });
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);
213 
214  cntDiv.appendChild(tb);
215  }
216 
217  }
218 
219 // private class ExplorerTreeitemRenderer implements TreeitemRenderer {
220 // public void render(Treeitem item, Object data) throws Exception {
221 // Map.Entry entry = (Map.Entry)((SimpleTreeNode)data).getData();
222 // item.setLabel(String.valueOf(entry.getKey()));
223 // Object value = entry.getValue();
224 // item.setValue(value);
225 // item.setOpen(true);
226 // if (item.getParentItem() == null)
227 // item.setSelected(true);
228 // }
229 // }
230 }