2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 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
;
40 * TDirectoryTreeItem is a single item in a disk directory tree view.
42 public class TDirectoryTreeItem
extends TTreeItem
{
44 // ------------------------------------------------------------------------
45 // Variables --------------------------------------------------------------
46 // ------------------------------------------------------------------------
49 * File corresponding to this list item.
54 * The TTreeViewWidget containing this directory tree.
56 private TTreeViewWidget treeViewWidget
;
58 // ------------------------------------------------------------------------
59 // Constructors -----------------------------------------------------------
60 // ------------------------------------------------------------------------
65 * @param view root TTreeViewWidget
66 * @param text text for this item
67 * @param expanded if true, have it expanded immediately
68 * @throws IOException if a java.io operation throws
70 public TDirectoryTreeItem(final TTreeViewWidget view
, final String text
,
71 final boolean expanded
) throws IOException
{
73 this(view
, text
, expanded
, true);
79 * @param view root TTreeViewWidget
80 * @param text text for this item
81 * @param expanded if true, have it expanded immediately
82 * @param openParents if true, expand all paths up the root path and
83 * return the root path entry
84 * @throws IOException if a java.io operation throws
86 public TDirectoryTreeItem(final TTreeViewWidget view
, final String text
,
87 final boolean expanded
, final boolean openParents
) throws IOException
{
89 super(view
.getTreeView(), text
, false);
91 this.treeViewWidget
= view
;
93 List
<String
> parentFiles
= new LinkedList
<String
>();
94 boolean oldExpanded
= expanded
;
96 // Convert to canonical path
97 File rootFile
= new File(text
);
98 rootFile
= rootFile
.getCanonicalFile();
103 // Go up the directory tree
104 File parent
= rootFile
.getParentFile();
105 while (parent
!= null) {
106 parentFiles
.add(rootFile
.getName());
107 rootFile
= rootFile
.getParentFile();
108 parent
= rootFile
.getParentFile();
112 if (rootFile
.getParentFile() == null) {
113 // This is a filesystem root, use its full name
114 setText(rootFile
.getCanonicalPath());
116 // This is a relative path. We got here because openParents was
118 assert (!openParents
);
119 setText(rootFile
.getName());
124 TDirectoryTreeItem childFile
= this;
125 Collections
.reverse(parentFiles
);
126 for (String p
: parentFiles
) {
127 for (TWidget widget
: childFile
.getChildren()) {
128 TDirectoryTreeItem child
= (TDirectoryTreeItem
) widget
;
129 if (child
.getText().equals(p
)) {
131 childFile
.setExpanded(true);
132 childFile
.onExpand();
138 getTreeView().setSelected(childFile
, true);
139 setExpanded(oldExpanded
);
145 // ------------------------------------------------------------------------
146 // TTreeItem --------------------------------------------------------------
147 // ------------------------------------------------------------------------
150 * Get the File corresponding to this list item.
154 public final File
getFile() {
159 * Called when this item is expanded or collapsed. this.expanded will be
160 * true if this item was just expanded from a mouse click or keypress.
163 public final void onExpand() {
164 // System.err.printf("onExpand() %s\n", file);
169 getChildren().clear();
171 // Make sure we can read it before trying to.
172 if (file
.canRead()) {
175 setSelectable(false);
177 assert (file
.isDirectory());
180 if (!isExpanded() || !isExpandable()) {
184 File
[] listFiles
= file
.listFiles();
185 if (listFiles
!= null) {
186 for (File f
: listFiles
) {
187 // System.err.printf(" -> file %s %s\n", file, file.getName());
189 if (f
.getName().startsWith(".")) {
193 if (!f
.isDirectory()) {
198 TDirectoryTreeItem item
= new TDirectoryTreeItem(treeViewWidget
,
199 f
.getCanonicalPath(), false, false);
201 item
.level
= this.level
+ 1;
202 getChildren().add(item
);
203 } catch (IOException e
) {
208 Collections
.sort(getChildren());