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 | ||
35 | /** | |
36 | * TRadioGroup is a collection of TRadioButtons with a box and label. | |
37 | */ | |
38 | public final class TRadioGroup extends TWidget { | |
39 | ||
40 | /** | |
41 | * Label for this radio button group. | |
42 | */ | |
43 | private String label; | |
44 | ||
45 | /** | |
46 | * Only one of my children can be selected. | |
47 | */ | |
48 | private TRadioButton selectedButton = null; | |
49 | ||
50 | /** | |
51 | * Get the radio button ID that was selected. | |
52 | * | |
53 | * @return ID of the selected button, or 0 if no button is selected | |
54 | */ | |
55 | public int getSelected() { | |
56 | if (selectedButton == null) { | |
57 | return 0; | |
58 | } | |
59 | return selectedButton.getId(); | |
60 | } | |
61 | ||
62 | /** | |
63 | * Set the new selected radio button. Note package private access. | |
64 | * | |
65 | * @param button new button that became selected | |
66 | */ | |
67 | void setSelected(final TRadioButton button) { | |
7c870d89 | 68 | assert (button.isSelected()); |
00d2622b KL |
69 | if (selectedButton != null) { |
70 | selectedButton.setSelected(false); | |
71 | } | |
72 | selectedButton = button; | |
73 | } | |
74 | ||
75 | /** | |
76 | * Public constructor. | |
77 | * | |
78 | * @param parent parent widget | |
79 | * @param x column relative to parent | |
80 | * @param y row relative to parent | |
81 | * @param label label to display on the group box | |
82 | */ | |
83 | public TRadioGroup(final TWidget parent, final int x, final int y, | |
84 | final String label) { | |
85 | ||
86 | // Set parent and window | |
a83fea2b | 87 | super(parent, x, y, label.length() + 4, 2); |
00d2622b | 88 | |
00d2622b | 89 | this.label = label; |
00d2622b KL |
90 | } |
91 | ||
92 | /** | |
93 | * Draw a radio button with label. | |
94 | */ | |
95 | @Override | |
96 | public void draw() { | |
97 | CellAttributes radioGroupColor; | |
98 | ||
7c870d89 | 99 | if (isAbsoluteActive()) { |
00d2622b KL |
100 | radioGroupColor = getTheme().getColor("tradiogroup.active"); |
101 | } else { | |
102 | radioGroupColor = getTheme().getColor("tradiogroup.inactive"); | |
103 | } | |
104 | ||
105 | getScreen().drawBox(0, 0, getWidth(), getHeight(), | |
106 | radioGroupColor, radioGroupColor, 3, false); | |
107 | ||
108 | getScreen().hLineXY(1, 0, label.length() + 2, ' ', radioGroupColor); | |
109 | getScreen().putStrXY(2, 0, label, radioGroupColor); | |
110 | } | |
111 | ||
112 | /** | |
113 | * Convenience function to add a radio button to this group. | |
114 | * | |
115 | * @param label label to display next to (right of) the radiobutton | |
116 | * @return the new radio button | |
117 | */ | |
118 | public TRadioButton addRadioButton(final String label) { | |
119 | int buttonX = 1; | |
120 | int buttonY = getChildren().size() + 1; | |
121 | if (label.length() + 4 > getWidth()) { | |
122 | setWidth(label.length() + 7); | |
123 | } | |
124 | setHeight(getChildren().size() + 3); | |
125 | return new TRadioButton(this, buttonX, buttonY, label, | |
126 | getChildren().size() + 1); | |
127 | } | |
128 | ||
129 | } |