如何使用带有复选框 SWT 按钮的自动文本绑定

发布于 2024-12-18 20:56:08 字数 1250 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青巷忧颜 2024-12-25 20:56:08

好的。据我了解,用户选择一个按钮,选择将转移到模型,然后您希望按钮显示一些新文本。如果是这种情况,解决方案如下:

DataBindingContext dbc = getBindingContext();
org.eclipse.swt.widgets.Button b = null; // New with the proper flag for CHECK
Object theModelBean = null;  // Initialize as you wish

现在,您需要两个绑定。第一个绑定,将选择绑定到模型中的布尔值:

ISWTObservableValue targetBool = WidgetProperties.selection().observe(b);
IObservableValue modelBool = BeanProperties.value("someBooleanField").observe(theModelBean);
dbc.bindValue(targetBool, modelBool);

现在,当发生选择时,将在模型对象上调用“setSomeBooleanField”方法。为了也更改按钮的文本,此设置器必须调用模型对象上的另一个设置器,设置您想要显示的新“文本”。第二个 setter 会触发一个 Property Change 事件,该事件又会唤醒您的第二个绑定,即:

IObservableValue targetText = BeanProperties.value("text").observe(b);
IObservableValue modelText = BeanProperties.value("theTextInTheBean").observe(theModelBean);
dbc.bindValue(targetText, modelText);

此绑定仅适用于从模型到目标的情况,因此您可以将 targetToModel 策略设置为 NEVER。

您会看到,ViewerProperties 不支持按钮的“文本”字段,但 WidgetProperties 和 BeanProperties 类也支持它。只要设置器触发属性更改事件,您就可以使用 BeanProperties 绑定几乎任何内容。

模型中的设置器必须如下所示:

public void setSomeBooleanField(boolean b){
    firePropertyChange("someBooleanField", this.someBooleanField, this.someBooleanField = b);
    // Form the new Text
    this.setNewText(newText);
}

public void setNewText(String newText){
    firePropertyChange("newText", this.newText, this.newText = newText); // Wake up the second Binding
}

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:

DataBindingContext dbc = getBindingContext();
org.eclipse.swt.widgets.Button b = null; // New with the proper flag for CHECK
Object theModelBean = null;  // Initialize as you wish

Now, You need TWO bindings. The first Binding, Binds the selection to a boolean in the model:

ISWTObservableValue targetBool = WidgetProperties.selection().observe(b);
IObservableValue modelBool = BeanProperties.value("someBooleanField").observe(theModelBean);
dbc.bindValue(targetBool, modelBool);

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:

IObservableValue targetText = BeanProperties.value("text").observe(b);
IObservableValue modelText = BeanProperties.value("theTextInTheBean").observe(theModelBean);
dbc.bindValue(targetText, modelText);

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:

public void setSomeBooleanField(boolean b){
    firePropertyChange("someBooleanField", this.someBooleanField, this.someBooleanField = b);
    // Form the new Text
    this.setNewText(newText);
}

public void setNewText(String newText){
    firePropertyChange("newText", this.newText, this.newText = newText); // Wake up the second Binding
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文