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