71be2ab5d2ddaa3f97ef17488fa2a1d4b007ee0f
2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2016 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
.List
;
36 * TDirectoryList shows the files within a directory.
38 public final class TDirectoryList
extends TList
{
41 * Files in the directory.
43 private List
<File
> files
;
46 * Root path containing files to display.
51 * Set the new path to display.
53 * @param path new path to list files for
55 public void setPath(final String path
) {
56 this.path
= new File(path
);
58 List
<String
> newStrings
= new ArrayList
<String
>();
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(".")) {
68 if (newFiles
[i
].isDirectory()) {
71 files
.add(newFiles
[i
]);
72 newStrings
.add(renderFile(files
.size() - 1));
79 * Get the path that is being displayed.
83 public File
getPath() {
84 path
= files
.get(getSelectedIndex());
89 * Format one of the entries for drawing on the screen.
91 * @param index index into files
92 * @return the line to draw
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) + "...";
100 return String
.format("%-20s %5dk", name
, (file
.length() / 1024));
104 * Public constructor.
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
113 public TDirectoryList(final TWidget parent
, final String path
, final int x
,
114 final int y
, final int width
, final int height
) {
116 this(parent
, path
, x
, y
, width
, height
, null);
120 * Public constructor.
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
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
) {
133 super(parent
, null, x
, y
, width
, height
, action
);
134 files
= new ArrayList
<File
>();