2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2017 Kevin Lamonte
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
32 import java
.util
.ArrayList
;
33 import java
.util
.Collections
;
34 import java
.util
.List
;
37 * TDirectoryList shows the files within a directory.
39 public class TDirectoryList
extends TList
{
41 // ------------------------------------------------------------------------
42 // Variables --------------------------------------------------------------
43 // ------------------------------------------------------------------------
46 * Files in the directory.
48 private List
<File
> files
;
51 * Root path containing files to display.
55 // ------------------------------------------------------------------------
56 // Constructors -----------------------------------------------------------
57 // ------------------------------------------------------------------------
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
69 public TDirectoryList(final TWidget parent
, final String path
, final int x
,
70 final int y
, final int width
, final int height
) {
72 this(parent
, path
, x
, y
, width
, height
, null);
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
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
) {
89 super(parent
, null, x
, y
, width
, height
, action
);
90 files
= new ArrayList
<File
>();
94 // ------------------------------------------------------------------------
95 // TList ------------------------------------------------------------------
96 // ------------------------------------------------------------------------
98 // ------------------------------------------------------------------------
99 // TDirectoryList ---------------------------------------------------------
100 // ------------------------------------------------------------------------
103 * Set the new path to display.
105 * @param path new path to list files for
107 public void setPath(final String path
) {
108 this.path
= new File(path
);
110 List
<String
> newStrings
= new ArrayList
<String
>();
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(".")) {
120 if (newFiles
[i
].isDirectory()) {
123 files
.add(newFiles
[i
]);
124 newStrings
.add(renderFile(files
.size() - 1));
127 Collections
.sort(newStrings
);
130 // Select the first entry
131 if (getMaxSelectedIndex() >= 0) {
137 * Get the path that is being displayed.
141 public File
getPath() {
142 path
= files
.get(getSelectedIndex());
147 * Format one of the entries for drawing on the screen.
149 * @param index index into files
150 * @return the line to draw
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) + "...";
158 return String
.format("%-20s %5dk", name
, (file
.length() / 1024));