BeanProperties.list()。observe()不传播ListChangeEvent

时间:2015-07-14 12:26:01

标签: java data-binding swt jface

我有一些包含可观察项目列表的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);

1 个答案:

答案 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);
    }
}