数据绑定和验证 TableViewer?
我使用 org.eclipse.core.databinding
框架来绑定 SWT 应用程序中的一些 Text
字段。我添加了一个更新策略来验证数据,并仅当用户单击“保存”按钮时才在模型上设置值:
UpdateValueStrategy toModel = new UpdateValueStrategy(UpdateValueStrategy.POLICY_CONVERT);
if (validator != null) {
toModel.setAfterGetValidator(validator);
}
UpdateValueStrategy fromModel = new UpdateValueStrategy(UpdateValueStrategy.POLICY_UPDATE);
binding = bindingContext.bindValue(SWTObservables.observeText(this, SWT.Modify),
BeansObservables.observeValue(pVO, propertyName), toModel, fromModel);
这段代码运行得非常好。
但是我如何在 TableViewer 上执行相同的操作呢?
我希望它能够工作,以便当我在 IHM 中添加某些内容时,模型保持不变,直到我调用 getBindingContext().updateModels();
I use the org.eclipse.core.databinding
framework to bind some Text
fields in an SWT application. I add an update strategy to validate the data and to set the value on the model only when the user click on the save button:
UpdateValueStrategy toModel = new UpdateValueStrategy(UpdateValueStrategy.POLICY_CONVERT);
if (validator != null) {
toModel.setAfterGetValidator(validator);
}
UpdateValueStrategy fromModel = new UpdateValueStrategy(UpdateValueStrategy.POLICY_UPDATE);
binding = bindingContext.bindValue(SWTObservables.observeText(this, SWT.Modify),
BeansObservables.observeValue(pVO, propertyName), toModel, fromModel);
This piece of code works really well.
But how can I do the same on a TableViewer
?
I want it to work so that when I add something in the IHM, the model stay unchanged until I call getBindingContext().updateModels();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不需要在
TableViewer
中使用 JFace 数据绑定框架。操作结构化数据比 SWT 控件(例如TableViewer
、ListViewer
和TreeViewer
)更简单。您可以以相同的方式使用这些查看器:创建查看器后,只需调用
viewer.setInput(data)
将所有内容呈现给您的查看者。有一个模型列表:
神奇的事情发生在内容提供者中:
每个模型将成为一个
TableItem
以及TableItem(item.getData())
中的模型。但是,对于由许多列组成的表格,您需要
LabelProvider
来帮助您将模型的属性映射到TableItem
:将模型传播到查看器很容易。如果您要将查看器传播到绑定模型,那么使用
CellEditor
也很简单。要使用
CellEditor
,您需要将列属性,单元格编辑器和单元格修改器设置为TableViewer
:CellModifier就像这样:
一切都很简单,但需要很多步骤才能将所有内容组合在一起。
You do not need use the JFace Databinding Framework in
TableViewer
. Manipulation the structured data is simpler then SWT controls, suchTableViewer
,ListViewer
andTreeViewer
. You can use those viewer in the same way:After the viewer created, just invoke
viewer.setInput(data)
to put all the things to your viewer.There are a list of model:
The magic happens in the content provider:
Each model will become a
TableItem
and the model in theTableItem(item.getData())
.However, a table composed by many columns, you need the
LabelProvider
to help you mapping the property of model to theTableItem
:The propagation of models to viewer is easy. If you will propagate viewer to the binded model, using the
CellEditor
is simple as well.To use
CellEditor
, you need set the column properties, cell editors and cell modifier toTableViewer
:The CellModifier likes this:
Everything is simple but there are many steps to make all together.
使用查看器支持:
Use ViewerSupport:
我同意资格。
jface.databinding
片段中的Snippet017TableViewerWithDerivedColumns
就是一个完整的示例。i agree with qualidafial.
Snippet017TableViewerWithDerivedColumns
from thejface.databinding
snippets is a full example of this.