Commit | Line | Data |
---|---|---|
daa4106c | 1 | /* |
00d2622b KL |
2 | * Jexer - Java Text User Interface |
3 | * | |
e16dda65 | 4 | * The MIT License (MIT) |
00d2622b | 5 | * |
a69ed767 | 6 | * Copyright (C) 2019 Kevin Lamonte |
00d2622b | 7 | * |
e16dda65 KL |
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: | |
00d2622b | 14 | * |
e16dda65 KL |
15 | * The above copyright notice and this permission notice shall be included in |
16 | * all copies or substantial portions of the Software. | |
00d2622b | 17 | * |
e16dda65 KL |
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. | |
00d2622b KL |
25 | * |
26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
27 | * @version 1 | |
28 | */ | |
29 | package jexer; | |
30 | ||
31 | import jexer.bits.CellAttributes; | |
32 | import jexer.bits.GraphicsChars; | |
33 | import jexer.event.TKeypressEvent; | |
34 | import jexer.event.TMouseEvent; | |
35 | import static jexer.TKeypress.*; | |
36 | ||
37 | /** | |
38 | * TRadioButton implements a selectable radio button. | |
39 | */ | |
051e2913 | 40 | public class TRadioButton extends TWidget { |
00d2622b | 41 | |
d36057df KL |
42 | // ------------------------------------------------------------------------ |
43 | // Variables -------------------------------------------------------------- | |
44 | // ------------------------------------------------------------------------ | |
45 | ||
00d2622b KL |
46 | /** |
47 | * RadioButton state, true means selected. | |
48 | */ | |
49 | private boolean selected = false; | |
50 | ||
00d2622b KL |
51 | /** |
52 | * Label for this radio button. | |
53 | */ | |
54 | private String label; | |
55 | ||
56 | /** | |
57 | * ID for this radio button. Buttons start counting at 1 in the | |
58 | * RadioGroup. | |
59 | */ | |
60 | private int id; | |
61 | ||
d36057df KL |
62 | // ------------------------------------------------------------------------ |
63 | // Constructors ----------------------------------------------------------- | |
64 | // ------------------------------------------------------------------------ | |
00d2622b KL |
65 | |
66 | /** | |
67 | * Public constructor. | |
68 | * | |
69 | * @param parent parent widget | |
70 | * @param x column relative to parent | |
71 | * @param y row relative to parent | |
72 | * @param label label to display next to (right of) the radiobutton | |
73 | * @param id ID for this radio button | |
74 | */ | |
75 | public TRadioButton(final TRadioGroup parent, final int x, final int y, | |
76 | final String label, final int id) { | |
77 | ||
78 | // Set parent and window | |
a83fea2b | 79 | super(parent, x, y, label.length() + 4, 1); |
00d2622b | 80 | |
00d2622b | 81 | this.label = label; |
00d2622b KL |
82 | this.id = id; |
83 | ||
7c870d89 | 84 | setCursorVisible(true); |
00d2622b KL |
85 | setCursorX(1); |
86 | } | |
87 | ||
d36057df KL |
88 | // ------------------------------------------------------------------------ |
89 | // Event handlers --------------------------------------------------------- | |
90 | // ------------------------------------------------------------------------ | |
91 | ||
00d2622b KL |
92 | /** |
93 | * Returns true if the mouse is currently on the radio button. | |
94 | * | |
95 | * @param mouse mouse event | |
96 | * @return if true the mouse is currently on the radio button | |
97 | */ | |
98 | private boolean mouseOnRadioButton(final TMouseEvent mouse) { | |
99 | if ((mouse.getY() == 0) | |
100 | && (mouse.getX() >= 0) | |
101 | && (mouse.getX() <= 2) | |
102 | ) { | |
103 | return true; | |
104 | } | |
105 | return false; | |
106 | } | |
107 | ||
00d2622b KL |
108 | /** |
109 | * Handle mouse button presses. | |
110 | * | |
111 | * @param mouse mouse button press event | |
112 | */ | |
113 | @Override | |
114 | public void onMouseDown(final TMouseEvent mouse) { | |
7c870d89 | 115 | if ((mouseOnRadioButton(mouse)) && (mouse.isMouse1())) { |
00d2622b KL |
116 | // Switch state |
117 | selected = !selected; | |
118 | if (selected) { | |
119 | ((TRadioGroup) getParent()).setSelected(this); | |
120 | } | |
121 | } | |
122 | } | |
123 | ||
124 | /** | |
125 | * Handle keystrokes. | |
126 | * | |
127 | * @param keypress keystroke event | |
128 | */ | |
129 | @Override | |
130 | public void onKeypress(final TKeypressEvent keypress) { | |
131 | ||
132 | if (keypress.equals(kbSpace)) { | |
133 | selected = !selected; | |
134 | if (selected) { | |
135 | ((TRadioGroup) getParent()).setSelected(this); | |
136 | } | |
137 | return; | |
138 | } | |
139 | ||
140 | // Pass to parent for the things we don't care about. | |
141 | super.onKeypress(keypress); | |
142 | } | |
143 | ||
d36057df KL |
144 | // ------------------------------------------------------------------------ |
145 | // TWidget ---------------------------------------------------------------- | |
146 | // ------------------------------------------------------------------------ | |
147 | ||
148 | /** | |
149 | * Draw a radio button with label. | |
150 | */ | |
151 | @Override | |
152 | public void draw() { | |
153 | CellAttributes radioButtonColor; | |
154 | ||
155 | if (isAbsoluteActive()) { | |
156 | radioButtonColor = getTheme().getColor("tradiobutton.active"); | |
157 | } else { | |
158 | radioButtonColor = getTheme().getColor("tradiobutton.inactive"); | |
159 | } | |
160 | ||
a69ed767 | 161 | putCharXY(0, 0, '(', radioButtonColor); |
d36057df | 162 | if (selected) { |
a69ed767 | 163 | putCharXY(1, 0, GraphicsChars.CP437[0x07], radioButtonColor); |
d36057df | 164 | } else { |
a69ed767 | 165 | putCharXY(1, 0, ' ', radioButtonColor); |
d36057df | 166 | } |
a69ed767 KL |
167 | putCharXY(2, 0, ')', radioButtonColor); |
168 | putStringXY(4, 0, label, radioButtonColor); | |
d36057df KL |
169 | } |
170 | ||
171 | // ------------------------------------------------------------------------ | |
172 | // TRadioButton ----------------------------------------------------------- | |
173 | // ------------------------------------------------------------------------ | |
174 | ||
175 | /** | |
176 | * Get RadioButton state, true means selected. | |
177 | * | |
178 | * @return if true then this is the one button in the group that is | |
179 | * selected | |
180 | */ | |
181 | public boolean isSelected() { | |
182 | return selected; | |
183 | } | |
184 | ||
185 | /** | |
186 | * Set RadioButton state, true means selected. Note package private | |
187 | * access. | |
188 | * | |
189 | * @param selected if true then this is the one button in the group that | |
190 | * is selected | |
191 | */ | |
192 | void setSelected(final boolean selected) { | |
193 | this.selected = selected; | |
194 | } | |
195 | ||
196 | /** | |
197 | * Get ID for this radio button. Buttons start counting at 1 in the | |
198 | * RadioGroup. | |
199 | * | |
200 | * @return the ID | |
201 | */ | |
202 | public int getId() { | |
203 | return id; | |
204 | } | |
205 | ||
00d2622b | 206 | } |