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