2 * This file is part of lanterna (http://code.google.com/p/lanterna/).
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.
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.
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/>.
17 * Copyright (C) 2010-2015 Martin
19 package com
.googlecode
.lanterna
.gui2
;
21 import com
.googlecode
.lanterna
.TerminalSize
;
22 import com
.googlecode
.lanterna
.input
.KeyStroke
;
23 import com
.googlecode
.lanterna
.input
.KeyType
;
25 import java
.util
.List
;
26 import java
.util
.concurrent
.CopyOnWriteArrayList
;
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.
35 public class RadioBoxList
<V
> extends AbstractListBox
<V
, RadioBoxList
<V
>> {
37 * Listener interface that can be attached to the {@code RadioBoxList} in order to be notified on user actions
39 public interface Listener
{
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
47 void onSelectionChanged(int selectedIndex
, int previousSelection
);
50 private final List
<Listener
> listeners
;
51 private int checkedIndex
;
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.
57 public RadioBoxList() {
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
67 public RadioBoxList(TerminalSize preferredSize
) {
69 this.listeners
= new CopyOnWriteArrayList
<Listener
>();
70 this.checkedIndex
= -1;
74 protected ListItemRenderer
<V
,RadioBoxList
<V
>> createDefaultListItemRenderer() {
75 return new RadioBoxListItemRenderer
<V
>();
79 public synchronized Result
handleKeyStroke(KeyStroke keyStroke
) {
80 if(keyStroke
.getKeyType() == KeyType
.Enter
||
81 (keyStroke
.getKeyType() == KeyType
.Character
&& keyStroke
.getCharacter() == ' ')) {
82 checkedIndex
= getSelectedIndex();
84 return Result
.HANDLED
;
86 return super.handleKeyStroke(keyStroke
);
90 public synchronized RadioBoxList
<V
> clearItems() {
92 return super.clearItems();
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.
101 public synchronized Boolean
isChecked(V object
) {
105 if(indexOf(object
) == -1)
108 return checkedIndex
== indexOf(object
);
112 * This method will see if an item, addressed by index, is the currently selected item in this
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.
118 @SuppressWarnings("SimplifiableIfStatement")
119 public synchronized boolean isChecked(int index
) {
120 if(index
< 0 || index
>= getItemCount()) {
124 return checkedIndex
== index
;
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
132 public synchronized void setCheckedItem(V item
) {
137 setCheckedItemIndex(indexOf(item
));
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
145 public synchronized void setCheckedItemIndex(int index
) {
146 if(index
< -1 || index
>= getItemCount())
149 setCheckedIndex(index
);
153 * @return The index of the item which is currently selected, or -1 if there is no selection
155 public int getCheckedItemIndex() {
160 * @return The object currently selected, or null if there is no selection
162 public synchronized V
getCheckedItem() {
163 if(checkedIndex
== -1 || checkedIndex
>= getItemCount())
166 return getItemAt(checkedIndex
);
170 * Un-checks the currently checked item (if any) and leaves the radio check box in a state where no item is checked.
172 public synchronized void clearSelection() {
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}
181 public RadioBoxList
<V
> addListener(Listener listener
) {
182 if(listener
!= null && !listeners
.contains(listener
)) {
183 listeners
.add(listener
);
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}
194 public RadioBoxList
<V
> removeListener(Listener listener
) {
195 listeners
.remove(listener
);
199 private void setCheckedIndex(int index
) {
200 final int previouslyChecked
= checkedIndex
;
201 this.checkedIndex
= index
;
203 runOnGUIThreadIfExistsOtherwiseRunDirect(new Runnable() {
206 for(Listener listener
: listeners
) {
207 listener
.onSelectionChanged(-1, previouslyChecked
);
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
218 public static class RadioBoxListItemRenderer
<V
> extends ListItemRenderer
<V
,RadioBoxList
<V
>> {
220 public int getHotSpotPositionOnLine(int selectedIndex
) {
225 public String
getLabel(RadioBoxList
<V
> listBox
, int index
, V item
) {
227 if(listBox
.checkedIndex
== index
)
230 String text
= (item
!= null ? item
: "<null>").toString();
231 return "<" + check
+ "> " + text
;