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]
32 import java
.util
.ArrayList
;
33 import java
.util
.HashMap
;
34 import java
.util
.List
;
38 * TDirectoryList shows the files within a directory.
40 public class TDirectoryList
extends TList
{
42 // ------------------------------------------------------------------------
43 // Variables --------------------------------------------------------------
44 // ------------------------------------------------------------------------
47 * Files in the directory.
49 private Map
<String
, File
> files
;
52 * Root path containing files to display.
57 * The list of filters that a file must match in order to be displayed.
59 private List
<String
> filters
;
61 // ------------------------------------------------------------------------
62 // Constructors -----------------------------------------------------------
63 // ------------------------------------------------------------------------
68 * @param parent parent widget
69 * @param path directory path, must be a directory
70 * @param x column relative to parent
71 * @param y row relative to parent
72 * @param width width of text area
73 * @param height height of text area
75 public TDirectoryList(final TWidget parent
, final String path
, final int x
,
76 final int y
, final int width
, final int height
) {
78 this(parent
, path
, x
, y
, width
, height
, null, null, null);
84 * @param parent parent widget
85 * @param path directory path, must be a directory
86 * @param x column relative to parent
87 * @param y row relative to parent
88 * @param width width of text area
89 * @param height height of text area
90 * @param action action to perform when an item is selected (enter or
93 public TDirectoryList(final TWidget parent
, final String path
, final int x
,
94 final int y
, final int width
, final int height
, final TAction action
) {
96 this(parent
, path
, x
, y
, width
, height
, action
, null, null);
100 * Public constructor.
102 * @param parent parent widget
103 * @param path directory path, must be a directory
104 * @param x column relative to parent
105 * @param y row relative to parent
106 * @param width width of text area
107 * @param height height of text area
108 * @param action action to perform when an item is selected (enter or
110 * @param singleClickAction action to perform when an item is selected
113 public TDirectoryList(final TWidget parent
, final String path
, final int x
,
114 final int y
, final int width
, final int height
, final TAction action
,
115 final TAction singleClickAction
) {
117 this(parent
, path
, x
, y
, width
, height
, action
, singleClickAction
,
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 (enter or
132 * @param singleClickAction action to perform when an item is selected
134 * @param filters a list of strings that files must match to be displayed
136 public TDirectoryList(final TWidget parent
, final String path
, final int x
,
137 final int y
, final int width
, final int height
, final TAction action
,
138 final TAction singleClickAction
, final List
<String
> filters
) {
140 super(parent
, null, x
, y
, width
, height
, action
);
141 files
= new HashMap
<String
, File
>();
142 this.filters
= filters
;
143 this.singleClickAction
= singleClickAction
;
148 // ------------------------------------------------------------------------
149 // TList ------------------------------------------------------------------
150 // ------------------------------------------------------------------------
152 // ------------------------------------------------------------------------
153 // TDirectoryList ---------------------------------------------------------
154 // ------------------------------------------------------------------------
157 * Set the new path to display.
159 * @param path new path to list files for
161 public void setPath(final String path
) {
162 this.path
= new File(path
);
164 List
<String
> newStrings
= new ArrayList
<String
>();
167 // Build a list of files in this directory
168 File
[] newFiles
= this.path
.listFiles();
169 if (newFiles
!= null) {
170 for (int i
= 0; i
< newFiles
.length
; i
++) {
171 if (newFiles
[i
].getName().startsWith(".")) {
174 if (newFiles
[i
].isDirectory()) {
177 if (filters
!= null) {
178 for (String pattern
: filters
) {
181 System.err.println("newFiles[i] " +
182 newFiles[i].getName() + " " + pattern +
183 " " + newFiles[i].getName().matches(pattern));
186 if (newFiles
[i
].getName().matches(pattern
)) {
187 String key
= renderFile(newFiles
[i
]);
188 files
.put(key
, newFiles
[i
]);
194 String key
= renderFile(newFiles
[i
]);
195 files
.put(key
, newFiles
[i
]);
202 // Select the first entry
203 if (getMaxSelectedIndex() >= 0) {
209 * Get the path that is being displayed.
213 public File
getPath() {
214 path
= files
.get(getSelected());
219 * Format one of the entries for drawing on the screen.
221 * @param file the File
222 * @return the line to draw
224 private String
renderFile(final File file
) {
225 String name
= file
.getName();
226 if (name
.length() > 20) {
227 name
= name
.substring(0, 17) + "...";
229 return String
.format("%-20s %5dk", name
, (file
.length() / 1024));