2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 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
{
44 // ------------------------------------------------------------------------
45 // Variables --------------------------------------------------------------
46 // ------------------------------------------------------------------------
49 * The list of strings to display.
51 private List
<String
> strings
;
56 private int selectedString
= -1;
59 * Maximum width of a single line.
61 private int maxLineWidth
;
64 * The action to perform when the user selects an item (double-clicks or
67 protected TAction enterAction
= null;
70 * The action to perform when the user selects an item (single-click).
72 protected TAction singleClickAction
= null;
75 * The action to perform when the user navigates with keyboard.
77 protected TAction moveAction
= null;
79 // ------------------------------------------------------------------------
80 // Constructors -----------------------------------------------------------
81 // ------------------------------------------------------------------------
86 * @param parent parent widget
87 * @param strings list of strings to show
88 * @param x column relative to parent
89 * @param y row relative to parent
90 * @param width width of text area
91 * @param height height of text area
93 public TList(final TWidget parent
, final List
<String
> strings
, final int x
,
94 final int y
, final int width
, final int height
) {
96 this(parent
, strings
, x
, y
, width
, height
, null, null, null);
100 * Public constructor.
102 * @param parent parent widget
103 * @param strings list of strings to show. This is allowed to be null
104 * and set later with setList() or by subclasses.
105 * @param x column relative to parent
106 * @param y row relative to parent
107 * @param width width of text area
108 * @param height height of text area
109 * @param enterAction action to perform when an item is selected
111 public TList(final TWidget parent
, final List
<String
> strings
, final int x
,
112 final int y
, final int width
, final int height
,
113 final TAction enterAction
) {
115 this(parent
, strings
, x
, y
, width
, height
, enterAction
, null, null);
119 * Public constructor.
121 * @param parent parent widget
122 * @param strings list of strings to show. This is allowed to be null
123 * and set later with setList() or by subclasses.
124 * @param x column relative to parent
125 * @param y row relative to parent
126 * @param width width of text area
127 * @param height height of text area
128 * @param enterAction action to perform when an item is selected
129 * @param moveAction action to perform when the user navigates to a new
130 * item with arrow/page keys
132 public TList(final TWidget parent
, final List
<String
> strings
, final int x
,
133 final int y
, final int width
, final int height
,
134 final TAction enterAction
, final TAction moveAction
) {
136 this(parent
, strings
, x
, y
, width
, height
, enterAction
, moveAction
,
141 * Public constructor.
143 * @param parent parent widget
144 * @param strings list of strings to show. This is allowed to be null
145 * and set later with setList() or by subclasses.
146 * @param x column relative to parent
147 * @param y row relative to parent
148 * @param width width of text area
149 * @param height height of text area
150 * @param enterAction action to perform when an item is selected
151 * @param moveAction action to perform when the user navigates to a new
152 * item with arrow/page keys
153 * @param singleClickAction action to perform when the user clicks on an
156 public TList(final TWidget parent
, final List
<String
> strings
, final int x
,
157 final int y
, final int width
, final int height
,
158 final TAction enterAction
, final TAction moveAction
,
159 final TAction singleClickAction
) {
161 super(parent
, x
, y
, width
, height
);
162 this.enterAction
= enterAction
;
163 this.moveAction
= moveAction
;
164 this.singleClickAction
= singleClickAction
;
165 this.strings
= new ArrayList
<String
>();
166 if (strings
!= null) {
167 this.strings
.addAll(strings
);
170 hScroller
= new THScroller(this, 0, getHeight() - 1, getWidth() - 1);
171 vScroller
= new TVScroller(this, getWidth() - 1, 0, getHeight() - 1);
175 // ------------------------------------------------------------------------
176 // Event handlers ---------------------------------------------------------
177 // ------------------------------------------------------------------------
180 * Handle mouse press events.
182 * @param mouse mouse button press event
185 public void onMouseDown(final TMouseEvent mouse
) {
186 if (mouse
.isMouseWheelUp()) {
190 if (mouse
.isMouseWheelDown()) {
195 if ((mouse
.getX() < getWidth() - 1)
196 && (mouse
.getY() < getHeight() - 1)
198 if (getVerticalValue() + mouse
.getY() < strings
.size()) {
199 selectedString
= getVerticalValue() + mouse
.getY();
200 dispatchSingleClick();
206 super.onMouseDown(mouse
);
210 * Handle mouse double click.
212 * @param mouse mouse double click event
215 public void onMouseDoubleClick(final TMouseEvent mouse
) {
216 if ((mouse
.getX() < getWidth() - 1)
217 && (mouse
.getY() < getHeight() - 1)
219 if (getVerticalValue() + mouse
.getY() < strings
.size()) {
220 selectedString
= getVerticalValue() + mouse
.getY();
227 super.onMouseDoubleClick(mouse
);
233 * @param keypress keystroke event
236 public void onKeypress(final TKeypressEvent keypress
) {
237 if (keypress
.equals(kbLeft
)) {
238 horizontalDecrement();
239 } else if (keypress
.equals(kbRight
)) {
240 horizontalIncrement();
241 } else if (keypress
.equals(kbUp
)) {
242 if (strings
.size() > 0) {
243 if (selectedString
>= 0) {
244 if (selectedString
> 0) {
245 if (selectedString
- getVerticalValue() == 0) {
251 selectedString
= strings
.size() - 1;
254 if (selectedString
>= 0) {
257 } else if (keypress
.equals(kbDown
)) {
258 if (strings
.size() > 0) {
259 if (selectedString
>= 0) {
260 if (selectedString
< strings
.size() - 1) {
262 if (selectedString
- getVerticalValue() == getHeight() - 1) {
270 if (selectedString
>= 0) {
273 } else if (keypress
.equals(kbPgUp
)) {
274 bigVerticalDecrement();
275 if (selectedString
>= 0) {
276 selectedString
-= getHeight() - 1;
277 if (selectedString
< 0) {
281 if (selectedString
>= 0) {
284 } else if (keypress
.equals(kbPgDn
)) {
285 bigVerticalIncrement();
286 if (selectedString
>= 0) {
287 selectedString
+= getHeight() - 1;
288 if (selectedString
> strings
.size() - 1) {
289 selectedString
= strings
.size() - 1;
292 if (selectedString
>= 0) {
295 } else if (keypress
.equals(kbHome
)) {
297 if (strings
.size() > 0) {
300 if (selectedString
>= 0) {
303 } else if (keypress
.equals(kbEnd
)) {
305 if (strings
.size() > 0) {
306 selectedString
= strings
.size() - 1;
308 if (selectedString
>= 0) {
311 } else if (keypress
.equals(kbTab
)) {
312 getParent().switchWidget(true);
313 } else if (keypress
.equals(kbShiftTab
) || keypress
.equals(kbBackTab
)) {
314 getParent().switchWidget(false);
315 } else if (keypress
.equals(kbEnter
)) {
316 if (selectedString
>= 0) {
320 // Pass other keys (tab etc.) on
321 super.onKeypress(keypress
);
325 // ------------------------------------------------------------------------
326 // TScrollableWidget ------------------------------------------------------
327 // ------------------------------------------------------------------------
330 * Resize for a new width/height.
333 public void reflowData() {
339 for (int i
= 0; i
< strings
.size(); i
++) {
340 String line
= strings
.get(i
);
341 if (line
.length() > maxLineWidth
) {
342 maxLineWidth
= line
.length();
346 setBottomValue(strings
.size() - getHeight() + 1);
347 if (getBottomValue() < 0) {
351 setRightValue(maxLineWidth
- getWidth() + 1);
352 if (getRightValue() < 0) {
362 CellAttributes color
= null;
363 int begin
= getVerticalValue();
365 for (int i
= begin
; i
< strings
.size(); i
++) {
366 String line
= strings
.get(i
);
367 if (getHorizontalValue() < line
.length()) {
368 line
= line
.substring(getHorizontalValue());
372 if (i
== selectedString
) {
373 if (isAbsoluteActive()) {
374 color
= getTheme().getColor("tlist.selected");
376 color
= getTheme().getColor("tlist.selected.inactive");
378 } else if (isAbsoluteActive()) {
379 color
= getTheme().getColor("tlist");
381 color
= getTheme().getColor("tlist.inactive");
383 String formatString
= "%-" + Integer
.toString(getWidth() - 1) + "s";
384 putStringXY(0, topY
, String
.format(formatString
, line
), color
);
386 if (topY
>= getHeight() - 1) {
391 if (isAbsoluteActive()) {
392 color
= getTheme().getColor("tlist");
394 color
= getTheme().getColor("tlist.inactive");
397 // Pad the rest with blank lines
398 for (int i
= topY
; i
< getHeight() - 1; i
++) {
399 hLineXY(0, i
, getWidth() - 1, ' ', color
);
403 // ------------------------------------------------------------------------
404 // TList ------------------------------------------------------------------
405 // ------------------------------------------------------------------------
408 * Get the selection index.
410 * @return -1 if nothing is selected, otherwise the index into the list
412 public final int getSelectedIndex() {
413 return selectedString
;
417 * Set the selected string index.
419 * @param index -1 to unselect, otherwise the index into the list
421 public final void setSelectedIndex(final int index
) {
422 selectedString
= index
;
426 * Get a selectable string by index.
428 * @param idx index into list
429 * @return the string at idx in the list
431 public final String
getListItem(final int idx
) {
432 return strings
.get(idx
);
436 * Get the selected string.
438 * @return the selected string, or null of nothing is selected yet
440 public final String
getSelected() {
441 if ((selectedString
>= 0) && (selectedString
<= strings
.size() - 1)) {
442 return strings
.get(selectedString
);
448 * Get the maximum selection index value.
450 * @return -1 if the list is empty
452 public final int getMaxSelectedIndex() {
453 return strings
.size() - 1;
457 * Get a copy of the list of strings to display.
459 * @return the list of strings
461 public final List
<String
> getList() {
462 return new ArrayList
<String
>(strings
);
466 * Set the new list of strings to display.
468 * @param list new list of strings
470 public final void setList(final List
<String
> list
) {
472 strings
.addAll(list
);
477 * Perform user selection action.
479 public void dispatchEnter() {
480 assert (selectedString
>= 0);
481 assert (selectedString
< strings
.size());
482 if (enterAction
!= null) {
488 * Perform list movement action.
490 public void dispatchMove() {
491 assert (selectedString
>= 0);
492 assert (selectedString
< strings
.size());
493 if (moveAction
!= null) {
499 * Perform single-click action.
501 public void dispatchSingleClick() {
502 assert (selectedString
>= 0);
503 assert (selectedString
< strings
.size());
504 if (singleClickAction
!= null) {
505 singleClickAction
.DO();