2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2017 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
31 import java
.util
.ArrayList
;
32 import java
.util
.List
;
34 import jexer
.bits
.CellAttributes
;
35 import jexer
.event
.TKeypressEvent
;
36 import jexer
.event
.TMouseEvent
;
37 import static jexer
.TKeypress
.*;
40 * TList shows a list of strings, and lets the user select one.
42 public class TList
extends TScrollableWidget
{
45 * The list of strings to display.
47 private List
<String
> strings
;
52 private int selectedString
= -1;
55 * Get the selection index.
57 * @return -1 if nothing is selected, otherwise the index into the list
59 public final int getSelectedIndex() {
60 return selectedString
;
64 * Set the selected string index.
66 * @param index -1 to unselect, otherwise the index into the list
68 public final void setSelectedIndex(final int index
) {
69 selectedString
= index
;
73 * Get the selected string.
75 * @return the selected string, or null of nothing is selected yet
77 public final String
getSelected() {
78 if ((selectedString
>= 0) && (selectedString
<= strings
.size() - 1)) {
79 return strings
.get(selectedString
);
85 * Get the maximum selection index value.
87 * @return -1 if the list is empty
89 public final int getMaxSelectedIndex() {
90 return strings
.size() - 1;
94 * Set the new list of strings to display.
96 * @param list new list of strings
98 public final void setList(final List
<String
> list
) {
100 strings
.addAll(list
);
105 * Maximum width of a single line.
107 private int maxLineWidth
;
110 * The action to perform when the user selects an item (clicks or enter).
112 private TAction enterAction
= null;
115 * The action to perform when the user navigates with keyboard.
117 private TAction moveAction
= null;
120 * Perform user selection action.
122 public void dispatchEnter() {
123 assert (selectedString
>= 0);
124 assert (selectedString
< strings
.size());
125 if (enterAction
!= null) {
131 * Perform list movement action.
133 public void dispatchMove() {
134 assert (selectedString
>= 0);
135 assert (selectedString
< strings
.size());
136 if (moveAction
!= null) {
142 * Resize for a new width/height.
145 public void reflowData() {
151 for (int i
= 0; i
< strings
.size(); i
++) {
152 String line
= strings
.get(i
);
153 if (line
.length() > maxLineWidth
) {
154 maxLineWidth
= line
.length();
158 setBottomValue(strings
.size() - getHeight() + 1);
159 if (getBottomValue() < 0) {
163 setRightValue(maxLineWidth
- getWidth() + 1);
164 if (getRightValue() < 0) {
170 * Public constructor.
172 * @param parent parent widget
173 * @param strings list of strings to show
174 * @param x column relative to parent
175 * @param y row relative to parent
176 * @param width width of text area
177 * @param height height of text area
179 public TList(final TWidget parent
, final List
<String
> strings
, final int x
,
180 final int y
, final int width
, final int height
) {
182 this(parent
, strings
, x
, y
, width
, height
, null);
186 * Public constructor.
188 * @param parent parent widget
189 * @param strings list of strings to show. This is allowed to be null
190 * and set later with setList() or by subclasses.
191 * @param x column relative to parent
192 * @param y row relative to parent
193 * @param width width of text area
194 * @param height height of text area
195 * @param enterAction action to perform when an item is selected
197 public TList(final TWidget parent
, final List
<String
> strings
, final int x
,
198 final int y
, final int width
, final int height
,
199 final TAction enterAction
) {
201 super(parent
, x
, y
, width
, height
);
202 this.enterAction
= enterAction
;
203 this.strings
= new ArrayList
<String
>();
204 if (strings
!= null) {
205 this.strings
.addAll(strings
);
208 hScroller
= new THScroller(this, 0, getHeight() - 1, getWidth() - 1);
209 vScroller
= new TVScroller(this, getWidth() - 1, 0, getHeight() - 1);
214 * Public constructor.
216 * @param parent parent widget
217 * @param strings list of strings to show. This is allowed to be null
218 * and set later with setList() or by subclasses.
219 * @param x column relative to parent
220 * @param y row relative to parent
221 * @param width width of text area
222 * @param height height of text area
223 * @param enterAction action to perform when an item is selected
224 * @param moveAction action to perform when the user navigates to a new
225 * item with arrow/page keys
227 public TList(final TWidget parent
, final List
<String
> strings
, final int x
,
228 final int y
, final int width
, final int height
,
229 final TAction enterAction
, final TAction moveAction
) {
231 super(parent
, x
, y
, width
, height
);
232 this.enterAction
= enterAction
;
233 this.moveAction
= moveAction
;
234 this.strings
= new ArrayList
<String
>();
235 if (strings
!= null) {
236 this.strings
.addAll(strings
);
239 hScroller
= new THScroller(this, 0, getHeight() - 1, getWidth() - 1);
240 vScroller
= new TVScroller(this, getWidth() - 1, 0, getHeight() - 1);
245 * Draw the files list.
249 CellAttributes color
= null;
250 int begin
= getVerticalValue();
252 for (int i
= begin
; i
< strings
.size(); i
++) {
253 String line
= strings
.get(i
);
254 if (getHorizontalValue() < line
.length()) {
255 line
= line
.substring(getHorizontalValue());
259 if (i
== selectedString
) {
260 color
= getTheme().getColor("tlist.selected");
261 } else if (isAbsoluteActive()) {
262 color
= getTheme().getColor("tlist");
264 color
= getTheme().getColor("tlist.inactive");
266 String formatString
= "%-" + Integer
.toString(getWidth() - 1) + "s";
267 getScreen().putStringXY(0, topY
, String
.format(formatString
, line
),
270 if (topY
>= getHeight() - 1) {
275 if (isAbsoluteActive()) {
276 color
= getTheme().getColor("tlist");
278 color
= getTheme().getColor("tlist.inactive");
281 // Pad the rest with blank lines
282 for (int i
= topY
; i
< getHeight() - 1; i
++) {
283 getScreen().hLineXY(0, i
, getWidth() - 1, ' ', color
);
288 * Handle mouse press events.
290 * @param mouse mouse button press event
293 public void onMouseDown(final TMouseEvent mouse
) {
294 if (mouse
.isMouseWheelUp()) {
298 if (mouse
.isMouseWheelDown()) {
303 if ((mouse
.getX() < getWidth() - 1)
304 && (mouse
.getY() < getHeight() - 1)) {
305 if (getVerticalValue() + mouse
.getY() < strings
.size()) {
306 selectedString
= getVerticalValue() + mouse
.getY();
313 super.onMouseDown(mouse
);
319 * @param keypress keystroke event
322 public void onKeypress(final TKeypressEvent keypress
) {
323 if (keypress
.equals(kbLeft
)) {
324 horizontalDecrement();
325 } else if (keypress
.equals(kbRight
)) {
326 horizontalIncrement();
327 } else if (keypress
.equals(kbUp
)) {
328 if (strings
.size() > 0) {
329 if (selectedString
>= 0) {
330 if (selectedString
> 0) {
331 if (selectedString
- getVerticalValue() == 0) {
337 selectedString
= strings
.size() - 1;
340 if (selectedString
>= 0) {
343 } else if (keypress
.equals(kbDown
)) {
344 if (strings
.size() > 0) {
345 if (selectedString
>= 0) {
346 if (selectedString
< strings
.size() - 1) {
348 if (selectedString
- getVerticalValue() == getHeight() - 1) {
356 if (selectedString
>= 0) {
359 } else if (keypress
.equals(kbPgUp
)) {
360 bigVerticalDecrement();
361 if (selectedString
>= 0) {
362 selectedString
-= getHeight() - 1;
363 if (selectedString
< 0) {
367 if (selectedString
>= 0) {
370 } else if (keypress
.equals(kbPgDn
)) {
371 bigVerticalIncrement();
372 if (selectedString
>= 0) {
373 selectedString
+= getHeight() - 1;
374 if (selectedString
> strings
.size() - 1) {
375 selectedString
= strings
.size() - 1;
378 if (selectedString
>= 0) {
381 } else if (keypress
.equals(kbHome
)) {
383 if (strings
.size() > 0) {
386 if (selectedString
>= 0) {
389 } else if (keypress
.equals(kbEnd
)) {
391 if (strings
.size() > 0) {
392 selectedString
= strings
.size() - 1;
394 if (selectedString
>= 0) {
397 } else if (keypress
.equals(kbTab
)) {
398 getParent().switchWidget(true);
399 } else if (keypress
.equals(kbShiftTab
) || keypress
.equals(kbBackTab
)) {
400 getParent().switchWidget(false);
401 } else if (keypress
.equals(kbEnter
)) {
402 if (selectedString
>= 0) {
406 // Pass other keys (tab etc.) on
407 super.onKeypress(keypress
);