Commit | Line | Data |
---|---|---|
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 | */ | |
31 | package jexer; | |
32 | ||
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.*; | |
38 | ||
39 | /** | |
40 | * TRadioButton implements a selectable radio button. | |
41 | */ | |
42 | public 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 | */ | |
7c870d89 | 55 | public boolean isSelected() { |
00d2622b KL |
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 | |
a83fea2b | 104 | super(parent, x, y, label.length() + 4, 1); |
00d2622b | 105 | |
00d2622b | 106 | this.label = label; |
00d2622b KL |
107 | this.id = id; |
108 | ||
7c870d89 | 109 | setCursorVisible(true); |
00d2622b KL |
110 | setCursorX(1); |
111 | } | |
112 | ||
113 | /** | |
114 | * Returns true if the mouse is currently on the radio button. | |
115 | * | |
116 | * @param mouse mouse event | |
117 | * @return if true the mouse is currently on the radio button | |
118 | */ | |
119 | private boolean mouseOnRadioButton(final TMouseEvent mouse) { | |
120 | if ((mouse.getY() == 0) | |
121 | && (mouse.getX() >= 0) | |
122 | && (mouse.getX() <= 2) | |
123 | ) { | |
124 | return true; | |
125 | } | |
126 | return false; | |
127 | } | |
128 | ||
129 | /** | |
130 | * Draw a radio button with label. | |
131 | */ | |
132 | @Override | |
133 | public void draw() { | |
134 | CellAttributes radioButtonColor; | |
135 | ||
7c870d89 | 136 | if (isAbsoluteActive()) { |
00d2622b KL |
137 | radioButtonColor = getTheme().getColor("tradiobutton.active"); |
138 | } else { | |
139 | radioButtonColor = getTheme().getColor("tradiobutton.inactive"); | |
140 | } | |
141 | ||
142 | getScreen().putCharXY(0, 0, '(', radioButtonColor); | |
143 | if (selected) { | |
144 | getScreen().putCharXY(1, 0, GraphicsChars.CP437[0x07], | |
145 | radioButtonColor); | |
146 | } else { | |
147 | getScreen().putCharXY(1, 0, ' ', radioButtonColor); | |
148 | } | |
149 | getScreen().putCharXY(2, 0, ')', radioButtonColor); | |
150 | getScreen().putStrXY(4, 0, label, radioButtonColor); | |
151 | } | |
152 | ||
153 | /** | |
154 | * Handle mouse button presses. | |
155 | * | |
156 | * @param mouse mouse button press event | |
157 | */ | |
158 | @Override | |
159 | public void onMouseDown(final TMouseEvent mouse) { | |
7c870d89 | 160 | if ((mouseOnRadioButton(mouse)) && (mouse.isMouse1())) { |
00d2622b KL |
161 | // Switch state |
162 | selected = !selected; | |
163 | if (selected) { | |
164 | ((TRadioGroup) getParent()).setSelected(this); | |
165 | } | |
166 | } | |
167 | } | |
168 | ||
169 | /** | |
170 | * Handle keystrokes. | |
171 | * | |
172 | * @param keypress keystroke event | |
173 | */ | |
174 | @Override | |
175 | public void onKeypress(final TKeypressEvent keypress) { | |
176 | ||
177 | if (keypress.equals(kbSpace)) { | |
178 | selected = !selected; | |
179 | if (selected) { | |
180 | ((TRadioGroup) getParent()).setSelected(this); | |
181 | } | |
182 | return; | |
183 | } | |
184 | ||
185 | // Pass to parent for the things we don't care about. | |
186 | super.onKeypress(keypress); | |
187 | } | |
188 | ||
189 | } |