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; | |
9f613a0c | 32 | import jexer.bits.StringUtils; |
00d2622b KL |
33 | |
34 | /** | |
35 | * TRadioGroup is a collection of TRadioButtons with a box and label. | |
36 | */ | |
051e2913 | 37 | public class TRadioGroup extends TWidget { |
00d2622b | 38 | |
615a0d99 KL |
39 | // ------------------------------------------------------------------------ |
40 | // Variables -------------------------------------------------------------- | |
41 | // ------------------------------------------------------------------------ | |
42 | ||
00d2622b KL |
43 | /** |
44 | * Label for this radio button group. | |
45 | */ | |
46 | private String label; | |
47 | ||
48 | /** | |
49 | * Only one of my children can be selected. | |
50 | */ | |
51 | private TRadioButton selectedButton = null; | |
52 | ||
00691e80 KL |
53 | /** |
54 | * If true, one of the children MUST be selected. Note package private | |
55 | * access. | |
56 | */ | |
57 | boolean requiresSelection = true; | |
58 | ||
615a0d99 KL |
59 | // ------------------------------------------------------------------------ |
60 | // Constructors ----------------------------------------------------------- | |
61 | // ------------------------------------------------------------------------ | |
00d2622b KL |
62 | |
63 | /** | |
64 | * Public constructor. | |
65 | * | |
66 | * @param parent parent widget | |
67 | * @param x column relative to parent | |
68 | * @param y row relative to parent | |
69 | * @param label label to display on the group box | |
70 | */ | |
71 | public TRadioGroup(final TWidget parent, final int x, final int y, | |
72 | final String label) { | |
73 | ||
74 | // Set parent and window | |
9f613a0c | 75 | super(parent, x, y, StringUtils.width(label) + 4, 2); |
00d2622b | 76 | |
00d2622b | 77 | this.label = label; |
00d2622b KL |
78 | } |
79 | ||
615a0d99 KL |
80 | // ------------------------------------------------------------------------ |
81 | // TWidget ---------------------------------------------------------------- | |
82 | // ------------------------------------------------------------------------ | |
83 | ||
00d2622b KL |
84 | /** |
85 | * Draw a radio button with label. | |
86 | */ | |
87 | @Override | |
88 | public void draw() { | |
89 | CellAttributes radioGroupColor; | |
90 | ||
7c870d89 | 91 | if (isAbsoluteActive()) { |
00d2622b KL |
92 | radioGroupColor = getTheme().getColor("tradiogroup.active"); |
93 | } else { | |
94 | radioGroupColor = getTheme().getColor("tradiogroup.inactive"); | |
95 | } | |
96 | ||
a69ed767 KL |
97 | drawBox(0, 0, getWidth(), getHeight(), radioGroupColor, radioGroupColor, |
98 | 3, false); | |
00d2622b | 99 | |
9f613a0c | 100 | hLineXY(1, 0, StringUtils.width(label) + 2, ' ', radioGroupColor); |
a69ed767 | 101 | putStringXY(2, 0, label, radioGroupColor); |
00d2622b KL |
102 | } |
103 | ||
615a0d99 KL |
104 | // ------------------------------------------------------------------------ |
105 | // TRadioGroup ------------------------------------------------------------ | |
106 | // ------------------------------------------------------------------------ | |
107 | ||
108 | /** | |
109 | * Get the radio button ID that was selected. | |
110 | * | |
111 | * @return ID of the selected button, or 0 if no button is selected | |
112 | */ | |
113 | public int getSelected() { | |
114 | if (selectedButton == null) { | |
115 | return 0; | |
116 | } | |
117 | return selectedButton.getId(); | |
118 | } | |
119 | ||
120 | /** | |
121 | * Set the new selected radio button. Note package private access. | |
122 | * | |
123 | * @param button new button that became selected | |
124 | */ | |
125 | void setSelected(final TRadioButton button) { | |
126 | assert (button.isSelected()); | |
00691e80 | 127 | if ((selectedButton != null) && (selectedButton != button)) { |
615a0d99 KL |
128 | selectedButton.setSelected(false); |
129 | } | |
130 | selectedButton = button; | |
131 | } | |
132 | ||
00691e80 KL |
133 | /** |
134 | * Set the new selected radio button. 1-based. | |
135 | * | |
136 | * @param id ID of the selected button, or 0 to unselect | |
137 | */ | |
138 | public void setSelected(final int id) { | |
139 | if ((id < 0) || (id > getChildren().size())) { | |
140 | return; | |
141 | } | |
142 | ||
143 | if (id == 0) { | |
144 | for (TWidget widget: getChildren()) { | |
145 | ((TRadioButton) widget).setSelected(false); | |
146 | } | |
147 | selectedButton = null; | |
148 | return; | |
149 | } | |
150 | assert ((id > 0) && (id <= getChildren().size())); | |
151 | TRadioButton button = (TRadioButton) (getChildren().get(id - 1)); | |
152 | button.setSelected(true); | |
153 | selectedButton = button; | |
154 | } | |
155 | ||
00d2622b KL |
156 | /** |
157 | * Convenience function to add a radio button to this group. | |
158 | * | |
159 | * @param label label to display next to (right of) the radiobutton | |
160 | * @return the new radio button | |
161 | */ | |
162 | public TRadioButton addRadioButton(final String label) { | |
163 | int buttonX = 1; | |
164 | int buttonY = getChildren().size() + 1; | |
9f613a0c KL |
165 | if (StringUtils.width(label) + 4 > getWidth()) { |
166 | setWidth(StringUtils.width(label) + 7); | |
00d2622b KL |
167 | } |
168 | setHeight(getChildren().size() + 3); | |
00691e80 | 169 | TRadioButton button = new TRadioButton(this, buttonX, buttonY, label, |
00d2622b | 170 | getChildren().size() + 1); |
00691e80 KL |
171 | |
172 | // Default to the first item on the list. | |
173 | activate(getChildren().get(0)); | |
174 | ||
175 | return button; | |
00d2622b KL |
176 | } |
177 | ||
178 | } |