Change build scripts
[jvcard.git] / src / com / googlecode / lanterna / gui2 / ActionListBox.java
CommitLineData
a3b510ab
NR
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 */
19package com.googlecode.lanterna.gui2;
20
21import com.googlecode.lanterna.TerminalPosition;
22import com.googlecode.lanterna.TerminalSize;
23import com.googlecode.lanterna.input.KeyStroke;
24import com.googlecode.lanterna.input.KeyType;
25
26/**
27 * This class is a list box implementation that displays a number of items that has actions associated with them. You
28 * can activate this action by pressing the Enter or Space keys on the keyboard and the action associated with the
29 * currently selected item will fire.
30 * @author Martin
31 */
32public class ActionListBox extends AbstractListBox<Runnable, ActionListBox> {
33
34 /**
35 * Default constructor, creates an {@code ActionListBox} with no pre-defined size that will request to be big enough
36 * to display all items
37 */
38 public ActionListBox() {
39 this(null);
40 }
41
42 /**
43 * Creates a new {@code ActionListBox} with a pre-set size. If the items don't fit in within this size, scrollbars
44 * will be used to accommodate. Calling {@code new ActionListBox(null)} has the same effect as calling
45 * {@code new ActionListBox()}.
46 * @param preferredSize
47 */
48 public ActionListBox(TerminalSize preferredSize) {
49 super(preferredSize);
50 }
51
52 /**
53 * {@inheritDoc}
54 *
55 * The label of the item in the list box will be the result of calling {@code .toString()} on the runnable, which
56 * might not be what you want to have unless you explicitly declare it. Consider using
57 * {@code addItem(String label, Runnable action} instead, if you want to just set the label easily without having
58 * to override {@code .toString()}.
59 *
60 * @param object Runnable to execute when the action was selected and fired in the list
61 * @return Itself
62 */
63 @Override
64 public ActionListBox addItem(Runnable object) {
65 return super.addItem(object);
66 }
67
68 /**
69 * Adds a new item to the list, which is displayed in the list using a supplied label.
70 * @param label Label to use in the list for the new item
71 * @param action Runnable to invoke when this action is selected and then triggered
72 * @return Itself
73 */
74 public ActionListBox addItem(final String label, final Runnable action) {
75 return addItem(new Runnable() {
76 @Override
77 public void run() {
78 action.run();
79 }
80
81 @Override
82 public String toString() {
83 return label;
84 }
85 });
86 }
87
88 @Override
89 public TerminalPosition getCursorLocation() {
90 return null;
91 }
92
93 @Override
94 public Result handleKeyStroke(KeyStroke keyStroke) {
95 Object selectedItem = getSelectedItem();
96 if(selectedItem != null &&
97 (keyStroke.getKeyType() == KeyType.Enter ||
98 (keyStroke.getKeyType() == KeyType.Character && keyStroke.getCharacter() == ' '))) {
99
100 ((Runnable)selectedItem).run();
101 return Result.HANDLED;
102 }
103 return super.handleKeyStroke(keyStroke);
104 }
105}