Merge branch 'master' of https://github.com/klamonte/jexer
[fanfix.git] / src / jexer / TDirectoryTreeItem.java
... / ...
CommitLineData
1/**
2 * Jexer - Java Text User Interface
3 *
4 * License: LGPLv3 or later
5 *
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.
9 *
10 * Copyright (C) 2015 Kevin Lamonte
11 *
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.
16 *
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.
21 *
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
26 * 02110-1301 USA
27 *
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 * @version 1
30 */
31package jexer;
32
33import java.io.File;
34import java.util.Collections;
35import java.util.List;
36import java.util.LinkedList;
37
38/**
39 * TDirectoryTreeItem is a single item in a disk directory tree view.
40 */
41public class TDirectoryTreeItem extends TTreeItem {
42
43 /**
44 * Directory entry corresponding to this list item.
45 */
46 File dir;
47
48 /**
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.
51 */
52 @Override
53 public void onExpand() {
54 if (dir == null) {
55 return;
56 }
57 getChildren().clear();
58
59 // Make sure we can read it before trying to.
60 if (dir.canRead()) {
61 setSelectable(true);
62 } else {
63 setSelectable(false);
64 }
65 assert (dir.isDirectory());
66 setExpandable(true);
67
68 if ((isExpanded() == false) || (isExpandable() == false)) {
69 getTreeView().reflow();
70 return;
71 }
72
73 // Refresh my child list
74 for (File file: dir.listFiles()) {
75 if (file.getName().equals(".")) {
76 continue;
77 }
78 if (!file.isDirectory()) {
79 continue;
80 }
81
82 TDirectoryTreeItem item = new TDirectoryTreeItem(getTreeView(),
83 file.getName(), false, false);
84
85 item.level = this.level + 1;
86 getChildren().add(item);
87 }
88 Collections.sort(getChildren());
89
90 getTreeView().reflow();
91 }
92
93 /**
94 * Add a child item. This method should never be used, it will throw an
95 * IllegalArgumentException every time.
96 *
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
101 */
102 @Override
103 public final TTreeItem addChild(final String text, final boolean expanded) {
104 throw new IllegalArgumentException("Do not call addChild(), use onExpand() instead");
105 }
106
107 /**
108 * Public constructor.
109 *
110 * @param view root TTreeView
111 * @param text text for this item
112 */
113 public TDirectoryTreeItem(final TTreeView view, final String text) {
114 this(view, text, false, true);
115 }
116
117 /**
118 * Public constructor.
119 *
120 * @param view root TTreeView
121 * @param text text for this item
122 * @param expanded if true, have it expanded immediately
123 */
124 public TDirectoryTreeItem(final TTreeView view, final String text,
125 final boolean expanded) {
126
127 this(view, text, expanded, true);
128 }
129
130 /**
131 * Public constructor.
132 *
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
138 */
139 public TDirectoryTreeItem(final TTreeView view, final String text,
140 final boolean expanded, final boolean openParents) {
141
142 super(view, text, false);
143
144 List<TDirectoryTreeItem> parentItems = new LinkedList<TDirectoryTreeItem>();
145 List<String> parentPaths = new LinkedList<String>();
146 boolean oldExpanded = expanded;
147
148 if (openParents == true) {
149 setExpanded(true);
150
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();
158 }
159 setText(rootPath.getName());
160 } else {
161 setText(text);
162 }
163
164 dir = new File(getText());
165 onExpand();
166
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)) {
174 childPath = child;
175 childPath.setExpanded(true);
176 childPath.onExpand();
177 break;
178 }
179 }
180 }
181 unselect();
182 getTreeView().setSelected(childPath);
183 setExpanded(oldExpanded);
184 }
185 getTreeView().reflow();
186 }
187}