Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » TreeViewer Databinding: Tree not showing changes
TreeViewer Databinding: Tree not showing changes [message #1064012] Mon, 17 June 2013 09:30 Go to next message
Markuz Surfing is currently offline Markuz SurfingFriend
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

Re: TreeViewer Databinding: Tree not showing changes [message #1064254 is a reply to message #1064012] Tue, 18 June 2013 12:45 Go to previous messageGo to next message
Markuz Surfing is currently offline Markuz SurfingFriend
Messages: 7
Registered: June 2013
Junior Member
Whew, I finally figured a way. Btw, helpful forum. -_-
Re: TreeViewer Databinding: Tree not showing changes [message #1064313 is a reply to message #1064254] Tue, 18 June 2013 15:42 Go to previous messageGo to next message
Denis Roy is currently offline Denis RoyFriend
Messages: 484
Registered: October 2004
Location: Ottawa, Ontario, Canada
Senior Member

Posting your solution would be helpful.

Denis Roy
Eclipse Webmaster -- webmaster@eclipse.org
Re: TreeViewer Databinding: Tree not showing changes [message #1622333 is a reply to message #1064313] Wed, 18 February 2015 12:24 Go to previous message
Piero Campalani is currently offline Piero CampalaniFriend
Messages: 114
Registered: January 2015
Senior Member

Indeed:
@Markuz, which solution did you find, please?

Btw: message #1064254 = helpful ANSWER
Previous Topic:How to extract class hierarchy/call graph relationships from java sources?
Next Topic:Eclipse Product export wizard setting file
Goto Forum:
  


Current Time: Tue Apr 23 06:05:59 GMT 2024

Powered by FUDForum. Page generated in 0.08058 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top