update README
[fanfix.git] / src / jexer / TDirectoryList.java
CommitLineData
daa4106c 1/*
0d47c546
KL
2 * Jexer - Java Text User Interface
3 *
e16dda65 4 * The MIT License (MIT)
0d47c546 5 *
a69ed767 6 * Copyright (C) 2019 Kevin Lamonte
0d47c546 7 *
e16dda65
KL
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:
0d47c546 14 *
e16dda65
KL
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
0d47c546 17 *
e16dda65
KL
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.
0d47c546
KL
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29package jexer;
30
31import java.io.File;
32import java.util.ArrayList;
a69ed767 33import java.util.HashMap;
0d47c546 34import java.util.List;
a69ed767 35import java.util.Map;
0d47c546 36
0d47c546
KL
37/**
38 * TDirectoryList shows the files within a directory.
39 */
051e2913 40public class TDirectoryList extends TList {
0d47c546 41
615a0d99
KL
42 // ------------------------------------------------------------------------
43 // Variables --------------------------------------------------------------
44 // ------------------------------------------------------------------------
45
0d47c546
KL
46 /**
47 * Files in the directory.
48 */
a69ed767 49 private Map<String, File> files;
0d47c546 50
0d47c546
KL
51 /**
52 * Root path containing files to display.
53 */
a043164f
KL
54 private File path;
55
a69ed767
KL
56 /**
57 * The list of filters that a file must match in order to be displayed.
58 */
59 private List<String> filters;
60
615a0d99
KL
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
a69ed767 78 this(parent, path, x, y, width, height, null, null, null);
615a0d99
KL
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
a69ed767
KL
90 * @param action action to perform when an item is selected (enter or
91 * double-click)
615a0d99
KL
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
a69ed767
KL
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
615a0d99 140 super(parent, null, x, y, width, height, action);
a69ed767
KL
141 files = new HashMap<String, File>();
142 this.filters = filters;
143 this.singleClickAction = singleClickAction;
144
615a0d99
KL
145 setPath(path);
146 }
147
148 // ------------------------------------------------------------------------
149 // TList ------------------------------------------------------------------
150 // ------------------------------------------------------------------------
151
152 // ------------------------------------------------------------------------
153 // TDirectoryList ---------------------------------------------------------
154 // ------------------------------------------------------------------------
155
a043164f
KL
156 /**
157 * Set the new path to display.
158 *
159 * @param path new path to list files for
160 */
329fd62e 161 public void setPath(final String path) {
a043164f 162 this.path = new File(path);
3649b921
KL
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 }
a69ed767
KL
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 }
3649b921
KL
198 }
199 }
200 setList(newStrings);
fe0770f9
KL
201
202 // Select the first entry
203 if (getMaxSelectedIndex() >= 0) {
204 setSelectedIndex(0);
205 }
a043164f
KL
206 }
207
208 /**
209 * Get the path that is being displayed.
210 *
211 * @return the path
212 */
213 public File getPath() {
a69ed767 214 path = files.get(getSelected());
a043164f
KL
215 return path;
216 }
0d47c546 217
0d47c546
KL
218 /**
219 * Format one of the entries for drawing on the screen.
220 *
a69ed767 221 * @param file the File
0d47c546
KL
222 * @return the line to draw
223 */
a69ed767 224 private String renderFile(final File file) {
0d47c546
KL
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
0d47c546 232}