2 * Jexer - Java Text User Interface
4 * License: LGPLv3 or later
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.
10 * Copyright (C) 2015 Kevin Lamonte
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.
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.
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
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
34 import java
.io
.IOException
;
35 import java
.util
.Collections
;
36 import java
.util
.List
;
37 import java
.util
.LinkedList
;
40 * TDirectoryTreeItem is a single item in a disk directory tree view.
42 public class TDirectoryTreeItem
extends TTreeItem
{
45 * Directory entry corresponding to this list item.
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.
54 public void onExpand() {
55 // System.err.printf("onExpand() %s\n", dir);
60 getChildren().clear();
62 // Make sure we can read it before trying to.
68 assert (dir
.isDirectory());
71 if ((isExpanded() == false) || (isExpandable() == false)) {
72 getTreeView().reflow();
76 for (File file
: dir
.listFiles()) {
77 // System.err.printf(" -> file %s %s\n", file, file.getName());
79 if (file
.getName().startsWith(".")) {
83 if (!file
.isDirectory()) {
88 TDirectoryTreeItem item
= new TDirectoryTreeItem(getTreeView(),
89 file
.getCanonicalPath(), false, false);
91 item
.level
= this.level
+ 1;
92 getChildren().add(item
);
93 } catch (IOException e
) {
97 Collections
.sort(getChildren());
99 getTreeView().reflow();
103 * Add a child item. This method should never be used, it will throw an
104 * IllegalArgumentException every time.
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
112 public final TTreeItem
addChild(final String text
, final boolean expanded
) {
113 throw new IllegalArgumentException("Do not call addChild(), use onExpand() instead");
117 * Public constructor.
119 * @param view root TTreeView
120 * @param text text for this item
122 public TDirectoryTreeItem(final TTreeView view
,
123 final String text
) throws IOException
{
125 this(view
, text
, false, true);
129 * Public constructor.
131 * @param view root TTreeView
132 * @param text text for this item
133 * @param expanded if true, have it expanded immediately
135 public TDirectoryTreeItem(final TTreeView view
, final String text
,
136 final boolean expanded
) throws IOException
{
138 this(view
, text
, expanded
, true);
142 * Public constructor.
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
150 public TDirectoryTreeItem(final TTreeView view
, final String text
,
151 final boolean expanded
, final boolean openParents
) throws IOException
{
153 super(view
, text
, false);
155 List
<String
> parentPaths
= new LinkedList
<String
>();
156 boolean oldExpanded
= expanded
;
158 // Convert to canonical path
159 File rootPath
= new File(text
);
160 rootPath
= rootPath
.getCanonicalFile();
162 if (openParents
== true) {
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();
174 if (rootPath
.getParentFile() == null) {
175 // This is a filesystem root, use its full name
176 setText(rootPath
.getCanonicalPath());
178 // This is a relative path. We got here because openParents was
180 assert (openParents
== false);
181 setText(rootPath
.getName());
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
)) {
193 childPath
.setExpanded(true);
194 childPath
.onExpand();
200 getTreeView().setSelected(childPath
);
201 setExpanded(oldExpanded
);
203 getTreeView().reflow();