FeaturedItemProviderAdapterFactory

如果想在viewer里过滤掉一些不需要的元素,除了直接修改XXXItemProvider(因为程序的其他地方也会用到这个类,修改它可能会带来问题),另一个办法是通过覆盖XXXItemProviderAdapterFactory,以使用自己继承自XXXItemProvider的ItemProvider,下面的例子是只显示一种类型的child:

/**
 * Adapter factory that only have one kind of child displayed
 * @author zhanghao
 *
 */
class FeaturedItemProviderAdapterFactory extends BMItemProviderAdapterFactory{
    EStructuralFeature feature;
    
    /**
     * @param feature
     */
    public FeaturedItemProviderAdapterFactory(EStructuralFeature feature) {
        super();
        this.feature = feature;
    }

    @Override
    public Adapter createBusinessModelAdapter() {
        if (businessModelItemProvider == null) {
            businessModelItemProvider = new FeaturedBusinessModelItemProvider(this, feature);
        }
        return businessModelItemProvider;
    }
    
    class FeaturedBusinessModelItemProvider extends BusinessModelItemProvider{
        EStructuralFeature feature;
        /**
         * @param adapterFactory
         */
        public FeaturedBusinessModelItemProvider(AdapterFactory adapterFactory, EStructuralFeature reference) {
            super(adapterFactory);
            this.feature=reference;
        }

        @Override
        public Collection getChildrenFeatures(Object object) {
            Collection collection = super.getChildrenFeatures(object);
            for (Iterator iterator = collection.iterator(); iterator.hasNext();) {
                EStructuralFeature feature = (EStructuralFeature) iterator.next();
                if(this.feature != feature)
                    iterator.remove();
            }
            return childrenFeatures;
        }
    }
}

使用的时候一般还是要用ComposedAdapterFactory:

List list = new ArrayList();
list.add(new ResourceItemProviderAdapterFactory());
list.add(new FeaturedItemProviderAdapterFactory(BMPackage.Literals.BUSINESS_MODEL__ENTITIES));//提供一个feature作为参数
list.add(new ReflectiveItemProviderAdapterFactory());
ComposedAdapterFactory factory=new ComposedAdapterFactory(list);
entityViewer.setContentProvider(new AdapterFactoryContentProvider(factory));

注:另一个方法是:

viewer.setInput(new ItemProvider(model.getXXXChildren()));

但这样一些notification会得不到响应,例如新增一个XXX元素后,viewer没有得到刷新。

posted @ 2007-04-09 19:04  八进制  阅读(3400)  评论(0编辑  收藏  举报