3a09bf38bc74dc1dda026f4089dde593fee94a32
2 * Jexer - Java Text User Interface
4 * License: LGPLv3 or later
6 * This module is licensed under the GNU Lesser General Public License
7 * Version 3. Please see the file "COPYING" in this directory for more
8 * information about the GNU Lesser General Public License Version 3.
10 * Copyright (C) 2015 Kevin Lamonte
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 3 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this program; if not, see
24 * http://www.gnu.org/licenses/, or write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
34 import java
.util
.ArrayList
;
35 import java
.util
.List
;
37 import jexer
.bits
.CellAttributes
;
38 import jexer
.event
.TKeypressEvent
;
39 import jexer
.event
.TMouseEvent
;
40 import static jexer
.TKeypress
.*;
43 * TDirectoryList shows the files within a directory.
45 public class TDirectoryList
extends TWidget
{
48 * Files in the directory.
50 private List
<File
> files
;
55 private int selectedFile
= -1;
58 * Root path containing files to display.
65 private TVScroller vScroller
;
68 * Horizontal scrollbar.
70 private THScroller hScroller
;
73 * Maximum width of a single line.
75 private int maxLineWidth
;
78 * The action to perform when the user selects an item.
80 private TAction action
= null;
83 * Perform user selection action.
85 public void dispatch() {
86 assert (selectedFile
>= 0);
87 assert (selectedFile
< files
.size());
94 * Format one of the entries for drawing on the screen.
96 * @param index index into files
97 * @return the line to draw
99 private String
renderFile(int index
) {
100 File file
= files
.get(index
);
101 String name
= file
.getName();
102 if (name
.length() > 20) {
103 name
= name
.substring(0, 17) + "...";
105 return String
.format("%-20s %5dk", name
, (file
.length() / 1024));
109 * Resize for a new width/height.
111 public void reflow() {
118 // Build a list of files in this directory
119 File
[] newFiles
= path
.listFiles();
120 for (int i
= 0; i
< newFiles
.length
; i
++) {
121 if (newFiles
[i
].getName().startsWith(".")) {
124 if (newFiles
[i
].isDirectory()) {
127 files
.add(newFiles
[i
]);
130 for (int i
= 0; i
< files
.size(); i
++) {
131 String line
= renderFile(i
);
132 if (line
.length() > maxLineWidth
) {
133 maxLineWidth
= line
.length();
138 if (vScroller
== null) {
139 vScroller
= new TVScroller(this, getWidth() - 1, 0,
142 vScroller
.setX(getWidth() - 1);
143 vScroller
.setHeight(getHeight() - 1);
145 vScroller
.setBottomValue(files
.size() - getHeight() - 1);
146 vScroller
.setTopValue(0);
147 vScroller
.setValue(0);
148 if (vScroller
.getBottomValue() < 0) {
149 vScroller
.setBottomValue(0);
151 vScroller
.setBigChange(getHeight() - 1);
154 if (hScroller
== null) {
155 hScroller
= new THScroller(this, 0, getHeight() - 1,
158 hScroller
.setY(getHeight() - 1);
159 hScroller
.setWidth(getWidth() - 1);
161 hScroller
.setRightValue(maxLineWidth
- getWidth() + 1);
162 hScroller
.setLeftValue(0);
163 hScroller
.setValue(0);
164 if (hScroller
.getRightValue() < 0) {
165 hScroller
.setRightValue(0);
167 hScroller
.setBigChange(getWidth() - 1);
171 * Public constructor.
173 * @param parent parent widget
174 * @param path directory path, must be a directory
175 * @param x column relative to parent
176 * @param y row relative to parent
177 * @param width width of text area
178 * @param height height of text area
180 public TDirectoryList(final TWidget parent
, final String path
, final int x
,
181 final int y
, final int width
, final int height
) {
183 this(parent
, path
, x
, y
, width
, height
, null);
187 * Public constructor.
189 * @param parent parent widget
190 * @param path directory path, must be a directory
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 action action to perform when an item is selected
197 public TDirectoryList(final TWidget parent
, final String path
, final int x
,
198 final int y
, final int width
, final int height
, final TAction action
) {
200 this.path
= new File(path
);
201 this.action
= action
;
202 files
= new ArrayList
<File
>();
207 * Draw the files list.
211 CellAttributes color
= null;
212 int begin
= vScroller
.getValue();
214 for (int i
= begin
; i
< files
.size() - 1; i
++) {
215 String line
= renderFile(i
);
216 if (hScroller
.getValue() < line
.length()) {
217 line
= line
.substring(hScroller
.getValue());
221 if (i
== selectedFile
) {
222 color
= getTheme().getColor("tdirectorylist.selected");
223 } else if (isAbsoluteActive()) {
224 color
= getTheme().getColor("tdirectorylist");
226 color
= getTheme().getColor("tdirectorylist.inactive");
228 String formatString
= "%-" + Integer
.toString(getWidth() - 1) + "s";
229 getScreen().putStringXY(0, topY
, String
.format(formatString
, line
),
232 if (topY
>= getHeight() - 1) {
237 // Pad the rest with blank lines
238 for (int i
= topY
; i
< getHeight() - 1; i
++) {
239 getScreen().hLineXY(0, i
, getWidth() - 1, ' ', color
);
244 * Handle mouse press events.
246 * @param mouse mouse button press event
249 public void onMouseDown(final TMouseEvent mouse
) {
250 if (mouse
.isMouseWheelUp()) {
251 vScroller
.decrement();
254 if (mouse
.isMouseWheelDown()) {
255 vScroller
.increment();
259 if ((mouse
.getX() < getWidth() - 1)
260 && (mouse
.getY() < getHeight() - 1)) {
261 if (vScroller
.getValue() + mouse
.getY() < files
.size()) {
262 selectedFile
= vScroller
.getValue() + mouse
.getY();
264 path
= files
.get(selectedFile
);
270 super.onMouseDown(mouse
);
274 * Handle mouse release events.
276 * @param mouse mouse button release event
279 public void onMouseUp(final TMouseEvent mouse
) {
281 super.onMouseDown(mouse
);
287 * @param keypress keystroke event
290 public void onKeypress(final TKeypressEvent keypress
) {
291 if (keypress
.equals(kbLeft
)) {
292 hScroller
.decrement();
293 } else if (keypress
.equals(kbRight
)) {
294 hScroller
.increment();
295 } else if (keypress
.equals(kbUp
)) {
296 vScroller
.decrement();
297 } else if (keypress
.equals(kbDown
)) {
298 vScroller
.increment();
299 } else if (keypress
.equals(kbPgUp
)) {
300 vScroller
.bigDecrement();
301 } else if (keypress
.equals(kbPgDn
)) {
302 vScroller
.bigIncrement();
303 } else if (keypress
.equals(kbHome
)) {
305 } else if (keypress
.equals(kbEnd
)) {
306 vScroller
.toBottom();
308 // Pass other keys (tab etc.) on
309 super.onKeypress(keypress
);