2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2017 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]
31 import jexer
.event
.TResizeEvent
;
34 * TScrollableWidget is a convenience superclass for widgets that have
37 public class TScrollableWidget
extends TWidget
implements Scrollable
{
40 * The horizontal scrollbar.
42 protected THScroller hScroller
= null;
45 * The vertical scrollbar.
47 protected TVScroller vScroller
= null;
50 * Place the scrollbars on the edge of this widget, and adjust bigChange
51 * to match the new size. This is called by onResize().
53 protected void placeScrollbars() {
54 if (hScroller
!= null) {
55 hScroller
.setY(getHeight() - 1);
56 hScroller
.setWidth(getWidth() - 1);
57 hScroller
.setBigChange(getWidth() - 1);
59 if (vScroller
!= null) {
60 vScroller
.setX(getWidth() - 1);
61 vScroller
.setHeight(getHeight() - 1);
62 vScroller
.setBigChange(getHeight() - 1);
67 * Recompute whatever data is displayed by this widget.
69 public void reflowData() {
70 // Default: nothing to do
74 * Handle window/screen resize events.
76 * @param event resize event
79 public void onResize(final TResizeEvent event
) {
80 if (event
.getType() == TResizeEvent
.Type
.WIDGET
) {
81 setWidth(event
.getWidth());
82 setHeight(event
.getHeight());
88 super.onResize(event
);
93 * Protected constructor.
95 * @param parent parent widget
97 protected TScrollableWidget(final TWidget parent
) {
102 * Protected constructor.
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
110 protected TScrollableWidget(final TWidget parent
, final int x
, final int y
,
111 final int width
, final int height
) {
113 super(parent
, x
, y
, width
, height
);
117 * Protected constructor used by subclasses that are disabled by default.
119 * @param parent parent widget
120 * @param enabled if true assume enabled
122 protected TScrollableWidget(final TWidget parent
, final boolean enabled
) {
124 super(parent
, enabled
);
128 * Protected constructor used by subclasses that are disabled by default.
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
137 protected TScrollableWidget(final TWidget parent
, final boolean enabled
,
138 final int x
, final int y
, final int width
, final int height
) {
140 super(parent
, enabled
, x
, y
, width
, height
);
144 * Get the horizontal scrollbar, or null if this Viewport does not
145 * support horizontal scrolling.
147 * @return the horizontal scrollbar
149 public THScroller
getHorizontalScroller() {
154 * Get the vertical scrollbar, or null if this Viewport does not support
155 * vertical scrolling.
157 * @return the vertical scrollbar
159 public TVScroller
getVerticalScroller() {
164 * Get the value that corresponds to being on the top edge of the
165 * vertical scroll bar.
167 * @return the scroll value
169 public int getTopValue() {
170 if (vScroller
== null) {
173 return vScroller
.getTopValue();
178 * Set the value that corresponds to being on the top edge of the
179 * vertical scroll bar.
181 * @param topValue the new scroll value
183 public void setTopValue(final int topValue
) {
184 if (vScroller
== null) {
187 vScroller
.setTopValue(topValue
);
192 * Get the value that corresponds to being on the bottom edge of the
193 * vertical scroll bar.
195 * @return the scroll value
197 public int getBottomValue() {
198 if (vScroller
== null) {
201 return vScroller
.getBottomValue();
206 * Set the value that corresponds to being on the bottom edge of the
207 * vertical scroll bar.
209 * @param bottomValue the new scroll value
211 public void setBottomValue(final int bottomValue
) {
212 if (vScroller
== null) {
215 vScroller
.setBottomValue(bottomValue
);
220 * Get current value of the vertical scroll.
222 * @return the scroll value
224 public int getVerticalValue() {
225 if (vScroller
== null) {
228 return vScroller
.getValue();
233 * Set current value of the vertical scroll.
235 * @param value the new scroll value
237 public void setVerticalValue(final int value
) {
238 if (vScroller
== null) {
241 vScroller
.setValue(value
);
246 * Get the increment for clicking on an arrow on the vertical scrollbar.
248 * @return the increment value
250 public int getVerticalSmallChange() {
251 if (vScroller
== null) {
254 return vScroller
.getSmallChange();
259 * Set the increment for clicking on an arrow on the vertical scrollbar.
261 * @param smallChange the new increment value
263 public void setVerticalSmallChange(final int smallChange
) {
264 if (vScroller
== null) {
267 vScroller
.setSmallChange(smallChange
);
272 * Get the increment for clicking in the bar between the box and an
273 * arrow on the vertical scrollbar.
275 * @return the increment value
277 public int getVerticalBigChange() {
278 if (vScroller
== null) {
281 return vScroller
.getBigChange();
286 * Set the increment for clicking in the bar between the box and an
287 * arrow on the vertical scrollbar.
289 * @param bigChange the new increment value
291 public void setVerticalBigChange(final int bigChange
) {
292 if (vScroller
== null) {
295 vScroller
.setBigChange(bigChange
);
300 * Perform a small step change up.
302 public void verticalDecrement() {
303 if (vScroller
== null) {
306 vScroller
.decrement();
311 * Perform a small step change down.
313 public void verticalIncrement() {
314 if (vScroller
== null) {
317 vScroller
.increment();
322 * Perform a big step change up.
324 public void bigVerticalDecrement() {
325 if (vScroller
== null) {
328 vScroller
.bigDecrement();
333 * Perform a big step change down.
335 public void bigVerticalIncrement() {
336 if (vScroller
== null) {
339 vScroller
.bigIncrement();
344 * Go to the top edge of the vertical scroller.
346 public void toTop() {
347 if (vScroller
== null) {
355 * Go to the bottom edge of the vertical scroller.
357 public void toBottom() {
358 if (vScroller
== null) {
361 vScroller
.toBottom();
366 * Get the value that corresponds to being on the left edge of the
367 * horizontal scroll bar.
369 * @return the scroll value
371 public int getLeftValue() {
372 if (hScroller
== null) {
375 return hScroller
.getLeftValue();
380 * Set the value that corresponds to being on the left edge of the
381 * horizontal scroll bar.
383 * @param leftValue the new scroll value
385 public void setLeftValue(final int leftValue
) {
386 if (hScroller
== null) {
389 hScroller
.setLeftValue(leftValue
);
394 * Get the value that corresponds to being on the right edge of the
395 * horizontal scroll bar.
397 * @return the scroll value
399 public int getRightValue() {
400 if (hScroller
== null) {
403 return hScroller
.getRightValue();
408 * Set the value that corresponds to being on the right edge of the
409 * horizontal scroll bar.
411 * @param rightValue the new scroll value
413 public void setRightValue(final int rightValue
) {
414 if (hScroller
== null) {
417 hScroller
.setRightValue(rightValue
);
422 * Get current value of the horizontal scroll.
424 * @return the scroll value
426 public int getHorizontalValue() {
427 if (hScroller
== null) {
430 return hScroller
.getValue();
435 * Set current value of the horizontal scroll.
437 * @param value the new scroll value
439 public void setHorizontalValue(final int value
) {
440 if (hScroller
== null) {
443 hScroller
.setValue(value
);
448 * Get the increment for clicking on an arrow on the horizontal
451 * @return the increment value
453 public int getHorizontalSmallChange() {
454 if (hScroller
== null) {
457 return hScroller
.getSmallChange();
462 * Set the increment for clicking on an arrow on the horizontal
465 * @param smallChange the new increment value
467 public void setHorizontalSmallChange(final int smallChange
) {
468 if (hScroller
== null) {
471 hScroller
.setSmallChange(smallChange
);
476 * Get the increment for clicking in the bar between the box and an
477 * arrow on the horizontal scrollbar.
479 * @return the increment value
481 public int getHorizontalBigChange() {
482 if (hScroller
== null) {
485 return hScroller
.getBigChange();
490 * Set the increment for clicking in the bar between the box and an
491 * arrow on the horizontal scrollbar.
493 * @param bigChange the new increment value
495 public void setHorizontalBigChange(final int bigChange
) {
496 if (hScroller
== null) {
499 hScroller
.setBigChange(bigChange
);
504 * Perform a small step change left.
506 public void horizontalDecrement() {
507 if (hScroller
== null) {
510 hScroller
.decrement();
515 * Perform a small step change right.
517 public void horizontalIncrement() {
518 if (hScroller
== null) {
521 hScroller
.increment();
526 * Perform a big step change left.
528 public void bigHorizontalDecrement() {
529 if (hScroller
== null) {
532 hScroller
.bigDecrement();
537 * Perform a big step change right.
539 public void bigHorizontalIncrement() {
540 if (hScroller
== null) {
543 hScroller
.bigIncrement();
548 * Go to the left edge of the horizontal scroller.
550 public void toLeft() {
551 if (hScroller
== null) {
559 * Go to the right edge of the horizontal scroller.
561 public void toRight() {
562 if (hScroller
== null) {
570 * Go to the top-left edge of the horizontal and vertical scrollers.
572 public void toHome() {
573 if (hScroller
!= null) {
576 if (vScroller
!= null) {
582 * Go to the bottom-right edge of the horizontal and vertical scrollers.
584 public void toEnd() {
585 if (hScroller
!= null) {
588 if (vScroller
!= null) {
589 vScroller
.toBottom();