a2c9fe3268d5fd3ab9937f9e95256857040acf46
[nikiroo-utils.git] / src / jexer / TDirectoryList.java
1 /*
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
37 /**
38 * TDirectoryList shows the files within a directory.
39 */
40 public final class TDirectoryList extends TList {
41
42 /**
43 * Files in the directory.
44 */
45 private List<File> files;
46
47 /**
48 * Root path containing files to display.
49 */
50 private File path;
51
52 /**
53 * Set the new path to display.
54 *
55 * @param path new path to list files for
56 */
57 public void setPath(final String path) {
58 this.path = new File(path);
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);
78 }
79
80 /**
81 * Get the path that is being displayed.
82 *
83 * @return the path
84 */
85 public File getPath() {
86 path = files.get(getSelectedIndex());
87 return path;
88 }
89
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 */
96 private String renderFile(final int index) {
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
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
135 super(parent, null, x, y, width, height, action);
136 files = new ArrayList<File>();
137 setPath(path);
138 }
139
140 }