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
;
34 import jexer
.bits
.GraphicsChars
;
35 import jexer
.event
.TKeypressEvent
;
36 import jexer
.event
.TMouseEvent
;
37 import static jexer
.TKeypress
.*;
40 * TRadioButton implements a selectable radio button.
42 public final class TRadioButton
extends TWidget
{
45 * RadioButton state, true means selected.
47 private boolean selected
= false;
50 * Get RadioButton state, true means selected.
52 * @return if true then this is the one button in the group that is
55 public boolean isSelected() {
60 * Set RadioButton state, true means selected. Note package private
63 * @param selected if true then this is the one button in the group that
66 void setSelected(final boolean selected
) {
67 this.selected
= selected
;
71 * Label for this radio button.
76 * ID for this radio button. Buttons start counting at 1 in the
82 * Get ID for this radio button. Buttons start counting at 1 in the
94 * @param parent parent widget
95 * @param x column relative to parent
96 * @param y row relative to parent
97 * @param label label to display next to (right of) the radiobutton
98 * @param id ID for this radio button
100 public TRadioButton(final TRadioGroup parent
, final int x
, final int y
,
101 final String label
, final int id
) {
103 // Set parent and window
104 super(parent
, x
, y
, label
.length() + 4, 1);
109 setCursorVisible(true);
114 * Returns true if the mouse is currently on the radio button.
116 * @param mouse mouse event
117 * @return if true the mouse is currently on the radio button
119 private boolean mouseOnRadioButton(final TMouseEvent mouse
) {
120 if ((mouse
.getY() == 0)
121 && (mouse
.getX() >= 0)
122 && (mouse
.getX() <= 2)
130 * Draw a radio button with label.
134 CellAttributes radioButtonColor
;
136 if (isAbsoluteActive()) {
137 radioButtonColor
= getTheme().getColor("tradiobutton.active");
139 radioButtonColor
= getTheme().getColor("tradiobutton.inactive");
142 getScreen().putCharXY(0, 0, '(', radioButtonColor
);
144 getScreen().putCharXY(1, 0, GraphicsChars
.CP437
[0x07],
147 getScreen().putCharXY(1, 0, ' ', radioButtonColor
);
149 getScreen().putCharXY(2, 0, ')', radioButtonColor
);
150 getScreen().putStringXY(4, 0, label
, radioButtonColor
);
154 * Handle mouse button presses.
156 * @param mouse mouse button press event
159 public void onMouseDown(final TMouseEvent mouse
) {
160 if ((mouseOnRadioButton(mouse
)) && (mouse
.isMouse1())) {
162 selected
= !selected
;
164 ((TRadioGroup
) getParent()).setSelected(this);
172 * @param keypress keystroke event
175 public void onKeypress(final TKeypressEvent keypress
) {
177 if (keypress
.equals(kbSpace
)) {
178 selected
= !selected
;
180 ((TRadioGroup
) getParent()).setSelected(this);
185 // Pass to parent for the things we don't care about.
186 super.onKeypress(keypress
);