[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Newsgroup Home]
|
[news.eclipse.platform] ObservabelList question and databinding
|
- From: joelgreg@xxxxxxxxx (Joel Gregoire)
- Date: Fri, 22 Feb 2008 20:06:30 +0000 (UTC)
- Newsgroups: eclipse.platform
- Organization: Eclipse
- User-agent: NewsPortal/0.36 (http://florian-amrhein.de/newsportal)
Hi there,
I've been playing with Observables for a while and I'm wondering if I'm
missing something. Is it possible to detect a change in one of the items
within an ObservableList?? I can surely detect additions and removals but
I can't detect a change of an item.
In the following snippet, 2 lists are bound together, I make a change to
one and the second gets updated. Good. However the list change event is
not fired. Do I need to register a listener for every item in the list? It
has to be simple.
Any help is appreciated.
Thanks, JG
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.observable.AbstractObservable;
import org.eclipse.core.databinding.observable.ChangeEvent;
import org.eclipse.core.databinding.observable.IChangeListener;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.list.IListChangeListener;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.list.ListChangeEvent;
import org.eclipse.core.databinding.observable.list.WritableList;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Test
{
public static void main(String[] args)
{
final Display display = new Display();
Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable()
{
public void run()
{
Shell shell = new View().createShell();
}
});
}
public static abstract class AbstractModelObject extends
AbstractObservable
{
public AbstractModelObject()
{
super( Realm.getDefault() );
}
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);
}
public boolean isStale()
{
return false;
}
}
private static class Month
{
private String name;
public Month(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public String toString()
{
return "month:" + name;
}
}
static class Person extends AbstractModelObject
{
String name = "John Smith";
Month month = null;
public Person(String name, Month month)
{
this.name = name;
this.month = month;
}
public String getName()
{
return name;
}
public void setName( String name )
{
String oldValue = this.name;
this.name = name;
firePropertyChange("name", oldValue, name);
}
public Month getMonth()
{
return month;
}
public void setMonth( Month month )
{
Month oldValue = this.month;
this.month = month;
firePropertyChange( "month", oldValue, month );
}
public String toString()
{
return "[Person] name:" + name + ", " + month.toString();
}
}
static class View
{
public Shell createShell()
{
Display display = Display.getDefault();
Shell shell = new Shell(display);
DataBindingContext bindingContext = new DataBindingContext();
shell.setLayout( new FillLayout(SWT.VERTICAL) );
bindGUI(bindingContext);
shell.open();
return shell;
}
protected void bindGUI(DataBindingContext bindingContext)
{
IObservableList sourceObservableList = new WritableList();
IObservableList targetObservableList = new WritableList();
Person stephane = new Person( "Brad", new Month("23") );
Person sylvain = new Person( "Rod", new Month("18") );
Person marc = new Person( "Jack", new Month("8") );
sourceObservableList.add( stephane );
sourceObservableList.add( sylvain );
sourceObservableList.add( marc );
bindingContext.bindList ( targetObservableList,
sourceObservableList,
null,
null );
targetObservableList.addListChangeListener( new ListChangeListener() );
targetObservableList.addChangeListener( new ChangeListener() );
stephane.setMonth( new Month("50") );
System.out.println( targetObservableList );
}
class ListChangeListener implements IListChangeListener
{
public void handleListChange( ListChangeEvent event )
{
System.out.println("List change!");
}
}
class ChangeListener implements IChangeListener
{
public void handleChange( ChangeEvent event )
{
System.out.println("Change!");
}
}
}
}