| 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.ArrayList; |
| 26 | import java.util.List; |
| 27 | import java.util.concurrent.CopyOnWriteArrayList; |
| 28 | |
| 29 | /** |
| 30 | * This is a list box implementation where each item has its own checked state that can be toggled on and off |
| 31 | * @author Martin |
| 32 | */ |
| 33 | public class CheckBoxList<V> extends AbstractListBox<V, CheckBoxList<V>> { |
| 34 | /** |
| 35 | * Listener interface that can be attached to the {@code CheckBoxList} in order to be notified on user actions |
| 36 | */ |
| 37 | public interface Listener { |
| 38 | /** |
| 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} |
| 42 | */ |
| 43 | void onStatusChanged(int itemIndex, boolean checked); |
| 44 | } |
| 45 | |
| 46 | private final List<Listener> listeners; |
| 47 | private final List<Boolean> itemStatus; |
| 48 | |
| 49 | /** |
| 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. |
| 52 | */ |
| 53 | public CheckBoxList() { |
| 54 | this(null); |
| 55 | } |
| 56 | |
| 57 | /** |
| 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 |
| 61 | */ |
| 62 | public CheckBoxList(TerminalSize preferredSize) { |
| 63 | super(preferredSize); |
| 64 | this.listeners = new CopyOnWriteArrayList<Listener>(); |
| 65 | this.itemStatus = new ArrayList<Boolean>(); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | protected ListItemRenderer<V,CheckBoxList<V>> createDefaultListItemRenderer() { |
| 70 | return new CheckBoxListItemRenderer<V>(); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public synchronized CheckBoxList<V> clearItems() { |
| 75 | itemStatus.clear(); |
| 76 | return super.clearItems(); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public CheckBoxList<V> addItem(V object) { |
| 81 | return addItem(object, false); |
| 82 | } |
| 83 | |
| 84 | /** |
| 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 |
| 88 | * @return Itself |
| 89 | */ |
| 90 | public synchronized CheckBoxList<V> addItem(V object, boolean checkedState) { |
| 91 | itemStatus.add(checkedState); |
| 92 | return super.addItem(object); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Checks if a particular item is part of the check box list and returns a boolean value depending on the toggle |
| 97 | * state of the item. |
| 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 |
| 101 | */ |
| 102 | public synchronized Boolean isChecked(V object) { |
| 103 | if(indexOf(object) == -1) |
| 104 | return null; |
| 105 | |
| 106 | return itemStatus.get(indexOf(object)); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Checks if a particular item is part of the check box list and returns a boolean value depending on the toggle |
| 111 | * state of the item. |
| 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 |
| 115 | */ |
| 116 | public synchronized Boolean isChecked(int index) { |
| 117 | if(index < 0 || index >= itemStatus.size()) |
| 118 | return null; |
| 119 | |
| 120 | return itemStatus.get(index); |
| 121 | } |
| 122 | |
| 123 | /** |
| 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 |
| 127 | * @return Itself |
| 128 | */ |
| 129 | public synchronized CheckBoxList<V> setChecked(V object, boolean checked) { |
| 130 | int index = indexOf(object); |
| 131 | if(index != -1) { |
| 132 | setChecked(index, checked); |
| 133 | } |
| 134 | return self(); |
| 135 | } |
| 136 | |
| 137 | private void setChecked(final int index, final boolean checked) { |
| 138 | itemStatus.set(index, checked); |
| 139 | runOnGUIThreadIfExistsOtherwiseRunDirect(new Runnable() { |
| 140 | @Override |
| 141 | public void run() { |
| 142 | for(Listener listener: listeners) { |
| 143 | listener.onStatusChanged(index, checked); |
| 144 | } |
| 145 | } |
| 146 | }); |
| 147 | } |
| 148 | |
| 149 | /** |
| 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 |
| 152 | */ |
| 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)); |
| 158 | } |
| 159 | } |
| 160 | return result; |
| 161 | } |
| 162 | |
| 163 | /** |
| 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} |
| 166 | * @return Itself |
| 167 | */ |
| 168 | public synchronized CheckBoxList<V> addListener(Listener listener) { |
| 169 | if(listener != null && !listeners.contains(listener)) { |
| 170 | listeners.add(listener); |
| 171 | } |
| 172 | return this; |
| 173 | } |
| 174 | |
| 175 | /** |
| 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} |
| 179 | * @return Itself |
| 180 | */ |
| 181 | public CheckBoxList<V> removeListener(Listener listener) { |
| 182 | listeners.remove(listener); |
| 183 | return this; |
| 184 | } |
| 185 | |
| 186 | @Override |
| 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); |
| 192 | else |
| 193 | setChecked(getSelectedIndex(), Boolean.TRUE); |
| 194 | return Result.HANDLED; |
| 195 | } |
| 196 | return super.handleKeyStroke(keyStroke); |
| 197 | } |
| 198 | |
| 199 | /** |
| 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 |
| 202 | * @param <V> |
| 203 | */ |
| 204 | public static class CheckBoxListItemRenderer<V> extends ListItemRenderer<V,CheckBoxList<V>> { |
| 205 | @Override |
| 206 | public int getHotSpotPositionOnLine(int selectedIndex) { |
| 207 | return 1; |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public String getLabel(CheckBoxList<V> listBox, int index, V item) { |
| 212 | String check = " "; |
| 213 | List<Boolean> itemStatus = listBox.itemStatus; |
| 214 | if(itemStatus.get(index)) |
| 215 | check = "x"; |
| 216 | |
| 217 | String text = item.toString(); |
| 218 | return "[" + check + "] " + text; |
| 219 | } |
| 220 | } |
| 221 | } |