ZK - Open Source Ajax Java FrameworkZK - Open Source Ajax Java Framework

tree node problem zk 5.0.7

erlangga
1 Jun 2011 03:22:01 GMT
1 Jun 2011 03:22:01 GMT

Hi all,

I recently updated my zk version to 5.0.7 and i find out that my Tree component that i use doesn't render the node as it was in 5.0.6.
the tree renders every node under the root, but the tree only renders the first items for every node after.
this is my code for creating the menuItem list

...
        private void createMenu(ApplicationMenu appMenu){
		final List<MenuItem> menuList = appMenu == null ? null : appMenu.getMenuList();
		if(menuList != null && !menuList.isEmpty()){
			
			List <DefaultTreeNode> children = createMenu(menuList);
			DefaultTreeNode root = new DefaultTreeNode("ROOT", children);
			
			DefaultTreeModel treeModel = new DefaultTreeModel(root);
			menuTree.setModel(treeModel);
		}
		
	}

	private List<DefaultTreeNode> createMenu(List<MenuItem> children){
		List<DefaultTreeNode> childrenList = new ArrayList<DefaultTreeNode>();
		if(children != null && !children.isEmpty()){
			for(MenuItem menuItem : children){
				DefaultTreeNode node = new DefaultTreeNode(menuItem, createMenu(menuItem.getChildren()));
				childrenList.add(node);
			}
		}
		
		return childrenList;
	}
...

this is my treeRenderer

          private class MenuTreeitemRenderer implements TreeitemRenderer, Serializable{

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		@Override
		public void render(Treeitem item, Object data) throws Exception {
			DefaultTreeNode node = (DefaultTreeNode)data;
			MenuItem menuItem = (MenuItem)node.getData();
			item.setOpen(false);
			//item.setLabel(CommonFns.getLabel(menuItem.getLabel()));
			item.setLabel(menuItem.getLabel());
			item.setImage(menuItem.getIcon());
			item.setValue(menuItem);
		}		
	}

and this is the MenuItem class

public class MenuItem implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	static int ROOT_ID = 0;
	private UUID id;
	private String label;
	private String icon;
	private String file;
	private String title;
	private MenuItem parent;
	private List<MenuItem> children = new ArrayList<MenuItem>();
	
	public MenuItem(UUID id, String label, String title, String icon, String file, List<MenuItem> children, MenuItem parent){
		this.id = id;
		this.label = label;
		this.setTitle(title);
		this.icon = icon;
		this.file = file;
		if(children != null){
			this.children = children;
		}
		this.parent = parent;
	}
	
	public MenuItem(UUID id, String label, String title, String icon, String file, List<MenuItem> children){
		this.id = id;
		this.label = label;
		this.setTitle(title);
		this.icon = icon;
		this.file = file;
		if(children != null){
			this.children = children;
		}
	}
	
	public MenuItem(UUID uuid, String label, String title, String icon, String file){
		this(uuid, label, title, icon, file, null);
	}
	public UUID getId() {
		return id;
	}
	public void setId(UUID id) {
		this.id = id;
	}
	public String getLabel() {
		return label;
	}
	public void setLabel(String label) {
		this.label = label;
	}
	public String getIcon() {
		return icon;
	}
	public void setIcon(String icon) {
		this.icon = icon;
	}
	public void setFile(String file) {
		this.file = file;
	}
	public String getFile() {
		return file;
	}
	
	public void setTitle(String title) {
		this.title = title;
	}

	public String getTitle() {
		return title;
	}

	public List<MenuItem> getChildren(){
		return children;
	}

	public boolean isCategory(){
		return !(children == null || children.size() == 0);
	}
	
	public void add(MenuItem item){
		children.add(item);
	}
	
	public boolean remove(MenuItem item){
		return children.remove(item);
	}

	public void setParent(MenuItem parent) {
		this.parent = parent;
	}

	public MenuItem getParent() {
		return parent;
	}
}

example result in 5.0.7

list content:
-MenuItem1
  -MenuItem1.1
  -MenuItem1.2
-MenuItem2
  -MenuItem2.1
  -MenuItem2.2

result in tree:
-MenuItem1
   -MenuItem1.1
-MenuItem2
   -MenuItem2.1

if I change the zk version to 5.0.6, all the menuItems are rendered correctly.
is this a bug, or is my implementation wrong?
any help would be appreciated.
thank you and regards,

erlangga

erlangga
6 Jun 2011 22:46:17 GMT
6 Jun 2011 22:46:17 GMT

can anyone help me with this?
thank you

hendytoya
8 Jun 2011 08:36:31 GMT
8 Jun 2011 08:36:31 GMT

same problem. is there anyone got the problem as us?

thanks.

valmarTop Contributor
8 Jun 2011 19:05:07 GMT
8 Jun 2011 19:05:07 GMT

Tree seems to be very buggy since 5.0.6 and 5.0.7.

See: http://www.zkoss.org/forum/listComment/16400 and http://www.zkoss.org/forum/listComment/16403

I think it's not your fault but rather a problem with the new ZK upgrades.

I am currently thinking about going back to ZK 5.0.5 if these problems persist.

jimmyshiau
9 Jun 2011 05:23:38 GMT
9 Jun 2011 05:23:38 GMT

I have tested with the following sample and ZK 5.0.7.1

