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
;
38 * TDirectoryList shows the files within a directory.
40 public final class TDirectoryList
extends TList
{
43 * Files in the directory.
45 private List
<File
> files
;
48 * Root path containing files to display.
53 * Set the new path to display.
55 * @param path new path to list files for
57 public void setPath(final String path
) {
58 this.path
= new File(path
);
60 List
<String
> newStrings
= new ArrayList
<String
>();
63 // Build a list of files in this directory
64 File
[] newFiles
= this.path
.listFiles();
65 if (newFiles
!= null) {
66 for (int i
= 0; i
< newFiles
.length
; i
++) {
67 if (newFiles
[i
].getName().startsWith(".")) {
70 if (newFiles
[i
].isDirectory()) {
73 files
.add(newFiles
[i
]);
74 newStrings
.add(renderFile(files
.size() - 1));
81 * Get the path that is being displayed.
85 public File
getPath() {
86 path
= files
.get(getSelectedIndex());
91 * Format one of the entries for drawing on the screen.
93 * @param index index into files
94 * @return the line to draw
96 private String
renderFile(final int index
) {
97 File file
= files
.get(index
);
98 String name
= file
.getName();
99 if (name
.length() > 20) {
100 name
= name
.substring(0, 17) + "...";
102 return String
.format("%-20s %5dk", name
, (file
.length() / 1024));
106 * Public constructor.
108 * @param parent parent widget
109 * @param path directory path, must be a directory
110 * @param x column relative to parent
111 * @param y row relative to parent
112 * @param width width of text area
113 * @param height height of text area
115 public TDirectoryList(final TWidget parent
, final String path
, final int x
,
116 final int y
, final int width
, final int height
) {
118 this(parent
, path
, x
, y
, width
, height
, null);
122 * Public constructor.
124 * @param parent parent widget
125 * @param path directory path, must be a directory
126 * @param x column relative to parent
127 * @param y row relative to parent
128 * @param width width of text area
129 * @param height height of text area
130 * @param action action to perform when an item is selected
132 public TDirectoryList(final TWidget parent
, final String path
, final int x
,
133 final int y
, final int width
, final int height
, final TAction action
) {
135 super(parent
, null, x
, y
, width
, height
, action
);
136 files
= new ArrayList
<File
>();