Commit | Line | Data |
---|---|---|
daa4106c | 1 | /* |
0d47c546 KL |
2 | * Jexer - Java Text User Interface |
3 | * | |
4 | * License: LGPLv3 or later | |
5 | * | |
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. | |
9 | * | |
10 | * Copyright (C) 2015 Kevin Lamonte | |
11 | * | |
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. | |
16 | * | |
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. | |
21 | * | |
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 | |
26 | * 02110-1301 USA | |
27 | * | |
28 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
29 | * @version 1 | |
30 | */ | |
31 | package jexer; | |
32 | ||
33 | import java.io.File; | |
34 | import java.util.ArrayList; | |
35 | import java.util.List; | |
36 | ||
0d47c546 KL |
37 | /** |
38 | * TDirectoryList shows the files within a directory. | |
39 | */ | |
3649b921 | 40 | public final class TDirectoryList extends TList { |
0d47c546 KL |
41 | |
42 | /** | |
43 | * Files in the directory. | |
44 | */ | |
45 | private List<File> files; | |
46 | ||
0d47c546 KL |
47 | /** |
48 | * Root path containing files to display. | |
49 | */ | |
a043164f KL |
50 | private File path; |
51 | ||
52 | /** | |
53 | * Set the new path to display. | |
54 | * | |
55 | * @param path new path to list files for | |
56 | */ | |
329fd62e | 57 | public void setPath(final String path) { |
a043164f | 58 | this.path = new File(path); |
3649b921 KL |
59 | |
60 | List<String> newStrings = new ArrayList<String>(); | |
61 | files.clear(); | |
62 | ||
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(".")) { | |
68 | continue; | |
69 | } | |
70 | if (newFiles[i].isDirectory()) { | |
71 | continue; | |
72 | } | |
73 | files.add(newFiles[i]); | |
74 | newStrings.add(renderFile(files.size() - 1)); | |
75 | } | |
76 | } | |
77 | setList(newStrings); | |
a043164f KL |
78 | } |
79 | ||
80 | /** | |
81 | * Get the path that is being displayed. | |
82 | * | |
83 | * @return the path | |
84 | */ | |
85 | public File getPath() { | |
3649b921 | 86 | path = files.get(getSelectedIndex()); |
a043164f KL |
87 | return path; |
88 | } | |
0d47c546 | 89 | |
0d47c546 KL |
90 | /** |
91 | * Format one of the entries for drawing on the screen. | |
92 | * | |
93 | * @param index index into files | |
94 | * @return the line to draw | |
95 | */ | |
329fd62e | 96 | private String renderFile(final int index) { |
0d47c546 KL |
97 | File file = files.get(index); |
98 | String name = file.getName(); | |
99 | if (name.length() > 20) { | |
100 | name = name.substring(0, 17) + "..."; | |
101 | } | |
102 | return String.format("%-20s %5dk", name, (file.length() / 1024)); | |
103 | } | |
104 | ||
0d47c546 KL |
105 | /** |
106 | * Public constructor. | |
107 | * | |
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 | |
114 | */ | |
115 | public TDirectoryList(final TWidget parent, final String path, final int x, | |
116 | final int y, final int width, final int height) { | |
117 | ||
118 | this(parent, path, x, y, width, height, 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 | |
131 | */ | |
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) { | |
134 | ||
3649b921 | 135 | super(parent, null, x, y, width, height, action); |
0d47c546 | 136 | files = new ArrayList<File>(); |
3649b921 | 137 | setPath(path); |
0d47c546 KL |
138 | } |
139 | ||
140 | } |