我将 Eclipse 数据绑定与 bean 和 SWT java 应用程序一起使用。我成功地将几个文本字段和复选框与我的模型绑定在一起。现在我有三个单选按钮:
图形用户界面:
...
Model model = new Model();
...
Button btnAllUsers = new Button(grpExport, SWT.RADIO);
Button btnBatchUsersOnly = new Button(grpExport, SWT.RADIO);
Button btnCurrentBatchOnly = new Button(grpExport, SWT.RADIO);
...
// here comes the big question mark... :(
IObservableValue swtObsBtnAllUsers = SWTObservables.observeSelection(btnAllUsers);
IObservableValue beanObsUserFilter = BeansObservables.observeValue(model, "userFilter");
bindingContext.bindValue(swtObsBtnAllUsers, beanObsUserFilter);
...
模型:
public class Model {
public enum UserFilter {
ALL_USERS, BATCH_USERS_ONLY, CURRENT_BATCH_ONLY
}
private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
private UserFilter userFilter;
public void addPropertyChangeListener(...) {...}
public void removePropertyChangeListener(...) {...}
}
现在我想将三个单选按钮绑定到属性userFilter
。转换逻辑应该是这样的:
GUI到模型的转换:
if (btnAllUsers != null && btnBatchUsersOnly != null && btnCurrentBatchOnly != null) {
if (btnAllUsers.getSelection()) {
model.userFilter = UserFilter.ALL_USERS;
} else if (btnBatchUsersOnly.getSelection()) {
model.userFilter = UserFilter.BATCH_USERS_ONLY;
} else {
model.userFilter = UserFilter.CURRENT_BATCH_ONLY;
}
}
模型到 GUI 的转换:
if (btnAllUsers != null && btnBatchUsersOnly != null && btnCurrentBatchOnly != null) {
btnAllUsers.setSelection(model.getUserFilter() == UserFilter.ALL_USERS);
btnBatchUsersOnly.setSelection(model.getUserFilter() == UserFilter.BATCH_USERS_ONLY);
btnCurrentBatchOnly.setSelection(model.getUserFilter() == UserFilter.CURRENT_BATCH_ONLY);
}
我阅读了有关特定 UpdateValueStrategies 和转换的信息,但并没有真正开始。有人可以帮我举一个简短的例子吗?提前致谢!