我有一些包含可观察项目列表的Data
类:
public class Data {
private final IObservableList items = WritableList.withElementType(Item.class);
public IObservableList getItems() {
return items;
}
}
当我直接绑定到项目时,一切都很好:
data.addListChangeListener(myListener); //OK
但是,当我通过BeanProperties.list()
ListChangeEvent
绑定到项目时,不会传播
BeanProperties.list(Data.class, "items", Item.class).
observe(data).addListChangeListener(myListener); //Not working
我的目标是自动向Tree添加行(并从中删除)。
Realm realm = SWTObservables.getRealm(composite.getDisplay());
IListProperty listProperty = BeanProperties.list(Data.class, "items", Item.class);
IObservableFactory observableFactory = listProperty.listFactory(realm);
ObservableListTreeContentProvider contentProvider = new ObservableListTreeContentProvider(observableFactory, null);
treeViewer.setContentProvider(contentProvider);
答案 0 :(得分:0)
似乎BeanProperties
并不期望该属性为IObservableList
。所以我实现了自己的ObservableListProperty
。它只使用 ,其属性为IObservableList
类型。
public class ObservableListProperty extends ListProperty {
private final PropertyDescriptor propertyDescriptor;
private final Class elementType;
public ObservableListProperty(Class beanClass, String propertyName) {
this(beanClass, propertyName, null);
}
public ObservableListProperty(Class beanClass, String propertyName, Class elementType) {
propertyDescriptor = BeanPropertyHelper.getPropertyDescriptor(beanClass, propertyName);
if (elementType == null) {
elementType = BeanPropertyHelper.getCollectionPropertyElementType(propertyDescriptor);
}
this.elementType = elementType;
}
@Override
public Object getElementType() {
return elementType;
}
@Override
public IObservableList observe(Realm realm, Object source) {
IObservableList list = (IObservableList)BeanPropertyHelper.readProperty(source, propertyDescriptor);
return new DecoratingObservableList(list, false);
}
}