2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2017 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
31 import jexer
.bits
.CellAttributes
;
34 * TRadioGroup is a collection of TRadioButtons with a box and label.
36 public final class TRadioGroup
extends TWidget
{
39 * Label for this radio button group.
44 * Only one of my children can be selected.
46 private TRadioButton selectedButton
= null;
49 * Get the radio button ID that was selected.
51 * @return ID of the selected button, or 0 if no button is selected
53 public int getSelected() {
54 if (selectedButton
== null) {
57 return selectedButton
.getId();
61 * Set the new selected radio button. Note package private access.
63 * @param button new button that became selected
65 void setSelected(final TRadioButton button
) {
66 assert (button
.isSelected());
67 if (selectedButton
!= null) {
68 selectedButton
.setSelected(false);
70 selectedButton
= button
;
76 * @param parent parent widget
77 * @param x column relative to parent
78 * @param y row relative to parent
79 * @param label label to display on the group box
81 public TRadioGroup(final TWidget parent
, final int x
, final int y
,
84 // Set parent and window
85 super(parent
, x
, y
, label
.length() + 4, 2);
91 * Draw a radio button with label.
95 CellAttributes radioGroupColor
;
97 if (isAbsoluteActive()) {
98 radioGroupColor
= getTheme().getColor("tradiogroup.active");
100 radioGroupColor
= getTheme().getColor("tradiogroup.inactive");
103 getScreen().drawBox(0, 0, getWidth(), getHeight(),
104 radioGroupColor
, radioGroupColor
, 3, false);
106 getScreen().hLineXY(1, 0, label
.length() + 2, ' ', radioGroupColor
);
107 getScreen().putStringXY(2, 0, label
, radioGroupColor
);
111 * Convenience function to add a radio button to this group.
113 * @param label label to display next to (right of) the radiobutton
114 * @return the new radio button
116 public TRadioButton
addRadioButton(final String label
) {
118 int buttonY
= getChildren().size() + 1;
119 if (label
.length() + 4 > getWidth()) {
120 setWidth(label
.length() + 7);
122 setHeight(getChildren().size() + 3);
123 return new TRadioButton(this, buttonX
, buttonY
, label
,
124 getChildren().size() + 1);