如何使用带有复选框 SWT 按钮的自动文本绑定
我正在使用 JFace 数据绑定框架将复选框选择与模型链接起来:
final DataBindingContext ctx = new DataBindingContext();
final Realm realm = ctx.getValidationRealm();
final IViewerObservableValue selection = ViewersObservables.observeSingleSelection(viewer);
final IObservableValue selectionObservable = BeansObservables.observeDetailValue(realm, selection, "isSelected", boolean.class);
final UpdateValueStrategy strategy = new UpdateValueStrategy(true, UpdateValueStrategy.POLICY_UPDATE);
ctx.bindValue(SWTObservables.observeSelection(button), selectionObservable, strategy, strategy);
复选框的文本来显示有关当前选择的特定信息,但不可能使用 observeText(Control)
:
final UpdateValueStrategy update = new UpdateValueStrategy(true, UpdateValueStrategy.POLICY_NEVER);
ctx.bindValue(SWTObservables.observeText(button), textObservable, null, update);
演示文稿需要使用 导致 SWTException:
无法创建视图:不支持小部件 [org.eclipse.swt.widgets.Button]。
有没有办法在 SWT Button 上进行文本绑定?
编辑
快速解决方案是在复选框旁边放置一个标签,然后可以使用 SWTObservables.observeText(label)
轻松完成绑定
解决方案
它适用于 eclipse 3.7
I'm using JFace databinding framework to link checkbox selection with model:
final DataBindingContext ctx = new DataBindingContext();
final Realm realm = ctx.getValidationRealm();
final IViewerObservableValue selection = ViewersObservables.observeSingleSelection(viewer);
final IObservableValue selectionObservable = BeansObservables.observeDetailValue(realm, selection, "isSelected", boolean.class);
final UpdateValueStrategy strategy = new UpdateValueStrategy(true, UpdateValueStrategy.POLICY_UPDATE);
ctx.bindValue(SWTObservables.observeSelection(button), selectionObservable, strategy, strategy);
The presentation needs to use the checkbox's text to display specific information about the current selection, but it's not possible to use observeText(Control)
:
final UpdateValueStrategy update = new UpdateValueStrategy(true, UpdateValueStrategy.POLICY_NEVER);
ctx.bindValue(SWTObservables.observeText(button), textObservable, null, update);
It causes an SWTException:
Could not create the view: Widget [org.eclipse.swt.widgets.Button] is not supported.
Is there a way to do text binding on SWT Button ?
EDIT
The fast solution is to put a Label next the checkbox, then the binding could be done easily with SWTObservables.observeText(label)
SOLUTION
It works on eclipse 3.7
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的。据我了解,用户选择一个按钮,选择将转移到模型,然后您希望按钮显示一些新文本。如果是这种情况,解决方案如下:
现在,您需要两个绑定。第一个绑定,将选择绑定到模型中的布尔值:
现在,当发生选择时,将在模型对象上调用“setSomeBooleanField”方法。为了也更改按钮的文本,此设置器必须调用模型对象上的另一个设置器,设置您想要显示的新“文本”。第二个 setter 会触发一个 Property Change 事件,该事件又会唤醒您的第二个绑定,即:
此绑定仅适用于从模型到目标的情况,因此您可以将 targetToModel 策略设置为 NEVER。
您会看到,ViewerProperties 不支持按钮的“文本”字段,但 WidgetProperties 和 BeanProperties 类也支持它。只要设置器触发属性更改事件,您就可以使用 BeanProperties 绑定几乎任何内容。
模型中的设置器必须如下所示:
OK. From what I understand, the user selects a button, the selection is transferred to the Model, and then you want the Button to show some new text. If that's the case, here is the solution:
Now, You need TWO bindings. The first Binding, Binds the selection to a boolean in the model:
Now, when the selection occurs, the "setSomeBooleanField" method is called on the model Object. In order to also change the Text of the Button, this setter MUST call another setter on your model Object setting the new "text" that you want to be shown. This second setter fires a Property Change event which in turn wakes up your second binding which is:
This binding is going to work only from model to target, so you can set thje targetToModel policy to NEVER.
You see, the "text" field of button is not supported by ViewerProperties, but it is Supported by WidgetProperties and also the BeanProperties class. You can Bind Pretty much ANYTHING with BeanProperties as long as the setter fires a Property Change Event.
The setters in the model must be like this: