| 1 | /** |
| 2 | * Jexer - Java Text User Interface |
| 3 | * |
| 4 | * License: LGPLv3 or later |
| 5 | * |
| 6 | * This module is licensed under the GNU Lesser General Public License |
| 7 | * Version 3. Please see the file "COPYING" in this directory for more |
| 8 | * information about the GNU Lesser General Public License Version 3. |
| 9 | * |
| 10 | * Copyright (C) 2015 Kevin Lamonte |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU Lesser General Public License |
| 14 | * as published by the Free Software Foundation; either version 3 of |
| 15 | * the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, but |
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 | * General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU Lesser General Public |
| 23 | * License along with this program; if not, see |
| 24 | * http://www.gnu.org/licenses/, or write to the Free Software |
| 25 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 26 | * 02110-1301 USA |
| 27 | * |
| 28 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] |
| 29 | * @version 1 |
| 30 | */ |
| 31 | package jexer; |
| 32 | |
| 33 | import java.io.File; |
| 34 | import java.io.IOException; |
| 35 | import java.util.Collections; |
| 36 | import java.util.List; |
| 37 | import java.util.LinkedList; |
| 38 | |
| 39 | /** |
| 40 | * TDirectoryTreeItem is a single item in a disk directory tree view. |
| 41 | */ |
| 42 | public class TDirectoryTreeItem extends TTreeItem { |
| 43 | |
| 44 | /** |
| 45 | * Directory entry corresponding to this list item. |
| 46 | */ |
| 47 | File dir; |
| 48 | |
| 49 | /** |
| 50 | * Called when this item is expanded or collapsed. this.expanded will be |
| 51 | * true if this item was just expanded from a mouse click or keypress. |
| 52 | */ |
| 53 | @Override |
| 54 | public void onExpand() { |
| 55 | // System.err.printf("onExpand() %s\n", dir); |
| 56 | |
| 57 | if (dir == null) { |
| 58 | return; |
| 59 | } |
| 60 | getChildren().clear(); |
| 61 | |
| 62 | // Make sure we can read it before trying to. |
| 63 | if (dir.canRead()) { |
| 64 | setSelectable(true); |
| 65 | } else { |
| 66 | setSelectable(false); |
| 67 | } |
| 68 | assert (dir.isDirectory()); |
| 69 | setExpandable(true); |
| 70 | |
| 71 | if ((isExpanded() == false) || (isExpandable() == false)) { |
| 72 | getTreeView().reflow(); |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | for (File file: dir.listFiles()) { |
| 77 | // System.err.printf(" -> file %s %s\n", file, file.getName()); |
| 78 | |
| 79 | if (file.getName().startsWith(".")) { |
| 80 | // Hide dot-files |
| 81 | continue; |
| 82 | } |
| 83 | if (!file.isDirectory()) { |
| 84 | continue; |
| 85 | } |
| 86 | |
| 87 | try { |
| 88 | TDirectoryTreeItem item = new TDirectoryTreeItem(getTreeView(), |
| 89 | file.getCanonicalPath(), false, false); |
| 90 | |
| 91 | item.level = this.level + 1; |
| 92 | getChildren().add(item); |
| 93 | } catch (IOException e) { |
| 94 | continue; |
| 95 | } |
| 96 | } |
| 97 | Collections.sort(getChildren()); |
| 98 | |
| 99 | getTreeView().reflow(); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Add a child item. This method should never be used, it will throw an |
| 104 | * IllegalArgumentException every time. |
| 105 | * |
| 106 | * @param text text for this item |
| 107 | * @param expanded if true, have it expanded immediately |
| 108 | * @return the new item |
| 109 | * @throws IllegalArgumentException if this function is called |
| 110 | */ |
| 111 | @Override |
| 112 | public final TTreeItem addChild(final String text, final boolean expanded) { |
| 113 | throw new IllegalArgumentException("Do not call addChild(), use onExpand() instead"); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Public constructor. |
| 118 | * |
| 119 | * @param view root TTreeView |
| 120 | * @param text text for this item |
| 121 | */ |
| 122 | public TDirectoryTreeItem(final TTreeView view, |
| 123 | final String text) throws IOException { |
| 124 | |
| 125 | this(view, text, false, true); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Public constructor. |
| 130 | * |
| 131 | * @param view root TTreeView |
| 132 | * @param text text for this item |
| 133 | * @param expanded if true, have it expanded immediately |
| 134 | */ |
| 135 | public TDirectoryTreeItem(final TTreeView view, final String text, |
| 136 | final boolean expanded) throws IOException { |
| 137 | |
| 138 | this(view, text, expanded, true); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Public constructor. |
| 143 | * |
| 144 | * @param view root TTreeView |
| 145 | * @param text text for this item |
| 146 | * @param expanded if true, have it expanded immediately |
| 147 | * @param openParents if true, expand all paths up the root path and |
| 148 | * return the root path entry |
| 149 | */ |
| 150 | public TDirectoryTreeItem(final TTreeView view, final String text, |
| 151 | final boolean expanded, final boolean openParents) throws IOException { |
| 152 | |
| 153 | super(view, text, false); |
| 154 | |
| 155 | List<String> parentPaths = new LinkedList<String>(); |
| 156 | boolean oldExpanded = expanded; |
| 157 | |
| 158 | // Convert to canonical path |
| 159 | File rootPath = new File(text); |
| 160 | rootPath = rootPath.getCanonicalFile(); |
| 161 | |
| 162 | if (openParents == true) { |
| 163 | setExpanded(true); |
| 164 | |
| 165 | // Go up the directory tree |
| 166 | File parent = rootPath.getParentFile(); |
| 167 | while (parent != null) { |
| 168 | parentPaths.add(rootPath.getName()); |
| 169 | rootPath = rootPath.getParentFile(); |
| 170 | parent = rootPath.getParentFile(); |
| 171 | } |
| 172 | } |
| 173 | dir = rootPath; |
| 174 | if (rootPath.getParentFile() == null) { |
| 175 | // This is a filesystem root, use its full name |
| 176 | setText(rootPath.getCanonicalPath()); |
| 177 | } else { |
| 178 | // This is a relative path. We got here because openParents was |
| 179 | // false. |
| 180 | assert (openParents == false); |
| 181 | setText(rootPath.getName()); |
| 182 | } |
| 183 | onExpand(); |
| 184 | |
| 185 | if (openParents == true) { |
| 186 | TDirectoryTreeItem childPath = this; |
| 187 | Collections.reverse(parentPaths); |
| 188 | for (String p: parentPaths) { |
| 189 | for (TWidget widget: childPath.getChildren()) { |
| 190 | TDirectoryTreeItem child = (TDirectoryTreeItem) widget; |
| 191 | if (child.getText().equals(p)) { |
| 192 | childPath = child; |
| 193 | childPath.setExpanded(true); |
| 194 | childPath.onExpand(); |
| 195 | break; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | unselect(); |
| 200 | getTreeView().setSelected(childPath); |
| 201 | setExpanded(oldExpanded); |
| 202 | } |
| 203 | getTreeView().reflow(); |
| 204 | } |
| 205 | } |