Not dead note
[fanfix.git] / src / jexer / TDirectoryList.java
CommitLineData
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 */
29package jexer;
30
31import java.io.File;
32import java.util.ArrayList;
615a0d99 33import java.util.Collections;
0d47c546
KL
34import java.util.List;
35
0d47c546
KL
36/**
37 * TDirectoryList shows the files within a directory.
38 */
051e2913 39public class TDirectoryList extends TList {
0d47c546 40
615a0d99
KL
41 // ------------------------------------------------------------------------
42 // Variables --------------------------------------------------------------
43 // ------------------------------------------------------------------------
44
0d47c546
KL
45 /**
46 * Files in the directory.
47 */
48 private List<File> files;
49
0d47c546
KL
50 /**
51 * Root path containing files to display.
52 */
a043164f
KL
53 private File path;
54
615a0d99
KL
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
a043164f
KL
102 /**
103 * Set the new path to display.
104 *
105 * @param path new path to list files for
106 */
329fd62e 107 public void setPath(final String path) {
a043164f 108 this.path = new File(path);
3649b921
KL
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 }
615a0d99 127 Collections.sort(newStrings);
3649b921 128 setList(newStrings);
fe0770f9
KL
129
130 // Select the first entry
131 if (getMaxSelectedIndex() >= 0) {
132 setSelectedIndex(0);
133 }
a043164f
KL
134 }
135
136 /**
137 * Get the path that is being displayed.
138 *
139 * @return the path
140 */
141 public File getPath() {
3649b921 142 path = files.get(getSelectedIndex());
a043164f
KL
143 return path;
144 }
0d47c546 145
0d47c546
KL
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 */
329fd62e 152 private String renderFile(final int index) {
0d47c546
KL
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
0d47c546 161}