Commit | Line | Data |
---|---|---|
daa4106c | 1 | /* |
7668cb45 KL |
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 | */ | |
31 | package jexer; | |
32 | ||
7668cb45 KL |
33 | import jexer.event.TKeypressEvent; |
34 | import jexer.event.TMouseEvent; | |
35 | import static jexer.TKeypress.*; | |
36 | ||
37 | /** | |
38 | * TTreeView implements a simple tree view. | |
39 | */ | |
40 | public class TTreeView extends TWidget { | |
41 | ||
42 | /** | |
43 | * Vertical scrollbar. | |
44 | */ | |
45 | private TVScroller vScroller; | |
46 | ||
47 | /** | |
0d47c546 | 48 | * Horizontal scrollbar. |
7668cb45 | 49 | */ |
0d47c546 KL |
50 | private THScroller hScroller; |
51 | ||
52 | /** | |
53 | * Get the horizontal scrollbar. This is used by TTreeItem.draw(), and | |
54 | * potentially subclasses. | |
329fd62e KL |
55 | * |
56 | * @return the horizontal scrollbar | |
0d47c546 KL |
57 | */ |
58 | public final THScroller getHScroller() { | |
59 | return hScroller; | |
60 | } | |
7668cb45 KL |
61 | |
62 | /** | |
63 | * Root of the tree. | |
64 | */ | |
65 | private TTreeItem treeRoot; | |
66 | ||
67 | /** | |
68 | * Get the root of the tree. | |
69 | * | |
70 | * @return the root of the tree | |
71 | */ | |
72 | public final TTreeItem getTreeRoot() { | |
73 | return treeRoot; | |
74 | } | |
75 | ||
76 | /** | |
77 | * Set the root of the tree. | |
78 | * | |
79 | * @param treeRoot the new root of the tree | |
80 | */ | |
81 | public final void setTreeRoot(final TTreeItem treeRoot) { | |
82 | this.treeRoot = treeRoot; | |
83 | } | |
84 | ||
85 | /** | |
86 | * Maximum width of a single line. | |
87 | */ | |
88 | private int maxLineWidth; | |
89 | ||
90 | /** | |
91 | * Only one of my children can be selected. | |
92 | */ | |
93 | private TTreeItem selectedItem = null; | |
94 | ||
95 | /** | |
96 | * If true, move the window to put the selected item in view. This | |
97 | * normally only happens once after setting treeRoot. | |
98 | */ | |
329fd62e | 99 | private boolean centerWindow = false; |
7668cb45 KL |
100 | |
101 | /** | |
102 | * The action to perform when the user selects an item. | |
103 | */ | |
104 | private TAction action = null; | |
105 | ||
106 | /** | |
107 | * Set treeRoot. | |
108 | * | |
109 | * @param treeRoot ultimate root of tree | |
110 | * @param centerWindow if true, move the window to put the root in view | |
111 | */ | |
329fd62e KL |
112 | public void setTreeRoot(final TTreeItem treeRoot, |
113 | final boolean centerWindow) { | |
114 | ||
7668cb45 KL |
115 | this.treeRoot = treeRoot; |
116 | this.centerWindow = centerWindow; | |
117 | } | |
118 | ||
119 | /** | |
120 | * Public constructor. | |
121 | * | |
122 | * @param parent parent widget | |
123 | * @param x column relative to parent | |
124 | * @param y row relative to parent | |
125 | * @param width width of tree view | |
126 | * @param height height of tree view | |
127 | */ | |
128 | public TTreeView(final TWidget parent, final int x, final int y, | |
129 | final int width, final int height) { | |
130 | ||
131 | this(parent, x, y, width, height, null); | |
132 | } | |
133 | ||
134 | /** | |
135 | * Public constructor. | |
136 | * | |
137 | * @param parent parent widget | |
138 | * @param x column relative to parent | |
139 | * @param y row relative to parent | |
140 | * @param width width of tree view | |
141 | * @param height height of tree view | |
142 | * @param action action to perform when an item is selected | |
143 | */ | |
144 | public TTreeView(final TWidget parent, final int x, final int y, | |
145 | final int width, final int height, final TAction action) { | |
146 | ||
147 | super(parent, x, y, width, height); | |
148 | this.action = action; | |
149 | } | |
150 | ||
151 | /** | |
152 | * Get the tree view item that was selected. | |
153 | * | |
154 | * @return the selected item, or null if no item is selected | |
155 | */ | |
156 | public final TTreeItem getSelected() { | |
157 | return selectedItem; | |
158 | } | |
159 | ||
160 | /** | |
0d47c546 | 161 | * Set the new selected tree view item. |
7668cb45 KL |
162 | * |
163 | * @param item new item that became selected | |
164 | */ | |
0d47c546 | 165 | public void setSelected(final TTreeItem item) { |
7668cb45 KL |
166 | if (item != null) { |
167 | item.setSelected(true); | |
168 | } | |
169 | if ((selectedItem != null) && (selectedItem != item)) { | |
170 | selectedItem.setSelected(false); | |
171 | } | |
172 | selectedItem = item; | |
173 | } | |
174 | ||
175 | /** | |
0d47c546 | 176 | * Perform user selection action. |
7668cb45 | 177 | */ |
0d47c546 | 178 | public void dispatch() { |
7668cb45 KL |
179 | if (action != null) { |
180 | action.DO(); | |
181 | } | |
182 | } | |
183 | ||
184 | /** | |
185 | * Update (or instantiate) vScroller and hScroller. | |
186 | */ | |
187 | private void updateScrollers() { | |
188 | // Setup vertical scroller | |
189 | if (vScroller == null) { | |
190 | vScroller = new TVScroller(this, getWidth() - 1, 0, | |
191 | getHeight() - 1); | |
192 | vScroller.setValue(0); | |
193 | vScroller.setTopValue(0); | |
194 | } | |
195 | vScroller.setX(getWidth() - 1); | |
196 | vScroller.setHeight(getHeight() - 1); | |
197 | vScroller.setBigChange(getHeight() - 1); | |
198 | ||
199 | // Setup horizontal scroller | |
200 | if (hScroller == null) { | |
201 | hScroller = new THScroller(this, 0, getHeight() - 1, | |
202 | getWidth() - 1); | |
203 | hScroller.setValue(0); | |
204 | hScroller.setLeftValue(0); | |
205 | } | |
206 | hScroller.setY(getHeight() - 1); | |
207 | hScroller.setWidth(getWidth() - 1); | |
208 | hScroller.setBigChange(getWidth() - 1); | |
209 | } | |
210 | ||
211 | /** | |
212 | * Resize text and scrollbars for a new width/height. | |
213 | */ | |
214 | public void reflow() { | |
215 | int selectedRow = 0; | |
216 | boolean foundSelectedRow = false; | |
217 | ||
218 | updateScrollers(); | |
219 | if (treeRoot == null) { | |
220 | return; | |
221 | } | |
222 | ||
223 | // Make each child invisible/inactive to start, expandTree() will | |
224 | // reactivate the visible ones. | |
225 | for (TWidget widget: getChildren()) { | |
226 | if (widget instanceof TTreeItem) { | |
227 | TTreeItem item = (TTreeItem) widget; | |
228 | item.setInvisible(true); | |
229 | item.setEnabled(false); | |
0d47c546 KL |
230 | item.keyboardPrevious = null; |
231 | item.keyboardNext = null; | |
7668cb45 KL |
232 | } |
233 | } | |
234 | ||
235 | // Expand the tree into a linear list | |
236 | getChildren().clear(); | |
237 | getChildren().addAll(treeRoot.expandTree("", true)); | |
0d47c546 KL |
238 | |
239 | // Locate the selected row and maximum line width | |
7668cb45 KL |
240 | for (TWidget widget: getChildren()) { |
241 | TTreeItem item = (TTreeItem) widget; | |
242 | ||
243 | if (item == selectedItem) { | |
244 | foundSelectedRow = true; | |
245 | } | |
329fd62e | 246 | if (!foundSelectedRow) { |
7668cb45 KL |
247 | selectedRow++; |
248 | } | |
249 | ||
250 | int lineWidth = item.getText().length() | |
329fd62e | 251 | + item.getPrefix().length() + 4; |
7668cb45 KL |
252 | if (lineWidth > maxLineWidth) { |
253 | maxLineWidth = lineWidth; | |
254 | } | |
255 | } | |
0d47c546 | 256 | |
7668cb45 KL |
257 | if ((centerWindow) && (foundSelectedRow)) { |
258 | if ((selectedRow < vScroller.getValue()) | |
259 | || (selectedRow > vScroller.getValue() + getHeight() - 2) | |
260 | ) { | |
261 | vScroller.setValue(selectedRow); | |
262 | centerWindow = false; | |
263 | } | |
264 | } | |
265 | updatePositions(); | |
266 | ||
267 | // Rescale the scroll bars | |
268 | vScroller.setBottomValue(getChildren().size() - getHeight() + 1); | |
269 | if (vScroller.getBottomValue() < 0) { | |
270 | vScroller.setBottomValue(0); | |
271 | } | |
272 | /* | |
273 | if (vScroller.getValue() > vScroller.getBottomValue()) { | |
274 | vScroller.setValue(vScroller.getBottomValue()); | |
275 | } | |
276 | */ | |
277 | hScroller.setRightValue(maxLineWidth - getWidth() + 3); | |
278 | if (hScroller.getRightValue() < 0) { | |
279 | hScroller.setRightValue(0); | |
280 | } | |
281 | /* | |
282 | if (hScroller.getValue() > hScroller.getRightValue()) { | |
283 | hScroller.setValue(hScroller.getRightValue()); | |
284 | } | |
285 | */ | |
286 | getChildren().add(hScroller); | |
287 | getChildren().add(vScroller); | |
288 | } | |
289 | ||
290 | /** | |
291 | * Update the Y positions of all the children items. | |
292 | */ | |
293 | private void updatePositions() { | |
294 | if (treeRoot == null) { | |
295 | return; | |
296 | } | |
297 | ||
298 | int begin = vScroller.getValue(); | |
299 | int topY = 0; | |
0d47c546 KL |
300 | |
301 | // As we walk the list we also adjust next/previous pointers, | |
302 | // resulting in a doubly-linked list but only of the expanded items. | |
303 | TTreeItem p = null; | |
304 | ||
7668cb45 KL |
305 | for (int i = 0; i < getChildren().size(); i++) { |
306 | if (!(getChildren().get(i) instanceof TTreeItem)) { | |
0d47c546 | 307 | // Skip the scrollbars |
7668cb45 KL |
308 | continue; |
309 | } | |
310 | TTreeItem item = (TTreeItem) getChildren().get(i); | |
311 | ||
0d47c546 KL |
312 | if (p != null) { |
313 | item.keyboardPrevious = p; | |
314 | p.keyboardNext = item; | |
315 | } | |
316 | p = item; | |
317 | ||
7668cb45 KL |
318 | if (i < begin) { |
319 | // Render invisible | |
320 | item.setEnabled(false); | |
321 | item.setInvisible(true); | |
322 | continue; | |
323 | } | |
324 | ||
325 | if (topY >= getHeight() - 1) { | |
326 | // Render invisible | |
327 | item.setEnabled(false); | |
328 | item.setInvisible(true); | |
329 | continue; | |
330 | } | |
331 | ||
332 | item.setY(topY); | |
333 | item.setEnabled(true); | |
334 | item.setInvisible(false); | |
335 | item.setWidth(getWidth() - 1); | |
336 | topY++; | |
337 | } | |
0d47c546 | 338 | |
7668cb45 KL |
339 | } |
340 | ||
341 | /** | |
342 | * Handle mouse press events. | |
343 | * | |
344 | * @param mouse mouse button press event | |
345 | */ | |
346 | @Override | |
347 | public void onMouseDown(final TMouseEvent mouse) { | |
348 | if (mouse.isMouseWheelUp()) { | |
349 | vScroller.decrement(); | |
350 | } else if (mouse.isMouseWheelDown()) { | |
351 | vScroller.increment(); | |
352 | } else { | |
353 | // Pass to children | |
354 | super.onMouseDown(mouse); | |
355 | } | |
356 | ||
357 | // Update the screen after the scrollbars have moved | |
358 | reflow(); | |
359 | } | |
360 | ||
361 | /** | |
362 | * Handle mouse release events. | |
363 | * | |
364 | * @param mouse mouse button release event | |
365 | */ | |
366 | @Override | |
329fd62e | 367 | public void onMouseUp(final TMouseEvent mouse) { |
7668cb45 KL |
368 | // Pass to children |
369 | super.onMouseDown(mouse); | |
370 | ||
371 | // Update the screen after any thing has expanded/contracted | |
372 | reflow(); | |
373 | } | |
374 | ||
375 | /** | |
376 | * Handle keystrokes. | |
377 | * | |
378 | * @param keypress keystroke event | |
379 | */ | |
380 | @Override | |
381 | public void onKeypress(final TKeypressEvent keypress) { | |
0d47c546 KL |
382 | if (keypress.equals(kbShiftLeft) |
383 | || keypress.equals(kbCtrlLeft) | |
384 | || keypress.equals(kbAltLeft) | |
385 | ) { | |
7668cb45 | 386 | hScroller.decrement(); |
0d47c546 KL |
387 | } else if (keypress.equals(kbShiftRight) |
388 | || keypress.equals(kbCtrlRight) | |
389 | || keypress.equals(kbAltRight) | |
390 | ) { | |
7668cb45 | 391 | hScroller.increment(); |
0d47c546 KL |
392 | } else if (keypress.equals(kbShiftUp) |
393 | || keypress.equals(kbCtrlUp) | |
394 | || keypress.equals(kbAltUp) | |
395 | ) { | |
7668cb45 | 396 | vScroller.decrement(); |
0d47c546 KL |
397 | } else if (keypress.equals(kbShiftDown) |
398 | || keypress.equals(kbCtrlDown) | |
399 | || keypress.equals(kbAltDown) | |
400 | ) { | |
7668cb45 | 401 | vScroller.increment(); |
0d47c546 KL |
402 | } else if (keypress.equals(kbShiftPgUp) |
403 | || keypress.equals(kbCtrlPgUp) | |
404 | || keypress.equals(kbAltPgUp) | |
405 | ) { | |
7668cb45 | 406 | vScroller.bigDecrement(); |
0d47c546 KL |
407 | } else if (keypress.equals(kbShiftPgDn) |
408 | || keypress.equals(kbCtrlPgDn) | |
409 | || keypress.equals(kbAltPgDn) | |
410 | ) { | |
7668cb45 KL |
411 | vScroller.bigIncrement(); |
412 | } else if (keypress.equals(kbHome)) { | |
413 | vScroller.toTop(); | |
414 | } else if (keypress.equals(kbEnd)) { | |
415 | vScroller.toBottom(); | |
416 | } else if (keypress.equals(kbEnter)) { | |
417 | if (selectedItem != null) { | |
418 | dispatch(); | |
419 | } | |
0d47c546 KL |
420 | } else if (keypress.equals(kbUp)) { |
421 | // Select the previous item | |
422 | if (selectedItem != null) { | |
423 | TTreeItem oldItem = selectedItem; | |
424 | if (selectedItem.keyboardPrevious != null) { | |
425 | setSelected(selectedItem.keyboardPrevious); | |
426 | if (oldItem.getY() == 0) { | |
427 | vScroller.decrement(); | |
428 | } | |
429 | } | |
430 | } | |
431 | } else if (keypress.equals(kbDown)) { | |
432 | // Select the next item | |
433 | if (selectedItem != null) { | |
434 | TTreeItem oldItem = selectedItem; | |
435 | if (selectedItem.keyboardNext != null) { | |
436 | setSelected(selectedItem.keyboardNext); | |
437 | if (oldItem.getY() == getHeight() - 2) { | |
438 | vScroller.increment(); | |
439 | } | |
440 | } | |
441 | } | |
a043164f KL |
442 | } else if (keypress.equals(kbTab)) { |
443 | getParent().switchWidget(true); | |
444 | return; | |
445 | } else if (keypress.equals(kbShiftTab) | |
446 | || keypress.equals(kbBackTab)) { | |
447 | getParent().switchWidget(false); | |
448 | return; | |
0d47c546 KL |
449 | } else if (selectedItem != null) { |
450 | // Give the TTreeItem a chance to handle arrow keys | |
451 | selectedItem.onKeypress(keypress); | |
7668cb45 | 452 | } else { |
0d47c546 | 453 | // Pass other keys (tab etc.) on to TWidget's handler. |
7668cb45 | 454 | super.onKeypress(keypress); |
a043164f | 455 | return; |
7668cb45 KL |
456 | } |
457 | ||
458 | // Update the screen after any thing has expanded/contracted | |
459 | reflow(); | |
460 | } | |
461 | ||
462 | } |