Pull demo into jar
[nikiroo-utils.git] / src / jexer / TRadioGroup.java
... / ...
CommitLineData
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 */
31package jexer;
32
33import jexer.bits.CellAttributes;
34
35/**
36 * TRadioGroup is a collection of TRadioButtons with a box and label.
37 */
38public 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) {
68 assert (button.getSelected());
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
87 super(parent);
88
89 setX(x);
90 setY(y);
91 setHeight(2);
92 this.label = label;
93 setWidth(label.length() + 4);
94 }
95
96 /**
97 * Draw a radio button with label.
98 */
99 @Override
100 public void draw() {
101 CellAttributes radioGroupColor;
102
103 if (getAbsoluteActive()) {
104 radioGroupColor = getTheme().getColor("tradiogroup.active");
105 } else {
106 radioGroupColor = getTheme().getColor("tradiogroup.inactive");
107 }
108
109 getScreen().drawBox(0, 0, getWidth(), getHeight(),
110 radioGroupColor, radioGroupColor, 3, false);
111
112 getScreen().hLineXY(1, 0, label.length() + 2, ' ', radioGroupColor);
113 getScreen().putStrXY(2, 0, label, radioGroupColor);
114 }
115
116 /**
117 * Convenience function to add a radio button to this group.
118 *
119 * @param label label to display next to (right of) the radiobutton
120 * @return the new radio button
121 */
122 public TRadioButton addRadioButton(final String label) {
123 int buttonX = 1;
124 int buttonY = getChildren().size() + 1;
125 if (label.length() + 4 > getWidth()) {
126 setWidth(label.length() + 7);
127 }
128 setHeight(getChildren().size() + 3);
129 return new TRadioButton(this, buttonX, buttonY, label,
130 getChildren().size() + 1);
131 }
132
133}