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 import jexer
.ttree
.TTreeViewWidget
;
41 * TDirectoryTreeItem is a single item in a disk directory tree view.
43 public class TDirectoryTreeItem
extends TTreeItem
{
45 // ------------------------------------------------------------------------
46 // Variables --------------------------------------------------------------
47 // ------------------------------------------------------------------------
50 * File corresponding to this list item.
55 * The TTreeViewWidget containing this directory tree.
57 private TTreeViewWidget treeViewWidget
;
59 // ------------------------------------------------------------------------
60 // Constructors -----------------------------------------------------------
61 // ------------------------------------------------------------------------
66 * @param view root TTreeViewWidget
67 * @param text text for this item
68 * @param expanded if true, have it expanded immediately
69 * @throws IOException if a java.io operation throws
71 public TDirectoryTreeItem(final TTreeViewWidget view
, final String text
,
72 final boolean expanded
) throws IOException
{
74 this(view
, text
, expanded
, true);
80 * @param view root TTreeViewWidget
81 * @param text text for this item
82 * @param expanded if true, have it expanded immediately
83 * @param openParents if true, expand all paths up the root path and
84 * return the root path entry
85 * @throws IOException if a java.io operation throws
87 public TDirectoryTreeItem(final TTreeViewWidget view
, final String text
,
88 final boolean expanded
, final boolean openParents
) throws IOException
{
90 super(view
.getTreeView(), text
, false);
92 this.treeViewWidget
= view
;
94 List
<String
> parentFiles
= new LinkedList
<String
>();
95 boolean oldExpanded
= expanded
;
97 // Convert to canonical path
98 File rootFile
= new File(text
);
99 rootFile
= rootFile
.getCanonicalFile();
104 // Go up the directory tree
105 File parent
= rootFile
.getParentFile();
106 while (parent
!= null) {
107 parentFiles
.add(rootFile
.getName());
108 rootFile
= rootFile
.getParentFile();
109 parent
= rootFile
.getParentFile();
113 if (rootFile
.getParentFile() == null) {
114 // This is a filesystem root, use its full name
115 setText(rootFile
.getCanonicalPath());
117 // This is a relative path. We got here because openParents was
119 assert (!openParents
);
120 setText(rootFile
.getName());
125 TDirectoryTreeItem childFile
= this;
126 Collections
.reverse(parentFiles
);
127 for (String p
: parentFiles
) {
128 for (TWidget widget
: childFile
.getChildren()) {
129 TDirectoryTreeItem child
= (TDirectoryTreeItem
) widget
;
130 if (child
.getText().equals(p
)) {
132 childFile
.setExpanded(true);
133 childFile
.onExpand();
139 getTreeView().setSelected(childFile
, true);
140 setExpanded(oldExpanded
);
146 // ------------------------------------------------------------------------
147 // TTreeItem --------------------------------------------------------------
148 // ------------------------------------------------------------------------
151 * Get the File corresponding to this list item.
155 public final File
getFile() {
160 * Called when this item is expanded or collapsed. this.expanded will be
161 * true if this item was just expanded from a mouse click or keypress.
164 public final void onExpand() {
165 // System.err.printf("onExpand() %s\n", file);
170 getChildren().clear();
172 // Make sure we can read it before trying to.
173 if (file
.canRead()) {
176 setSelectable(false);
178 assert (file
.isDirectory());
181 if (!isExpanded() || !isExpandable()) {
185 for (File f
: file
.listFiles()) {
186 // System.err.printf(" -> file %s %s\n", file, file.getName());
188 if (f
.getName().startsWith(".")) {
192 if (!f
.isDirectory()) {
197 TDirectoryTreeItem item
= new TDirectoryTreeItem(treeViewWidget
,
198 f
.getCanonicalPath(), false, false);
200 item
.level
= this.level
+ 1;
201 getChildren().add(item
);
202 } catch (IOException e
) {
206 Collections
.sort(getChildren());