Commit | Line | Data |
---|---|---|
daa4106c | 1 | /* |
7668cb45 KL |
2 | * Jexer - Java Text User Interface |
3 | * | |
e16dda65 | 4 | * The MIT License (MIT) |
7668cb45 | 5 | * |
a2018e99 | 6 | * Copyright (C) 2017 Kevin Lamonte |
7668cb45 | 7 | * |
e16dda65 KL |
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: | |
7668cb45 | 14 | * |
e16dda65 KL |
15 | * The above copyright notice and this permission notice shall be included in |
16 | * all copies or substantial portions of the Software. | |
7668cb45 | 17 | * |
e16dda65 KL |
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. | |
7668cb45 KL |
25 | * |
26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
27 | * @version 1 | |
28 | */ | |
d36057df KL |
29 | package jexer.ttree; |
30 | ||
31 | import jexer.TAction; | |
32 | import jexer.TApplication; | |
33 | import jexer.THScroller; | |
34 | import jexer.TScrollableWindow; | |
35 | import jexer.TVScroller; | |
36 | import jexer.TWidget; | |
7668cb45 KL |
37 | import jexer.event.TKeypressEvent; |
38 | import jexer.event.TMouseEvent; | |
d36057df | 39 | import jexer.event.TResizeEvent; |
7668cb45 KL |
40 | import static jexer.TKeypress.*; |
41 | ||
42 | /** | |
d36057df KL |
43 | * TTreeViewWindow wraps a tree view with horizontal and vertical scrollbars |
44 | * in a standalone window. | |
7668cb45 | 45 | */ |
d36057df | 46 | public class TTreeViewWindow extends TScrollableWindow { |
7668cb45 | 47 | |
d36057df KL |
48 | // ------------------------------------------------------------------------ |
49 | // Variables -------------------------------------------------------------- | |
50 | // ------------------------------------------------------------------------ | |
7668cb45 KL |
51 | |
52 | /** | |
d36057df | 53 | * The TTreeView |
7668cb45 | 54 | */ |
d36057df | 55 | private TTreeView treeView; |
7668cb45 KL |
56 | |
57 | /** | |
58 | * If true, move the window to put the selected item in view. This | |
59 | * normally only happens once after setting treeRoot. | |
60 | */ | |
329fd62e | 61 | private boolean centerWindow = false; |
7668cb45 KL |
62 | |
63 | /** | |
d36057df | 64 | * Maximum width of a single line. |
7668cb45 | 65 | */ |
d36057df | 66 | private int maxLineWidth; |
329fd62e | 67 | |
d36057df KL |
68 | // ------------------------------------------------------------------------ |
69 | // Constructors ----------------------------------------------------------- | |
70 | // ------------------------------------------------------------------------ | |
7668cb45 KL |
71 | |
72 | /** | |
73 | * Public constructor. | |
74 | * | |
d36057df KL |
75 | * @param parent the main application |
76 | * @param title the window title | |
7668cb45 KL |
77 | * @param x column relative to parent |
78 | * @param y row relative to parent | |
79 | * @param width width of tree view | |
d36057df | 80 | * @param flags bitmask of RESIZABLE, CENTERED, or MODAL |
7668cb45 KL |
81 | * @param height height of tree view |
82 | */ | |
d36057df KL |
83 | public TTreeViewWindow(final TApplication parent, final String title, |
84 | final int x, final int y, final int width, final int height, | |
85 | final int flags) { | |
7668cb45 | 86 | |
d36057df | 87 | this(parent, title, x, y, width, height, flags, null); |
7668cb45 KL |
88 | } |
89 | ||
90 | /** | |
91 | * Public constructor. | |
92 | * | |
d36057df KL |
93 | * @param parent the main application |
94 | * @param title the window title | |
7668cb45 KL |
95 | * @param x column relative to parent |
96 | * @param y row relative to parent | |
97 | * @param width width of tree view | |
98 | * @param height height of tree view | |
d36057df | 99 | * @param flags bitmask of RESIZABLE, CENTERED, or MODAL |
7668cb45 KL |
100 | * @param action action to perform when an item is selected |
101 | */ | |
d36057df KL |
102 | public TTreeViewWindow(final TApplication parent, final String title, |
103 | final int x, final int y, final int width, final int height, | |
104 | final int flags, final TAction action) { | |
105 | ||
106 | super(parent, title, x, y, width, height, flags); | |
7668cb45 | 107 | |
d36057df KL |
108 | treeView = new TTreeView(this, 0, 0, getWidth() - 2, getHeight() - 2, |
109 | action); | |
56661844 | 110 | |
d36057df KL |
111 | hScroller = new THScroller(this, 17, getHeight() - 2, getWidth() - 20); |
112 | vScroller = new TVScroller(this, getWidth() - 2, 0, getHeight() - 2); | |
113 | ||
114 | /* | |
115 | System.err.println("TTreeViewWindow()"); | |
116 | for (TWidget w: getChildren()) { | |
117 | System.err.println(" " + w + " " + w.isActive()); | |
118 | } | |
119 | */ | |
7668cb45 KL |
120 | } |
121 | ||
d36057df KL |
122 | // ------------------------------------------------------------------------ |
123 | // Event handlers --------------------------------------------------------- | |
124 | // ------------------------------------------------------------------------ | |
125 | ||
7668cb45 | 126 | /** |
d36057df | 127 | * Handle mouse press events. |
7668cb45 | 128 | * |
d36057df | 129 | * @param mouse mouse button press event |
7668cb45 | 130 | */ |
d36057df KL |
131 | @Override |
132 | public void onMouseDown(final TMouseEvent mouse) { | |
133 | if (mouse.isMouseWheelUp()) { | |
134 | verticalDecrement(); | |
135 | } else if (mouse.isMouseWheelDown()) { | |
136 | verticalIncrement(); | |
137 | } else { | |
138 | // Pass to the TreeView or scrollbars | |
139 | super.onMouseDown(mouse); | |
140 | } | |
141 | ||
142 | // Update the view to reflect the new scrollbar positions | |
143 | treeView.setTopLine(getVerticalValue()); | |
144 | treeView.setLeftColumn(getHorizontalValue()); | |
145 | reflowData(); | |
7668cb45 KL |
146 | } |
147 | ||
148 | /** | |
d36057df | 149 | * Handle mouse release events. |
7668cb45 | 150 | * |
d36057df KL |
151 | * @param mouse mouse button release event |
152 | */ | |
153 | @Override | |
154 | public void onMouseUp(final TMouseEvent mouse) { | |
155 | // Pass to the TreeView or scrollbars | |
156 | super.onMouseUp(mouse); | |
157 | ||
158 | // Update the view to reflect the new scrollbar positions | |
159 | treeView.setTopLine(getVerticalValue()); | |
160 | treeView.setLeftColumn(getHorizontalValue()); | |
161 | reflowData(); | |
162 | } | |
163 | ||
164 | /** | |
165 | * Handle mouse motion events. | |
166 | * | |
167 | * @param mouse mouse motion event | |
168 | */ | |
169 | @Override | |
170 | public void onMouseMotion(final TMouseEvent mouse) { | |
171 | // Pass to the TreeView or scrollbars | |
172 | super.onMouseMotion(mouse); | |
173 | ||
174 | // Update the view to reflect the new scrollbar positions | |
175 | treeView.setTopLine(getVerticalValue()); | |
176 | treeView.setLeftColumn(getHorizontalValue()); | |
177 | reflowData(); | |
178 | } | |
179 | ||
180 | /** | |
181 | * Handle keystrokes. | |
182 | * | |
183 | * @param keypress keystroke event | |
7668cb45 | 184 | */ |
d36057df KL |
185 | @Override |
186 | public void onKeypress(final TKeypressEvent keypress) { | |
187 | if (inKeyboardResize) { | |
188 | // Let TWindow do its job. | |
189 | super.onKeypress(keypress); | |
190 | return; | |
7668cb45 | 191 | } |
d36057df KL |
192 | |
193 | if (keypress.equals(kbShiftLeft) | |
194 | || keypress.equals(kbCtrlLeft) | |
195 | || keypress.equals(kbAltLeft) | |
196 | ) { | |
197 | horizontalDecrement(); | |
198 | } else if (keypress.equals(kbShiftRight) | |
199 | || keypress.equals(kbCtrlRight) | |
200 | || keypress.equals(kbAltRight) | |
201 | ) { | |
202 | horizontalIncrement(); | |
203 | } else if (keypress.equals(kbShiftUp) | |
204 | || keypress.equals(kbCtrlUp) | |
205 | || keypress.equals(kbAltUp) | |
206 | ) { | |
207 | verticalDecrement(); | |
208 | } else if (keypress.equals(kbShiftDown) | |
209 | || keypress.equals(kbCtrlDown) | |
210 | || keypress.equals(kbAltDown) | |
211 | ) { | |
212 | verticalIncrement(); | |
213 | } else if (keypress.equals(kbShiftPgUp) | |
214 | || keypress.equals(kbCtrlPgUp) | |
215 | || keypress.equals(kbAltPgUp) | |
216 | ) { | |
217 | bigVerticalDecrement(); | |
218 | } else if (keypress.equals(kbShiftPgDn) | |
219 | || keypress.equals(kbCtrlPgDn) | |
220 | || keypress.equals(kbAltPgDn) | |
221 | ) { | |
222 | bigVerticalIncrement(); | |
223 | } else { | |
224 | treeView.onKeypress(keypress); | |
225 | ||
226 | // Update the scrollbars to reflect the new data position | |
227 | reflowData(); | |
228 | return; | |
7668cb45 | 229 | } |
d36057df KL |
230 | |
231 | // Update the view to reflect the new scrollbar position | |
232 | treeView.setTopLine(getVerticalValue()); | |
233 | treeView.setLeftColumn(getHorizontalValue()); | |
234 | reflowData(); | |
7668cb45 KL |
235 | } |
236 | ||
d36057df KL |
237 | // ------------------------------------------------------------------------ |
238 | // TScrollableWindow ------------------------------------------------------ | |
239 | // ------------------------------------------------------------------------ | |
240 | ||
7668cb45 | 241 | /** |
d36057df KL |
242 | * Handle window/screen resize events. |
243 | * | |
244 | * @param resize resize event | |
7668cb45 | 245 | */ |
d36057df KL |
246 | @Override |
247 | public void onResize(final TResizeEvent resize) { | |
248 | if (resize.getType() == TResizeEvent.Type.WIDGET) { | |
249 | // Resize the treeView field. | |
250 | TResizeEvent treeSize = new TResizeEvent(TResizeEvent.Type.WIDGET, | |
251 | resize.getWidth() - 2, resize.getHeight() - 2); | |
252 | treeView.onResize(treeSize); | |
253 | ||
254 | // Have TScrollableWindow handle the scrollbars. | |
255 | super.onResize(resize); | |
256 | ||
257 | // Now re-center the treeView field. | |
258 | if (treeView.getSelected() != null) { | |
259 | treeView.setSelected(treeView.getSelected(), true); | |
260 | } | |
261 | reflowData(); | |
262 | return; | |
7668cb45 KL |
263 | } |
264 | } | |
265 | ||
7668cb45 KL |
266 | /** |
267 | * Resize text and scrollbars for a new width/height. | |
268 | */ | |
56661844 KL |
269 | @Override |
270 | public void reflowData() { | |
7668cb45 KL |
271 | int selectedRow = 0; |
272 | boolean foundSelectedRow = false; | |
273 | ||
d36057df KL |
274 | // Reset the keyboard list, expandTree() will recreate it. |
275 | for (TWidget widget: treeView.getChildren()) { | |
276 | TTreeItem item = (TTreeItem) widget; | |
277 | item.keyboardPrevious = null; | |
278 | item.keyboardNext = null; | |
7668cb45 KL |
279 | } |
280 | ||
281 | // Expand the tree into a linear list | |
d36057df KL |
282 | treeView.getChildren().clear(); |
283 | treeView.getChildren().addAll(treeView.getTreeRoot().expandTree("", | |
284 | true)); | |
0d47c546 KL |
285 | |
286 | // Locate the selected row and maximum line width | |
d36057df | 287 | for (TWidget widget: treeView.getChildren()) { |
7668cb45 KL |
288 | TTreeItem item = (TTreeItem) widget; |
289 | ||
d36057df | 290 | if (item == treeView.getSelected()) { |
7668cb45 KL |
291 | foundSelectedRow = true; |
292 | } | |
329fd62e | 293 | if (!foundSelectedRow) { |
7668cb45 KL |
294 | selectedRow++; |
295 | } | |
296 | ||
297 | int lineWidth = item.getText().length() | |
329fd62e | 298 | + item.getPrefix().length() + 4; |
7668cb45 KL |
299 | if (lineWidth > maxLineWidth) { |
300 | maxLineWidth = lineWidth; | |
301 | } | |
302 | } | |
0d47c546 | 303 | |
7668cb45 | 304 | if ((centerWindow) && (foundSelectedRow)) { |
56661844 | 305 | if ((selectedRow < getVerticalValue()) |
d36057df | 306 | || (selectedRow > getVerticalValue() + getHeight() - 3) |
7668cb45 | 307 | ) { |
d36057df | 308 | treeView.setTopLine(selectedRow); |
7668cb45 KL |
309 | centerWindow = false; |
310 | } | |
311 | } | |
d36057df | 312 | treeView.alignTree(); |
7668cb45 KL |
313 | |
314 | // Rescale the scroll bars | |
d36057df KL |
315 | setVerticalValue(treeView.getTopLine()); |
316 | setBottomValue(treeView.getTotalLineCount() - (getHeight() - 2)); | |
317 | if (getBottomValue() < getTopValue()) { | |
318 | setBottomValue(getTopValue()); | |
7668cb45 | 319 | } |
56661844 KL |
320 | if (getVerticalValue() > getBottomValue()) { |
321 | setVerticalValue(getBottomValue()); | |
7668cb45 | 322 | } |
d36057df | 323 | setRightValue(maxLineWidth - 4); |
56661844 KL |
324 | if (getHorizontalValue() > getRightValue()) { |
325 | setHorizontalValue(getRightValue()); | |
7668cb45 | 326 | } |
7668cb45 KL |
327 | } |
328 | ||
d36057df KL |
329 | // ------------------------------------------------------------------------ |
330 | // TTreeView -------------------------------------------------------------- | |
331 | // ------------------------------------------------------------------------ | |
332 | ||
7668cb45 | 333 | /** |
d36057df KL |
334 | * Get the underlying TTreeView. |
335 | * | |
336 | * @return the TTreeView | |
7668cb45 | 337 | */ |
d36057df KL |
338 | public TTreeView getTreeView() { |
339 | return treeView; | |
340 | } | |
7668cb45 | 341 | |
d36057df KL |
342 | /** |
343 | * Get the root of the tree. | |
344 | * | |
345 | * @return the root of the tree | |
346 | */ | |
347 | public final TTreeItem getTreeRoot() { | |
348 | return treeView.getTreeRoot(); | |
349 | } | |
0d47c546 | 350 | |
d36057df KL |
351 | /** |
352 | * Set the root of the tree. | |
353 | * | |
354 | * @param treeRoot the new root of the tree | |
355 | */ | |
356 | public final void setTreeRoot(final TTreeItem treeRoot) { | |
357 | treeView.setTreeRoot(treeRoot); | |
7668cb45 KL |
358 | } |
359 | ||
360 | /** | |
d36057df | 361 | * Set treeRoot. |
7668cb45 | 362 | * |
d36057df KL |
363 | * @param treeRoot ultimate root of tree |
364 | * @param centerWindow if true, move the window to put the root in view | |
7668cb45 | 365 | */ |
d36057df KL |
366 | public void setTreeRoot(final TTreeItem treeRoot, |
367 | final boolean centerWindow) { | |
7668cb45 | 368 | |
d36057df KL |
369 | treeView.setTreeRoot(treeRoot); |
370 | this.centerWindow = centerWindow; | |
7668cb45 KL |
371 | } |
372 | ||
373 | /** | |
d36057df | 374 | * Get the tree view item that was selected. |
7668cb45 | 375 | * |
d36057df | 376 | * @return the selected item, or null if no item is selected |
7668cb45 | 377 | */ |
d36057df KL |
378 | public final TTreeItem getSelected() { |
379 | return treeView.getSelected(); | |
7668cb45 KL |
380 | } |
381 | ||
382 | /** | |
d36057df | 383 | * Set the new selected tree view item. |
7668cb45 | 384 | * |
d36057df KL |
385 | * @param item new item that became selected |
386 | * @param centerWindow if true, move the window to put the selected into | |
387 | * view | |
7668cb45 | 388 | */ |
d36057df KL |
389 | public void setSelected(final TTreeItem item, final boolean centerWindow) { |
390 | treeView.setSelected(item, centerWindow); | |
391 | } | |
7668cb45 | 392 | |
d36057df KL |
393 | /** |
394 | * Perform user selection action. | |
395 | */ | |
396 | public void dispatch() { | |
397 | treeView.dispatch(); | |
7668cb45 KL |
398 | } |
399 | ||
400 | } |