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