Prep for 2019 release
[fanfix.git] / src / jexer / TDirectoryList.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2019 Kevin Lamonte
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
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.
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29 package jexer;
30
31 import java.io.File;
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36
37 /**
38 * TDirectoryList shows the files within a directory.
39 */
40 public class TDirectoryList extends TList {
41
42 // ------------------------------------------------------------------------
43 // Variables --------------------------------------------------------------
44 // ------------------------------------------------------------------------
45
46 /**
47 * Files in the directory.
48 */
49 private Map<String, File> files;
50
51 /**
52 * Root path containing files to display.
53 */
54 private File path;
55
56 /**
57 * The list of filters that a file must match in order to be displayed.
58 */
59 private List<String> filters;
60
61 // ------------------------------------------------------------------------
62 // Constructors -----------------------------------------------------------
63 // ------------------------------------------------------------------------
64
65 /**
66 * Public constructor.
67 *
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
74 */
75 public TDirectoryList(final TWidget parent, final String path, final int x,
76 final int y, final int width, final int height) {
77
78 this(parent, path, x, y, width, height, null, null, null);
79 }
80
81 /**
82 * Public constructor.
83 *
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
91 * double-click)
92 */
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) {
95
96 this(parent, path, x, y, width, height, action, null, null);
97 }
98
99 /**
100 * Public constructor.
101 *
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
109 * double-click)
110 * @param singleClickAction action to perform when an item is selected
111 * (single-click)
112 */
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) {
116
117 this(parent, path, x, y, width, height, action, singleClickAction,
118 null);
119 }
120
121 /**
122 * Public constructor.
123 *
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
131 * double-click)
132 * @param singleClickAction action to perform when an item is selected
133 * (single-click)
134 * @param filters a list of strings that files must match to be displayed
135 */
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) {
139
140 super(parent, null, x, y, width, height, action);
141 files = new HashMap<String, File>();
142 this.filters = filters;
143 this.singleClickAction = singleClickAction;
144
145 setPath(path);
146 }
147
148 // ------------------------------------------------------------------------
149 // TList ------------------------------------------------------------------
150 // ------------------------------------------------------------------------
151
152 // ------------------------------------------------------------------------
153 // TDirectoryList ---------------------------------------------------------
154 // ------------------------------------------------------------------------
155
156 /**
157 * Set the new path to display.
158 *
159 * @param path new path to list files for
160 */
161 public void setPath(final String path) {
162 this.path = new File(path);
163
164 List<String> newStrings = new ArrayList<String>();
165 files.clear();
166
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(".")) {
172 continue;
173 }
174 if (newFiles[i].isDirectory()) {
175 continue;
176 }
177 if (filters != null) {
178 for (String pattern: filters) {
179
180 /*
181 System.err.println("newFiles[i] " +
182 newFiles[i].getName() + " " + pattern +
183 " " + newFiles[i].getName().matches(pattern));
184 */
185
186 if (newFiles[i].getName().matches(pattern)) {
187 String key = renderFile(newFiles[i]);
188 files.put(key, newFiles[i]);
189 newStrings.add(key);
190 break;
191 }
192 }
193 } else {
194 String key = renderFile(newFiles[i]);
195 files.put(key, newFiles[i]);
196 newStrings.add(key);
197 }
198 }
199 }
200 setList(newStrings);
201
202 // Select the first entry
203 if (getMaxSelectedIndex() >= 0) {
204 setSelectedIndex(0);
205 }
206 }
207
208 /**
209 * Get the path that is being displayed.
210 *
211 * @return the path
212 */
213 public File getPath() {
214 path = files.get(getSelected());
215 return path;
216 }
217
218 /**
219 * Format one of the entries for drawing on the screen.
220 *
221 * @param file the File
222 * @return the line to draw
223 */
224 private String renderFile(final File file) {
225 String name = file.getName();
226 if (name.length() > 20) {
227 name = name.substring(0, 17) + "...";
228 }
229 return String.format("%-20s %5dk", name, (file.length() / 1024));
230 }
231
232 }