<zk>
	<zscript><![CDATA[
		import java.util.*;
		import org.zkoss.zul.*;
		
		TreeModel model = new DefaultTreeModel(
			new DefaultTreeNode(null, new DefaultTreeNode[] {
	      		new DefaultTreeNode("item 0", new DefaultTreeNode[] {
	          		new DefaultTreeNode("item 00"),new DefaultTreeNode("item 01")
	          	}),
	          	new DefaultTreeNode("item 1", new DefaultTreeNode[] {
	          		new DefaultTreeNode("item 10"),new DefaultTreeNode("item 11")
	          	}),
	          	new DefaultTreeNode("item 2", new ArrayList())
			})
		);
		
		
		TreeitemRenderer renderer = new TreeitemRenderer() {
		    public void render(Treeitem item, Object data) throws Exception {
		    	TreeNode node = (TreeNode) data;
		        item.setLabel(node.getData().toString());
		    }
		};
	]]></zscript>
	<tree width="300px" itemRenderer="${renderer}" model="${model}"/>
</zk>

it works fine.

erlangga
9 Jun 2011 07:09:45 GMT
9 Jun 2011 07:09:45 GMT

hi Jimmy,

yes, i have tried that scenario too, and it does worked well.
I debugged my code and found out that in the Tree class, before the renderChildren method in renderItem0, DefaultTreeNode had all the items that I put in there.

any enlightment to this condition?

thanks

jimmyshiau
9 Jun 2011 21:20:08 GMT
9 Jun 2011 21:20:08 GMT

I have tried another sample

<zk>
	<zscript><![CDATA[
		import java.util.*;
		import org.zkoss.zul.*;
		import org.zkoss.util.CollectionsX.ArrayCollection;
		
		Map data = new HashMap();		
		data.put("item 0", new String[]{"item 00","item 01"});
		data.put("item 1", new String[]{"item 10","item 11"});
		

		public List createMenu(Collection children){
			List childrenList = new ArrayList();
			if(children != null && !children.isEmpty()){
				if (children instanceof Set) {
					for (Object o: children) {
						Map.Entry entry = (Map.Entry) o;
						childrenList.add(new DefaultTreeNode(entry.getKey(), 
								createMenu(new ArrayCollection((String[]) entry.getValue()))));
					}
				} else {
					for (Object o: children) {
						childrenList.add(new DefaultTreeNode(o.toString()));
					}
				}
			}
			
			return childrenList;
		}
		
		DefaultTreeModel model = 
			new DefaultTreeModel(new DefaultTreeNode("ROOT", createMenu(data.entrySet())));
		
		
		TreeitemRenderer renderer = new TreeitemRenderer() {
		    public void render(Treeitem item, Object data) throws Exception {
		    	TreeNode node = (TreeNode) data;
		        item.setLabel(node.getData().toString());
		    }
		};
	]]></zscript>
	<tree width="300px" itemRenderer="${renderer}" model="${model}"/>
</zk>

can you try to print all of children in the model?

jimmyshiau
9 Jun 2011 21:42:42 GMT
9 Jun 2011 21:42:42 GMT

BTW, we release zk fiddle
the sample in here
http://zkfiddle.org/sample/31s8vtg/2
Can you provide your sample in zk fiddle?

erlangga
10 Jun 2011 06:14:40 GMT
10 Jun 2011 06:14:40 GMT

i have tried to create a new zk project to try and reproduce this issue. but for some reason I cannot. it works fine.
then i tried to use a manually created list to supply the model. when i put it in the new project, it works fine, but when i put it in my old project this issue shows.

both project use zk 5.0.7.1, but my old project one uses maven, and the new one doesn't.

any idea?

as for zkfiddle, since i cannot reproduce the issue, i can't put my code in it, my old project is a bit complicated and involves many classes.

thank you.

terrytornadoTop Contributor
10 Jun 2011 06:39:57 GMT
10 Jun 2011 06:39:57 GMT

Only a hint.

Maven does not load the zk dependend files which are stored behind the /WEB-INF folder like

WEB-INF/tld
WEB-INF/xsd

I have exported an upgraded project as .war-file. I'm wondering why the design of that exported application lacks. All the 3d mold gradient background pictures are not shown.
By manually copy these version dependend files all is runing well.

isisis
9 Feb 2012 06:04:54 GMT
9 Feb 2012 06:04:54 GMT

Hi,

I think the problem is when you try to call setOpen when treenode is rendering.

 private class MenuTreeitemRenderer implements TreeitemRenderer, Serializable{

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		@Override
		public void render(Treeitem item, Object data) throws Exception {
			DefaultTreeNode node = (DefaultTreeNode)data;
			MenuItem menuItem = (MenuItem)node.getData();
			item.setOpen(false);
			//item.setLabel(CommonFns.getLabel(menuItem.getLabel()));
			item.setLabel(menuItem.getLabel());
			item.setImage(menuItem.getIcon());
			item.setValue(menuItem);
		}		
	}

I change the code to :

private class MenuTreeitemRenderer implements TreeitemRenderer, Serializable{

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		@Override
		public void render(Treeitem item, Object data) throws Exception {
			DefaultTreeNode node = (DefaultTreeNode)data;
			MenuItem menuItem = (MenuItem)node.getData();
			item.setOpen(true);
			item.setLabel(menuItem.getLabel());
			item.setImage(menuItem.getIcon());
			item.setValue(menuItem);			
			Treerow row = item.getTreerow();				
			row.addEventListener(Events.ON_CLICK, MenuPanel.this);
			item.setOpen(false);
		}		
	}

And it's work properly now.
I don't know if this a bug in zk or not.
I am using zk 5.0.10.

FYI, I am in the same team with erlangga and hendytoya..:D

Thanks!!