* If true, one of the children MUST be selected. Note package private
* access.
*/
- boolean requiresSelection = true;
+ boolean requiresSelection = false;
// ------------------------------------------------------------------------
// Constructors -----------------------------------------------------------
// TWidget ----------------------------------------------------------------
// ------------------------------------------------------------------------
+ /**
+ * Override TWidget's width: we can only set width at construction time.
+ *
+ * @param width new widget width (ignored)
+ */
+ @Override
+ public void setWidth(final int width) {
+ // Do nothing
+ }
+
+ /**
+ * Override TWidget's height: we can only set height at construction
+ * time.
+ *
+ * @param height new widget height (ignored)
+ */
+ @Override
+ public void setHeight(final int height) {
+ // Do nothing
+ }
+
/**
* Draw a radio button with label.
*/
return selectedButton.getId();
}
- /**
- * Set the new selected radio button. Note package private access.
- *
- * @param button new button that became selected
- */
- void setSelected(final TRadioButton button) {
- assert (button.isSelected());
- if ((selectedButton != null) && (selectedButton != button)) {
- selectedButton.setSelected(false);
- }
- selectedButton = button;
- }
-
/**
* Set the new selected radio button. 1-based.
*
return;
}
+ for (TWidget widget: getChildren()) {
+ ((TRadioButton) widget).selected = false;
+ }
if (id == 0) {
- for (TWidget widget: getChildren()) {
- ((TRadioButton) widget).setSelected(false);
- }
selectedButton = null;
return;
}
assert ((id > 0) && (id <= getChildren().size()));
TRadioButton button = (TRadioButton) (getChildren().get(id - 1));
- button.setSelected(true);
+ button.selected = true;
selectedButton = button;
}
+ /**
+ * Get the radio button that was selected.
+ *
+ * @return the selected button, or null if no button is selected
+ */
+ public TRadioButton getSelectedButton() {
+ return selectedButton;
+ }
+
+ /**
+ * Convenience function to add a radio button to this group.
+ *
+ * @param label label to display next to (right of) the radiobutton
+ * @param selected if true, this will be the selected radiobutton
+ * @return the new radio button
+ */
+ public TRadioButton addRadioButton(final String label,
+ final boolean selected) {
+
+ TRadioButton button = addRadioButton(label);
+ setSelected(button.id);
+ return button;
+ }
+
/**
* Convenience function to add a radio button to this group.
*
* @return the new radio button
*/
public TRadioButton addRadioButton(final String label) {
- int buttonX = 1;
- int buttonY = getChildren().size() + 1;
+ return new TRadioButton(this, 0, 0, label, 0);
+ }
+
+ /**
+ * Package private method for RadioButton to add itself to a RadioGroup
+ * container.
+ *
+ * @param button the button to add
+ */
+ void addRadioButton(final TRadioButton button) {
+ super.setHeight(getChildren().size() + 2);
+ button.setX(1);
+ button.setY(getChildren().size());
+ button.id = getChildren().size();
+ String label = button.getMnemonic().getRawLabel();
+
if (StringUtils.width(label) + 4 > getWidth()) {
- setWidth(StringUtils.width(label) + 7);
+ super.setWidth(StringUtils.width(label) + 7);
+ }
+
+ if (getParent().getLayoutManager() != null) {
+ getParent().getLayoutManager().resetSize(this);
}
- setHeight(getChildren().size() + 3);
- TRadioButton button = new TRadioButton(this, buttonX, buttonY, label,
- getChildren().size() + 1);
// Default to the first item on the list.
activate(getChildren().get(0));
+ }
- return button;
+ /**
+ * Get the requires selection flag.
+ *
+ * @return true if this radiogroup requires that one of the buttons be
+ * selected
+ */
+ public boolean getRequiresSelection() {
+ return requiresSelection;
+ }
+
+ /**
+ * Set the requires selection flag.
+ *
+ * @param requiresSelection if true, then this radiogroup requires that
+ * one of the buttons be selected
+ */
+ public void setRequiresSelection(final boolean requiresSelection) {
+ this.requiresSelection = requiresSelection;
+ if (requiresSelection) {
+ if ((getChildren().size() > 0) && (selectedButton == null)) {
+ setSelected(1);
+ }
+ }
}
}