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
.ArrayList
;
26 import java
.util
.List
;
27 import java
.util
.concurrent
.CopyOnWriteArrayList
;
30 * This is a list box implementation where each item has its own checked state that can be toggled on and off
33 public class CheckBoxList
<V
> extends AbstractListBox
<V
, CheckBoxList
<V
>> {
35 * Listener interface that can be attached to the {@code CheckBoxList} in order to be notified on user actions
37 public interface Listener
{
39 * Called by the {@code CheckBoxList} when the user changes the toggle state of one item
40 * @param itemIndex Index of the item that was toggled
41 * @param checked If the state of the item is now checked, this will be {@code true}, otherwise {@code false}
43 void onStatusChanged(int itemIndex
, boolean checked
);
46 private final List
<Listener
> listeners
;
47 private final List
<Boolean
> itemStatus
;
50 * Creates a new {@code CheckBoxList} that is initially empty and has no hardcoded preferred size, so it will
51 * attempt to be as big as necessary to draw all items.
53 public CheckBoxList() {
58 * Creates a new {@code CheckBoxList} that is initially empty and has a pre-defined size that it will request. If
59 * there are more items that can fit in this size, the list box will use scrollbars.
60 * @param preferredSize Size the list box should request, no matter how many items it contains
62 public CheckBoxList(TerminalSize preferredSize
) {
64 this.listeners
= new CopyOnWriteArrayList
<Listener
>();
65 this.itemStatus
= new ArrayList
<Boolean
>();
69 protected ListItemRenderer
<V
,CheckBoxList
<V
>> createDefaultListItemRenderer() {
70 return new CheckBoxListItemRenderer
<V
>();
74 public synchronized CheckBoxList
<V
> clearItems() {
76 return super.clearItems();
80 public CheckBoxList
<V
> addItem(V object
) {
81 return addItem(object
, false);
85 * Adds an item to the checkbox list with an explicit checked status
86 * @param object Object to add to the list
87 * @param checkedState If <code>true</code>, the new item will be initially checked
90 public synchronized CheckBoxList
<V
> addItem(V object
, boolean checkedState
) {
91 itemStatus
.add(checkedState
);
92 return super.addItem(object
);
96 * Checks if a particular item is part of the check box list and returns a boolean value depending on the toggle
98 * @param object Object to check the status of
99 * @return If the item wasn't found in the list box, {@code null} is returned, otherwise {@code true} or
100 * {@code false} depending on checked state of the item
102 public synchronized Boolean
isChecked(V object
) {
103 if(indexOf(object
) == -1)
106 return itemStatus
.get(indexOf(object
));
110 * Checks if a particular item is part of the check box list and returns a boolean value depending on the toggle
112 * @param index Index of the item to check the status of
113 * @return If the index was not valid in the list box, {@code null} is returned, otherwise {@code true} or
114 * {@code false} depending on checked state of the item at that index
116 public synchronized Boolean
isChecked(int index
) {
117 if(index
< 0 || index
>= itemStatus
.size())
120 return itemStatus
.get(index
);
124 * Programmatically sets the checked state of an item in the list box
125 * @param object Object to set the checked state of
126 * @param checked If {@code true}, then the item is set to checked, otherwise not
129 public synchronized CheckBoxList
<V
> setChecked(V object
, boolean checked
) {
130 int index
= indexOf(object
);
132 setChecked(index
, checked
);
137 private void setChecked(final int index
, final boolean checked
) {
138 itemStatus
.set(index
, checked
);
139 runOnGUIThreadIfExistsOtherwiseRunDirect(new Runnable() {
142 for(Listener listener
: listeners
) {
143 listener
.onStatusChanged(index
, checked
);
150 * Returns all the items in the list box that have checked state, as a list
151 * @return List of all items in the list box that has checked state on
153 public synchronized List
<V
> getCheckedItems() {
154 List
<V
> result
= new ArrayList
<V
>();
155 for(int i
= 0; i
< itemStatus
.size(); i
++) {
156 if(itemStatus
.get(i
)) {
157 result
.add(getItemAt(i
));
164 * Adds a new listener to the {@code CheckBoxList} that will be called on certain user actions
165 * @param listener Listener to attach to this {@code CheckBoxList}
168 public synchronized CheckBoxList
<V
> addListener(Listener listener
) {
169 if(listener
!= null && !listeners
.contains(listener
)) {
170 listeners
.add(listener
);
176 * Removes a listener from this {@code CheckBoxList} so that if it had been added earlier, it will no longer be
177 * called on user actions
178 * @param listener Listener to remove from this {@code CheckBoxList}
181 public CheckBoxList
<V
> removeListener(Listener listener
) {
182 listeners
.remove(listener
);
187 public synchronized Result
handleKeyStroke(KeyStroke keyStroke
) {
188 if(keyStroke
.getKeyType() == KeyType
.Enter
||
189 (keyStroke
.getKeyType() == KeyType
.Character
&& keyStroke
.getCharacter() == ' ')) {
190 if(itemStatus
.get(getSelectedIndex()))
191 setChecked(getSelectedIndex(), Boolean
.FALSE
);
193 setChecked(getSelectedIndex(), Boolean
.TRUE
);
194 return Result
.HANDLED
;
196 return super.handleKeyStroke(keyStroke
);
200 * Default renderer for this component which is used unless overridden. The checked state is drawn on the left side
201 * of the item label using a "[ ]" block filled with an X if the item has checked state on
204 public static class CheckBoxListItemRenderer
<V
> extends ListItemRenderer
<V
,CheckBoxList
<V
>> {
206 public int getHotSpotPositionOnLine(int selectedIndex
) {
211 public String
getLabel(CheckBoxList
<V
> listBox
, int index
, V item
) {
213 List
<Boolean
> itemStatus
= listBox
.itemStatus
;
214 if(itemStatus
.get(index
))
217 String text
= item
.toString();
218 return "[" + check
+ "] " + text
;