Commit | Line | Data |
---|---|---|
daa4106c | 1 | /* |
0d47c546 KL |
2 | * Jexer - Java Text User Interface |
3 | * | |
e16dda65 | 4 | * The MIT License (MIT) |
0d47c546 | 5 | * |
a2018e99 | 6 | * Copyright (C) 2017 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; | |
33 | import java.util.List; | |
34 | ||
0d47c546 KL |
35 | /** |
36 | * TDirectoryList shows the files within a directory. | |
37 | */ | |
3649b921 | 38 | public final class TDirectoryList extends TList { |
0d47c546 KL |
39 | |
40 | /** | |
41 | * Files in the directory. | |
42 | */ | |
43 | private List<File> files; | |
44 | ||
0d47c546 KL |
45 | /** |
46 | * Root path containing files to display. | |
47 | */ | |
a043164f KL |
48 | private File path; |
49 | ||
50 | /** | |
51 | * Set the new path to display. | |
52 | * | |
53 | * @param path new path to list files for | |
54 | */ | |
329fd62e | 55 | public void setPath(final String path) { |
a043164f | 56 | this.path = new File(path); |
3649b921 KL |
57 | |
58 | List<String> newStrings = new ArrayList<String>(); | |
59 | files.clear(); | |
60 | ||
61 | // Build a list of files in this directory | |
62 | File [] newFiles = this.path.listFiles(); | |
63 | if (newFiles != null) { | |
64 | for (int i = 0; i < newFiles.length; i++) { | |
65 | if (newFiles[i].getName().startsWith(".")) { | |
66 | continue; | |
67 | } | |
68 | if (newFiles[i].isDirectory()) { | |
69 | continue; | |
70 | } | |
71 | files.add(newFiles[i]); | |
72 | newStrings.add(renderFile(files.size() - 1)); | |
73 | } | |
74 | } | |
75 | setList(newStrings); | |
a043164f KL |
76 | } |
77 | ||
78 | /** | |
79 | * Get the path that is being displayed. | |
80 | * | |
81 | * @return the path | |
82 | */ | |
83 | public File getPath() { | |
3649b921 | 84 | path = files.get(getSelectedIndex()); |
a043164f KL |
85 | return path; |
86 | } | |
0d47c546 | 87 | |
0d47c546 KL |
88 | /** |
89 | * Format one of the entries for drawing on the screen. | |
90 | * | |
91 | * @param index index into files | |
92 | * @return the line to draw | |
93 | */ | |
329fd62e | 94 | private String renderFile(final int index) { |
0d47c546 KL |
95 | File file = files.get(index); |
96 | String name = file.getName(); | |
97 | if (name.length() > 20) { | |
98 | name = name.substring(0, 17) + "..."; | |
99 | } | |
100 | return String.format("%-20s %5dk", name, (file.length() / 1024)); | |
101 | } | |
102 | ||
0d47c546 KL |
103 | /** |
104 | * Public constructor. | |
105 | * | |
106 | * @param parent parent widget | |
107 | * @param path directory path, must be a directory | |
108 | * @param x column relative to parent | |
109 | * @param y row relative to parent | |
110 | * @param width width of text area | |
111 | * @param height height of text area | |
112 | */ | |
113 | public TDirectoryList(final TWidget parent, final String path, final int x, | |
114 | final int y, final int width, final int height) { | |
115 | ||
116 | this(parent, path, x, y, width, height, null); | |
117 | } | |
118 | ||
119 | /** | |
120 | * Public constructor. | |
121 | * | |
122 | * @param parent parent widget | |
123 | * @param path directory path, must be a directory | |
124 | * @param x column relative to parent | |
125 | * @param y row relative to parent | |
126 | * @param width width of text area | |
127 | * @param height height of text area | |
128 | * @param action action to perform when an item is selected | |
129 | */ | |
130 | public TDirectoryList(final TWidget parent, final String path, final int x, | |
131 | final int y, final int width, final int height, final TAction action) { | |
132 | ||
3649b921 | 133 | super(parent, null, x, y, width, height, action); |
0d47c546 | 134 | files = new ArrayList<File>(); |
3649b921 | 135 | setPath(path); |
0d47c546 KL |
136 | } |
137 | ||
138 | } |