TreeViewer Databinding: Tree not showing changes [message #1064012] |
Mon, 17 June 2013 09:30 |
Markuz Surfing Messages: 7 Registered: June 2013 |
Junior Member |
|
|
[align=left][align=center][align=right][align=left][align=left][align=left]I have a tree which displays like this:
-Ben
-Chad
-Kidd
-Ken
-Jack
-Jill
The tree displays correctly but the problem is, when I add an item, the tree does not update/refresh automatically. I still need to explicitly call the refresh() function (See creating the tree part). The model and the tree should be in sync right? The changes should appear in the tree automatically, no need to explicitly call refresh().
I think the problem is in the second parameter of ViewerSupport.bind(), the object input:
//I tried using WritableList as input but the tree does not show.
// input = new WritableList(root.getPersons(), Person.class);
//So, I tried an instance of the root node. The tree displays but
//its not (bidirectional) data-binding. There's a need to call
//refresh() function of the tree to show its changes.
input = new RootTree();
ViewerSupport.bind(viewer, input, childrenProp, labelProps);
Creating the tree:
IValueProperty[] labelProps = BeanProperties
.values(new String[] { "name" });
IListProperty childrenProp = new DelegatingListProperty() {
IListProperty root_list = BeanProperties.list(RootTree.class,
"persons");
IListProperty person_list = BeanProperties.list(Person.class,
"children");
protected IListProperty doGetDelegate(Object source) {
if (source instanceof RootTree)
return root_list;
if (source instanceof Person)
return person_list;
return null;
}
};
ViewerSupport.bind(viewer, input, childrenProp, labelProps);
Button add = new Button(parent, SWT.PUSH);
add.setText("Add");
add.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
Person p = new Person();
p.setName("Ken");
Child c = new Child();
c.setName("Jack");
p.add(c);
c = new Child();
c.setName("Jill");
p.add(c);
input.add(p); //or set a new list if input is RootTree
//viewer.refresh();
}
});
Models:
public abstract class AbstractModelObject {
private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
this);
public void addPropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(listener);
}
public void addPropertyChangeListener(String propertyName,
PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(propertyName,
listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.removePropertyChangeListener(listener);
}
public void removePropertyChangeListener(String propertyName,
PropertyChangeListener listener) {
propertyChangeSupport.removePropertyChangeListener(propertyName,
listener);
}
protected void firePropertyChange(String propertyName, Object oldValue,
Object newValue) {
propertyChangeSupport.firePropertyChange(propertyName, oldValue,
newValue);
}
}
// Root Node of the tree
public class RootTree extends AbstractModelObject {
List<Person> persons = new ArrayList<Person>();
public RootTree() {
// TODO Auto-generated constructor stub
Person p = new Person();
p.setName("Ben");
Child c = new Child();
c.setName("Chad");
p.add(c);
c = new Child();
c.setName("Kidd");
p.add(c);
persons.add(p);
}
public List<Person> getPersons() {
return persons;
}
public void setPersons(List<Person> personas) {
firePropertyChange("persons", this.persons, this.persons = personas);
}
}
// First node inside root
public class Person extends AbstractModelObject{
String name;
List<Child> children = new ArrayList<Child>();
public List<Child> getChildren() {
return children;
}
public void setChildren(List<Child> children) {
firePropertyChange("children", this.children, this.children = children);
}
public String getName() {
return name;
}
public void setName(String name) {
firePropertyChange("name", this.name, this.name = name);
}
public void add(Child c){
children.add(c);
setChildren(children);
}
}
// Node inside Person
public class Child extends AbstractModelObject{
String name = "child";
public String getName() {
return name;
}
public void setName(String name){
firePropertyChange("name", this.name, this.name = name);
[/ALIGN] }
}
[Updated on: Tue, 18 June 2013 07:12] Report message to a moderator
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.08058 seconds