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
.io
.IOException
;
33 import java
.util
.Collections
;
34 import java
.util
.List
;
35 import java
.util
.LinkedList
;
38 * TDirectoryTreeItem is a single item in a disk directory tree view.
40 public class TDirectoryTreeItem
extends TTreeItem
{
43 * File corresponding to this list item.
48 * Get the File corresponding to this list item.
52 public final File
getFile() {
57 * Called when this item is expanded or collapsed. this.expanded will be
58 * true if this item was just expanded from a mouse click or keypress.
61 public final void onExpand() {
62 // System.err.printf("onExpand() %s\n", file);
67 getChildren().clear();
69 // Make sure we can read it before trying to.
75 assert (file
.isDirectory());
78 if (!isExpanded() || !isExpandable()) {
79 getTreeView().reflowData();
83 for (File f
: file
.listFiles()) {
84 // System.err.printf(" -> file %s %s\n", file, file.getName());
86 if (f
.getName().startsWith(".")) {
90 if (!f
.isDirectory()) {
95 TDirectoryTreeItem item
= new TDirectoryTreeItem(getTreeView(),
96 f
.getCanonicalPath(), false, false);
98 item
.level
= this.level
+ 1;
99 getChildren().add(item
);
100 } catch (IOException e
) {
104 Collections
.sort(getChildren());
106 getTreeView().reflowData();
110 * Add a child item. This method should never be used, it will throw an
111 * IllegalArgumentException every time.
113 * @param text text for this item
114 * @param expanded if true, have it expanded immediately
115 * @return the new item
116 * @throws IllegalArgumentException if this function is called
119 public final TTreeItem
addChild(final String text
,
120 final boolean expanded
) throws IllegalArgumentException
{
122 throw new IllegalArgumentException("Do not call addChild(), use onExpand() instead");
126 * Public constructor.
128 * @param view root TTreeView
129 * @param text text for this item
130 * @throws IOException if a java.io operation throws
132 public TDirectoryTreeItem(final TTreeView view
,
133 final String text
) throws IOException
{
135 this(view
, text
, false, true);
139 * Public constructor.
141 * @param view root TTreeView
142 * @param text text for this item
143 * @param expanded if true, have it expanded immediately
144 * @throws IOException if a java.io operation throws
146 public TDirectoryTreeItem(final TTreeView view
, final String text
,
147 final boolean expanded
) throws IOException
{
149 this(view
, text
, expanded
, true);
153 * Public constructor.
155 * @param view root TTreeView
156 * @param text text for this item
157 * @param expanded if true, have it expanded immediately
158 * @param openParents if true, expand all paths up the root path and
159 * return the root path entry
160 * @throws IOException if a java.io operation throws
162 public TDirectoryTreeItem(final TTreeView view
, final String text
,
163 final boolean expanded
, final boolean openParents
) throws IOException
{
165 super(view
, text
, false);
167 List
<String
> parentFiles
= new LinkedList
<String
>();
168 boolean oldExpanded
= expanded
;
170 // Convert to canonical path
171 File rootFile
= new File(text
);
172 rootFile
= rootFile
.getCanonicalFile();
177 // Go up the directory tree
178 File parent
= rootFile
.getParentFile();
179 while (parent
!= null) {
180 parentFiles
.add(rootFile
.getName());
181 rootFile
= rootFile
.getParentFile();
182 parent
= rootFile
.getParentFile();
186 if (rootFile
.getParentFile() == null) {
187 // This is a filesystem root, use its full name
188 setText(rootFile
.getCanonicalPath());
190 // This is a relative path. We got here because openParents was
192 assert (!openParents
);
193 setText(rootFile
.getName());
198 TDirectoryTreeItem childFile
= this;
199 Collections
.reverse(parentFiles
);
200 for (String p
: parentFiles
) {
201 for (TWidget widget
: childFile
.getChildren()) {
202 TDirectoryTreeItem child
= (TDirectoryTreeItem
) widget
;
203 if (child
.getText().equals(p
)) {
205 childFile
.setExpanded(true);
206 childFile
.onExpand();
212 getTreeView().setSelected(childFile
);
213 setExpanded(oldExpanded
);
215 getTreeView().reflowData();