如何将索引属性绑定到jface viewer

时间:2022-10-09 22:24:12

I want to bind an indexed property to JFace ComboViewer.

我想将索引属性绑定到JFace ComboViewer。

Lets say that I have a DataModel class like this:

让我们说我有一个像这样的DataModel类:

class DataModel {
   private String[] props = {"A","B","C"};
   private PropertyChangeSupport pcs = new PropertyChangeSupport(this);

   public String getProperties( int idx ){
      return props[idx];
   }

   public void setProperties( int idx, String value ){
      String oldVal = props[idx];
      props[idx] = value;
      pcs.fireIndexedPropertyChange( "properties", idx, oldVal, value );
   }

   // code to add/remove PropertyChangeListener
   // ...
}

The data binding code for simple property would look like this:

简单属性的数据绑定代码如下所示:

DataModel dataModel = ...
ComboViewer propertyChoice = ...

DataBindingContext ctx = new DataBindingContext();

IObservableValue target = ViewerProperties.singleSelection().observe( propertyChoice );
IObservableValue model = BeanProperties.value( DataModel.class, "properties" ).observe(dataModel);
ctx.bindValue( target, model ); 

but with an indexed property I have to inform the ctx at which index is the value that I want to bind. I have tried

但是对于索引属性,我必须通知ctx哪个索引是我想要绑定的值。我试过了

IObservableValue model = BeanProperties.value( DataModel.class, "properties[0]" ).observe(dataModel);

but it doesn't work.

但它不起作用。

Is it possible to bind indexed property instead of simple property? How?

是否可以绑定索引属性而不是简单属性?怎么样?

1 个解决方案

#1


0  

Unfortunately this seems to be unsupported. I was looking for exactly the same functionality. There is no documentation in BeanProperties that says it is supported.

不幸的是,这似乎没有得到支持。我一直在寻找完全相同的功能。 BeanProperties中没有文档表明它受支持。

When looking into the implementation of BeanProperties.value, you find that it delegates to BeanPropertyHelper for reading and writing a property. The method Object readProperty(Object source, PropertyDescriptor propertyDescriptor) does not know about the subclass IndexedPropertyDescriptor. When it is invoked for an indexed property, readProperty tries to use a read method that reads the entire array. I think this method is optional for indexed properties. For indexed properties it should use the IndexedPropertyDescriptor.getIndexedReadMethod().

在查看BeanProperties.value的实现时,您会发现它委托给BeanPropertyHelper来读取和写入属性。 Object readProperty(Object source,PropertyDescriptor propertyDescriptor)方法不知道子类IndexedPropertyDescriptor。当为索引属性调用它时,readProperty尝试使用读取整个数组的read方法。我认为这个方法对于索引属性是可选的。对于索引属性,它应该使用IndexedPropertyDescriptor.getIndexedReadMethod()。

Depending on your use case you may be able to workaround the problem by using BeanProperties.list. However you cannot use this in combination with indexed properties. I tried this by adding a method that returns the entire array but still keeping the method that does a "fireIndexedPropertyChange". Unfortunately this gives a ClassCastException: Eclipse's BeanListProperty seems to suppose that the value in the change event is an array or list. However for an indexed property it is a single element of the array.

根据您的使用情况,您可以使用BeanProperties.list解决问题。但是,您不能将其与索引属性结合使用。我尝试通过添加一个方法来返回整个数组,但仍然保持执行“fireIndexedPropertyChange”的方法。不幸的是,这给出了一个ClassCastException:Eclipse的BeanListProperty似乎假设change事件中的值是一个数组或列表。但是对于索引属性,它是数组的单个元素。

Or perhaps you can use an observable map instead?

或许您可以使用可观察的地图代替?

#1


0  

Unfortunately this seems to be unsupported. I was looking for exactly the same functionality. There is no documentation in BeanProperties that says it is supported.

不幸的是,这似乎没有得到支持。我一直在寻找完全相同的功能。 BeanProperties中没有文档表明它受支持。

When looking into the implementation of BeanProperties.value, you find that it delegates to BeanPropertyHelper for reading and writing a property. The method Object readProperty(Object source, PropertyDescriptor propertyDescriptor) does not know about the subclass IndexedPropertyDescriptor. When it is invoked for an indexed property, readProperty tries to use a read method that reads the entire array. I think this method is optional for indexed properties. For indexed properties it should use the IndexedPropertyDescriptor.getIndexedReadMethod().

在查看BeanProperties.value的实现时,您会发现它委托给BeanPropertyHelper来读取和写入属性。 Object readProperty(Object source,PropertyDescriptor propertyDescriptor)方法不知道子类IndexedPropertyDescriptor。当为索引属性调用它时,readProperty尝试使用读取整个数组的read方法。我认为这个方法对于索引属性是可选的。对于索引属性,它应该使用IndexedPropertyDescriptor.getIndexedReadMethod()。

Depending on your use case you may be able to workaround the problem by using BeanProperties.list. However you cannot use this in combination with indexed properties. I tried this by adding a method that returns the entire array but still keeping the method that does a "fireIndexedPropertyChange". Unfortunately this gives a ClassCastException: Eclipse's BeanListProperty seems to suppose that the value in the change event is an array or list. However for an indexed property it is a single element of the array.

根据您的使用情况,您可以使用BeanProperties.list解决问题。但是,您不能将其与索引属性结合使用。我尝试通过添加一个方法来返回整个数组,但仍然保持执行“fireIndexedPropertyChange”的方法。不幸的是,这给出了一个ClassCastException:Eclipse的BeanListProperty似乎假设change事件中的值是一个数组或列表。但是对于索引属性,它是数组的单个元素。

Or perhaps you can use an observable map instead?

或许您可以使用可观察的地图代替?