Commit | Line | Data |
---|---|---|
daa4106c | 1 | /* |
7668cb45 KL |
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; | |
0d47c546 | 34 | import java.io.IOException; |
7668cb45 KL |
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 | /** | |
a043164f | 45 | * File corresponding to this list item. |
7668cb45 | 46 | */ |
a043164f KL |
47 | private File file; |
48 | ||
49 | /** | |
50 | * Get the File corresponding to this list item. | |
51 | * | |
52 | * @return the File | |
53 | */ | |
54 | public final File getFile() { | |
55 | return file; | |
56 | } | |
7668cb45 KL |
57 | |
58 | /** | |
59 | * Called when this item is expanded or collapsed. this.expanded will be | |
60 | * true if this item was just expanded from a mouse click or keypress. | |
61 | */ | |
62 | @Override | |
329fd62e | 63 | public final void onExpand() { |
a043164f | 64 | // System.err.printf("onExpand() %s\n", file); |
0d47c546 | 65 | |
a043164f | 66 | if (file == null) { |
7668cb45 KL |
67 | return; |
68 | } | |
69 | getChildren().clear(); | |
70 | ||
71 | // Make sure we can read it before trying to. | |
a043164f | 72 | if (file.canRead()) { |
7668cb45 KL |
73 | setSelectable(true); |
74 | } else { | |
75 | setSelectable(false); | |
76 | } | |
a043164f | 77 | assert (file.isDirectory()); |
7668cb45 KL |
78 | setExpandable(true); |
79 | ||
329fd62e | 80 | if (!isExpanded() || !isExpandable()) { |
7668cb45 KL |
81 | getTreeView().reflow(); |
82 | return; | |
83 | } | |
84 | ||
a043164f | 85 | for (File f: file.listFiles()) { |
0d47c546 KL |
86 | // System.err.printf(" -> file %s %s\n", file, file.getName()); |
87 | ||
a043164f | 88 | if (f.getName().startsWith(".")) { |
0d47c546 | 89 | // Hide dot-files |
7668cb45 KL |
90 | continue; |
91 | } | |
a043164f | 92 | if (!f.isDirectory()) { |
7668cb45 KL |
93 | continue; |
94 | } | |
95 | ||
0d47c546 KL |
96 | try { |
97 | TDirectoryTreeItem item = new TDirectoryTreeItem(getTreeView(), | |
a043164f | 98 | f.getCanonicalPath(), false, false); |
7668cb45 | 99 | |
0d47c546 KL |
100 | item.level = this.level + 1; |
101 | getChildren().add(item); | |
102 | } catch (IOException e) { | |
103 | continue; | |
104 | } | |
7668cb45 KL |
105 | } |
106 | Collections.sort(getChildren()); | |
107 | ||
108 | getTreeView().reflow(); | |
109 | } | |
110 | ||
111 | /** | |
112 | * Add a child item. This method should never be used, it will throw an | |
113 | * IllegalArgumentException every time. | |
114 | * | |
115 | * @param text text for this item | |
116 | * @param expanded if true, have it expanded immediately | |
117 | * @return the new item | |
118 | * @throws IllegalArgumentException if this function is called | |
119 | */ | |
120 | @Override | |
329fd62e KL |
121 | public final TTreeItem addChild(final String text, |
122 | final boolean expanded) throws IllegalArgumentException { | |
123 | ||
7668cb45 KL |
124 | throw new IllegalArgumentException("Do not call addChild(), use onExpand() instead"); |
125 | } | |
126 | ||
127 | /** | |
128 | * Public constructor. | |
129 | * | |
130 | * @param view root TTreeView | |
131 | * @param text text for this item | |
329fd62e | 132 | * @throws IOException if a java.io operation throws |
7668cb45 | 133 | */ |
0d47c546 KL |
134 | public TDirectoryTreeItem(final TTreeView view, |
135 | final String text) throws IOException { | |
136 | ||
7668cb45 KL |
137 | this(view, text, false, true); |
138 | } | |
139 | ||
140 | /** | |
141 | * Public constructor. | |
142 | * | |
143 | * @param view root TTreeView | |
144 | * @param text text for this item | |
145 | * @param expanded if true, have it expanded immediately | |
329fd62e | 146 | * @throws IOException if a java.io operation throws |
7668cb45 KL |
147 | */ |
148 | public TDirectoryTreeItem(final TTreeView view, final String text, | |
0d47c546 | 149 | final boolean expanded) throws IOException { |
7668cb45 KL |
150 | |
151 | this(view, text, expanded, true); | |
152 | } | |
153 | ||
154 | /** | |
155 | * Public constructor. | |
156 | * | |
157 | * @param view root TTreeView | |
158 | * @param text text for this item | |
159 | * @param expanded if true, have it expanded immediately | |
160 | * @param openParents if true, expand all paths up the root path and | |
161 | * return the root path entry | |
329fd62e | 162 | * @throws IOException if a java.io operation throws |
7668cb45 KL |
163 | */ |
164 | public TDirectoryTreeItem(final TTreeView view, final String text, | |
0d47c546 | 165 | final boolean expanded, final boolean openParents) throws IOException { |
7668cb45 KL |
166 | |
167 | super(view, text, false); | |
168 | ||
a043164f | 169 | List<String> parentFiles = new LinkedList<String>(); |
7668cb45 KL |
170 | boolean oldExpanded = expanded; |
171 | ||
0d47c546 | 172 | // Convert to canonical path |
a043164f KL |
173 | File rootFile = new File(text); |
174 | rootFile = rootFile.getCanonicalFile(); | |
0d47c546 | 175 | |
329fd62e | 176 | if (openParents) { |
7668cb45 KL |
177 | setExpanded(true); |
178 | ||
179 | // Go up the directory tree | |
a043164f | 180 | File parent = rootFile.getParentFile(); |
7668cb45 | 181 | while (parent != null) { |
a043164f KL |
182 | parentFiles.add(rootFile.getName()); |
183 | rootFile = rootFile.getParentFile(); | |
184 | parent = rootFile.getParentFile(); | |
7668cb45 | 185 | } |
0d47c546 | 186 | } |
a043164f KL |
187 | file = rootFile; |
188 | if (rootFile.getParentFile() == null) { | |
0d47c546 | 189 | // This is a filesystem root, use its full name |
a043164f | 190 | setText(rootFile.getCanonicalPath()); |
7668cb45 | 191 | } else { |
0d47c546 KL |
192 | // This is a relative path. We got here because openParents was |
193 | // false. | |
329fd62e | 194 | assert (!openParents); |
a043164f | 195 | setText(rootFile.getName()); |
7668cb45 | 196 | } |
7668cb45 KL |
197 | onExpand(); |
198 | ||
329fd62e | 199 | if (openParents) { |
a043164f KL |
200 | TDirectoryTreeItem childFile = this; |
201 | Collections.reverse(parentFiles); | |
202 | for (String p: parentFiles) { | |
203 | for (TWidget widget: childFile.getChildren()) { | |
7668cb45 KL |
204 | TDirectoryTreeItem child = (TDirectoryTreeItem) widget; |
205 | if (child.getText().equals(p)) { | |
a043164f KL |
206 | childFile = child; |
207 | childFile.setExpanded(true); | |
208 | childFile.onExpand(); | |
7668cb45 KL |
209 | break; |
210 | } | |
211 | } | |
212 | } | |
213 | unselect(); | |
a043164f | 214 | getTreeView().setSelected(childFile); |
7668cb45 KL |
215 | setExpanded(oldExpanded); |
216 | } | |
217 | getTreeView().reflow(); | |
218 | } | |
219 | } |