UpdateValueStrategy and custom conversion multiple GUI elements to one property

250 Views Asked by At

I'm using the eclipse databinding with beans and an SWT java application. I successfully binded several textfields and checkboxes with my model. Now I have three radio buttons:

GUI:

...
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);
...

Model:

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(...) {...}
}

Now I want to bind the three radio buttons to the property userFilter. The conversion logic should be something like this:

GUI to model conversion:

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;
    }
}

Model to GUI conversion:

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);
}

I read about specific UpdateValueStrategies and conversions but did not really get the start. Can anybody help me with a short example? Thanks in advance!

0

There are 0 best solutions below