Commit | Line | Data |
---|---|---|
56661844 KL |
1 | /* |
2 | * Jexer - Java Text User Interface | |
3 | * | |
4 | * The MIT License (MIT) | |
5 | * | |
6 | * Copyright (C) 2017 Kevin Lamonte | |
7 | * | |
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: | |
14 | * | |
15 | * The above copyright notice and this permission notice shall be included in | |
16 | * all copies or substantial portions of the Software. | |
17 | * | |
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. | |
25 | * | |
26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
27 | * @version 1 | |
28 | */ | |
29 | package jexer; | |
30 | ||
31 | import jexer.event.TResizeEvent; | |
32 | ||
33 | /** | |
34 | * TScrollableWidget is a convenience superclass for widgets that have | |
35 | * scrollbars. | |
36 | */ | |
37 | public class TScrollableWidget extends TWidget implements Scrollable { | |
38 | ||
39 | /** | |
40 | * The horizontal scrollbar. | |
41 | */ | |
42 | protected THScroller hScroller = null; | |
43 | ||
44 | /** | |
45 | * The vertical scrollbar. | |
46 | */ | |
47 | protected TVScroller vScroller = null; | |
48 | ||
49 | /** | |
50 | * Place the scrollbars on the edge of this widget, and adjust bigChange | |
51 | * to match the new size. This is called by onResize(). | |
52 | */ | |
53 | protected void placeScrollbars() { | |
54 | if (hScroller != null) { | |
55 | hScroller.setY(getHeight() - 1); | |
56 | hScroller.setWidth(getWidth() - 1); | |
57 | hScroller.setBigChange(getWidth() - 1); | |
58 | } | |
59 | if (vScroller != null) { | |
60 | vScroller.setX(getWidth() - 1); | |
61 | vScroller.setHeight(getHeight() - 1); | |
62 | vScroller.setBigChange(getHeight() - 1); | |
63 | } | |
64 | } | |
65 | ||
66 | /** | |
67 | * Recompute whatever data is displayed by this widget. | |
68 | */ | |
69 | public void reflowData() { | |
70 | // Default: nothing to do | |
71 | } | |
72 | ||
73 | /** | |
74 | * Handle window/screen resize events. | |
75 | * | |
76 | * @param event resize event | |
77 | */ | |
78 | @Override | |
79 | public void onResize(final TResizeEvent event) { | |
80 | if (event.getType() == TResizeEvent.Type.WIDGET) { | |
81 | setWidth(event.getWidth()); | |
82 | setHeight(event.getHeight()); | |
83 | ||
84 | reflowData(); | |
85 | placeScrollbars(); | |
86 | return; | |
87 | } else { | |
88 | super.onResize(event); | |
89 | } | |
90 | } | |
91 | ||
92 | /** | |
93 | * Protected constructor. | |
94 | * | |
95 | * @param parent parent widget | |
96 | */ | |
97 | protected TScrollableWidget(final TWidget parent) { | |
98 | super(parent); | |
99 | } | |
100 | ||
101 | /** | |
102 | * Protected constructor. | |
103 | * | |
104 | * @param parent parent widget | |
105 | * @param x column relative to parent | |
106 | * @param y row relative to parent | |
107 | * @param width width of widget | |
108 | * @param height height of widget | |
109 | */ | |
110 | protected TScrollableWidget(final TWidget parent, final int x, final int y, | |
111 | final int width, final int height) { | |
112 | ||
113 | super(parent, x, y, width, height); | |
114 | } | |
115 | ||
116 | /** | |
117 | * Protected constructor used by subclasses that are disabled by default. | |
118 | * | |
119 | * @param parent parent widget | |
120 | * @param enabled if true assume enabled | |
121 | */ | |
122 | protected TScrollableWidget(final TWidget parent, final boolean enabled) { | |
123 | ||
124 | super(parent, enabled); | |
125 | } | |
126 | ||
127 | /** | |
128 | * Protected constructor used by subclasses that are disabled by default. | |
129 | * | |
130 | * @param parent parent widget | |
131 | * @param enabled if true assume enabled | |
132 | * @param x column relative to parent | |
133 | * @param y row relative to parent | |
134 | * @param width width of widget | |
135 | * @param height height of widget | |
136 | */ | |
137 | protected TScrollableWidget(final TWidget parent, final boolean enabled, | |
138 | final int x, final int y, final int width, final int height) { | |
139 | ||
140 | super(parent, enabled, x, y, width, height); | |
141 | } | |
142 | ||
143 | /** | |
144 | * Get the horizontal scrollbar, or null if this Viewport does not | |
145 | * support horizontal scrolling. | |
146 | * | |
147 | * @return the horizontal scrollbar | |
148 | */ | |
149 | public THScroller getHorizontalScroller() { | |
150 | return hScroller; | |
151 | } | |
152 | ||
153 | /** | |
154 | * Get the vertical scrollbar, or null if this Viewport does not support | |
155 | * vertical scrolling. | |
156 | * | |
157 | * @return the vertical scrollbar | |
158 | */ | |
159 | public TVScroller getVerticalScroller() { | |
160 | return vScroller; | |
161 | } | |
162 | ||
163 | /** | |
164 | * Get the value that corresponds to being on the top edge of the | |
165 | * vertical scroll bar. | |
166 | * | |
167 | * @return the scroll value | |
168 | */ | |
169 | public int getTopValue() { | |
170 | if (vScroller == null) { | |
171 | return 0; | |
172 | } else { | |
173 | return vScroller.getTopValue(); | |
174 | } | |
175 | } | |
176 | ||
177 | /** | |
178 | * Set the value that corresponds to being on the top edge of the | |
179 | * vertical scroll bar. | |
180 | * | |
181 | * @param topValue the new scroll value | |
182 | */ | |
183 | public void setTopValue(final int topValue) { | |
184 | if (vScroller == null) { | |
185 | return; | |
186 | } else { | |
187 | vScroller.setTopValue(topValue); | |
188 | } | |
189 | } | |
190 | ||
191 | /** | |
192 | * Get the value that corresponds to being on the bottom edge of the | |
193 | * vertical scroll bar. | |
194 | * | |
195 | * @return the scroll value | |
196 | */ | |
197 | public int getBottomValue() { | |
198 | if (vScroller == null) { | |
199 | return 0; | |
200 | } else { | |
201 | return vScroller.getBottomValue(); | |
202 | } | |
203 | } | |
204 | ||
205 | /** | |
206 | * Set the value that corresponds to being on the bottom edge of the | |
207 | * vertical scroll bar. | |
208 | * | |
209 | * @param bottomValue the new scroll value | |
210 | */ | |
211 | public void setBottomValue(final int bottomValue) { | |
212 | if (vScroller == null) { | |
213 | return; | |
214 | } else { | |
215 | vScroller.setBottomValue(bottomValue); | |
216 | } | |
217 | } | |
218 | ||
219 | /** | |
220 | * Get current value of the vertical scroll. | |
221 | * | |
222 | * @return the scroll value | |
223 | */ | |
224 | public int getVerticalValue() { | |
225 | if (vScroller == null) { | |
226 | return 0; | |
227 | } else { | |
228 | return vScroller.getValue(); | |
229 | } | |
230 | } | |
231 | ||
232 | /** | |
233 | * Set current value of the vertical scroll. | |
234 | * | |
235 | * @param value the new scroll value | |
236 | */ | |
237 | public void setVerticalValue(final int value) { | |
238 | if (vScroller == null) { | |
239 | return; | |
240 | } else { | |
241 | vScroller.setValue(value); | |
242 | } | |
243 | } | |
244 | ||
245 | /** | |
246 | * Get the increment for clicking on an arrow on the vertical scrollbar. | |
247 | * | |
248 | * @return the increment value | |
249 | */ | |
250 | public int getVerticalSmallChange() { | |
251 | if (vScroller == null) { | |
252 | return 0; | |
253 | } else { | |
254 | return vScroller.getSmallChange(); | |
255 | } | |
256 | } | |
257 | ||
258 | /** | |
259 | * Set the increment for clicking on an arrow on the vertical scrollbar. | |
260 | * | |
261 | * @param smallChange the new increment value | |
262 | */ | |
263 | public void setVerticalSmallChange(final int smallChange) { | |
264 | if (vScroller == null) { | |
265 | return; | |
266 | } else { | |
267 | vScroller.setSmallChange(smallChange); | |
268 | } | |
269 | } | |
270 | ||
271 | /** | |
272 | * Get the increment for clicking in the bar between the box and an | |
273 | * arrow on the vertical scrollbar. | |
274 | * | |
275 | * @return the increment value | |
276 | */ | |
277 | public int getVerticalBigChange() { | |
278 | if (vScroller == null) { | |
279 | return 0; | |
280 | } else { | |
281 | return vScroller.getBigChange(); | |
282 | } | |
283 | } | |
284 | ||
285 | /** | |
286 | * Set the increment for clicking in the bar between the box and an | |
287 | * arrow on the vertical scrollbar. | |
288 | * | |
289 | * @param bigChange the new increment value | |
290 | */ | |
291 | public void setVerticalBigChange(final int bigChange) { | |
292 | if (vScroller == null) { | |
293 | return; | |
294 | } else { | |
295 | vScroller.setBigChange(bigChange); | |
296 | } | |
297 | } | |
298 | ||
299 | /** | |
300 | * Perform a small step change up. | |
301 | */ | |
302 | public void verticalDecrement() { | |
303 | if (vScroller == null) { | |
304 | return; | |
305 | } else { | |
306 | vScroller.decrement(); | |
307 | } | |
308 | } | |
309 | ||
310 | /** | |
311 | * Perform a small step change down. | |
312 | */ | |
313 | public void verticalIncrement() { | |
314 | if (vScroller == null) { | |
315 | return; | |
316 | } else { | |
317 | vScroller.increment(); | |
318 | } | |
319 | } | |
320 | ||
321 | /** | |
322 | * Perform a big step change up. | |
323 | */ | |
324 | public void bigVerticalDecrement() { | |
325 | if (vScroller == null) { | |
326 | return; | |
327 | } else { | |
328 | vScroller.bigDecrement(); | |
329 | } | |
330 | } | |
331 | ||
332 | /** | |
333 | * Perform a big step change down. | |
334 | */ | |
335 | public void bigVerticalIncrement() { | |
336 | if (vScroller == null) { | |
337 | return; | |
338 | } else { | |
339 | vScroller.bigIncrement(); | |
340 | } | |
341 | } | |
342 | ||
343 | /** | |
344 | * Go to the top edge of the vertical scroller. | |
345 | */ | |
346 | public void toTop() { | |
347 | if (vScroller == null) { | |
348 | return; | |
349 | } else { | |
350 | vScroller.toTop(); | |
351 | } | |
352 | } | |
353 | ||
354 | /** | |
355 | * Go to the bottom edge of the vertical scroller. | |
356 | */ | |
357 | public void toBottom() { | |
358 | if (vScroller == null) { | |
359 | return; | |
360 | } else { | |
361 | vScroller.toBottom(); | |
362 | } | |
363 | } | |
364 | ||
365 | /** | |
366 | * Get the value that corresponds to being on the left edge of the | |
367 | * horizontal scroll bar. | |
368 | * | |
369 | * @return the scroll value | |
370 | */ | |
371 | public int getLeftValue() { | |
372 | if (hScroller == null) { | |
373 | return 0; | |
374 | } else { | |
375 | return hScroller.getLeftValue(); | |
376 | } | |
377 | } | |
378 | ||
379 | /** | |
380 | * Set the value that corresponds to being on the left edge of the | |
381 | * horizontal scroll bar. | |
382 | * | |
383 | * @param leftValue the new scroll value | |
384 | */ | |
385 | public void setLeftValue(final int leftValue) { | |
386 | if (hScroller == null) { | |
387 | return; | |
388 | } else { | |
389 | hScroller.setLeftValue(leftValue); | |
390 | } | |
391 | } | |
392 | ||
393 | /** | |
394 | * Get the value that corresponds to being on the right edge of the | |
395 | * horizontal scroll bar. | |
396 | * | |
397 | * @return the scroll value | |
398 | */ | |
399 | public int getRightValue() { | |
400 | if (hScroller == null) { | |
401 | return 0; | |
402 | } else { | |
403 | return hScroller.getRightValue(); | |
404 | } | |
405 | } | |
406 | ||
407 | /** | |
408 | * Set the value that corresponds to being on the right edge of the | |
409 | * horizontal scroll bar. | |
410 | * | |
411 | * @param rightValue the new scroll value | |
412 | */ | |
413 | public void setRightValue(final int rightValue) { | |
414 | if (hScroller == null) { | |
415 | return; | |
416 | } else { | |
417 | hScroller.setRightValue(rightValue); | |
418 | } | |
419 | } | |
420 | ||
421 | /** | |
422 | * Get current value of the horizontal scroll. | |
423 | * | |
424 | * @return the scroll value | |
425 | */ | |
426 | public int getHorizontalValue() { | |
427 | if (hScroller == null) { | |
428 | return 0; | |
429 | } else { | |
430 | return hScroller.getValue(); | |
431 | } | |
432 | } | |
433 | ||
434 | /** | |
435 | * Set current value of the horizontal scroll. | |
436 | * | |
437 | * @param value the new scroll value | |
438 | */ | |
439 | public void setHorizontalValue(final int value) { | |
440 | if (hScroller == null) { | |
441 | return; | |
442 | } else { | |
443 | hScroller.setValue(value); | |
444 | } | |
445 | } | |
446 | ||
447 | /** | |
448 | * Get the increment for clicking on an arrow on the horizontal | |
449 | * scrollbar. | |
450 | * | |
451 | * @return the increment value | |
452 | */ | |
453 | public int getHorizontalSmallChange() { | |
454 | if (hScroller == null) { | |
455 | return 0; | |
456 | } else { | |
457 | return hScroller.getSmallChange(); | |
458 | } | |
459 | } | |
460 | ||
461 | /** | |
462 | * Set the increment for clicking on an arrow on the horizontal | |
463 | * scrollbar. | |
464 | * | |
465 | * @param smallChange the new increment value | |
466 | */ | |
467 | public void setHorizontalSmallChange(final int smallChange) { | |
468 | if (hScroller == null) { | |
469 | return; | |
470 | } else { | |
471 | hScroller.setSmallChange(smallChange); | |
472 | } | |
473 | } | |
474 | ||
475 | /** | |
476 | * Get the increment for clicking in the bar between the box and an | |
477 | * arrow on the horizontal scrollbar. | |
478 | * | |
479 | * @return the increment value | |
480 | */ | |
481 | public int getHorizontalBigChange() { | |
482 | if (hScroller == null) { | |
483 | return 0; | |
484 | } else { | |
485 | return hScroller.getBigChange(); | |
486 | } | |
487 | } | |
488 | ||
489 | /** | |
490 | * Set the increment for clicking in the bar between the box and an | |
491 | * arrow on the horizontal scrollbar. | |
492 | * | |
493 | * @param bigChange the new increment value | |
494 | */ | |
495 | public void setHorizontalBigChange(final int bigChange) { | |
496 | if (hScroller == null) { | |
497 | return; | |
498 | } else { | |
499 | hScroller.setBigChange(bigChange); | |
500 | } | |
501 | } | |
502 | ||
503 | /** | |
504 | * Perform a small step change left. | |
505 | */ | |
506 | public void horizontalDecrement() { | |
507 | if (hScroller == null) { | |
508 | return; | |
509 | } else { | |
510 | hScroller.decrement(); | |
511 | } | |
512 | } | |
513 | ||
514 | /** | |
515 | * Perform a small step change right. | |
516 | */ | |
517 | public void horizontalIncrement() { | |
518 | if (hScroller == null) { | |
519 | return; | |
520 | } else { | |
521 | hScroller.increment(); | |
522 | } | |
523 | } | |
524 | ||
525 | /** | |
526 | * Perform a big step change left. | |
527 | */ | |
528 | public void bigHorizontalDecrement() { | |
529 | if (hScroller == null) { | |
530 | return; | |
531 | } else { | |
532 | hScroller.bigDecrement(); | |
533 | } | |
534 | } | |
535 | ||
536 | /** | |
537 | * Perform a big step change right. | |
538 | */ | |
539 | public void bigHorizontalIncrement() { | |
540 | if (hScroller == null) { | |
541 | return; | |
542 | } else { | |
543 | hScroller.bigIncrement(); | |
544 | } | |
545 | } | |
546 | ||
547 | /** | |
548 | * Go to the left edge of the horizontal scroller. | |
549 | */ | |
550 | public void toLeft() { | |
551 | if (hScroller == null) { | |
552 | return; | |
553 | } else { | |
554 | hScroller.toLeft(); | |
555 | } | |
556 | } | |
557 | ||
558 | /** | |
559 | * Go to the right edge of the horizontal scroller. | |
560 | */ | |
561 | public void toRight() { | |
562 | if (hScroller == null) { | |
563 | return; | |
564 | } else { | |
565 | hScroller.toRight(); | |
566 | } | |
567 | } | |
568 | ||
569 | /** | |
570 | * Go to the top-left edge of the horizontal and vertical scrollers. | |
571 | */ | |
572 | public void toHome() { | |
573 | if (hScroller != null) { | |
574 | hScroller.toLeft(); | |
575 | } | |
576 | if (vScroller != null) { | |
577 | vScroller.toTop(); | |
578 | } | |
579 | } | |
580 | ||
581 | /** | |
582 | * Go to the bottom-right edge of the horizontal and vertical scrollers. | |
583 | */ | |
584 | public void toEnd() { | |
585 | if (hScroller != null) { | |
586 | hScroller.toRight(); | |
587 | } | |
588 | if (vScroller != null) { | |
589 | vScroller.toBottom(); | |
590 | } | |
591 | } | |
592 | ||
593 | } |