ivan
(ivan bolcina)
September 16, 2009, 6:39am
1
Hi.
I would like to have a tree that would display icon (based on type of node) and caption for each node.
I managed to display caption, but I’am having problems displaying icon.
I have extended Container.Hierarchical model and set it to tree.
Usage:
tree.setContainerDataSource(new LDAPTreeModel(mw.TheConnection,null,null));
tree.setItemIconPropertyId("Icon");
tree.setItemCaptionPropertyId("Name");
tree.setItemCaptionMode(AbstractSelect.ITEM_CAPTION_MODE_PROPERTY);
and here is some code from model that returns properties:
public Collection<?> getContainerPropertyIds()
{
ArrayList out=new ArrayList();
out.add("ID");
out.add("Name");
out.add("Icon");
return out;
}
public Property getContainerProperty(Object itemId, Object propertyId)
{
System.out.println("Looking for "+ propertyId);
LDAPNode lnode=(LDAPNode) itemId;
if ("Icon".equalsIgnoreCase(""+propertyId))
return new MethodProperty(new IconGiver(lnode),""+propertyId);
else
return new MethodProperty(lnode,""+propertyId);
}
As I examined output, I saw that “Icon” property never gets called.
What am I doing wrong?
thanks for answer,ivan
Here is a simplified example:
package com.example.puu;
import com.vaadin.Application;
import com.vaadin.data.util.HierarchicalContainer;
import com.vaadin.terminal.Resource;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Tree;
import com.vaadin.ui.Window;
public class PuuApplication extends Application {
@Override
public void init() {
Window mainWindow = new Window("Test");
setMainWindow(mainWindow);
HierarchicalContainer c = new HierarchicalContainer();
c.addContainerProperty("icon", Resource.class, null);
c.addContainerProperty("caption", String.class, null);
for (int i = 0; i < 10; i++) {
Object id = c.addItem();
c.getContainerProperty(id, "icon").setValue(
new ThemeResource("common/icons/error.png"));
c.getContainerProperty(id, "caption").setValue("Item " + i);
}
Tree t = new Tree();
t.setContainerDataSource(c);
t.setItemIconPropertyId("icon");
t.setItemCaptionPropertyId("caption");
t.setItemCaptionMode(Tree.ITEM_CAPTION_MODE_PROPERTY);
mainWindow.addComponent(t);
}
}
Artur
(Artur Signell)
September 18, 2009, 4:24pm
4
What is the type of the “Icon” property? It must be assignable to a Resource. The following works as expected:
Tree tree = new Tree();
tree.addContainerProperty("icon", Resource.class, null);
tree.addContainerProperty("caption", String.class, null);
tree.setItemIconPropertyId("icon");
tree.setItemCaptionPropertyId("caption");
tree.setItemCaptionMode(AbstractSelect.ITEM_CAPTION_MODE_PROPERTY);
Item item = tree.addItem("item 1");
item.getItemProperty("icon").setValue(new ThemeResource("../runo/icons/16/ok.png"));
item.getItemProperty("caption").setValue("The item");
ivan
(ivan bolcina)
September 24, 2009, 12:57pm
5
My mistake was that in container, i didn’t report right type for icon… Container interface is BIG, and not so very easy like in swing. But I managed.
thanks
Richard9
(Richard Kolb)
January 6, 2011, 9:40am
6
Very nice sample.
Thank you.
Richard.
Andreas60
(Andreas Lundmark)
August 10, 2012, 6:49am
7
How do you accomplish this (icons in a Tree) using a JPAContainer (which does not support addContainerProperty)?
Iker
(Iker Ames)
January 17, 2013, 12:39pm
8
I have faced the same issue just now. I have resolved it using:
for (Object itemId : tree.getItemIds()) {
//Here the icon can be set to anything. Just an example.
tree.setItemIcon(itemId, new ThemeResource("img/edit-icon.png"));
}