JsonIO: progress can have a key 'meta'
[fanfix.git] / src / jexer / TRadioButton.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2019 Kevin Lamonte
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
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.
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.bits.MnemonicString;
34 import jexer.bits.StringUtils;
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 * If the user clicks or presses space on this button, it is selected.
43 *
44 * If the user presses escape on this button, it is unselected.
45 */
46 public class TRadioButton extends TWidget {
47
48 // ------------------------------------------------------------------------
49 // Variables --------------------------------------------------------------
50 // ------------------------------------------------------------------------
51
52 /**
53 * RadioButton state, true means selected. Note package private access.
54 */
55 boolean selected = false;
56
57 /**
58 * The shortcut and radio button label.
59 */
60 private MnemonicString mnemonic;
61
62 /**
63 * ID for this radio button. Buttons start counting at 1 in the
64 * RadioGroup. Note package private access.
65 */
66 int id;
67
68 // ------------------------------------------------------------------------
69 // Constructors -----------------------------------------------------------
70 // ------------------------------------------------------------------------
71
72 /**
73 * Package private constructor.
74 *
75 * @param parent parent widget
76 * @param x column relative to parent
77 * @param y row relative to parent
78 * @param label label to display next to (right of) the radiobutton
79 * @param id ID for this radio button
80 */
81 TRadioButton(final TRadioGroup parent, final int x, final int y,
82 final String label, final int id) {
83
84 // Set parent and window
85 super(parent, x, y, StringUtils.width(label) + 4, 1);
86
87 mnemonic = new MnemonicString(label);
88 this.id = id;
89
90 setCursorVisible(true);
91 setCursorX(1);
92
93 parent.addRadioButton(this);
94 }
95
96 // ------------------------------------------------------------------------
97 // Event handlers ---------------------------------------------------------
98 // ------------------------------------------------------------------------
99
100 /**
101 * Returns true if the mouse is currently on the radio button.
102 *
103 * @param mouse mouse event
104 * @return if true the mouse is currently on the radio button
105 */
106 private boolean mouseOnRadioButton(final TMouseEvent mouse) {
107 if ((mouse.getY() == 0)
108 && (mouse.getX() >= 0)
109 && (mouse.getX() <= 2)
110 ) {
111 return true;
112 }
113 return false;
114 }
115
116 /**
117 * Handle mouse button presses.
118 *
119 * @param mouse mouse button press event
120 */
121 @Override
122 public void onMouseDown(final TMouseEvent mouse) {
123 if ((mouseOnRadioButton(mouse)) && (mouse.isMouse1())) {
124 // Switch state
125 ((TRadioGroup) getParent()).setSelected(id);
126 }
127 }
128
129 /**
130 * Handle keystrokes.
131 *
132 * @param keypress keystroke event
133 */
134 @Override
135 public void onKeypress(final TKeypressEvent keypress) {
136
137 if (keypress.equals(kbSpace)) {
138 ((TRadioGroup) getParent()).setSelected(id);
139 return;
140 }
141
142 if (keypress.equals(kbEsc)) {
143 TRadioGroup parent = (TRadioGroup) getParent();
144 if (parent.requiresSelection == false) {
145 selected = false;
146 parent.setSelected(0);
147 }
148 return;
149 }
150
151 // Pass to parent for the things we don't care about.
152 super.onKeypress(keypress);
153 }
154
155 // ------------------------------------------------------------------------
156 // TWidget ----------------------------------------------------------------
157 // ------------------------------------------------------------------------
158
159 /**
160 * Override TWidget's width: we can only set width at construction time.
161 *
162 * @param width new widget width (ignored)
163 */
164 @Override
165 public void setWidth(final int width) {
166 // Do nothing
167 }
168
169 /**
170 * Override TWidget's height: we can only set height at construction
171 * time.
172 *
173 * @param height new widget height (ignored)
174 */
175 @Override
176 public void setHeight(final int height) {
177 // Do nothing
178 }
179
180 /**
181 * Draw a radio button with label.
182 */
183 @Override
184 public void draw() {
185 CellAttributes radioButtonColor;
186 CellAttributes mnemonicColor;
187
188 if (isAbsoluteActive()) {
189 radioButtonColor = getTheme().getColor("tradiobutton.active");
190 mnemonicColor = getTheme().getColor("tradiobutton.mnemonic.highlighted");
191 } else {
192 radioButtonColor = getTheme().getColor("tradiobutton.inactive");
193 mnemonicColor = getTheme().getColor("tradiobutton.mnemonic");
194 }
195
196 putCharXY(0, 0, '(', radioButtonColor);
197 if (selected) {
198 putCharXY(1, 0, GraphicsChars.CP437[0x07], radioButtonColor);
199 } else {
200 putCharXY(1, 0, ' ', radioButtonColor);
201 }
202 putCharXY(2, 0, ')', radioButtonColor);
203 putStringXY(4, 0, mnemonic.getRawLabel(), radioButtonColor);
204 if (mnemonic.getScreenShortcutIdx() >= 0) {
205 putCharXY(4 + mnemonic.getScreenShortcutIdx(), 0,
206 mnemonic.getShortcut(), mnemonicColor);
207 }
208 }
209
210 // ------------------------------------------------------------------------
211 // TRadioButton -----------------------------------------------------------
212 // ------------------------------------------------------------------------
213
214 /**
215 * Get RadioButton state, true means selected.
216 *
217 * @return if true then this is the one button in the group that is
218 * selected
219 */
220 public boolean isSelected() {
221 return selected;
222 }
223
224 /**
225 * Set RadioButton state, true means selected.
226 *
227 * @param selected if true then this is the one button in the group that
228 * is selected
229 */
230 public void setSelected(final boolean selected) {
231 if (selected == true) {
232 ((TRadioGroup) getParent()).setSelected(id);
233 } else {
234 ((TRadioGroup) getParent()).setSelected(0);
235 }
236 }
237
238 /**
239 * Get ID for this radio button. Buttons start counting at 1 in the
240 * RadioGroup.
241 *
242 * @return the ID
243 */
244 public int getId() {
245 return id;
246 }
247
248 /**
249 * Get the mnemonic string for this button.
250 *
251 * @return mnemonic string
252 */
253 public MnemonicString getMnemonic() {
254 return mnemonic;
255 }
256
257 }