当前位置: 代码迷 >> Web前端 >> zk tree 浏览磁盘可能目录
  详细解决方案

zk tree 浏览磁盘可能目录

热度:639   发布时间:2012-11-11 10:07:57.0
zk tree 浏览磁盘或者目录

index.zul
<?xml version="1.0" encoding="utf-8"?>
<window
apply="test.IndexController">

<!--?
liulan
<textbox id="txtRoot"></textbox>
-->

<button forward="onLoadFiles" label="Browse..."></button>
<tree id="treeDir" multiple="true" checkmark="true">

</tree>
</window>

IndexController .java
import org.apache.commons.lang.StringUtils;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.util.GenericAutowireComposer;
import org.zkoss.zul.Treeitem;
import org.zkoss.zul.Treerow;
import org.zkoss.zul.api.Textbox;
import org.zkoss.zul.api.Tree;
import org.zkoss.zul.api.Treechildren;


public class IndexController extends GenericAutowireComposer {
private static final long serialVersionUID = 5909684044186358102L;
//private Textbox txtRoot;
private Tree treeDir;

public void onLoadFiles() {

//String root = txtRoot.getText();
File roots[] = File.listRoots();
for(int i=0;i<roots.length;i++){

//root= new File(roots[i].toString());


File file = new File(roots[i].toString());
if (!file.exists() || file.isFile()) {
return;
}
if (treeDir.getTreechildrenApi() == null) {
new org.zkoss.zul.Treechildren().setParent(treeDir);
}
createTreeitem(file, treeDir.getTreechildrenApi());
}
}
private void createTreeitem(final File file, Treechildren tc) {
String fileName = StringUtils.isBlank(file.getName()) ? file.getAbsolutePath() : file.getName();

//System.out.println(fileName);

final Treeitem treeitem = new Treeitem(fileName, file);

Treerow rw=treeitem.getTreerow();//treerow add eventListener

if (file.isDirectory()) {
treeitem.setImage("/images/ClosedFolder.gif");
//treeitem.setCheckable()
rw.addEventListener("onClick", new EventListener() {
public void onEvent(Event event)? {
try {
File[] fs = file.listFiles();
if (fs == null) {
return;
}
if (!createTreechildren(treeitem)) {
return;
}
for (File f : fs) {
//System.out.println(f.getName());
createTreeitem(f, treeitem.getTreechildren());
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
} else {
treeitem.setImage("/images/file_16x16.gif");
}
treeitem.setParent(tc);

}
private boolean createTreechildren(Treeitem item) {
if (item.getTreechildrenApi() == null) {
new org.zkoss.zul.Treechildren().setParent(item);
return true;
} else {
return false;
}
}

}

转载自http://sun4love.iteye.com/blog/844372,并做了点小小的改动,使用zk版本3.6,没有spring,同时为treerow添加监听,而不是treeitem。treeitem在我 的电脑上实现不了监听。有什么想法可以相互讨论。呵呵

?

  相关解决方案