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
.util
.Collections
;
35 import java
.util
.List
;
36 import java
.util
.LinkedList
;
39 * TDirectoryTreeItem is a single item in a disk directory tree view.
41 public class TDirectoryTreeItem
extends TTreeItem
{
44 * Directory entry corresponding to this list item.
49 * Called when this item is expanded or collapsed. this.expanded will be
50 * true if this item was just expanded from a mouse click or keypress.
53 public void onExpand() {
57 getChildren().clear();
59 // Make sure we can read it before trying to.
65 assert (dir
.isDirectory());
68 if ((isExpanded() == false) || (isExpandable() == false)) {
69 getTreeView().reflow();
73 // Refresh my child list
74 for (File file
: dir
.listFiles()) {
75 if (file
.getName().equals(".")) {
78 if (!file
.isDirectory()) {
82 TDirectoryTreeItem item
= new TDirectoryTreeItem(getTreeView(),
83 file
.getName(), false, false);
85 item
.level
= this.level
+ 1;
86 getChildren().add(item
);
88 Collections
.sort(getChildren());
90 getTreeView().reflow();
94 * Add a child item. This method should never be used, it will throw an
95 * IllegalArgumentException every time.
97 * @param text text for this item
98 * @param expanded if true, have it expanded immediately
99 * @return the new item
100 * @throws IllegalArgumentException if this function is called
103 public final TTreeItem
addChild(final String text
, final boolean expanded
) {
104 throw new IllegalArgumentException("Do not call addChild(), use onExpand() instead");
108 * Public constructor.
110 * @param view root TTreeView
111 * @param text text for this item
113 public TDirectoryTreeItem(final TTreeView view
, final String text
) {
114 this(view
, text
, false, true);
118 * Public constructor.
120 * @param view root TTreeView
121 * @param text text for this item
122 * @param expanded if true, have it expanded immediately
124 public TDirectoryTreeItem(final TTreeView view
, final String text
,
125 final boolean expanded
) {
127 this(view
, text
, expanded
, true);
131 * Public constructor.
133 * @param view root TTreeView
134 * @param text text for this item
135 * @param expanded if true, have it expanded immediately
136 * @param openParents if true, expand all paths up the root path and
137 * return the root path entry
139 public TDirectoryTreeItem(final TTreeView view
, final String text
,
140 final boolean expanded
, final boolean openParents
) {
142 super(view
, text
, false);
144 List
<TDirectoryTreeItem
> parentItems
= new LinkedList
<TDirectoryTreeItem
>();
145 List
<String
> parentPaths
= new LinkedList
<String
>();
146 boolean oldExpanded
= expanded
;
148 if (openParents
== true) {
151 // Go up the directory tree
152 File rootPath
= new File(text
);
153 File parent
= rootPath
.getParentFile();
154 while (parent
!= null) {
155 parentPaths
.add(rootPath
.getName());
156 rootPath
= rootPath
.getParentFile();
157 parent
= rootPath
.getParentFile();
159 setText(rootPath
.getName());
164 dir
= new File(getText());
167 if (openParents
== true) {
168 TDirectoryTreeItem childPath
= this;
169 Collections
.reverse(parentPaths
);
170 for (String p
: parentPaths
) {
171 for (TWidget widget
: childPath
.getChildren()) {
172 TDirectoryTreeItem child
= (TDirectoryTreeItem
) widget
;
173 if (child
.getText().equals(p
)) {
175 childPath
.setExpanded(true);
176 childPath
.onExpand();
182 getTreeView().setSelected(childPath
);
183 setExpanded(oldExpanded
);
185 getTreeView().reflow();