-
FEATURED COMPONENTS
First time here? Check out the FAQ!
I have a ZK-Tree with BindingComposer and as Master/Detail a Listbox. If i select a Treeitem, the Listbox Content is actualized.
The Listbox contains the Path in the Tree for the selected Treeitem. The Detail-Listbox is dynamically generated with Listcells, each with an Listener. The Listcells are related to Treeitems in the Tree, each row is the Path to an treeitem in the Tree.
When clicking on a Listcell in the detaillist, i want to select the Treeitem and to let the binder actualize the Detaillist. Is there a solution to involve the Binder? Can i sent an Event to simulate the Click on an Treeitem?
Here is the sourcecode. It ist simplified as possible. Another Problem is, that if you try to open all nodes in the tree, you will get some node which are not openable. There must be a problem in the Javascript library. The effect is the same with Firefox and IE. Zk is of
Version 6.
<code>
<?page title="ME" contentType="text/html;charset=UTF-8"?>
<zk>
<window id="win" title="APP" border="normal"
apply="org.zkoss.bind.BindComposer"
viewModel="@id('vm') @init('hs.appl.documentstore.gui.viewmodel.UITreeViewModel1')">
<hlayout>
<tree id="dyntree" model="@load(vm.meTreeModel)"
zclass="z-dottree" context="treePopup"
selectedItem="@bind(vm.selectedItem)" width="400px">
<template name="model" var="me">
<treeitem
onOpen="@command('onOpen', item=me, titem=self)">
<treerow>
<treecell label="@load(me.title)" />
</treerow>
</treeitem>
</template>
</tree>
<vlayout>
<listbox model="@load(vm.selectedItem.thisPath)"
itemRenderer="hs.appl.documentstore.gui.renderer.PathRenderer1"
sizedByContent="true">
<custom-attributes tree="${dyntree}" />
<listhead>
<listheader label="Directories" />
</listhead>
</listbox>
</vlayout>
</hlayout>
</window>
</zk>
</code>
<code>
package hs.appl.documentstore.gui.viewmodel;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.Init;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.select.Selectors;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Tree;
import org.zkoss.zul.Window;
public class UITreeViewModel1<T> extends TreeViewModel1<T> {
@Wire
Window win;
@Wire
Tree dyntree;
@SuppressWarnings("unchecked")
public UITreeViewModel1() {
super();
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Init
public void init(@ContextParam(ContextType.VIEW) Component view){
Selectors.wireComponents(view, this, false);
}
}
</code>
<code>
package hs.appl.documentstore.gui.viewmodel;
import java.util.ArrayList;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.zul.Treeitem;
public class TreeViewModel1<T> {
MeTreeModel1 meTreeModel = null;
PathList selectedItem = null;
public MeTreeModel1 getMeTreeModel() {
return meTreeModel;
}
/**
* @return the selectedItem
*/
public PathList getSelectedItem() {
return selectedItem;
}
/**
* @param selectedItem the selectedItem to set
*/
public void setSelectedItem(PathList selectedItem) {
this.selectedItem = selectedItem;
}
@SuppressWarnings("unchecked")
public TreeViewModel1() {
super();
meTreeModel = new MeTreeModel1(new ArrayList(new BigList(1000)));
}
/**
* Oeffenen eines Knotens
* @param item
* @param titem
*/
@Command
public void onOpen(@BindingParam("item") String item, @BindingParam("titem") Treeitem titem ){
System.out.println(item);
boolean open = titem.isOpen();
titem.unload();
titem.setOpen(open);
}
}
</code>
<code>
package hs.appl.documentstore.gui.renderer;
import hs.appl.documentstore.gui.viewmodel.PathList;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;
import org.zkoss.zul.Tree;
import org.zkoss.zul.Treeitem;
public class PathRenderer1 implements ListitemRenderer {
@Override
public void render(Listitem item, Object data, int index) throws Exception {
System.out.println(item+"/"+data+"/"+index);
PathList p = (PathList)data;
Listcell listcell = new Listcell();
listcell.setParent(item);
for (String str : p.getPathes()) {
Label label = new Label(str);
label.setAttribute("val", new Integer(str));
label.setStyle("background-color:lightgrey; margin-right: 10px; white-space: nowrap;");
label.addEventListener("onClick", new EventListener<Event>() {
@Override
public void onEvent(Event event) throws Exception {
Tree tree = (Tree)((Listcell)event.getTarget().getParent()).getListbox().getAttribute("tree");
System.out.println(event.getTarget());
System.out.println(event.getTarget().getAttribute("val"));
PathList p = new PathList((Integer)event.getTarget().getAttribute("val"));
Treeitem ti = tree.renderItemByNode(p);
/*
* Here the Treeitem ist selected, but the binder is not involved. The details is not actualized.
* I tried here Events.sendEvent and Events.postEvent and other
*/
tree.setSelectedItem(ti);
}
});
label.setParent(listcell);
}
}
}
</code>
<code>
package hs.appl.documentstore.gui.viewmodel;
import java.util.ArrayList;
import java.util.List;
public class PathList {
List<String> pathes = new ArrayList<String>();
public PathList(int i) {
super();
int s = i;
List<String> tmp = new ArrayList<String>();
tmp.add(new Integer(s).toString());
while (s > 2){
s--;
tmp.add(new Integer(s / 2).toString());
s /= 2;
}
for (int k = tmp.size()-1; k >=0; k--) {
pathes.add(tmp.get(k));
}
}
/**
* @return the pathes
*/
public List<String> getPathes() {
return pathes;
}
public List<PathList> getThisPath() {
List<PathList> lp = new ArrayList<PathList>();
lp.add(this);
return lp;
}
public String getTitle(){
return pathes.get(pathes.size()-1);
}
/**
* @param pathes the pathes to set
*/
public void setPathes(List<String> pathes) {
this.pathes = pathes;
}
public static void main(String[] args) {
for (int i = 0; i < 30; i++) {
System.out.println("---------------");
PathList p = new PathList(i);
for (String string : p.getPathes()) {
System.out.println(string);
}
}
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((pathes == null) ? 0 : pathes.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof PathList))
return false;
PathList other = (PathList) obj;
if (pathes == null) {
if (other.pathes != null)
return false;
} else if (!pathes.get(pathes.size()-1).equals(other.pathes.get(other.pathes.size()-1)))
return false;
return true;
}
}
</code>
<?page title="ME" contentType="text/html;charset=UTF-8"?> <zk> <window id="win" title="APP" border="normal" apply="org.zkoss.bind.BindComposer" viewModel="@id('vm') @init('hs.appl.documentstore.gui.viewmodel.UITreeViewModel1')"> <hlayout> <tree id="dyntree" model="@load(vm.meTreeModel)" zclass="z-dottree" context="treePopup" selectedItem="@bind(vm.selectedItem)" width="400px"> <template name="model" var="me"> <treeitem onOpen="@command('onOpen', item=me, titem=self)"> <treerow> <treecell label="@load(me.title)" /> </treerow> </treeitem> </template> </tree> <vlayout> <listbox model="@load(vm.selectedItem.thisPath)" itemRenderer="hs.appl.documentstore.gui.renderer.PathRenderer1" sizedByContent="true"> <custom-attributes tree="${dyntree}" /> <listhead> <listheader label="Directories" /> </listhead> </listbox> </vlayout> </hlayout> </window> </zk>
package hs.appl.documentstore.gui.viewmodel; import org.zkoss.bind.annotation.ContextParam; import org.zkoss.bind.annotation.ContextType; import org.zkoss.bind.annotation.Init; import org.zkoss.zk.ui.Component; import org.zkoss.zk.ui.select.Selectors; import org.zkoss.zk.ui.select.annotation.Wire; import org.zkoss.zul.Tree; import org.zkoss.zul.Window; public class UITreeViewModel1<T> extends TreeViewModel1<T> { @Wire Window win; @Wire Tree dyntree; @SuppressWarnings("unchecked") public UITreeViewModel1() { super(); } @SuppressWarnings({ "rawtypes", "unchecked" }) @Init public void init(@ContextParam(ContextType.VIEW) Component view){ Selectors.wireComponents(view, this, false); } }
package hs.appl.documentstore.gui.viewmodel; import java.util.ArrayList; import org.zkoss.bind.annotation.BindingParam; import org.zkoss.bind.annotation.Command; import org.zkoss.zul.Treeitem; public class TreeViewModel1<T> { MeTreeModel1 meTreeModel = null; PathList selectedItem = null; public MeTreeModel1 getMeTreeModel() { return meTreeModel; } /** * @return the selectedItem */ public PathList getSelectedItem() { return selectedItem; } /** * @param selectedItem the selectedItem to set */ public void setSelectedItem(PathList selectedItem) { this.selectedItem = selectedItem; } @SuppressWarnings("unchecked") public TreeViewModel1() { super(); meTreeModel = new MeTreeModel1(new ArrayList(new BigList(1000))); } /** * Oeffenen eines Knotens * @param item * @param titem */ @Command public void onOpen(@BindingParam("item") String item, @BindingParam("titem") Treeitem titem ){ System.out.println(item); boolean open = titem.isOpen(); titem.unload(); titem.setOpen(open); } }
package hs.appl.documentstore.gui.renderer; import hs.appl.documentstore.gui.viewmodel.PathList; import org.zkoss.zk.ui.event.Event; import org.zkoss.zk.ui.event.EventListener; import org.zkoss.zul.Label; import org.zkoss.zul.Listcell; import org.zkoss.zul.Listitem; import org.zkoss.zul.ListitemRenderer; import org.zkoss.zul.Tree; import org.zkoss.zul.Treeitem; public class PathRenderer1 implements ListitemRenderer { @Override public void render(Listitem item, Object data, int index) throws Exception { System.out.println(item+"/"+data+"/"+index); PathList p = (PathList)data; Listcell listcell = new Listcell(); listcell.setParent(item); for (String str : p.getPathes()) { Label label = new Label(str); label.setAttribute("val", new Integer(str)); label.setStyle("background-color:lightgrey; margin-right: 10px; white-space: nowrap;"); label.addEventListener("onClick", new EventListener<Event>() { @Override public void onEvent(Event event) throws Exception { Tree tree = (Tree)((Listcell)event.getTarget().getParent()).getListbox().getAttribute("tree"); System.out.println(event.getTarget()); System.out.println(event.getTarget().getAttribute("val")); PathList p = new PathList((Integer)event.getTarget().getAttribute("val")); Treeitem ti = tree.renderItemByNode(p); /* * Here the Treeitem ist selected, but the binder is not involved. The details is not actualized. * I tried here Events.sendEvent and Events.postEvent and other */ tree.setSelectedItem(ti); } }); label.setParent(listcell); } } }
package hs.appl.documentstore.gui.viewmodel; import java.util.ArrayList; import java.util.List; public class PathList { List<String> pathes = new ArrayList<String>(); public PathList(int i) { super(); int s = i; List<String> tmp = new ArrayList<String>(); tmp.add(new Integer(s).toString()); while (s > 2){ s--; tmp.add(new Integer(s / 2).toString()); s /= 2; } for (int k = tmp.size()-1; k >=0; k--) { pathes.add(tmp.get(k)); } } /** * @return the pathes */ public List<String> getPathes() { return pathes; } public List<PathList> getThisPath() { List<PathList> lp = new ArrayList<PathList>(); lp.add(this); return lp; } public String getTitle(){ return pathes.get(pathes.size()-1); } /** * @param pathes the pathes to set */ public void setPathes(List<String> pathes) { this.pathes = pathes; } public static void main(String[] args) { for (int i = 0; i < 30; i++) { System.out.println("---------------"); PathList p = new PathList(i); for (String string : p.getPathes()) { System.out.println(string); } } } /* (non-Javadoc) * @see java.lang.Object#hashCode() */ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((pathes == null) ? 0 : pathes.hashCode()); return result; } /* (non-Javadoc) * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof PathList)) return false; PathList other = (PathList) obj; if (pathes == null) { if (other.pathes != null) return false; } else if (!pathes.get(pathes.size()-1).equals(other.pathes.get(other.pathes.size()-1))) return false; return true; } }
Hi,
when clicking a listcell update the X property and binding Tree's selectedItem to the X property. It might solve your problem
Thanks for the answer. Sorry, I was in vacancy. What do you mean with X Property? Do you mean in the onClick Event in PathRenderer1?
public void onEvent(Event event) throws Exception {
Tree tree = (Tree)((Listcell)event.getTarget().getParent()).getListbox().getAttribute("tree");
System.out.println(event.getTarget());
System.out.println(event.getTarget().getAttribute("val"));
PathList p = new PathList((Integer)event.getTarget().getAttribute("val"));
Treeitem ti = tree.renderItemByNode(p);
/*
* Here the Treeitem ist selected, but the binder is not involved. The details is not actualized.
* I tried here Events.sendEvent and Events.postEvent and other
*/
tree.setSelectedItem(ti);
------------------------------------------------------------------------------------------------------------
new Code here?????? like
ti.??????
------------------------------------------------------------------------------------------------------------
}
Asked: 2012-06-20 07:46:43 +0800
Seen: 310 times
Last updated: Sep 10 '12