| 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.List; |
| 34 | |
| 35 | /** |
| 36 | * TDirectoryList shows the files within a directory. |
| 37 | */ |
| 38 | public 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 | // Select the first entry |
| 78 | if (getMaxSelectedIndex() >= 0) { |
| 79 | setSelectedIndex(0); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Get the path that is being displayed. |
| 85 | * |
| 86 | * @return the path |
| 87 | */ |
| 88 | public File getPath() { |
| 89 | path = files.get(getSelectedIndex()); |
| 90 | return path; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Format one of the entries for drawing on the screen. |
| 95 | * |
| 96 | * @param index index into files |
| 97 | * @return the line to draw |
| 98 | */ |
| 99 | private String renderFile(final int index) { |
| 100 | File file = files.get(index); |
| 101 | String name = file.getName(); |
| 102 | if (name.length() > 20) { |
| 103 | name = name.substring(0, 17) + "..."; |
| 104 | } |
| 105 | return String.format("%-20s %5dk", name, (file.length() / 1024)); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Public constructor. |
| 110 | * |
| 111 | * @param parent parent widget |
| 112 | * @param path directory path, must be a directory |
| 113 | * @param x column relative to parent |
| 114 | * @param y row relative to parent |
| 115 | * @param width width of text area |
| 116 | * @param height height of text area |
| 117 | */ |
| 118 | public TDirectoryList(final TWidget parent, final String path, final int x, |
| 119 | final int y, final int width, final int height) { |
| 120 | |
| 121 | this(parent, path, x, y, width, height, null); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Public constructor. |
| 126 | * |
| 127 | * @param parent parent widget |
| 128 | * @param path directory path, must be a directory |
| 129 | * @param x column relative to parent |
| 130 | * @param y row relative to parent |
| 131 | * @param width width of text area |
| 132 | * @param height height of text area |
| 133 | * @param action action to perform when an item is selected |
| 134 | */ |
| 135 | public TDirectoryList(final TWidget parent, final String path, final int x, |
| 136 | final int y, final int width, final int height, final TAction action) { |
| 137 | |
| 138 | super(parent, null, x, y, width, height, action); |
| 139 | files = new ArrayList<File>(); |
| 140 | setPath(path); |
| 141 | } |
| 142 | |
| 143 | } |