2 * Jexer - Java Text User Interface
4 * License: LGPLv3 or later
6 * This module is licensed under the GNU Lesser General Public License
7 * Version 3. Please see the file "COPYING" in this directory for more
8 * information about the GNU Lesser General Public License Version 3.
10 * Copyright (C) 2015 Kevin Lamonte
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 3 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this program; if not, see
24 * http://www.gnu.org/licenses/, or write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
33 import jexer
.bits
.CellAttributes
;
36 * TRadioGroup is a collection of TRadioButtons with a box and label.
38 public final class TRadioGroup
extends TWidget
{
41 * Label for this radio button group.
46 * Only one of my children can be selected.
48 private TRadioButton selectedButton
= null;
51 * Get the radio button ID that was selected.
53 * @return ID of the selected button, or 0 if no button is selected
55 public int getSelected() {
56 if (selectedButton
== null) {
59 return selectedButton
.getId();
63 * Set the new selected radio button. Note package private access.
65 * @param button new button that became selected
67 void setSelected(final TRadioButton button
) {
68 assert (button
.isSelected());
69 if (selectedButton
!= null) {
70 selectedButton
.setSelected(false);
72 selectedButton
= button
;
78 * @param parent parent widget
79 * @param x column relative to parent
80 * @param y row relative to parent
81 * @param label label to display on the group box
83 public TRadioGroup(final TWidget parent
, final int x
, final int y
,
86 // Set parent and window
87 super(parent
, x
, y
, label
.length() + 4, 2);
93 * Draw a radio button with label.
97 CellAttributes radioGroupColor
;
99 if (isAbsoluteActive()) {
100 radioGroupColor
= getTheme().getColor("tradiogroup.active");
102 radioGroupColor
= getTheme().getColor("tradiogroup.inactive");
105 getScreen().drawBox(0, 0, getWidth(), getHeight(),
106 radioGroupColor
, radioGroupColor
, 3, false);
108 getScreen().hLineXY(1, 0, label
.length() + 2, ' ', radioGroupColor
);
109 getScreen().putStringXY(2, 0, label
, radioGroupColor
);
113 * Convenience function to add a radio button to this group.
115 * @param label label to display next to (right of) the radiobutton
116 * @return the new radio button
118 public TRadioButton
addRadioButton(final String label
) {
120 int buttonY
= getChildren().size() + 1;
121 if (label
.length() + 4 > getWidth()) {
122 setWidth(label
.length() + 7);
124 setHeight(getChildren().size() + 3);
125 return new TRadioButton(this, buttonX
, buttonY
, label
,
126 getChildren().size() + 1);