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 * File corresponding to this list item.
50 * Get the File corresponding to this list item.
54 public final File
getFile() {
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.
63 public void onExpand() {
64 // System.err.printf("onExpand() %s\n", file);
69 getChildren().clear();
71 // Make sure we can read it before trying to.
77 assert (file
.isDirectory());
80 if ((isExpanded() == false) || (isExpandable() == false)) {
81 getTreeView().reflow();
85 for (File f
: file
.listFiles()) {
86 // System.err.printf(" -> file %s %s\n", file, file.getName());
88 if (f
.getName().startsWith(".")) {
92 if (!f
.isDirectory()) {
97 TDirectoryTreeItem item
= new TDirectoryTreeItem(getTreeView(),
98 f
.getCanonicalPath(), false, false);
100 item
.level
= this.level
+ 1;
101 getChildren().add(item
);
102 } catch (IOException e
) {
106 Collections
.sort(getChildren());
108 getTreeView().reflow();
112 * Add a child item. This method should never be used, it will throw an
113 * IllegalArgumentException every time.
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
121 public final TTreeItem
addChild(final String text
, final boolean expanded
) {
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
131 public TDirectoryTreeItem(final TTreeView view
,
132 final String text
) throws IOException
{
134 this(view
, text
, false, true);
138 * Public constructor.
140 * @param view root TTreeView
141 * @param text text for this item
142 * @param expanded if true, have it expanded immediately
144 public TDirectoryTreeItem(final TTreeView view
, final String text
,
145 final boolean expanded
) throws IOException
{
147 this(view
, text
, expanded
, true);
151 * Public constructor.
153 * @param view root TTreeView
154 * @param text text for this item
155 * @param expanded if true, have it expanded immediately
156 * @param openParents if true, expand all paths up the root path and
157 * return the root path entry
159 public TDirectoryTreeItem(final TTreeView view
, final String text
,
160 final boolean expanded
, final boolean openParents
) throws IOException
{
162 super(view
, text
, false);
164 List
<String
> parentFiles
= new LinkedList
<String
>();
165 boolean oldExpanded
= expanded
;
167 // Convert to canonical path
168 File rootFile
= new File(text
);
169 rootFile
= rootFile
.getCanonicalFile();
171 if (openParents
== true) {
174 // Go up the directory tree
175 File parent
= rootFile
.getParentFile();
176 while (parent
!= null) {
177 parentFiles
.add(rootFile
.getName());
178 rootFile
= rootFile
.getParentFile();
179 parent
= rootFile
.getParentFile();
183 if (rootFile
.getParentFile() == null) {
184 // This is a filesystem root, use its full name
185 setText(rootFile
.getCanonicalPath());
187 // This is a relative path. We got here because openParents was
189 assert (openParents
== false);
190 setText(rootFile
.getName());
194 if (openParents
== true) {
195 TDirectoryTreeItem childFile
= this;
196 Collections
.reverse(parentFiles
);
197 for (String p
: parentFiles
) {
198 for (TWidget widget
: childFile
.getChildren()) {
199 TDirectoryTreeItem child
= (TDirectoryTreeItem
) widget
;
200 if (child
.getText().equals(p
)) {
202 childFile
.setExpanded(true);
203 childFile
.onExpand();
209 getTreeView().setSelected(childFile
);
210 setExpanded(oldExpanded
);
212 getTreeView().reflow();