radio buttons
[fanfix.git] / src / jexer / TRadioButton.java
CommitLineData
00d2622b
KL
1/**
2 * Jexer - Java Text User Interface
3 *
4 * License: LGPLv3 or later
5 *
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.
9 *
10 * Copyright (C) 2015 Kevin Lamonte
11 *
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.
16 *
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.
21 *
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
26 * 02110-1301 USA
27 *
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 * @version 1
30 */
31package jexer;
32
33import jexer.bits.CellAttributes;
34import jexer.bits.GraphicsChars;
35import jexer.event.TKeypressEvent;
36import jexer.event.TMouseEvent;
37import static jexer.TKeypress.*;
38
39/**
40 * TRadioButton implements a selectable radio button.
41 */
42public final class TRadioButton extends TWidget {
43
44 /**
45 * RadioButton state, true means selected.
46 */
47 private boolean selected = false;
48
49 /**
50 * Get RadioButton state, true means selected.
51 *
52 * @return if true then this is the one button in the group that is
53 * selected
54 */
55 public boolean getSelected() {
56 return selected;
57 }
58
59 /**
60 * Set RadioButton state, true means selected. Note package private
61 * access.
62 *
63 * @param selected if true then this is the one button in the group that
64 * is selected
65 */
66 void setSelected(final boolean selected) {
67 this.selected = selected;
68 }
69
70 /**
71 * Label for this radio button.
72 */
73 private String label;
74
75 /**
76 * ID for this radio button. Buttons start counting at 1 in the
77 * RadioGroup.
78 */
79 private int id;
80
81 /**
82 * Get ID for this radio button. Buttons start counting at 1 in the
83 * RadioGroup.
84 *
85 * @return the ID
86 */
87 public int getId() {
88 return id;
89 }
90
91 /**
92 * Public constructor.
93 *
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
99 */
100 public TRadioButton(final TRadioGroup parent, final int x, final int y,
101 final String label, final int id) {
102
103 // Set parent and window
104 super(parent);
105
106 setX(x);
107 setY(y);
108 setHeight(1);
109 this.label = label;
110 setWidth(label.length() + 4);
111 this.id = id;
112
113 setHasCursor(true);
114 setCursorX(1);
115 }
116
117 /**
118 * Returns true if the mouse is currently on the radio button.
119 *
120 * @param mouse mouse event
121 * @return if true the mouse is currently on the radio button
122 */
123 private boolean mouseOnRadioButton(final TMouseEvent mouse) {
124 if ((mouse.getY() == 0)
125 && (mouse.getX() >= 0)
126 && (mouse.getX() <= 2)
127 ) {
128 return true;
129 }
130 return false;
131 }
132
133 /**
134 * Draw a radio button with label.
135 */
136 @Override
137 public void draw() {
138 CellAttributes radioButtonColor;
139
140 if (getAbsoluteActive()) {
141 radioButtonColor = getTheme().getColor("tradiobutton.active");
142 } else {
143 radioButtonColor = getTheme().getColor("tradiobutton.inactive");
144 }
145
146 getScreen().putCharXY(0, 0, '(', radioButtonColor);
147 if (selected) {
148 getScreen().putCharXY(1, 0, GraphicsChars.CP437[0x07],
149 radioButtonColor);
150 } else {
151 getScreen().putCharXY(1, 0, ' ', radioButtonColor);
152 }
153 getScreen().putCharXY(2, 0, ')', radioButtonColor);
154 getScreen().putStrXY(4, 0, label, radioButtonColor);
155 }
156
157 /**
158 * Handle mouse button presses.
159 *
160 * @param mouse mouse button press event
161 */
162 @Override
163 public void onMouseDown(final TMouseEvent mouse) {
164 if ((mouseOnRadioButton(mouse)) && (mouse.getMouse1())) {
165 // Switch state
166 selected = !selected;
167 if (selected) {
168 ((TRadioGroup) getParent()).setSelected(this);
169 }
170 }
171 }
172
173 /**
174 * Handle keystrokes.
175 *
176 * @param keypress keystroke event
177 */
178 @Override
179 public void onKeypress(final TKeypressEvent keypress) {
180
181 if (keypress.equals(kbSpace)) {
182 selected = !selected;
183 if (selected) {
184 ((TRadioGroup) getParent()).setSelected(this);
185 }
186 return;
187 }
188
189 // Pass to parent for the things we don't care about.
190 super.onKeypress(keypress);
191 }
192
193}