18 package org.turro.file.zul.tree;
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.*;
41 private class InnerFileFilter
implements FileFilter {
44 public InnerFileFilter(
FileTree tree) {
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;
54 return tree.expressionFilter.
accept(pathname);
61 private Treechildren children;
62 private Toolbar searchbar;
63 private Menupopup actionMenu;
65 private FileFilter fileFilter, innerFileFilter;
67 private String title, role =
"x";
78 innerFileFilter =
new InnerFileFilter(
this);
82 addColumns(fileColumns);
84 addEventListener(Events.ON_CLICK,
new EventListener() {
86 public void onEvent(Event event)
throws Exception {
93 actionMenu =
new Menupopup();
94 actionMenu.addEventListener(Events.ON_OPEN,
new EventListener() {
96 public void onEvent(Event event)
throws Exception {
97 if(lastSelection ==
null || !lastSelection.equals(
FileTree.this.getSelectedItem())) {
103 getParent().appendChild(actionMenu);
107 return innerFileFilter;
111 this.fileFilter = fileFilter;
119 this.fileListener = listener;
140 searchbar =
new Toolbar();
141 getParent().insertBefore(searchbar,
FileTree.this);
142 addSearchbarButtons();
148 children.appendChild(fi);
151 if(fi instanceof AfterCompose) {
159 return CollectionUtil.from(getTreecols().getChildren()).<List<FileColumn>>cast();
163 if(getSelectedItem() instanceof
FileItem) {
164 return ((
FileItem) getSelectedItem()).getFile();
169 private void addColumns(List<FileColumn> fileColumns) {
170 Treecols cols =
new Treecols();
171 cols.setSizable(
true);
174 for(Treecol tc : fileColumns) {
175 cols.appendChild(tc);
179 private void addChildrenSpace() {
180 children =
new Treechildren();
181 appendChild(children);
184 private void updateActions() {
185 actionMenu.getChildren().clear();
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());
199 mi.addEventListener(Events.ON_CLICK,
new EventListener() {
201 public void onEvent(Event event)
throws Exception {
202 if(!fa.isClientSide()) {
204 if(fa.refreshParent()) {
205 if(getSelectedItem().getParentItem() !=
null) {
206 ((FileItem) getSelectedItem().getParentItem()).reloadContents();
208 }
else if(fa.refreshSelf()) {
209 ((FileItem) getSelectedItem()).reloadContents();
214 actionMenu.appendChild(mi);
219 private void addSearchbarButtons() {
221 searchbar.appendChild(
new Separator(
"vertical"));
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() {
228 public void onEvent(Event event)
throws Exception {
229 expressionFilter.
setFullPath(byFullPath.isChecked());
232 searchbar.appendChild(byFullPath);
234 searchbar.appendChild(
new Separator(
"vertical"));
236 final Textbox searchValue =
new Textbox(expressionFilter.
getExpression());
237 searchValue.setStyle(
"font-size:11px");
238 searchValue.addEventListener(Events.ON_CHANGE,
new EventListener() {
240 public void onEvent(Event event)
throws Exception {
244 searchValue.addEventListener(Events.ON_OK,
new EventListener() {
246 public void onEvent(Event event)
throws Exception {
247 for(Object o : children.getChildren()) {
248 if(o instanceof FileItem) {
249 ((FileItem) o).reloadContents();
254 searchbar.appendChild(searchValue);
256 Toolbarbutton searchButton =
new Toolbarbutton(
258 "/_zul/images/search.png"
260 searchButton.addEventListener(Events.ON_CLICK,
new EventListener() {
262 public void onEvent(Event event)
throws Exception {
263 for(Object o : children.getChildren()) {
264 if(o instanceof FileItem) {
265 ((FileItem) o).reloadContents();
270 searchbar.appendChild(searchButton);
272 searchbar.appendChild(
new Separator(
"vertical"));
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() {
279 public void onEvent(Event event)
throws Exception {
280 expressionFilter.
setPartial(byPartialMatch.isChecked());
283 searchbar.appendChild(byPartialMatch);
285 searchbar.appendChild(
new Separator(
"vertical"));
287 final Checkbox byRegExp =
new Checkbox(I_.get(
"By regular expression"));
289 byRegExp.setStyle(
"font-size:11px");
290 byRegExp.addEventListener(Events.ON_CHECK,
new EventListener() {
292 public void onEvent(Event event)
throws Exception {
296 searchbar.appendChild(byRegExp);
boolean isInRole(String role)
static Application getApplication()
void setRegularExpression(boolean regularExpression)
void setPartial(boolean partial)
boolean isRegularExpression()
void setExpression(String expression)
void setFullPath(boolean fullPath)
boolean accept(File pathname)
void processSelection(boolean openItem)
Menupopup getActionMenu()
void setFileFilter(FileFilter fileFilter)
FileListener getFileListener()
void setFileListener(FileListener listener)
List< FileColumn > getColumns()
boolean isInRole(String role)
void initTreeColumns(List< FileColumn > fileColumns)
FileFilter getFileFilter()
void setAddToolbar(boolean addToolbar)
FileItem addFolder(String label, String path)
void setRole(String role)