Home » Eclipse Projects » JFace » [DataBinding] Problem with model
[DataBinding] Problem with model [message #513918] |
Thu, 11 February 2010 16:32 |
Luke Jedrych Messages: 5 Registered: February 2010 |
Junior Member |
|
|
Hello,
I'm trying to create RCP application which uses JFace TreeViewer and databinding. My application displays structure of university as a tree (Departments->Institutes->Groups/Teams->People). When I add new department to a tree there is no problem with refreshing the view. The problem occurs with adding other types and editing objects. I've been looking for similar programs/models in tutorials and snippets and I haven't found any solution of my problem. I'm using Eclipse 3.5.
My model looks like this:
package model;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
public abstract class AbstractModelObject {
private final 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);
}
}
package model;
import java.util.Set;
import java.util.TreeSet;
public class Department extends AbstractModelObject implements Comparable{
private String name;
private String description;
private String address;
private String phone;
private String email;
private String website;
private String photo;
private Person boss;
private Set institutes=new TreeSet();
public Department(){
}
public Department(String name, String description, String address,
String phone, String email, String website, String photo,
Person boss) {
super();
this.name = name;
this.description = description;
this.address = address;
this.phone = phone;
this.email = email;
this.website = website;
this.photo = photo;
this.boss = boss;
}
public String getName() {
return name;
}
public void setName(String name) {
firePropertyChange("name", this.name, this.name = name);
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
firePropertyChange("description", this.description, this.description = description);
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
firePropertyChange("address", this.address, this.address = address);
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
firePropertyChange("phone", this.phone, this.phone = phone);
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
firePropertyChange("email", this.email, this.email = email);
}
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
firePropertyChange("website", this.website, this.website = website);
}
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
firePropertyChange("photo", this.photo, this.photo = photo);
}
public Person getBoss() {
return boss;
}
public void setBoss(Person boss) {
firePropertyChange("boss", this.boss, this.boss = boss);
}
public String toString(){
return name;
}
public Set getInstitutes() {
return institutes;
}
public void deleteDepartment(){
if(institutes.size()==0) {}//usun
}
@Override
public int compareTo(Object o) {
return this.toString().compareTo(o.toString());
}
public boolean hasListeners(String propertyName) {
return hasListeners(propertyName);
}
public boolean addInstitute(Institute i){
boolean isAdded=institutes.add(i);
firePropertyChange("institutes", null, institutes);
System.out.println("addInstitute");
return isAdded;
}
}
package model;
import java.util.Set;
import java.util.TreeSet;
public class Institute extends AbstractModelObject implements Comparable{
private String name;
private String description;
private String address;
private String phone;
private String email;
private String website;
private String photo;
private Department department;
private Person boss;
private Set<Group> groups=new TreeSet<Group>();
public Institute(){
}
public Institute(String name, String description, String address,
String phone, String email, String website, String photo,
Department department, Person boss) {
super();
this.name = name;
this.description = description;
this.address = address;
this.phone = phone;
this.email = email;
this.website = website;
this.photo = photo;
this.department = department;
this.boss = boss;
department.addInstitute(this);
}
public Set<Group> getGroups() {
return groups;
}
public String getName() {
return name;
}
public void setName(String name) {
firePropertyChange("name", this.name, this.name = name);
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
firePropertyChange("description", this.description, this.description = description);
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
firePropertyChange("address", this.address, this.address = address);
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
firePropertyChange("phone", this.phone, this.phone = phone);
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
firePropertyChange("email", this.email, this.email = email);
}
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
firePropertyChange("website", this.website, this.website = website);
}
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
firePropertyChange("photo", this.photo, this.photo = photo);
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
firePropertyChange("department", this.department, this.department = department);
}
public Person getBoss() {
return boss;
}
public void setBoss(Person boss) {
firePropertyChange("boss", this.boss, this.boss = boss);
}
public String toString(){
return name;
}
public void deleteInstitute(){
if(groups.size()==0) department.getInstitutes().remove(this);
}
public int compareTo(Object o) {
return this.toString().compareTo(o.toString());
}
public boolean hasListeners(String propertyName) {
return hasListeners(propertyName);
}
public boolean addGroup(Group i){
boolean isAdded=groups.add(i);
firePropertyChange("groups", null, groups);
return isAdded;
}
}
Other classes are similar to Department and Instititute.
package model;
import java.util.Set;
import java.util.TreeSet;
import org.eclipse.core.databinding.observable.map.WritableMap;
import org.eclipse.core.databinding.observable.set.WritableSet;
public class ModelProvider {
private static ModelProvider content;
public /*private*/ Set<AbstractModelObject> departments=new TreeSet<AbstractModelObject>();
public /*private*/ WritableSet wdepartments;
public /*private*/ WritableMap institutes=new WritableMap(AbstractModelObject.class, AbstractModelObject.class);
public /*private*/ WritableMap groups=new WritableMap(AbstractModelObject.class, AbstractModelObject.class);
public /*private*/ WritableMap people=new WritableMap(AbstractModelObject.class, AbstractModelObject.class);
private ModelProvider(){
}
public static synchronized ModelProvider getInstance() {
if (content != null) {
return content;
}
content = new ModelProvider();
return content;
}
public Set<AbstractModelObject> getDepartments() {
return departments;
}
public WritableSet getWdepartments(){
return wdepartments;
}
public WritableMap getInstitutes() {
return institutes;
}
public WritableMap getGroups() {
return groups;
}
public WritableMap getPeople() {
return people;
}
}
My ObservableFactory, TreeStructureAdvisor:
package providers;
import model.Department;
import model.Group;
import model.Institute;
import model.ModelProvider;
import model.Person;
import org.eclipse.core.databinding.observable.IObservable;
import org.eclipse.core.databinding.observable.masterdetail.IObservableFactory;
import org.eclipse.core.databinding.observable.set.WritableSet;
public class MyObservableFactory implements IObservableFactory {
@Override
public IObservable createObservable(Object parentElement) {
if(parentElement instanceof Person) return null;
else if(parentElement instanceof Group) return new WritableSet(((Group) parentElement).getPeople(), Person.class);
else if(parentElement instanceof Institute) return new WritableSet(((Institute) parentElement).getGroups(), Group.class);
else if(parentElement instanceof Department) return new WritableSet(((Department) parentElement).getInstitutes(), Institute.class);
else if(parentElement instanceof IObservable) return (IObservable) parentElement;
else{
return ModelProvider.getInstance().getWdepartments();
}
}
}
package providers;
import model.Department;
import model.Group;
import model.Institute;
import model.Person;
import org.eclipse.jface.databinding.viewers.TreeStructureAdvisor;
public class MyTreeStructureAdvisor extends TreeStructureAdvisor {
@Override
public Object getParent(Object element) {
if(element instanceof Person) return null;
else if(element instanceof Group) return ((Group) element).getInstitute();
else if(element instanceof Institute) return ((Institute) element).getDepartment();
else if(element instanceof Department) return null;
return super.getParent(element);
}
@Override
public Boolean hasChildren(Object element) {
if(element instanceof Person) return false;
else if(element instanceof Group){
return !((Group) element).getPeople().isEmpty();
}
else if(element instanceof Institute){
return !((Institute) element).getGroups().isEmpty();
}
else if(element instanceof Department){
return !((Department) element).getInstitutes().isEmpty();
}
return super.hasChildren(element);
}
}
And the last is View
public class View extends ViewPart {
public View() {
}
public static final String ID = "myproject1.view";
private static TreeViewer viewer;
/**
* This is a callback that will allow us to create the viewer and initialize
* it.
*/
public void createPartControl(Composite parent) {
createViewer(parent);
}
public void createViewer(Composite parent){
viewer=new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL
| SWT.V_SCROLL | SWT.FULL_SELECTION );
viewer.setContentProvider(new ObservableSetTreeContentProvider(new MyObservableFactory(), new MyTreeStructureAdvisor()));
viewer.setLabelProvider(new MyLabelProvider());
viewer.setSorter(new ViewerSorter());
viewer.refresh(true);
viewer.setInput(ModelProvider.getInstance().getDepartments().toArray());
viewer.refresh();
getSite().setSelectionProvider(viewer);
}
/**
* Passing the focus request to the viewer's control.
*/
public void setFocus() {
viewer.getControl().setFocus();
}
}
I would be grateful for any help.
Thanks,
Luke
|
|
|
Re: [DataBinding] Problem with model [message #513927 is a reply to message #513918] |
Thu, 11 February 2010 16:49 |
Thomas Schindl Messages: 6651 Registered: July 2009 |
Senior Member |
|
|
a) Your IObservableFactory is wrong, it should create IObservableLists
by using the BeanProperties-Factory
b) Your LabelProvider (though you don't show us the code) looks like
an ordinary one so it won't update if a property changes
Tom
Am 11.02.10 17:32, schrieb Luke Jedrych:
> Hello,
>
> I'm trying to create RCP application which uses JFace TreeViewer and
> databinding. My application displays structure of university as a tree
> (Departments->Institutes->Groups/Teams->People). When I add new
> department to a tree there is no problem with refreshing the view. The
> problem occurs with adding other types and editing objects. I've been
> looking for similar programs/models in tutorials and snippets and I
> haven't found any solution of my problem. I'm using Eclipse 3.5.
> My model looks like this:
>
> package model;
>
> import java.beans.PropertyChangeListener;
> import java.beans.PropertyChangeSupport;
>
> public abstract class AbstractModelObject {
>
> private final 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(propertyN ame,
> listener);
> }
>
> protected void firePropertyChange(String propertyName, Object oldValue,
> Object newValue) {
> propertyChangeSupport.firePropertyChange(propertyName, oldValue,
> newValue);
> }
>
> }
>
>
>
> package model;
>
> import java.util.Set;
> import java.util.TreeSet;
>
>
> public class Department extends AbstractModelObject implements Comparable{
> private String name; private String description; private
> String address; private String phone; private String email;
> private String website; private String photo; private Person
> boss; private Set institutes=new TreeSet();
>
> public Department(){
>
> }
>
> public Department(String name, String description, String address,
> String phone, String email, String website, String photo,
> Person boss) {
> super();
> this.name = name;
> this.description = description;
> this.address = address;
> this.phone = phone;
> this.email = email;
> this.website = website;
> this.photo = photo;
> this.boss = boss;
> }
>
> public String getName() {
> return name;
> }
>
> public void setName(String name) {
> firePropertyChange("name", this.name, this.name = name);
> }
>
> public String getDescription() {
> return description;
> }
>
> public void setDescription(String description) {
> firePropertyChange("description", this.description,
> this.description = description);
> }
>
> public String getAddress() {
> return address;
> }
>
> public void setAddress(String address) {
> firePropertyChange("address", this.address, this.address =
> address);
> }
>
> public String getPhone() {
> return phone;
> }
>
> public void setPhone(String phone) {
> firePropertyChange("phone", this.phone, this.phone = phone);
> }
>
> public String getEmail() {
> return email;
> }
>
> public void setEmail(String email) {
> firePropertyChange("email", this.email, this.email = email);
> }
>
> public String getWebsite() {
> return website;
> }
>
> public void setWebsite(String website) {
> firePropertyChange("website", this.website, this.website =
> website);
> }
>
> public String getPhoto() {
> return photo;
> }
>
> public void setPhoto(String photo) {
> firePropertyChange("photo", this.photo, this.photo = photo);
> }
>
> public Person getBoss() {
> return boss;
> }
>
> public void setBoss(Person boss) {
> firePropertyChange("boss", this.boss, this.boss = boss);
> }
>
> public String toString(){
> return name;
> }
>
> public Set getInstitutes() {
> return institutes;
> }
>
> public void deleteDepartment(){
> if(institutes.size()==0) {}//usun
> }
>
> @Override
> public int compareTo(Object o) {
> return this.toString().compareTo(o.toString());
> }
>
> public boolean hasListeners(String propertyName) {
> return hasListeners(propertyName);
> }
>
> public boolean addInstitute(Institute i){
> boolean isAdded=institutes.add(i);
> firePropertyChange("institutes", null, institutes);
> System.out.println("addInstitute");
> return isAdded;
> }
>
> }
>
>
>
>
> package model;
>
> import java.util.Set;
> import java.util.TreeSet;
>
> public class Institute extends AbstractModelObject implements Comparable{
> private String name; private String description; private
> String address; private String phone;
> private String email; private String website;
> private String photo; private Department department; private
> Person boss;
> private Set<Group> groups=new TreeSet<Group>();
> public Institute(){
>
> }
>
> public Institute(String name, String description, String address,
> String phone, String email, String website, String photo,
> Department department, Person boss) {
> super();
> this.name = name;
> this.description = description;
> this.address = address;
> this.phone = phone;
> this.email = email;
> this.website = website;
> this.photo = photo;
> this.department = department;
> this.boss = boss;
> department.addInstitute(this);
> }
>
> public Set<Group> getGroups() {
> return groups;
> }
>
> public String getName() {
> return name;
> }
>
> public void setName(String name) {
> firePropertyChange("name", this.name, this.name = name);
> }
>
> public String getDescription() {
> return description;
> }
>
> public void setDescription(String description) {
> firePropertyChange("description", this.description,
> this.description = description);
> }
>
> public String getAddress() {
> return address;
> }
>
> public void setAddress(String address) {
> firePropertyChange("address", this.address, this.address =
> address);
> }
>
> public String getPhone() {
> return phone;
> }
>
> public void setPhone(String phone) {
> firePropertyChange("phone", this.phone, this.phone = phone);
> }
>
> public String getEmail() {
> return email;
> }
>
> public void setEmail(String email) {
> firePropertyChange("email", this.email, this.email = email);
> }
>
> public String getWebsite() {
> return website;
> }
>
> public void setWebsite(String website) {
> firePropertyChange("website", this.website, this.website =
> website);
> }
>
> public String getPhoto() {
> return photo;
> }
>
> public void setPhoto(String photo) {
> firePropertyChange("photo", this.photo, this.photo = photo);
> }
>
> public Department getDepartment() {
> return department;
> }
>
> public void setDepartment(Department department) {
> firePropertyChange("department", this.department,
> this.department = department);
> }
>
> public Person getBoss() {
> return boss;
> }
>
> public void setBoss(Person boss) {
> firePropertyChange("boss", this.boss, this.boss = boss);
> }
>
> public String toString(){
> return name;
> }
>
> public void deleteInstitute(){
> if(groups.size()==0) department.getInstitutes().remove(this);
> }
>
> public int compareTo(Object o) {
> return this.toString().compareTo(o.toString());
> }
>
> public boolean hasListeners(String propertyName) {
> return hasListeners(propertyName);
> }
>
> public boolean addGroup(Group i){
> boolean isAdded=groups.add(i);
> firePropertyChange("groups", null, groups);
> return isAdded;
> }
> }
>
>
>
> Other classes are similar to Department and Instititute.
>
>
> package model;
>
> import java.util.Set;
> import java.util.TreeSet;
>
> import org.eclipse.core.databinding.observable.map.WritableMap;
> import org.eclipse.core.databinding.observable.set.WritableSet;
>
> public class ModelProvider {
> private static ModelProvider content;
> public /*private*/ Set<AbstractModelObject> departments=new
> TreeSet<AbstractModelObject>();
> public /*private*/ WritableSet wdepartments;
> public /*private*/ WritableMap institutes=new
> WritableMap(AbstractModelObject.class, AbstractModelObject.class);
> public /*private*/ WritableMap groups=new
> WritableMap(AbstractModelObject.class, AbstractModelObject.class);
> public /*private*/ WritableMap people=new
> WritableMap(AbstractModelObject.class, AbstractModelObject.class);
>
> private ModelProvider(){
> }
>
> public static synchronized ModelProvider getInstance() {
> if (content != null) {
> return content;
> }
> content = new ModelProvider();
> return content;
> }
>
>
> public Set<AbstractModelObject> getDepartments() {
> return departments;
> }
>
> public WritableSet getWdepartments(){
> return wdepartments;
> }
>
> public WritableMap getInstitutes() {
> return institutes;
> }
>
>
> public WritableMap getGroups() {
> return groups;
> }
>
>
> public WritableMap getPeople() {
> return people;
> }
>
>
> }
>
>
>
> My ObservableFactory, TreeStructureAdvisor:
>
>
> package providers;
>
> import model.Department;
> import model.Group;
> import model.Institute;
> import model.ModelProvider;
> import model.Person;
>
> import org.eclipse.core.databinding.observable.IObservable;
> import
> org.eclipse.core.databinding.observable.masterdetail.IObserv ableFactory;
> import org.eclipse.core.databinding.observable.set.WritableSet;
>
> public class MyObservableFactory implements IObservableFactory {
>
> @Override
> public IObservable createObservable(Object parentElement) {
> if(parentElement instanceof Person) return null;
> else if(parentElement instanceof Group) return new
> WritableSet(((Group) parentElement).getPeople(), Person.class);
> else if(parentElement instanceof Institute) return new
> WritableSet(((Institute) parentElement).getGroups(), Group.class);
> else if(parentElement instanceof Department) return new
> WritableSet(((Department) parentElement).getInstitutes(), Institute.class);
> else if(parentElement instanceof IObservable) return
> (IObservable) parentElement;
> else{
> return ModelProvider.getInstance().getWdepartments();
> }
>
> }
>
> }
>
>
>
> package providers;
>
> import model.Department;
> import model.Group;
> import model.Institute;
> import model.Person;
>
> import org.eclipse.jface.databinding.viewers.TreeStructureAdvisor;
>
> public class MyTreeStructureAdvisor extends TreeStructureAdvisor {
>
> @Override
> public Object getParent(Object element) {
> if(element instanceof Person) return null;
> else if(element instanceof Group) return ((Group)
> element).getInstitute();
> else if(element instanceof Institute) return ((Institute)
> element).getDepartment();
> else if(element instanceof Department) return null;
> return super.getParent(element);
> }
>
> @Override
> public Boolean hasChildren(Object element) {
> if(element instanceof Person) return false;
> else if(element instanceof Group){
> return !((Group) element).getPeople().isEmpty();
> }
> else if(element instanceof Institute){
> return !((Institute) element).getGroups().isEmpty();
> }
> else if(element instanceof Department){
> return !((Department) element).getInstitutes().isEmpty();
> }
> return super.hasChildren(element);
> }
>
> }
>
>
> And the last is View
>
> public class View extends ViewPart {
> public View() {
> }
> public static final String ID = "myproject1.view";
>
> private static TreeViewer viewer;
>
>
> /**
> * This is a callback that will allow us to create the viewer and
> initialize
> * it.
> */
> public void createPartControl(Composite parent) {
> createViewer(parent);
> }
>
> public void createViewer(Composite parent){
> viewer=new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL
> | SWT.V_SCROLL | SWT.FULL_SELECTION );
> viewer.setContentProvider(new
> ObservableSetTreeContentProvider(new MyObservableFactory(), new
> MyTreeStructureAdvisor()));
> viewer.setLabelProvider(new MyLabelProvider());
> viewer.setSorter(new ViewerSorter());
> viewer.refresh(true);
>
> viewer.setInput(ModelProvider.getInstance().getDepartments() .toArray());
> viewer.refresh();
> getSite().setSelectionProvider(viewer);
> }
>
>
> /**
> * Passing the focus request to the viewer's control.
> */
> public void setFocus() {
> viewer.getControl().setFocus();
> }
>
>
> }
>
>
>
> I would be grateful for any help.
>
> Thanks,
> Luke
>
>
>
|
|
| | |
Re: [DataBinding] Problem with model [message #514064 is a reply to message #513970] |
Fri, 12 February 2010 10:51 |
Luke Jedrych Messages: 5 Registered: February 2010 |
Junior Member |
|
|
Sorry for problems , but it's my first application which uses databinding.
I've changed IObservableFactory as you wrote in your previous post. Also I've changed View that now it uses ViewSupport.bind method. (http:// fire-change-event.blogspot.com/2009/02/bind-viewer-in-one-st atement-with.html and http:// dev.eclipse.org/viewcvs/index.cgi/org.eclipse.jface.examples .databinding/src/org/eclipse/jface/examples/databinding/snip pets/Snippet009TableViewer.java?view=markup).
Now my TreeViewer doesn't display anything and throws java.lang.NoSuchMethodException ( Cound not remove listener from [Ljava.lang.Object;@166f9b9). I think that it could be connected with the second argument of ViewSupport.bind method. Should I write artificial root of my tree? And if I want to bind Institutes, Groups etc. with their parents should I use DataBindingContext.bindValue or anything else?
package myproject1;
import model.Department;
import model.ModelProvider;
import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.jface.databinding.viewers.ObservableSetTreeContentProvider;
import org.eclipse.jface.databinding.viewers.ViewerSupport;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import providers.MyObservableFactory;
import providers.MyTreeStructureAdvisor;
public class View extends ViewPart {
public View() {
}
public static final String ID = "myproject1.view";
private static TreeViewer viewer;
/**
* This is a callback that will allow us to create the viewer and initialize
* it.
*/
public void createPartControl(Composite parent) {
createViewer(parent);
}
public void createViewer(Composite parent){
viewer=new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL
| SWT.V_SCROLL | SWT.FULL_SELECTION );
viewer.setContentProvider(new ObservableSetTreeContentProvider(new MyObservableFactory(), new MyTreeStructureAdvisor()));
//viewer.setLabelProvider(new ObservableMapLabelProvider(BeansObservables.observeMaps(ModelProvider.getInstance().wdepartments, new String[]{"name", "institutes"})));
//viewer.setLabelProvider(new MyLabelProvider());
viewer.setSorter(new ViewerSorter());
viewer.refresh(true);
ViewerSupport.bind(viewer, ModelProvider.getInstance().departments.toArray(), BeanProperties.list(Department.class, "institutes"), BeanProperties.value(Department.class, "name"));
viewer.setInput(ModelProvider.getInstance().getDepartments().toArray());
getSite().setSelectionProvider(viewer);
}
/**
* Passing the focus request to the viewer's control.
*/
public void setFocus() {
viewer.getControl().setFocus();
}
public static void refresh(){
viewer.refresh();
}
}
Luke
|
|
|
Re: [DataBinding] Problem with model [message #514073 is a reply to message #514064] |
Fri, 12 February 2010 10:56 |
Thomas Schindl Messages: 6651 Registered: July 2009 |
Senior Member |
|
|
The input you set is in correct and has to be an IObserableList,
something like:
viewer.setInput(
new WritableList(ModelProvider.getInstance().getDepartments())
);
Tom
Am 12.02.10 11:51, schrieb Luke Jedrych:
> Sorry for problems :( , but it's my first application which uses
> databinding. I've changed IObservableFactory as you wrote in your
> previous post. Also I've changed View that now it uses ViewSupport.bind
> method.
> ( http://fire-change-event.blogspot.com/2009/02/bind-viewer-in -one-statement-with.html
> and
> http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.jface.e xamples.databinding/src/org/eclipse/jface/examples/databindi ng/snippets/Snippet009TableViewer.java?view=markup).
> Now my TreeViewer doesn't display anything and throws
> java.lang.NoSuchMethodException ( Cound not remove listener from
> [Ljava.lang.Object;@166f9b9). I think that it could be connected with
> the second argument of ViewSupport.bind method. Should I write
> artificial root of my tree? And if I want to bind Institutes, Groups
> etc. with their parents should I use DataBindingContext.bindValue or
> anything else?
>
>
> package myproject1;
>
> import model.Department;
> import model.ModelProvider;
>
> import org.eclipse.core.databinding.beans.BeanProperties;
> import org.eclipse.core.databinding.beans.BeansObservables;
> import org.eclipse.jface.databinding.viewers.ObservableMapLabelProv ider;
> import
> org.eclipse.jface.databinding.viewers.ObservableSetTreeConte ntProvider;
> import org.eclipse.jface.databinding.viewers.ViewerSupport;
> import org.eclipse.jface.viewers.TreeViewer;
> import org.eclipse.jface.viewers.ViewerSorter;
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.widgets.Composite;
> import org.eclipse.ui.part.ViewPart;
>
> import providers.MyObservableFactory;
> import providers.MyTreeStructureAdvisor;
>
> public class View extends ViewPart {
> public View() {
> }
> public static final String ID = "myproject1.view";
>
> private static TreeViewer viewer;
>
>
> /**
> * This is a callback that will allow us to create the viewer and
> initialize
> * it.
> */
> public void createPartControl(Composite parent) {
> createViewer(parent);
> }
>
> public void createViewer(Composite parent){
> viewer=new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL
> | SWT.V_SCROLL | SWT.FULL_SELECTION );
>
> viewer.setContentProvider(new
> ObservableSetTreeContentProvider(new MyObservableFactory(), new
> MyTreeStructureAdvisor()));
> //viewer.setLabelProvider(new
> ObservableMapLabelProvider(BeansObservables.observeMaps(Mode lProvider.getInstance().wdepartments,
> new String[]{"name", "institutes"})));
> //viewer.setLabelProvider(new MyLabelProvider());
> viewer.setSorter(new ViewerSorter());
> viewer.refresh(true);
> ViewerSupport.bind(viewer,
> ModelProvider.getInstance().departments.toArray(),
> BeanProperties.list(Department.class, "institutes"),
> BeanProperties.value(Department.class, "name"));
>
>
> viewer.setInput(ModelProvider.getInstance().getDepartments() .toArray());
>
> getSite().setSelectionProvider(viewer);
> }
>
>
> /**
> * Passing the focus request to the viewer's control.
> */
> public void setFocus() {
> viewer.getControl().setFocus();
> }
>
>
> public static void refresh(){
> viewer.refresh();
> }
>
> }
>
>
> Luke
|
|
|
Re: [DataBinding] Problem with model [message #514092 is a reply to message #514073] |
Fri, 12 February 2010 12:23 |
Luke Jedrych Messages: 5 Registered: February 2010 |
Junior Member |
|
|
Nevertheless I've change that code, it doesn't work correctly and throws:
!ENTRY org.eclipse.core.databinding 2 0 2010-02-12 13:18:59.603
!MESSAGE Could not read value of [].institutes
!STACK 0
java.lang.IllegalArgumentException: object is not an instance of declaring class
!ENTRY org.eclipse.core.databinding 2 0 2010-02-12 13:18:59.620
!MESSAGE Cound not remove listener from []
!STACK 0
java.lang.NoSuchMethodException: org.eclipse.core.databinding.observable.set.WritableSet.remo vePropertyChangeListener(java.beans.PropertyChangeListener)
!ENTRY org.eclipse.core.databinding 2 0 2010-02-12 13:18:59.626
!MESSAGE Could not attach listener to []
!STACK 0
package myproject1;
import model.Department;
import model.Institute;
import model.ModelProvider;
import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.core.databinding.observable.list.WritableList;
import org.eclipse.jface.databinding.viewers.ObservableSetTreeContentProvider;
import org.eclipse.jface.databinding.viewers.ViewerSupport;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import providers.MyObservableFactory;
import providers.MyTreeStructureAdvisor;
public class View extends ViewPart {
public View() {
}
public static final String ID = "myproject1.view";
private static TreeViewer viewer;
/**
* This is a callback that will allow us to create the viewer and initialize
* it.
*/
public void createPartControl(Composite parent) {
createViewer(parent);
}
public void createViewer(Composite parent){
viewer=new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL
| SWT.V_SCROLL | SWT.FULL_SELECTION );
viewer.setContentProvider(new ObservableSetTreeContentProvider(new MyObservableFactory(), new MyTreeStructureAdvisor()));
//viewer.setLabelProvider(new ObservableMapLabelProvider(BeansObservables.observeMaps(ModelProvider.getInstance().wdepartments, new String[]{"name", "institutes"})));
//viewer.setLabelProvider(new MyLabelProvider());
viewer.setSorter(new ViewerSorter());
viewer.refresh(true);
ViewerSupport.bind(viewer, new WritableList(ModelProvider.getInstance().departments,Department.class), BeanProperties.list(Department.class, "institutes"), BeanProperties.value(Department.class, "name"));
viewer.setInput(new WritableList(ModelProvider.getInstance().departments, Department.class));
getSite().setSelectionProvider(viewer);
}
/**
* Passing the focus request to the viewer's control.
*/
public void setFocus() {
viewer.getControl().setFocus();
}
public static void refresh(){
viewer.refresh();
}
}
|
|
|
Re: [DataBinding] Problem with model [message #514109 is a reply to message #514092] |
Fri, 12 February 2010 13:02 |
Thomas Schindl Messages: 6651 Registered: July 2009 |
Senior Member |
|
|
Your problem now is your observable map creation:
ObservableSetTreeContentProvider pv = ... ;
BeansObservables.observeMaps(pv.getKnowElements(),....);
Is the correct one!
Tom
Am 12.02.10 13:23, schrieb Luke Jedrych:
> Nevertheless I've change that code, it doesn't work correctly and throws:
>
> !ENTRY org.eclipse.core.databinding 2 0 2010-02-12 13:18:59.603
> !MESSAGE Could not read value of [].institutes
> !STACK 0
> java.lang.IllegalArgumentException: object is not an instance of
> declaring class
>
> !ENTRY org.eclipse.core.databinding 2 0 2010-02-12 13:18:59.620
> !MESSAGE Cound not remove listener from []
> !STACK 0
> java.lang.NoSuchMethodException:
> org.eclipse.core.databinding.observable.set.WritableSet.remo
> vePropertyChangeListener(java.beans.PropertyChangeListener)
>
> !ENTRY org.eclipse.core.databinding 2 0 2010-02-12 13:18:59.626
> !MESSAGE Could not attach listener to []
> !STACK 0
>
>
> package myproject1;
>
> import model.Department;
> import model.Institute;
> import model.ModelProvider;
>
> import org.eclipse.core.databinding.beans.BeanProperties;
> import org.eclipse.core.databinding.observable.list.WritableList;
> import
> org.eclipse.jface.databinding.viewers.ObservableSetTreeConte ntProvider;
> import org.eclipse.jface.databinding.viewers.ViewerSupport;
> import org.eclipse.jface.viewers.TreeViewer;
> import org.eclipse.jface.viewers.ViewerSorter;
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.widgets.Composite;
> import org.eclipse.ui.part.ViewPart;
>
> import providers.MyObservableFactory;
> import providers.MyTreeStructureAdvisor;
>
> public class View extends ViewPart {
> public View() {
> }
> public static final String ID = "myproject1.view";
>
> private static TreeViewer viewer;
>
>
> /**
> * This is a callback that will allow us to create the viewer and
> initialize
> * it.
> */
> public void createPartControl(Composite parent) {
> createViewer(parent);
> }
>
> public void createViewer(Composite parent){
> viewer=new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL
> | SWT.V_SCROLL | SWT.FULL_SELECTION );
>
> viewer.setContentProvider(new
> ObservableSetTreeContentProvider(new MyObservableFactory(), new
> MyTreeStructureAdvisor()));
> //viewer.setLabelProvider(new
> ObservableMapLabelProvider(BeansObservables.observeMaps(Mode lProvider.getInstance().wdepartments,
> new String[]{"name", "institutes"})));
> //viewer.setLabelProvider(new MyLabelProvider());
> viewer.setSorter(new ViewerSorter());
> viewer.refresh(true);
>
> ViewerSupport.bind(viewer, new
> WritableList(ModelProvider.getInstance().departments,Departm ent.class),
> BeanProperties.list(Department.class, "institutes"),
> BeanProperties.value(Department.class, "name"));
> viewer.setInput(new
> WritableList(ModelProvider.getInstance().departments, Department.class));
>
> getSite().setSelectionProvider(viewer);
> }
>
>
> /**
> * Passing the focus request to the viewer's control.
> */
> public void setFocus() {
> viewer.getControl().setFocus();
> }
>
>
> public static void refresh(){
> viewer.refresh();
> }
>
> }
>
>
>
|
|
|
Re: [DataBinding] Problem with model [message #514123 is a reply to message #513918] |
Fri, 12 February 2010 13:55 |
Luke Jedrych Messages: 5 Registered: February 2010 |
Junior Member |
|
|
That didn't help. The same exception are thrown.
!ENTRY org.eclipse.core.databinding 2 0 2010-02-12 14:30:48.326
!MESSAGE Could not attach listener to [AAA, BBB]
!STACK 0
java.lang.NoSuchMethodException: org.eclipse.core.databinding.observable.list.WritableList.ad dPropertyChangeListener(java.beans.PropertyChangeListener)
!ENTRY org.eclipse.core.databinding 2 0 2010-02-12 14:30:48.330
!MESSAGE Could not read value of [AAA, BBB].institutes
!STACK 0
java.lang.IllegalArgumentException: object is not an instance of declaring class
!ENTRY org.eclipse.core.databinding 2 0 2010-02-12 14:30:48.321
!MESSAGE Cound not remove listener from [AAA, BBB]
!STACK 0
java.lang.NoSuchMethodException: org.eclipse.core.databinding.observable.set.WritableSet.remo vePropertyChangeListener(java.beans.PropertyChangeListener)
I have to look for bug in the line:
ViewerSupport.bind(viewer, new WritableSet(ModelProvider.getInstance().departments,Department.class), BeanProperties.list(Department.class, "institutes"), BeanProperties.value(Department.class, "name"));
Great thanks for your help and sorry for problems.
Luke
|
|
|
Goto Forum:
Current Time: Fri Apr 26 08:07:30 GMT 2024
Powered by FUDForum. Page generated in 0.04945 seconds
|