I was wondering if anyone could explain to me how I could properly bind a group of radio buttons to a boolean variable in the model using JFace data binding.
Let me explain the situation first: I've created a class that represents a group of SWT buttons (with the style set to 'SWT.RADIO') that consists of three elements: A label with the question and two buttons, one for the "yes" answer and one for a "no". I would like to create a binding to a boolean variable in the model in such a way that when a user selects the "yes" radio button the boolean is set to true, and when he/she selects the "no" button the boolean is set to false.
Here's the code of my class:
private class YesOrNoRadioButtonGroup {
private static final String YES = "yes";
private static final String NO = "no";
private Button m_yesButton;
private Button m_noButton;
public YesOrNoRadioButtonGroup(final Composite p_parent,
final String p_questionText,
final IObservableValue p_modelProperty
final DataBindingContext p_dbContext)
{
Composite radioButtonGroupContainer = new Composite(p_parent, SWT.NONE);
radioButtonGroupContainer.setLayout(new GridLayout());
Label question = new Label(radioButtonGroupContainer, SWT.NONE);
question.setText(p_questionText);
m_yesButton = new Button(radioButtonGroupContainer, SWT.RADIO);
m_yesButton.setText(YES);
m_noButton = new Button(radioButtonGroupContainer, SWT.RADIO);
m_noButton.setText(NO);
m_noButton.setSelection(true);
Listener yesOrNoRadioGroupListener = new Listener() {
public void handleEvent(Event p_event) {
Button button = (Button) p_event.widget;
if (m_yesButton.equals(button)) {
m_yesButton.setSelection(true);
m_noButton.setSelection(false);
}
else {
m_yesButton.setSelection(false);
m_noButton.setSelection(true);
}
}
};
m_yesButton.addListener(SWT.Selection, yesOrNoRadioGroupListener);
m_noButton.addListener(SWT.Selection, yesOrNoRadioGroupListener);
p_dbContext.bindValue(SWTObservables.observeSelection(this.getYesButton()),
p_modelProperty, null, null);
}
public Button getYesButton() {
return m_yesButton;
}
public Button getNoButton() {
return m_noButton;
}
}
Now, as you can see, I'm binding my "yes" button to the boolean. Specifically, the value will be bound on the SWT.selection event. This is, it seems, the only valid event for the binding of a radio button. However, because of this, once the "no" button is selected, the value of the boolean remains unchanged (since no SWT.selection event on the "yes" button was fired).
What can I do to make this work the way it's supposed to, i.e. to be able to change the value of the boolean in the model based on which of the buttons the user selects?
Am I missing something obvious here?
Thanks!