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