71600485fe2b9cb1646a8df8a3c0fa5c7af5755d
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 * Set the new list of strings to display.
87 * @param list new list of strings
89 public final void setList(final List
<String
> list
) {
96 * Maximum width of a single line.
98 private int maxLineWidth
;
101 * The action to perform when the user selects an item (clicks or enter).
103 private TAction enterAction
= null;
106 * The action to perform when the user navigates with keyboard.
108 private TAction moveAction
= null;
111 * Perform user selection action.
113 public void dispatchEnter() {
114 assert (selectedString
>= 0);
115 assert (selectedString
< strings
.size());
116 if (enterAction
!= null) {
122 * Perform list movement action.
124 public void dispatchMove() {
125 assert (selectedString
>= 0);
126 assert (selectedString
< strings
.size());
127 if (moveAction
!= null) {
133 * Resize for a new width/height.
136 public void reflowData() {
142 for (int i
= 0; i
< strings
.size(); i
++) {
143 String line
= strings
.get(i
);
144 if (line
.length() > maxLineWidth
) {
145 maxLineWidth
= line
.length();
149 setBottomValue(strings
.size() - getHeight() + 1);
150 if (getBottomValue() < 0) {
154 setRightValue(maxLineWidth
- getWidth() + 1);
155 if (getRightValue() < 0) {
161 * Public constructor.
163 * @param parent parent widget
164 * @param strings list of strings to show
165 * @param x column relative to parent
166 * @param y row relative to parent
167 * @param width width of text area
168 * @param height height of text area
170 public TList(final TWidget parent
, final List
<String
> strings
, final int x
,
171 final int y
, final int width
, final int height
) {
173 this(parent
, strings
, x
, y
, width
, height
, null);
177 * Public constructor.
179 * @param parent parent widget
180 * @param strings list of strings to show. This is allowed to be null
181 * and set later with setList() or by subclasses.
182 * @param x column relative to parent
183 * @param y row relative to parent
184 * @param width width of text area
185 * @param height height of text area
186 * @param enterAction action to perform when an item is selected
188 public TList(final TWidget parent
, final List
<String
> strings
, final int x
,
189 final int y
, final int width
, final int height
,
190 final TAction enterAction
) {
192 super(parent
, x
, y
, width
, height
);
193 this.enterAction
= enterAction
;
194 this.strings
= new ArrayList
<String
>();
195 if (strings
!= null) {
196 this.strings
.addAll(strings
);
199 hScroller
= new THScroller(this, 0, getHeight() - 1, getWidth() - 1);
200 vScroller
= new TVScroller(this, getWidth() - 1, 0, getHeight() - 1);
205 * Public constructor.
207 * @param parent parent widget
208 * @param strings list of strings to show. This is allowed to be null
209 * and set later with setList() or by subclasses.
210 * @param x column relative to parent
211 * @param y row relative to parent
212 * @param width width of text area
213 * @param height height of text area
214 * @param enterAction action to perform when an item is selected
215 * @param moveAction action to perform when the user navigates to a new
216 * item with arrow/page keys
218 public TList(final TWidget parent
, final List
<String
> strings
, final int x
,
219 final int y
, final int width
, final int height
,
220 final TAction enterAction
, final TAction moveAction
) {
222 super(parent
, x
, y
, width
, height
);
223 this.enterAction
= enterAction
;
224 this.moveAction
= moveAction
;
225 this.strings
= new ArrayList
<String
>();
226 if (strings
!= null) {
227 this.strings
.addAll(strings
);
230 hScroller
= new THScroller(this, 0, getHeight() - 1, getWidth() - 1);
231 vScroller
= new TVScroller(this, getWidth() - 1, 0, getHeight() - 1);
236 * Draw the files list.
240 CellAttributes color
= null;
241 int begin
= getVerticalValue();
243 for (int i
= begin
; i
< strings
.size(); i
++) {
244 String line
= strings
.get(i
);
245 if (getHorizontalValue() < line
.length()) {
246 line
= line
.substring(getHorizontalValue());
250 if (i
== selectedString
) {
251 color
= getTheme().getColor("tlist.selected");
252 } else if (isAbsoluteActive()) {
253 color
= getTheme().getColor("tlist");
255 color
= getTheme().getColor("tlist.inactive");
257 String formatString
= "%-" + Integer
.toString(getWidth() - 1) + "s";
258 getScreen().putStringXY(0, topY
, String
.format(formatString
, line
),
261 if (topY
>= getHeight() - 1) {
266 if (isAbsoluteActive()) {
267 color
= getTheme().getColor("tlist");
269 color
= getTheme().getColor("tlist.inactive");
272 // Pad the rest with blank lines
273 for (int i
= topY
; i
< getHeight() - 1; i
++) {
274 getScreen().hLineXY(0, i
, getWidth() - 1, ' ', color
);
279 * Handle mouse press events.
281 * @param mouse mouse button press event
284 public void onMouseDown(final TMouseEvent mouse
) {
285 if (mouse
.isMouseWheelUp()) {
289 if (mouse
.isMouseWheelDown()) {
294 if ((mouse
.getX() < getWidth() - 1)
295 && (mouse
.getY() < getHeight() - 1)) {
296 if (getVerticalValue() + mouse
.getY() < strings
.size()) {
297 selectedString
= getVerticalValue() + mouse
.getY();
304 super.onMouseDown(mouse
);
310 * @param keypress keystroke event
313 public void onKeypress(final TKeypressEvent keypress
) {
314 if (keypress
.equals(kbLeft
)) {
315 horizontalDecrement();
316 } else if (keypress
.equals(kbRight
)) {
317 horizontalIncrement();
318 } else if (keypress
.equals(kbUp
)) {
319 if (strings
.size() > 0) {
320 if (selectedString
>= 0) {
321 if (selectedString
> 0) {
322 if (selectedString
- getVerticalValue() == 0) {
328 selectedString
= strings
.size() - 1;
331 if (selectedString
>= 0) {
334 } else if (keypress
.equals(kbDown
)) {
335 if (strings
.size() > 0) {
336 if (selectedString
>= 0) {
337 if (selectedString
< strings
.size() - 1) {
339 if (selectedString
- getVerticalValue() == getHeight() - 1) {
347 if (selectedString
>= 0) {
350 } else if (keypress
.equals(kbPgUp
)) {
351 bigVerticalDecrement();
352 if (selectedString
>= 0) {
353 selectedString
-= getHeight() - 1;
354 if (selectedString
< 0) {
358 if (selectedString
>= 0) {
361 } else if (keypress
.equals(kbPgDn
)) {
362 bigVerticalIncrement();
363 if (selectedString
>= 0) {
364 selectedString
+= getHeight() - 1;
365 if (selectedString
> strings
.size() - 1) {
366 selectedString
= strings
.size() - 1;
369 if (selectedString
>= 0) {
372 } else if (keypress
.equals(kbHome
)) {
374 if (strings
.size() > 0) {
377 if (selectedString
>= 0) {
380 } else if (keypress
.equals(kbEnd
)) {
382 if (strings
.size() > 0) {
383 selectedString
= strings
.size() - 1;
385 if (selectedString
>= 0) {
388 } else if (keypress
.equals(kbTab
)) {
389 getParent().switchWidget(true);
390 } else if (keypress
.equals(kbShiftTab
) || keypress
.equals(kbBackTab
)) {
391 getParent().switchWidget(false);
392 } else if (keypress
.equals(kbEnter
)) {
393 if (selectedString
>= 0) {
397 // Pass other keys (tab etc.) on
398 super.onKeypress(keypress
);