There are 2 major stories how radio groups are used:
In both stories there are two possibilities to create the UI:
All scenarios have in common that radio buttons are created using RCPRadioButton and you wrap these buttons together with their associated model values into a RadioGroupManager?; this manager represents the value of the group.
This RadioGroupManager? can be bound to a model property using validationManager.bindValue(RadioGroupManager?,...). The binding bidirectionally synchronizes the model value to the RadioGroupManager? value.
There are some convenience methods provided which do some of these actions behind the scenes, but the technical implementation always is the same.
This represents 1b from the alternatives above. Make sure the enum has a range adapter registered in the ModelAdapter?; the default bean model adapter has one registered for standard java enums which is used here.
create a property in the data model of type e.g. GeschlechtCode?
public static final String P_Geschlecht = "geschlecht"; private GeschlechtCode m_Geschlecht; public GeschlechtCode getGeschlecht() { return m_Geschlecht; } public void setGeschlecht(GeschlechtCode value) { Object oldValue = m_Geschlecht; m_Geschlecht = value; propertyChangeSupport.firePropertyChange(P_Geschlecht, oldValue, value); }
in the createUI() method of the view model let the builder dynamically create the radios for all code values:
private RCPRadioGroup geschlechtGroup = new RCPRadioGroup("Geschlecht:"); /** each radio group needs a manager */ private RadioGroupManager geschlechtRadioGroupManager; public void createUI(FormToolkit toolkit, Composite parent) { ... // create a group for the radio buttons, if you dont like group borders, use a composite instead. // You can even use the section directly, anyhow the createEnumRadios group creates each radio // eating up one cell, so make sure to get your layout right. builder.addContainer(m_geschlechtGroup,1); // radio buttons are not created here yet }
In the bind method use bindValue to bind the created RadioGroupManager? to the model property:
public void bind(ValidationManager v, Object model) { ... // validation manager creates radio buttons dynamically in method handleRangeAdapter // and sets the value to the radio group manager v.bindValue(geschlechtRadioGroupManager, model, Sandbox2DataModel.P_Geschlecht); }
This represents 2a from the alternatives above.
create a boolean property in the model of type Boolean or boolean; recommended ist Boolean to be able to handle null values
public static final String P_OverdrawAccount = "overdrawAccount";
private Boolean m_OverdrawAccount;
public Boolean getOverdrawAccount()
{
return (Boolean) m_OverdrawAccount;
}
public void setOverdrawAccount(Boolean value)
{
Object oldValue = m_OverdrawAccount;
m_OverdrawAccount = value;
propertyChangeSupport.firePropertyChange(P_OverdrawAccount, oldValue, value);
}
in the createUI() method of the view model create the radios yourself using standard mechanism:
public void createUI(FormToolkit toolkit, Composite parent) { m_kontoueberzugYesRadio = new RCPSimpleButton("Konto überziehbar", SWT.RADIO); m_kontoueberzugNoRadio = new RCPSimpleButton("Konto nicht überziehbar", SWT.RADIO); ... widgetBuilder.fill(1).add(m_kontoueberzugYesRadio); widgetBuilder.fillLine(); widgetBuilder.addSkipLeftLine(m_kontoueberzugNoRadio, 1); }
In the bind method use bindRadioToBoolean to implicitly create a RadioGroupManager? and bind it to the model propertiy:
public void bind(ValidationManager context, Object dataModel) { ... // bind explicitly using a radio group manager context.bindRadioToBoolean(m_kontoueberzugYesRadio, m_kontoueberzugNoRadio, dataModel, TestModel.P_OverdrawAccount); }
This is the most flexible usage of radio group binding. Actually it follows the same pattern as binding to Boolean, but you create and bind your RadioGroupManager? yourself. In the example above you enhance the createUI() method to create a RadioGroupManager? explicitly which associates radio buttons with model values:
in the createUI() method of the view model create the radios yourself using standard DlamWidget? mechanism:
public void createUI(FormToolkit toolkit, Composite parent) { m_kontoueberzugYesRadio = new RCPSimpleButton("Konto überziehbar", SWT.RADIO); m_kontoueberzugNoRadio = new RCPSimpleButton("Konto nicht überziehbar", SWT.RADIO); ... widgetBuilder.addLine(m_kontoueberzugYesRadio); widgetBuilder.addLine(m_kontoueberzugNoRadio); }
In the bind method explicitly create RadioGroupManager? and bind it:
public void bind(ValidationManager context, Object dataModel) { ... // bind explicitly using a radio group manager RadioGroupManager rgm = new RadioGroupManager(new RCPSimpleButton[]{ m_kontoueberzugYesRadio, m_kontoueberzugNoRadio}, new Object[]{Boolean.TRUE, Boolean.FALSE}); context.bindValue(rgm, dataModel, TestModel.P_OverdrawAccount); }
The state binding for radio buttons works not exactly like the state binding for other widgets, but similar. You have to use a Radio Group and bind to the radio group widget, (see [HowtoStateBinding?]) not to the radio buttons themselves since state is maintained for the group.
The RadioGroupManager?.getSelection() method returns the model object associated with the currently selected button, if you set a model object via setSelection() the associated button will be selected. A null value will be associated with no selected button.
If the group is required, a radio button must be selected.