| 1 | /* |
| 2 | * This file is part of lanterna (http://code.google.com/p/lanterna/). |
| 3 | * |
| 4 | * lanterna is free software: you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Lesser General Public License as published by |
| 6 | * the Free Software Foundation, either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public License |
| 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | * Copyright (C) 2010-2015 Martin |
| 18 | */ |
| 19 | package com.googlecode.lanterna.gui2; |
| 20 | |
| 21 | import com.googlecode.lanterna.TerminalSize; |
| 22 | import com.googlecode.lanterna.input.KeyStroke; |
| 23 | import com.googlecode.lanterna.input.KeyType; |
| 24 | |
| 25 | import java.util.List; |
| 26 | import java.util.concurrent.CopyOnWriteArrayList; |
| 27 | |
| 28 | /** |
| 29 | * The list box will display a number of items, of which one and only one can be marked as selected. |
| 30 | * The user can select an item in the list box by pressing the return key or space bar key. If you |
| 31 | * select one item when another item is already selected, the previously selected item will be |
| 32 | * deselected and the highlighted item will be the selected one instead. |
| 33 | * @author Martin |
| 34 | */ |
| 35 | public class RadioBoxList<V> extends AbstractListBox<V, RadioBoxList<V>> { |
| 36 | /** |
| 37 | * Listener interface that can be attached to the {@code RadioBoxList} in order to be notified on user actions |
| 38 | */ |
| 39 | public interface Listener { |
| 40 | /** |
| 41 | * Called by the {@code RadioBoxList} when the user changes which item is selected |
| 42 | * @param selectedIndex Index of the newly selected item, or -1 if the selection has been cleared (can only be |
| 43 | * done programmatically) |
| 44 | * @param previousSelection The index of the previously selected item which is now no longer selected, or -1 if |
| 45 | * nothing was previously selected |
| 46 | */ |
| 47 | void onSelectionChanged(int selectedIndex, int previousSelection); |
| 48 | } |
| 49 | |
| 50 | private final List<Listener> listeners; |
| 51 | private int checkedIndex; |
| 52 | |
| 53 | /** |
| 54 | * Creates a new RadioCheckBoxList with no items. The size of the {@code RadioBoxList} will be as big as is required |
| 55 | * to display all items. |
| 56 | */ |
| 57 | public RadioBoxList() { |
| 58 | this(null); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Creates a new RadioCheckBoxList with a specified size. If the items in the {@code RadioBoxList} cannot fit in the |
| 63 | * size specified, scrollbars will be used |
| 64 | * @param preferredSize Size of the {@code RadioBoxList} or {@code null} to have it try to be as big as necessary to |
| 65 | * be able to draw all items |
| 66 | */ |
| 67 | public RadioBoxList(TerminalSize preferredSize) { |
| 68 | super(preferredSize); |
| 69 | this.listeners = new CopyOnWriteArrayList<Listener>(); |
| 70 | this.checkedIndex = -1; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | protected ListItemRenderer<V,RadioBoxList<V>> createDefaultListItemRenderer() { |
| 75 | return new RadioBoxListItemRenderer<V>(); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public synchronized Result handleKeyStroke(KeyStroke keyStroke) { |
| 80 | if(keyStroke.getKeyType() == KeyType.Enter || |
| 81 | (keyStroke.getKeyType() == KeyType.Character && keyStroke.getCharacter() == ' ')) { |
| 82 | checkedIndex = getSelectedIndex(); |
| 83 | invalidate(); |
| 84 | return Result.HANDLED; |
| 85 | } |
| 86 | return super.handleKeyStroke(keyStroke); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public synchronized RadioBoxList<V> clearItems() { |
| 91 | setCheckedIndex(-1); |
| 92 | return super.clearItems(); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * This method will see if an object is the currently selected item in this RadioCheckBoxList |
| 97 | * @param object Object to test if it's the selected one |
| 98 | * @return {@code true} if the supplied object is what's currently selected in the list box, |
| 99 | * {@code false} otherwise. Returns null if the supplied object is not an item in the list box. |
| 100 | */ |
| 101 | public synchronized Boolean isChecked(V object) { |
| 102 | if(object == null) |
| 103 | return null; |
| 104 | |
| 105 | if(indexOf(object) == -1) |
| 106 | return null; |
| 107 | |
| 108 | return checkedIndex == indexOf(object); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * This method will see if an item, addressed by index, is the currently selected item in this |
| 113 | * RadioCheckBoxList |
| 114 | * @param index Index of the item to check if it's currently selected |
| 115 | * @return {@code true} if the currently selected object is at the supplied index, |
| 116 | * {@code false} otherwise. Returns false if the index is out of range. |
| 117 | */ |
| 118 | @SuppressWarnings("SimplifiableIfStatement") |
| 119 | public synchronized boolean isChecked(int index) { |
| 120 | if(index < 0 || index >= getItemCount()) { |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | return checkedIndex == index; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Sets the currently checked item by the value itself. If null, the selection is cleared. When changing selection, |
| 129 | * any previously selected item is deselected. |
| 130 | * @param item Item to be checked |
| 131 | */ |
| 132 | public synchronized void setCheckedItem(V item) { |
| 133 | if(item == null) { |
| 134 | setCheckedIndex(-1); |
| 135 | } |
| 136 | else { |
| 137 | setCheckedItemIndex(indexOf(item)); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Sets the currently selected item by index. If the index is out of range, it does nothing. |
| 143 | * @param index Index of the item to be selected |
| 144 | */ |
| 145 | public synchronized void setCheckedItemIndex(int index) { |
| 146 | if(index < -1 || index >= getItemCount()) |
| 147 | return; |
| 148 | |
| 149 | setCheckedIndex(index); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * @return The index of the item which is currently selected, or -1 if there is no selection |
| 154 | */ |
| 155 | public int getCheckedItemIndex() { |
| 156 | return checkedIndex; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @return The object currently selected, or null if there is no selection |
| 161 | */ |
| 162 | public synchronized V getCheckedItem() { |
| 163 | if(checkedIndex == -1 || checkedIndex >= getItemCount()) |
| 164 | return null; |
| 165 | |
| 166 | return getItemAt(checkedIndex); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Un-checks the currently checked item (if any) and leaves the radio check box in a state where no item is checked. |
| 171 | */ |
| 172 | public synchronized void clearSelection() { |
| 173 | setCheckedIndex(-1); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Adds a new listener to the {@code RadioBoxList} that will be called on certain user actions |
| 178 | * @param listener Listener to attach to this {@code RadioBoxList} |
| 179 | * @return Itself |
| 180 | */ |
| 181 | public RadioBoxList<V> addListener(Listener listener) { |
| 182 | if(listener != null && !listeners.contains(listener)) { |
| 183 | listeners.add(listener); |
| 184 | } |
| 185 | return this; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Removes a listener from this {@code RadioBoxList} so that if it had been added earlier, it will no longer be |
| 190 | * called on user actions |
| 191 | * @param listener Listener to remove from this {@code RadioBoxList} |
| 192 | * @return Itself |
| 193 | */ |
| 194 | public RadioBoxList<V> removeListener(Listener listener) { |
| 195 | listeners.remove(listener); |
| 196 | return this; |
| 197 | } |
| 198 | |
| 199 | private void setCheckedIndex(int index) { |
| 200 | final int previouslyChecked = checkedIndex; |
| 201 | this.checkedIndex = index; |
| 202 | invalidate(); |
| 203 | runOnGUIThreadIfExistsOtherwiseRunDirect(new Runnable() { |
| 204 | @Override |
| 205 | public void run() { |
| 206 | for(Listener listener: listeners) { |
| 207 | listener.onSelectionChanged(-1, previouslyChecked); |
| 208 | } |
| 209 | } |
| 210 | }); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Default renderer for this component which is used unless overridden. The selected state is drawn on the left side |
| 215 | * of the item label using a "< >" block filled with an "o" if the item is the selected one |
| 216 | * @param <V> |
| 217 | */ |
| 218 | public static class RadioBoxListItemRenderer<V> extends ListItemRenderer<V,RadioBoxList<V>> { |
| 219 | @Override |
| 220 | public int getHotSpotPositionOnLine(int selectedIndex) { |
| 221 | return 1; |
| 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public String getLabel(RadioBoxList<V> listBox, int index, V item) { |
| 226 | String check = " "; |
| 227 | if(listBox.checkedIndex == index) |
| 228 | check = "o"; |
| 229 | |
| 230 | String text = (item != null ? item : "<null>").toString(); |
| 231 | return "<" + check + "> " + text; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | } |