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
.Collections
;
33 import java
.util
.List
;
35 import jexer
.bits
.CellAttributes
;
36 import jexer
.event
.TKeypressEvent
;
37 import jexer
.event
.TMouseEvent
;
38 import static jexer
.TKeypress
.*;
41 * TList shows a list of strings, and lets the user select one.
43 public class TList
extends TScrollableWidget
{
45 // ------------------------------------------------------------------------
46 // Variables --------------------------------------------------------------
47 // ------------------------------------------------------------------------
50 * The list of strings to display.
52 private List
<String
> strings
;
57 private int selectedString
= -1;
60 * Maximum width of a single line.
62 private int maxLineWidth
;
65 * The action to perform when the user selects an item (double-clicks or
68 protected TAction enterAction
= null;
71 * The action to perform when the user selects an item (single-click).
73 protected TAction singleClickAction
= null;
76 * The action to perform when the user navigates with keyboard.
78 protected TAction moveAction
= null;
80 // ------------------------------------------------------------------------
81 // Constructors -----------------------------------------------------------
82 // ------------------------------------------------------------------------
87 * @param parent parent widget
88 * @param strings list of strings to show
89 * @param x column relative to parent
90 * @param y row relative to parent
91 * @param width width of text area
92 * @param height height of text area
94 public TList(final TWidget parent
, final List
<String
> strings
, final int x
,
95 final int y
, final int width
, final int height
) {
97 this(parent
, strings
, x
, y
, width
, height
, null);
101 * Public constructor.
103 * @param parent parent widget
104 * @param strings list of strings to show. This is allowed to be null
105 * and set later with setList() or by subclasses.
106 * @param x column relative to parent
107 * @param y row relative to parent
108 * @param width width of text area
109 * @param height height of text area
110 * @param enterAction action to perform when an item is selected
112 public TList(final TWidget parent
, final List
<String
> strings
, final int x
,
113 final int y
, final int width
, final int height
,
114 final TAction enterAction
) {
116 super(parent
, x
, y
, width
, height
);
117 this.enterAction
= enterAction
;
118 this.strings
= new ArrayList
<String
>();
119 if (strings
!= null) {
120 this.strings
.addAll(strings
);
123 hScroller
= new THScroller(this, 0, getHeight() - 1, getWidth() - 1);
124 vScroller
= new TVScroller(this, getWidth() - 1, 0, getHeight() - 1);
129 * Public constructor.
131 * @param parent parent widget
132 * @param strings list of strings to show. This is allowed to be null
133 * and set later with setList() or by subclasses.
134 * @param x column relative to parent
135 * @param y row relative to parent
136 * @param width width of text area
137 * @param height height of text area
138 * @param enterAction action to perform when an item is selected
139 * @param moveAction action to perform when the user navigates to a new
140 * item with arrow/page keys
142 public TList(final TWidget parent
, final List
<String
> strings
, final int x
,
143 final int y
, final int width
, final int height
,
144 final TAction enterAction
, final TAction moveAction
) {
146 super(parent
, x
, y
, width
, height
);
147 this.enterAction
= enterAction
;
148 this.moveAction
= moveAction
;
149 this.strings
= new ArrayList
<String
>();
150 if (strings
!= null) {
151 this.strings
.addAll(strings
);
154 hScroller
= new THScroller(this, 0, getHeight() - 1, getWidth() - 1);
155 vScroller
= new TVScroller(this, getWidth() - 1, 0, getHeight() - 1);
159 // ------------------------------------------------------------------------
160 // Event handlers ---------------------------------------------------------
161 // ------------------------------------------------------------------------
164 * Handle mouse press events.
166 * @param mouse mouse button press event
169 public void onMouseDown(final TMouseEvent mouse
) {
170 if (mouse
.isMouseWheelUp()) {
174 if (mouse
.isMouseWheelDown()) {
179 if ((mouse
.getX() < getWidth() - 1)
180 && (mouse
.getY() < getHeight() - 1)
182 if (getVerticalValue() + mouse
.getY() < strings
.size()) {
183 selectedString
= getVerticalValue() + mouse
.getY();
184 dispatchSingleClick();
190 super.onMouseDown(mouse
);
194 * Handle mouse double click.
196 * @param mouse mouse double click event
199 public void onMouseDoubleClick(final TMouseEvent mouse
) {
200 if ((mouse
.getX() < getWidth() - 1)
201 && (mouse
.getY() < getHeight() - 1)
203 if (getVerticalValue() + mouse
.getY() < strings
.size()) {
204 selectedString
= getVerticalValue() + mouse
.getY();
211 super.onMouseDoubleClick(mouse
);
217 * @param keypress keystroke event
220 public void onKeypress(final TKeypressEvent keypress
) {
221 if (keypress
.equals(kbLeft
)) {
222 horizontalDecrement();
223 } else if (keypress
.equals(kbRight
)) {
224 horizontalIncrement();
225 } else if (keypress
.equals(kbUp
)) {
226 if (strings
.size() > 0) {
227 if (selectedString
>= 0) {
228 if (selectedString
> 0) {
229 if (selectedString
- getVerticalValue() == 0) {
235 selectedString
= strings
.size() - 1;
238 if (selectedString
>= 0) {
241 } else if (keypress
.equals(kbDown
)) {
242 if (strings
.size() > 0) {
243 if (selectedString
>= 0) {
244 if (selectedString
< strings
.size() - 1) {
246 if (selectedString
- getVerticalValue() == getHeight() - 1) {
254 if (selectedString
>= 0) {
257 } else if (keypress
.equals(kbPgUp
)) {
258 bigVerticalDecrement();
259 if (selectedString
>= 0) {
260 selectedString
-= getHeight() - 1;
261 if (selectedString
< 0) {
265 if (selectedString
>= 0) {
268 } else if (keypress
.equals(kbPgDn
)) {
269 bigVerticalIncrement();
270 if (selectedString
>= 0) {
271 selectedString
+= getHeight() - 1;
272 if (selectedString
> strings
.size() - 1) {
273 selectedString
= strings
.size() - 1;
276 if (selectedString
>= 0) {
279 } else if (keypress
.equals(kbHome
)) {
281 if (strings
.size() > 0) {
284 if (selectedString
>= 0) {
287 } else if (keypress
.equals(kbEnd
)) {
289 if (strings
.size() > 0) {
290 selectedString
= strings
.size() - 1;
292 if (selectedString
>= 0) {
295 } else if (keypress
.equals(kbTab
)) {
296 getParent().switchWidget(true);
297 } else if (keypress
.equals(kbShiftTab
) || keypress
.equals(kbBackTab
)) {
298 getParent().switchWidget(false);
299 } else if (keypress
.equals(kbEnter
)) {
300 if (selectedString
>= 0) {
304 // Pass other keys (tab etc.) on
305 super.onKeypress(keypress
);
309 // ------------------------------------------------------------------------
310 // TScrollableWidget ------------------------------------------------------
311 // ------------------------------------------------------------------------
314 * Resize for a new width/height.
317 public void reflowData() {
323 for (int i
= 0; i
< strings
.size(); i
++) {
324 String line
= strings
.get(i
);
325 if (line
.length() > maxLineWidth
) {
326 maxLineWidth
= line
.length();
330 setBottomValue(strings
.size() - getHeight() + 1);
331 if (getBottomValue() < 0) {
335 setRightValue(maxLineWidth
- getWidth() + 1);
336 if (getRightValue() < 0) {
346 CellAttributes color
= null;
347 int begin
= getVerticalValue();
349 for (int i
= begin
; i
< strings
.size(); i
++) {
350 String line
= strings
.get(i
);
351 if (getHorizontalValue() < line
.length()) {
352 line
= line
.substring(getHorizontalValue());
356 if (i
== selectedString
) {
357 if (isAbsoluteActive()) {
358 color
= getTheme().getColor("tlist.selected");
360 color
= getTheme().getColor("tlist.selected.inactive");
362 } else if (isAbsoluteActive()) {
363 color
= getTheme().getColor("tlist");
365 color
= getTheme().getColor("tlist.inactive");
367 String formatString
= "%-" + Integer
.toString(getWidth() - 1) + "s";
368 putStringXY(0, topY
, String
.format(formatString
, line
), color
);
370 if (topY
>= getHeight() - 1) {
375 if (isAbsoluteActive()) {
376 color
= getTheme().getColor("tlist");
378 color
= getTheme().getColor("tlist.inactive");
381 // Pad the rest with blank lines
382 for (int i
= topY
; i
< getHeight() - 1; i
++) {
383 hLineXY(0, i
, getWidth() - 1, ' ', color
);
387 // ------------------------------------------------------------------------
388 // TList ------------------------------------------------------------------
389 // ------------------------------------------------------------------------
392 * Get the selection index.
394 * @return -1 if nothing is selected, otherwise the index into the list
396 public final int getSelectedIndex() {
397 return selectedString
;
401 * Set the selected string index.
403 * @param index -1 to unselect, otherwise the index into the list
405 public final void setSelectedIndex(final int index
) {
406 selectedString
= index
;
410 * Get a selectable string by index.
412 * @param idx index into list
413 * @return the string at idx in the list
415 public final String
getListItem(final int idx
) {
416 return strings
.get(idx
);
420 * Get the selected string.
422 * @return the selected string, or null of nothing is selected yet
424 public final String
getSelected() {
425 if ((selectedString
>= 0) && (selectedString
<= strings
.size() - 1)) {
426 return strings
.get(selectedString
);
432 * Get the maximum selection index value.
434 * @return -1 if the list is empty
436 public final int getMaxSelectedIndex() {
437 return strings
.size() - 1;
441 * Get a copy of the list of strings to display.
443 * @return the list of strings
445 public final List
<String
> getList() {
446 return new ArrayList
<String
>(strings
);
450 * Set the new list of strings to display.
452 * @param list new list of strings
454 public final void setList(final List
<String
> list
) {
456 strings
.addAll(list
);
461 * Perform user selection action.
463 public void dispatchEnter() {
464 assert (selectedString
>= 0);
465 assert (selectedString
< strings
.size());
466 if (enterAction
!= null) {
472 * Perform list movement action.
474 public void dispatchMove() {
475 assert (selectedString
>= 0);
476 assert (selectedString
< strings
.size());
477 if (moveAction
!= null) {
483 * Perform single-click action.
485 public void dispatchSingleClick() {
486 assert (selectedString
>= 0);
487 assert (selectedString
< strings
.size());
488 if (singleClickAction
!= null) {
489 singleClickAction
.DO();