Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to bind a radio button group using JFace data binding

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!

like image 580
Sandman Avatar asked Mar 16 '09 15:03

Sandman


2 Answers

It seems similar to binding a POJO to a property.

That means you should have one object with your two buttons implementing an IObservable, and then bind it to your property.
As you mention in the comments, you should extend AbstractObservable.

You have an example of such an extension here with this class Test about an observable list (not necessary what you need but it can give you ideas about the use of Observable when it comes to detect the change upon notification)

like image 127
VonC Avatar answered Oct 24 '22 01:10

VonC


I think I found the most appropiate implementation for this. You need to use the org.eclipse.core.databinding.observable.value.SelectObservableValue. here is the code

class YesNoModel{
   public static enum RESPONSE {YES,NO};
   private RESPONSE value;
   public RESPONSE getValue(){return value;}
   public void setValue(RESPONSE value){this.value = value;}
}
class YouGraphicalClass {
   YesNoModel yesNomodel  = new YesNoModel();
   void iniDataBinding(){
        IObservableValue yesBtnSelection  = SWTObservables.observeSelection(this.getYesButton());
        IObservableValue noBtnSelection  = SWTObservables.observeSelection(this.getNoButton());
        SelectObservableValue featureRepoPolicyObservable = new SelectObservableValue(YesNoModel.RESPONSE.class);
        featureRepoPolicyObservable.addOption(RESPONSE.YES, yesBtnSelection);
        featureRepoPolicyObservable.addOption(RESPONSE.NO, noBtnSelection);
        p_dbContext.bindValue(featureRepoPolicyObservable,
                PojoObservables.observeValue(yesNomodel, "value"));
    }

This works of course for al kind of enums or other kind of selectable values. You could of course use you String YES and NO, but I prefer enums

like image 29
SeB.Fr Avatar answered Oct 24 '22 01:10

SeB.Fr