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 jexer
.THScroller
;
33 import jexer
.TKeypress
;
34 import jexer
.TScrollableWidget
;
35 import jexer
.TVScroller
;
37 import jexer
.event
.TKeypressEvent
;
38 import jexer
.event
.TMouseEvent
;
39 import static jexer
.TKeypress
.*;
42 * TTreeViewWidget wraps a tree view with horizontal and vertical scrollbars.
44 public class TTreeViewWidget
extends TScrollableWidget
{
46 // ------------------------------------------------------------------------
47 // Variables --------------------------------------------------------------
48 // ------------------------------------------------------------------------
53 private TTreeView treeView
;
56 * If true, move the window to put the selected item in view. This
57 * normally only happens once after setting treeRoot.
59 private boolean centerWindow
= false;
62 * Maximum width of a single line.
64 private int maxLineWidth
;
66 // ------------------------------------------------------------------------
67 // Constructors -----------------------------------------------------------
68 // ------------------------------------------------------------------------
73 * @param parent parent widget
74 * @param x column relative to parent
75 * @param y row relative to parent
76 * @param width width of tree view
77 * @param height height of tree view
79 public TTreeViewWidget(final TWidget parent
, final int x
, final int y
,
80 final int width
, final int height
) {
82 this(parent
, x
, y
, width
, height
, null);
88 * @param parent parent widget
89 * @param x column relative to parent
90 * @param y row relative to parent
91 * @param width width of tree view
92 * @param height height of tree view
93 * @param action action to perform when an item is selected
95 public TTreeViewWidget(final TWidget parent
, final int x
, final int y
,
96 final int width
, final int height
, final TAction action
) {
98 super(parent
, x
, y
, width
, height
);
100 treeView
= new TTreeView(this, 0, 0, getWidth() - 1, getHeight() - 1,
103 vScroller
= new TVScroller(this, getWidth() - 1, 0, getHeight() - 1);
104 hScroller
= new THScroller(this, 0, getHeight() - 1, getWidth() - 1);
108 // ------------------------------------------------------------------------
109 // Event handlers ---------------------------------------------------------
110 // ------------------------------------------------------------------------
113 * Handle mouse press events.
115 * @param mouse mouse button press event
118 public void onMouseDown(final TMouseEvent mouse
) {
119 if (mouse
.isMouseWheelUp()) {
121 } else if (mouse
.isMouseWheelDown()) {
124 // Pass to the TreeView or scrollbars
125 super.onMouseDown(mouse
);
128 // Update the view to reflect the new scrollbar positions
129 treeView
.setTopLine(getVerticalValue());
130 treeView
.setLeftColumn(getHorizontalValue());
135 * Handle mouse release events.
137 * @param mouse mouse button release event
140 public void onMouseUp(final TMouseEvent mouse
) {
141 // Pass to the TreeView or scrollbars
142 super.onMouseUp(mouse
);
144 // Update the view to reflect the new scrollbar positions
145 treeView
.setTopLine(getVerticalValue());
146 treeView
.setLeftColumn(getHorizontalValue());
151 * Handle mouse motion events.
153 * @param mouse mouse motion event
156 public void onMouseMotion(final TMouseEvent mouse
) {
157 // Pass to the TreeView or scrollbars
158 super.onMouseMotion(mouse
);
160 // Update the view to reflect the new scrollbar positions
161 treeView
.setTopLine(getVerticalValue());
162 treeView
.setLeftColumn(getHorizontalValue());
169 * @param keypress keystroke event
172 public void onKeypress(final TKeypressEvent keypress
) {
173 if (keypress
.equals(kbShiftLeft
)
174 || keypress
.equals(kbCtrlLeft
)
175 || keypress
.equals(kbAltLeft
)
177 horizontalDecrement();
178 } else if (keypress
.equals(kbShiftRight
)
179 || keypress
.equals(kbCtrlRight
)
180 || keypress
.equals(kbAltRight
)
182 horizontalIncrement();
183 } else if (keypress
.equals(kbShiftUp
)
184 || keypress
.equals(kbCtrlUp
)
185 || keypress
.equals(kbAltUp
)
188 } else if (keypress
.equals(kbShiftDown
)
189 || keypress
.equals(kbCtrlDown
)
190 || keypress
.equals(kbAltDown
)
193 } else if (keypress
.equals(kbShiftPgUp
)
194 || keypress
.equals(kbCtrlPgUp
)
195 || keypress
.equals(kbAltPgUp
)
197 bigVerticalDecrement();
198 } else if (keypress
.equals(kbShiftPgDn
)
199 || keypress
.equals(kbCtrlPgDn
)
200 || keypress
.equals(kbAltPgDn
)
202 bigVerticalIncrement();
203 } else if (keypress
.equals(kbPgDn
)) {
204 for (int i
= 0; i
< getHeight() - 2; i
++) {
205 treeView
.onKeypress(new TKeypressEvent(TKeypress
.kbDown
));
209 } else if (keypress
.equals(kbPgUp
)) {
210 for (int i
= 0; i
< getHeight() - 2; i
++) {
211 treeView
.onKeypress(new TKeypressEvent(TKeypress
.kbUp
));
215 } else if (keypress
.equals(kbHome
)) {
216 treeView
.setSelected((TTreeItem
) treeView
.getChildren().get(0),
218 treeView
.setTopLine(0);
221 } else if (keypress
.equals(kbEnd
)) {
222 treeView
.setSelected((TTreeItem
) treeView
.getChildren().get(
223 treeView
.getChildren().size() - 1), true);
226 } else if (keypress
.equals(kbTab
)) {
227 getParent().switchWidget(true);
229 } else if (keypress
.equals(kbShiftTab
)
230 || keypress
.equals(kbBackTab
)) {
231 getParent().switchWidget(false);
234 treeView
.onKeypress(keypress
);
236 // Update the scrollbars to reflect the new data position
241 // Update the view to reflect the new scrollbar position
242 treeView
.setTopLine(getVerticalValue());
243 treeView
.setLeftColumn(getHorizontalValue());
247 // ------------------------------------------------------------------------
248 // TScrollableWidget ------------------------------------------------------
249 // ------------------------------------------------------------------------
252 * Resize text and scrollbars for a new width/height.
255 public void reflowData() {
257 boolean foundSelectedRow
= false;
259 // Reset the keyboard list, expandTree() will recreate it.
260 for (TWidget widget
: treeView
.getChildren()) {
261 TTreeItem item
= (TTreeItem
) widget
;
262 item
.keyboardPrevious
= null;
263 item
.keyboardNext
= null;
266 // Expand the tree into a linear list
267 treeView
.getChildren().clear();
268 treeView
.getChildren().addAll(treeView
.getTreeRoot().expandTree("",
271 // Locate the selected row and maximum line width
272 for (TWidget widget
: treeView
.getChildren()) {
273 TTreeItem item
= (TTreeItem
) widget
;
275 if (item
== treeView
.getSelected()) {
276 foundSelectedRow
= true;
278 if (!foundSelectedRow
) {
282 int lineWidth
= item
.getText().length()
283 + item
.getPrefix().length() + 4;
284 if (lineWidth
> maxLineWidth
) {
285 maxLineWidth
= lineWidth
;
289 if ((centerWindow
) && (foundSelectedRow
)) {
290 if ((selectedRow
< getVerticalValue())
291 || (selectedRow
> getVerticalValue() + getHeight() - 2)
293 treeView
.setTopLine(selectedRow
);
294 centerWindow
= false;
297 treeView
.alignTree();
299 // Rescale the scroll bars
300 setVerticalValue(treeView
.getTopLine());
301 setBottomValue(treeView
.getTotalLineCount() - (getHeight() - 1));
302 if (getBottomValue() < getTopValue()) {
303 setBottomValue(getTopValue());
305 if (getVerticalValue() > getBottomValue()) {
306 setVerticalValue(getBottomValue());
308 setRightValue(maxLineWidth
- 2);
309 if (getHorizontalValue() > getRightValue()) {
310 setHorizontalValue(getRightValue());
315 // ------------------------------------------------------------------------
316 // TTreeView --------------------------------------------------------------
317 // ------------------------------------------------------------------------
320 * Get the underlying TTreeView.
322 * @return the TTreeView
324 public TTreeView
getTreeView() {
329 * Get the root of the tree.
331 * @return the root of the tree
333 public final TTreeItem
getTreeRoot() {
334 return treeView
.getTreeRoot();
338 * Set the root of the tree.
340 * @param treeRoot the new root of the tree
342 public final void setTreeRoot(final TTreeItem treeRoot
) {
343 treeView
.setTreeRoot(treeRoot
);
349 * @param treeRoot ultimate root of tree
350 * @param centerWindow if true, move the window to put the root in view
352 public void setTreeRoot(final TTreeItem treeRoot
,
353 final boolean centerWindow
) {
355 treeView
.setTreeRoot(treeRoot
);
356 this.centerWindow
= centerWindow
;
360 * Get the tree view item that was selected.
362 * @return the selected item, or null if no item is selected
364 public final TTreeItem
getSelected() {
365 return treeView
.getSelected();
369 * Set the new selected tree view item.
371 * @param item new item that became selected
372 * @param centerWindow if true, move the window to put the selected into
375 public void setSelected(final TTreeItem item
, final boolean centerWindow
) {
376 treeView
.setSelected(item
, centerWindow
);
380 * Perform user selection action.
382 public void dispatch() {