BrightSide Workbench Full Report + Source Code
tree/FileTree.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.turro.file.zul.tree;
19 
20 import java.io.File;
21 import java.io.FileFilter;
22 import java.util.List;
23 import org.turro.collections.CollectionUtil;
24 import org.turro.elephant.context.Application;
25 import org.turro.file.ExpressionFilter;
26 import org.turro.file.FileWrapper;
27 import org.turro.file.action.FileAction;
28 import org.turro.i18n.I_;
29 import org.zkoss.zk.ui.event.Event;
30 import org.zkoss.zk.ui.event.EventListener;
31 import org.zkoss.zk.ui.event.Events;
32 import org.zkoss.zk.ui.ext.AfterCompose;
33 import org.zkoss.zul.*;
34 
39 public class FileTree extends Tree {
40 
41  private class InnerFileFilter implements FileFilter {
42  private FileTree tree;
43 
44  public InnerFileFilter(FileTree tree) {
45  this.tree = tree;
46  }
47 
48  @Override
49  public boolean accept(File pathname) {
50  if(tree.fileFilter == null || tree.fileFilter.accept(pathname)) {
51  if(pathname.isDirectory()) {
52  return FileTree.this.expressionFilter.isEmpty() ? true : pathname.listFiles(this).length > 0;
53  } else {
54  return tree.expressionFilter.accept(pathname);
55  }
56  }
57  return false;
58  }
59  }
60 
61  private Treechildren children;
62  private Toolbar searchbar;
63  private Menupopup actionMenu;
64  private FileListener fileListener;
65  private FileFilter fileFilter, innerFileFilter;
66  private ExpressionFilter expressionFilter;
67  private String title, role = "x";
68  private FileItem lastSelection;
69 
70  public FileTree() {
71  this(null);
72  }
73 
74  public FileTree(String title) {
75  super();
76  this.title = title;
77  expressionFilter = new ExpressionFilter();
78  innerFileFilter = new InnerFileFilter(this);
79  }
80 
81  public void initTreeColumns(List<FileColumn> fileColumns) {
82  addColumns(fileColumns);
83  addChildrenSpace();
84  addEventListener(Events.ON_CLICK, new EventListener() {
85  @Override
86  public void onEvent(Event event) throws Exception {
87  FileItem fi = (FileItem) FileTree.this.getSelectedItem();
88  if(fi != null) {
89  fi.processSelection(true);
90  }
91  }
92  });
93  actionMenu = new Menupopup();
94  actionMenu.addEventListener(Events.ON_OPEN, new EventListener() {
95  @Override
96  public void onEvent(Event event) throws Exception {
97  if(lastSelection == null || !lastSelection.equals(FileTree.this.getSelectedItem())) {
98  lastSelection = (FileItem) FileTree.this.getSelectedItem();
99  updateActions();
100  }
101  }
102  });
103  getParent().appendChild(actionMenu);
104  }
105 
106  public FileFilter getFileFilter() {
107  return innerFileFilter;
108  }
109 
110  public void setFileFilter(FileFilter fileFilter) {
111  this.fileFilter = fileFilter;
112  }
113 
115  return fileListener;
116  }
117 
118  public void setFileListener(FileListener listener) {
119  this.fileListener = listener;
120  }
121 
122  public String getRole() {
123  return role;
124  }
125 
126  public void setRole(String role) {
127  this.role = role;
128  }
129 
130  public boolean isInRole(String role) {
131  return Application.getApplication().isInRole(this.role + ":" + role);
132  }
133 
134  public Menupopup getActionMenu() {
135  return actionMenu;
136  }
137 
138  public void setAddToolbar(boolean addToolbar) {
139  if(addToolbar) {
140  searchbar = new Toolbar();
141  getParent().insertBefore(searchbar, FileTree.this);
142  addSearchbarButtons();
143  }
144  }
145 
146  public FileItem addFolder(String label, String path) {
147  FileItem fi = new FileItem(label, new File(path));
148  children.appendChild(fi);
149  fi.initPreview();
150  fi.setContext(getActionMenu());
151  if(fi instanceof AfterCompose) {
152  ((AfterCompose) fi).afterCompose();
153  }
154  fi.showContents();
155  return fi;
156  }
157 
158  public List<FileColumn> getColumns() {
159  return CollectionUtil.from(getTreecols().getChildren()).<List<FileColumn>>cast();
160  }
161 
162  public File getSelectedFile() {
163  if(getSelectedItem() instanceof FileItem) {
164  return ((FileItem) getSelectedItem()).getFile();
165  }
166  return null;
167  }
168 
169  private void addColumns(List<FileColumn> fileColumns) {
170  Treecols cols = new Treecols();
171  cols.setSizable(true);
172  appendChild(cols);
173  cols.appendChild(new FileColumn(FileColumnType.FILE_NAME_COLUMN));
174  for(Treecol tc : fileColumns) {
175  cols.appendChild(tc);
176  }
177  }
178 
179  private void addChildrenSpace() {
180  children = new Treechildren();
181  appendChild(children);
182  }
183 
184  private void updateActions() {
185  actionMenu.getChildren().clear();
186 
187  // TODO: implement role against actions using tree isinrole
188  File file = getSelectedFile();
189  if(file != null) {
190  FileWrapper fw = FileWrapper.getFileByType(file);
191  fw.setFileItem((FileItem) getSelectedItem());
192  for(final FileAction fa : fw.getActions()) {
193  Menuitem mi = new Menuitem();
194  mi.setLabel(fa.getLabel());
195  mi.setImage(fa.getImage());
196  if(fa.isClientSide()) {
197  mi.setAction("onclick: " + fa.doAction());
198  }
199  mi.addEventListener(Events.ON_CLICK, new EventListener() {
200  @Override
201  public void onEvent(Event event) throws Exception {
202  if(!fa.isClientSide()) {
203  fa.doAction();
204  if(fa.refreshParent()) {
205  if(getSelectedItem().getParentItem() != null) {
206  ((FileItem) getSelectedItem().getParentItem()).reloadContents();
207  }
208  } else if(fa.refreshSelf()) {
209  ((FileItem) getSelectedItem()).reloadContents();
210  }
211  }
212  }
213  });
214  actionMenu.appendChild(mi);
215  }
216  }
217  }
218 
219  private void addSearchbarButtons() {
220 
221  searchbar.appendChild(new Separator("vertical"));
222 
223  final Checkbox byFullPath = new Checkbox(I_.get("By full path"));
224  byFullPath.setChecked(expressionFilter.isFullPath());
225  byFullPath.setStyle("font-size:11px");
226  byFullPath.addEventListener(Events.ON_CHECK, new EventListener() {
227  @Override
228  public void onEvent(Event event) throws Exception {
229  expressionFilter.setFullPath(byFullPath.isChecked());
230  }
231  });
232  searchbar.appendChild(byFullPath);
233 
234  searchbar.appendChild(new Separator("vertical"));
235 
236  final Textbox searchValue = new Textbox(expressionFilter.getExpression());
237  searchValue.setStyle("font-size:11px");
238  searchValue.addEventListener(Events.ON_CHANGE, new EventListener() {
239  @Override
240  public void onEvent(Event event) throws Exception {
241  expressionFilter.setExpression(searchValue.getText());
242  }
243  });
244  searchValue.addEventListener(Events.ON_OK, new EventListener() {
245  @Override
246  public void onEvent(Event event) throws Exception {
247  for(Object o : children.getChildren()) {
248  if(o instanceof FileItem) {
249  ((FileItem) o).reloadContents();
250  }
251  }
252  }
253  });
254  searchbar.appendChild(searchValue);
255 
256  Toolbarbutton searchButton = new Toolbarbutton(
257  I_.get("Search"),
258  "/_zul/images/search.png"
259  );
260  searchButton.addEventListener(Events.ON_CLICK, new EventListener() {
261  @Override
262  public void onEvent(Event event) throws Exception {
263  for(Object o : children.getChildren()) {
264  if(o instanceof FileItem) {
265  ((FileItem) o).reloadContents();
266  }
267  }
268  }
269  });
270  searchbar.appendChild(searchButton);
271 
272  searchbar.appendChild(new Separator("vertical"));
273 
274  final Checkbox byPartialMatch = new Checkbox(I_.get("Partial match"));
275  byPartialMatch.setChecked(expressionFilter.isPartial());
276  byPartialMatch.setStyle("font-size:11px");
277  byPartialMatch.addEventListener(Events.ON_CHECK, new EventListener() {
278  @Override
279  public void onEvent(Event event) throws Exception {
280  expressionFilter.setPartial(byPartialMatch.isChecked());
281  }
282  });
283  searchbar.appendChild(byPartialMatch);
284 
285  searchbar.appendChild(new Separator("vertical"));
286 
287  final Checkbox byRegExp = new Checkbox(I_.get("By regular expression"));
288  byRegExp.setChecked(expressionFilter.isRegularExpression());
289  byRegExp.setStyle("font-size:11px");
290  byRegExp.addEventListener(Events.ON_CHECK, new EventListener() {
291  @Override
292  public void onEvent(Event event) throws Exception {
293  expressionFilter.setRegularExpression(byRegExp.isChecked());
294  }
295  });
296  searchbar.appendChild(byRegExp);
297 
298  }
299 }
void setRegularExpression(boolean regularExpression)
void setPartial(boolean partial)
void setExpression(String expression)
void setFullPath(boolean fullPath)
void processSelection(boolean openItem)
Definition: FileItem.java:182
void setFileFilter(FileFilter fileFilter)
void setFileListener(FileListener listener)
void initTreeColumns(List< FileColumn > fileColumns)
void setAddToolbar(boolean addToolbar)
FileItem addFolder(String label, String path)