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