Commit | Line | Data |
---|---|---|
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 | */ | |
29 | package jexer; | |
30 | ||
31 | import java.io.File; | |
32 | import java.util.ArrayList; | |
a69ed767 | 33 | import java.util.HashMap; |
0d47c546 | 34 | import java.util.List; |
a69ed767 | 35 | import java.util.Map; |
0d47c546 | 36 | |
e820d5dd KL |
37 | import jexer.bits.StringUtils; |
38 | ||
0d47c546 KL |
39 | /** |
40 | * TDirectoryList shows the files within a directory. | |
41 | */ | |
051e2913 | 42 | public class TDirectoryList extends TList { |
0d47c546 | 43 | |
615a0d99 KL |
44 | // ------------------------------------------------------------------------ |
45 | // Variables -------------------------------------------------------------- | |
46 | // ------------------------------------------------------------------------ | |
47 | ||
0d47c546 KL |
48 | /** |
49 | * Files in the directory. | |
50 | */ | |
a69ed767 | 51 | private Map<String, File> files; |
0d47c546 | 52 | |
0d47c546 KL |
53 | /** |
54 | * Root path containing files to display. | |
55 | */ | |
a043164f KL |
56 | private File path; |
57 | ||
a69ed767 KL |
58 | /** |
59 | * The list of filters that a file must match in order to be displayed. | |
60 | */ | |
61 | private List<String> filters; | |
62 | ||
615a0d99 KL |
63 | // ------------------------------------------------------------------------ |
64 | // Constructors ----------------------------------------------------------- | |
65 | // ------------------------------------------------------------------------ | |
66 | ||
67 | /** | |
68 | * Public constructor. | |
69 | * | |
70 | * @param parent parent widget | |
71 | * @param path directory path, must be a directory | |
72 | * @param x column relative to parent | |
73 | * @param y row relative to parent | |
74 | * @param width width of text area | |
75 | * @param height height of text area | |
76 | */ | |
77 | public TDirectoryList(final TWidget parent, final String path, final int x, | |
78 | final int y, final int width, final int height) { | |
79 | ||
a69ed767 | 80 | this(parent, path, x, y, width, height, null, null, null); |
615a0d99 KL |
81 | } |
82 | ||
83 | /** | |
84 | * Public constructor. | |
85 | * | |
86 | * @param parent parent widget | |
87 | * @param path directory path, must be a directory | |
88 | * @param x column relative to parent | |
89 | * @param y row relative to parent | |
90 | * @param width width of text area | |
91 | * @param height height of text area | |
a69ed767 KL |
92 | * @param action action to perform when an item is selected (enter or |
93 | * double-click) | |
615a0d99 KL |
94 | */ |
95 | public TDirectoryList(final TWidget parent, final String path, final int x, | |
96 | final int y, final int width, final int height, final TAction action) { | |
97 | ||
a69ed767 KL |
98 | this(parent, path, x, y, width, height, action, null, null); |
99 | } | |
100 | ||
101 | /** | |
102 | * Public constructor. | |
103 | * | |
104 | * @param parent parent widget | |
105 | * @param path directory path, must be a directory | |
106 | * @param x column relative to parent | |
107 | * @param y row relative to parent | |
108 | * @param width width of text area | |
109 | * @param height height of text area | |
110 | * @param action action to perform when an item is selected (enter or | |
111 | * double-click) | |
112 | * @param singleClickAction action to perform when an item is selected | |
113 | * (single-click) | |
114 | */ | |
115 | public TDirectoryList(final TWidget parent, final String path, final int x, | |
116 | final int y, final int width, final int height, final TAction action, | |
117 | final TAction singleClickAction) { | |
118 | ||
119 | this(parent, path, x, y, width, height, action, singleClickAction, | |
120 | null); | |
121 | } | |
122 | ||
123 | /** | |
124 | * Public constructor. | |
125 | * | |
126 | * @param parent parent widget | |
127 | * @param path directory path, must be a directory | |
128 | * @param x column relative to parent | |
129 | * @param y row relative to parent | |
130 | * @param width width of text area | |
131 | * @param height height of text area | |
132 | * @param action action to perform when an item is selected (enter or | |
133 | * double-click) | |
134 | * @param singleClickAction action to perform when an item is selected | |
135 | * (single-click) | |
136 | * @param filters a list of strings that files must match to be displayed | |
137 | */ | |
138 | public TDirectoryList(final TWidget parent, final String path, final int x, | |
139 | final int y, final int width, final int height, final TAction action, | |
140 | final TAction singleClickAction, final List<String> filters) { | |
141 | ||
615a0d99 | 142 | super(parent, null, x, y, width, height, action); |
a69ed767 KL |
143 | files = new HashMap<String, File>(); |
144 | this.filters = filters; | |
145 | this.singleClickAction = singleClickAction; | |
146 | ||
615a0d99 KL |
147 | setPath(path); |
148 | } | |
149 | ||
150 | // ------------------------------------------------------------------------ | |
151 | // TList ------------------------------------------------------------------ | |
152 | // ------------------------------------------------------------------------ | |
153 | ||
154 | // ------------------------------------------------------------------------ | |
155 | // TDirectoryList --------------------------------------------------------- | |
156 | // ------------------------------------------------------------------------ | |
157 | ||
a043164f KL |
158 | /** |
159 | * Set the new path to display. | |
160 | * | |
161 | * @param path new path to list files for | |
162 | */ | |
329fd62e | 163 | public void setPath(final String path) { |
a043164f | 164 | this.path = new File(path); |
3649b921 KL |
165 | |
166 | List<String> newStrings = new ArrayList<String>(); | |
167 | files.clear(); | |
168 | ||
169 | // Build a list of files in this directory | |
170 | File [] newFiles = this.path.listFiles(); | |
171 | if (newFiles != null) { | |
172 | for (int i = 0; i < newFiles.length; i++) { | |
173 | if (newFiles[i].getName().startsWith(".")) { | |
174 | continue; | |
175 | } | |
176 | if (newFiles[i].isDirectory()) { | |
177 | continue; | |
178 | } | |
a69ed767 KL |
179 | if (filters != null) { |
180 | for (String pattern: filters) { | |
181 | ||
182 | /* | |
183 | System.err.println("newFiles[i] " + | |
184 | newFiles[i].getName() + " " + pattern + | |
185 | " " + newFiles[i].getName().matches(pattern)); | |
186 | */ | |
187 | ||
188 | if (newFiles[i].getName().matches(pattern)) { | |
189 | String key = renderFile(newFiles[i]); | |
190 | files.put(key, newFiles[i]); | |
191 | newStrings.add(key); | |
192 | break; | |
193 | } | |
194 | } | |
195 | } else { | |
196 | String key = renderFile(newFiles[i]); | |
197 | files.put(key, newFiles[i]); | |
198 | newStrings.add(key); | |
199 | } | |
3649b921 KL |
200 | } |
201 | } | |
202 | setList(newStrings); | |
fe0770f9 KL |
203 | |
204 | // Select the first entry | |
205 | if (getMaxSelectedIndex() >= 0) { | |
206 | setSelectedIndex(0); | |
207 | } | |
a043164f KL |
208 | } |
209 | ||
210 | /** | |
211 | * Get the path that is being displayed. | |
212 | * | |
213 | * @return the path | |
214 | */ | |
215 | public File getPath() { | |
a69ed767 | 216 | path = files.get(getSelected()); |
a043164f KL |
217 | return path; |
218 | } | |
0d47c546 | 219 | |
0d47c546 KL |
220 | /** |
221 | * Format one of the entries for drawing on the screen. | |
222 | * | |
a69ed767 | 223 | * @param file the File |
0d47c546 KL |
224 | * @return the line to draw |
225 | */ | |
a69ed767 | 226 | private String renderFile(final File file) { |
0d47c546 | 227 | String name = file.getName(); |
e820d5dd | 228 | if (StringUtils.width(name) > 20) { |
0d47c546 KL |
229 | name = name.substring(0, 17) + "..."; |
230 | } | |
231 | return String.format("%-20s %5dk", name, (file.length() / 1024)); | |
232 | } | |
233 | ||
0d47c546 | 234 | } |