ViewerSupport ComboViewer,如何绑定List <long>?</long>

时间:2014-06-05 18:24:23

标签: java data-binding combobox swt jface

这个问题似乎很容易,但我不知道如何做到这一点。 让我们假设这个简短的片段:

List<Long> model = new ArrayList<Long>();
model.add(Long.valueOf(1d));
model.add(Long.valueOf(2d));
model.add(Long.valueOf(3d));
WritableList list = new WritableList(model, Long.class);

comboViewer = new ComboViewer(composite, SWT.READ_ONLY);
comboViewer.setContentProvider(new ObservableListContentProvider());
ViewerSupport.bind(comboViewer, list, ???);

我需要在ViewerSupport.bind中插入第三个参数来简单地显示ComboViewer中的数字?

通常必须有类似“BeanProperties.values(new String [] {”name“})”但我希望使用long的toString()getter。

1 个答案:

答案 0 :(得分:0)

您可以通过以下方式绑定ComboViewer:

import org.eclipse.swt.widgets.Combo;

/*
 * model - your model object
 * modelAttrName - List<Long> attribute name, within your model
 * toModel, fromModel - update value strategies. can be jsut new UpdateValueStrategy() in 
 * the beginning
*/
public static Binding bindCombo(DataBindingContext bindingContext, Object model, String modelAttrName, UpdateValueStrategy toModel, UpdateValueStrategy fromModel) {

Combo combo = comboViewer.getCombo();

return dataBindingContext.bindValue(WidgetProperties.selection().observe(combo),
    BeansObservables.observeValue(model, modelAttrName), toModel, fromModel);

}

您的模型应该如下所示:

class MyModel {

private List<Long> longs;

// provide getters and setters for logs class and fire property change events, using property change support

}

您可以这样使用它:

MyModel model = new MyModel();
bindCombo(new DataBindingContext(), model, "longs", new UpdateValueStrategy(), new UpdateValueStrategy());