LICENSE CHANGED TO MIT
[nikiroo-utils.git] / src / jexer / TDirectoryList.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2016 Kevin Lamonte
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
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.
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
35 /**
36 * TDirectoryList shows the files within a directory.
37 */
38 public final class TDirectoryList extends TList {
39
40 /**
41 * Files in the directory.
42 */
43 private List<File> files;
44
45 /**
46 * Root path containing files to display.
47 */
48 private File path;
49
50 /**
51 * Set the new path to display.
52 *
53 * @param path new path to list files for
54 */
55 public void setPath(final String path) {
56 this.path = new File(path);
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);
76 }
77
78 /**
79 * Get the path that is being displayed.
80 *
81 * @return the path
82 */
83 public File getPath() {
84 path = files.get(getSelectedIndex());
85 return path;
86 }
87
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 */
94 private String renderFile(final int index) {
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
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
133 super(parent, null, x, y, width, height, action);
134 files = new ArrayList<File>();
135 setPath(path);
136 }
137
138 }