試試這個:
public class MyMenuItem {
private String name;
public MyMenuItem(String name) {
super();
this.name = name;
list = new ArrayList<MyMenuItem>();
}
//...
public boolean hasChildrens() {
return list.size()>0;
}
}
public class MyTreeModel implements TreeViewModel {
ArrayList<MyMenuItem> all;
public MyTreeModel() {
all = new ArrayList<MyMenuItem>();
//Default items
all.add(... //fill
}
@Override
public <T> NodeInfo<?> getNodeInfo(T value) {
ListDataProvider<MyMenuItem> dataProvider;
if (value == null) { // root
dataProvider = new ListDataProvider<MyMenuItem>(all);
} else {
dataProvider = new ListDataProvider<MyMenuItem>(((MyMenuItem) value).getList());
}
Cell<MyMenuItem> cell = new MyCell(); //my Cell with dblclick event
return new DefaultNodeInfo<MyMenuItem>(dataProvider, cell);
}
@Override
public boolean isLeaf(Object value) {
if (value instanceof MyMenuItem) {
MyMenuItem t = (MyMenuItem) value;
if (!t.hasChildrens())
return true;
return false;
}
return false;
}
}
而且電池類:
private static class MyCell extends AbstractCell<MyMenuItem> {
public MyCell() {
super("keydown","dblclick"); //click?
}
@Override
public void onBrowserEvent(Context context, Element parent, MyMenuItem value,
NativeEvent event, ValueUpdater<MyMenuItem> valueUpdater) {
// Check that the value is not null.
if (value == null) {
return;
}
super.onBrowserEvent(context, parent, value, event, valueUpdater);
if ("dblclick".equals(event.getType())) {
this.onEnterKeyDown(context, parent, value, event, valueUpdater);
}
}
@Override
public void render(Context context, MyMenuItem value, SafeHtmlBuilder sb) {
if (value == null) {
return;
}
sb.appendEscaped(value.getName());
}
@Override
protected void onEnterKeyDown(Context context, Element parent,
MyMenuItem value, NativeEvent event, ValueUpdater<MyMenuItem> valueUpdater) {
Window.alert("You clicked "+event.getType()+" " + value.getName());
}
}
`
在模塊:
treeModel = new MyTreeModel();
CellTree tree = new CellTree(treeModel,null);
tree.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
TreeNode rootNode = tree.getRootTreeNode();
TreeNode firstPlaylist = rootNode.setChildOpen(0, true);
//...
verticalPanelWest.add(tree);
這不是寫我的代碼給我的網站! – Zasz 2011-04-14 13:38:52