PDA

View Full Version : Leaf name in a BaseTreeModel



zaccret
30 Apr 2008, 5:02 AM
When you build a tree with a BaseTreeModel, the manner to set
leaf names is a little confusing. On each leaf, you have to
set("name","my folder name")We don't know which property have to be set (it's not very intuitive and it's not in the javadoc ;-)).
Why don't we have get/setName(String) or get/setTest(String) ?
Is there a good reason to that ?


public class MyBug implements EntryPoint {
private Tree tree;
private TreeViewer folders;

/**
* This is the entry point method.
*/
public void onModuleLoad() {
tree = new Tree();

folders = new TreeViewer(tree);
folders.setLabelProvider(new ModelLabelProvider());
folders.setContentProvider(new ModelTreeContentProvider() {
@Override
public boolean hasChildren(Object element) {
return false;
}
});

HashMap<String, Object> maMap = new HashMap<String, Object>();
maMap.put("name", "My folder 1");
final BaseTreeModel model = new BaseTreeModel();
BaseTreeModel folder1 = new BaseTreeModel();
folder1.set("name","Folder 1");
model.add(folder1);
folders.setInput(model);
RootPanel.get().add(tree);
}
}

cdasher
30 Apr 2008, 8:09 AM
Although the demo and the demo model use name, you don't have to. Just extend BseTreeModel and add your own String to use for the label and then set the LabelProvider.

You can see the getText method returns etSomeTextToDisplayAsTheLabel

I have a getter/setter on my model class get/setIconStyle which holds a string for css background image like "icon-myObject" which you then style in your CSS like:

.icon-myobject {
background-image: url(mycustomIcon.png);
}

and add this image to root of webapp.

then
viewer.setLabelProvider(new LabelProvider() {
public String getText(Object element) {
if (element instanceof MyObject {
return ((MyObject) element).getSomeTextToDisplayAsTheLabel();
} else {
return "unknown";
}

}

public String getIconStyle(Object element) {
if (element instanceof MyObject {
return ((MyObject) element).getIconStyle();
} else {
return "some-other-css-style";
}
}
});

I hope this helps.

zaccret
5 May 2008, 12:01 AM
Yes. That will help. It feels more "OOP" ;-) Thank you.

zaccret
6 May 2008, 4:52 AM
Maybe for the basic tree we should have these 2 classes :


public class DefaultTreeLabelProvider extends LabelProvider<DefaultTreeModel> {
@Override
public String getText(DefaultTreeModel element) {
return element.getName();
}
}

public class DefaultTreeModel extends BaseTreeModel {
private String name;

public DefaultTreeModel(String name) {
super();
setName(name);
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
then the code to build the tree becomes :



TreeViewer<SingleTreeSelectionModel> folders = new TreeViewer<SingleTreeSelectionModel>(tree);
folders.setLabelProvider(new DefaultTreeLabelProvider());
folders.setContentProvider(new ModelTreeContentProvider());

final BaseTreeModel model = new BaseTreeModel();
model.add(new DefaultTreeModel("Folder 1 name"));
model.add(new DefaultTreeModel("Folder 2 name"));
model.add(new DefaultTreeModel("Folder 3 name"));
folders.setInput(model);
What do you think about that ?