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
.getSelected());
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
93 setWidth(label
.length() + 4);
97 * Draw a radio button with label.
101 CellAttributes radioGroupColor
;
103 if (getAbsoluteActive()) {
104 radioGroupColor
= getTheme().getColor("tradiogroup.active");
106 radioGroupColor
= getTheme().getColor("tradiogroup.inactive");
109 getScreen().drawBox(0, 0, getWidth(), getHeight(),
110 radioGroupColor
, radioGroupColor
, 3, false);
112 getScreen().hLineXY(1, 0, label
.length() + 2, ' ', radioGroupColor
);
113 getScreen().putStrXY(2, 0, label
, radioGroupColor
);
117 * Convenience function to add a radio button to this group.
119 * @param label label to display next to (right of) the radiobutton
120 * @return the new radio button
122 public TRadioButton
addRadioButton(final String label
) {
124 int buttonY
= getChildren().size() + 1;
125 if (label
.length() + 4 > getWidth()) {
126 setWidth(label
.length() + 7);
128 setHeight(getChildren().size() + 3);
129 return new TRadioButton(this, buttonX
, buttonY
, label
,
130 getChildren().size() + 1);