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]
31 import jexer
.event
.TResizeEvent
;
34 * TScrollableWidget is a convenience superclass for widgets that have
37 public class TScrollableWidget
extends TWidget
implements Scrollable
{
39 // ------------------------------------------------------------------------
40 // Variables --------------------------------------------------------------
41 // ------------------------------------------------------------------------
44 * The horizontal scrollbar.
46 protected THScroller hScroller
= null;
49 * The vertical scrollbar.
51 protected TVScroller vScroller
= null;
53 // ------------------------------------------------------------------------
54 // Constructors -----------------------------------------------------------
55 // ------------------------------------------------------------------------
58 * Protected constructor.
60 * @param parent parent widget
62 protected TScrollableWidget(final TWidget parent
) {
67 * Protected constructor.
69 * @param parent parent widget
70 * @param x column relative to parent
71 * @param y row relative to parent
72 * @param width width of widget
73 * @param height height of widget
75 protected TScrollableWidget(final TWidget parent
, final int x
, final int y
,
76 final int width
, final int height
) {
78 super(parent
, x
, y
, width
, height
);
82 * Protected constructor used by subclasses that are disabled by default.
84 * @param parent parent widget
85 * @param enabled if true assume enabled
87 protected TScrollableWidget(final TWidget parent
, final boolean enabled
) {
89 super(parent
, enabled
);
93 * Protected constructor used by subclasses that are disabled by default.
95 * @param parent parent widget
96 * @param enabled if true assume enabled
97 * @param x column relative to parent
98 * @param y row relative to parent
99 * @param width width of widget
100 * @param height height of widget
102 protected TScrollableWidget(final TWidget parent
, final boolean enabled
,
103 final int x
, final int y
, final int width
, final int height
) {
105 super(parent
, enabled
, x
, y
, width
, height
);
108 // ------------------------------------------------------------------------
109 // TWidget ----------------------------------------------------------------
110 // ------------------------------------------------------------------------
113 * Handle window/screen resize events.
115 * @param event resize event
118 public void onResize(final TResizeEvent event
) {
119 if (event
.getType() == TResizeEvent
.Type
.WIDGET
) {
120 setWidth(event
.getWidth());
121 setHeight(event
.getHeight());
127 super.onResize(event
);
131 // ------------------------------------------------------------------------
132 // TScrollableWidget ------------------------------------------------------
133 // ------------------------------------------------------------------------
136 * Place the scrollbars on the edge of this widget, and adjust bigChange
137 * to match the new size. This is called by onResize().
139 protected void placeScrollbars() {
140 if (hScroller
!= null) {
141 hScroller
.setY(getHeight() - 1);
142 hScroller
.setWidth(getWidth() - 1);
143 hScroller
.setBigChange(getWidth() - 1);
145 if (vScroller
!= null) {
146 vScroller
.setX(getWidth() - 1);
147 vScroller
.setHeight(getHeight() - 1);
148 vScroller
.setBigChange(getHeight() - 1);
153 * Recompute whatever data is displayed by this widget.
155 public void reflowData() {
156 // Default: nothing to do
160 * Get the horizontal scrollbar, or null if this Viewport does not
161 * support horizontal scrolling.
163 * @return the horizontal scrollbar
165 public THScroller
getHorizontalScroller() {
170 * Get the vertical scrollbar, or null if this Viewport does not support
171 * vertical scrolling.
173 * @return the vertical scrollbar
175 public TVScroller
getVerticalScroller() {
180 * Get the value that corresponds to being on the top edge of the
181 * vertical scroll bar.
183 * @return the scroll value
185 public int getTopValue() {
186 if (vScroller
== null) {
189 return vScroller
.getTopValue();
194 * Set the value that corresponds to being on the top edge of the
195 * vertical scroll bar.
197 * @param topValue the new scroll value
199 public void setTopValue(final int topValue
) {
200 if (vScroller
== null) {
203 vScroller
.setTopValue(topValue
);
208 * Get the value that corresponds to being on the bottom edge of the
209 * vertical scroll bar.
211 * @return the scroll value
213 public int getBottomValue() {
214 if (vScroller
== null) {
217 return vScroller
.getBottomValue();
222 * Set the value that corresponds to being on the bottom edge of the
223 * vertical scroll bar.
225 * @param bottomValue the new scroll value
227 public void setBottomValue(final int bottomValue
) {
228 if (vScroller
== null) {
231 vScroller
.setBottomValue(bottomValue
);
236 * Get current value of the vertical scroll.
238 * @return the scroll value
240 public int getVerticalValue() {
241 if (vScroller
== null) {
244 return vScroller
.getValue();
249 * Set current value of the vertical scroll.
251 * @param value the new scroll value
253 public void setVerticalValue(final int value
) {
254 if (vScroller
== null) {
257 vScroller
.setValue(value
);
262 * Get the increment for clicking on an arrow on the vertical scrollbar.
264 * @return the increment value
266 public int getVerticalSmallChange() {
267 if (vScroller
== null) {
270 return vScroller
.getSmallChange();
275 * Set the increment for clicking on an arrow on the vertical scrollbar.
277 * @param smallChange the new increment value
279 public void setVerticalSmallChange(final int smallChange
) {
280 if (vScroller
== null) {
283 vScroller
.setSmallChange(smallChange
);
288 * Get the increment for clicking in the bar between the box and an
289 * arrow on the vertical scrollbar.
291 * @return the increment value
293 public int getVerticalBigChange() {
294 if (vScroller
== null) {
297 return vScroller
.getBigChange();
302 * Set the increment for clicking in the bar between the box and an
303 * arrow on the vertical scrollbar.
305 * @param bigChange the new increment value
307 public void setVerticalBigChange(final int bigChange
) {
308 if (vScroller
== null) {
311 vScroller
.setBigChange(bigChange
);
316 * Perform a small step change up.
318 public void verticalDecrement() {
319 if (vScroller
== null) {
322 vScroller
.decrement();
327 * Perform a small step change down.
329 public void verticalIncrement() {
330 if (vScroller
== null) {
333 vScroller
.increment();
338 * Perform a big step change up.
340 public void bigVerticalDecrement() {
341 if (vScroller
== null) {
344 vScroller
.bigDecrement();
349 * Perform a big step change down.
351 public void bigVerticalIncrement() {
352 if (vScroller
== null) {
355 vScroller
.bigIncrement();
360 * Go to the top edge of the vertical scroller.
362 public void toTop() {
363 if (vScroller
== null) {
371 * Go to the bottom edge of the vertical scroller.
373 public void toBottom() {
374 if (vScroller
== null) {
377 vScroller
.toBottom();
382 * Get the value that corresponds to being on the left edge of the
383 * horizontal scroll bar.
385 * @return the scroll value
387 public int getLeftValue() {
388 if (hScroller
== null) {
391 return hScroller
.getLeftValue();
396 * Set the value that corresponds to being on the left edge of the
397 * horizontal scroll bar.
399 * @param leftValue the new scroll value
401 public void setLeftValue(final int leftValue
) {
402 if (hScroller
== null) {
405 hScroller
.setLeftValue(leftValue
);
410 * Get the value that corresponds to being on the right edge of the
411 * horizontal scroll bar.
413 * @return the scroll value
415 public int getRightValue() {
416 if (hScroller
== null) {
419 return hScroller
.getRightValue();
424 * Set the value that corresponds to being on the right edge of the
425 * horizontal scroll bar.
427 * @param rightValue the new scroll value
429 public void setRightValue(final int rightValue
) {
430 if (hScroller
== null) {
433 hScroller
.setRightValue(rightValue
);
438 * Get current value of the horizontal scroll.
440 * @return the scroll value
442 public int getHorizontalValue() {
443 if (hScroller
== null) {
446 return hScroller
.getValue();
451 * Set current value of the horizontal scroll.
453 * @param value the new scroll value
455 public void setHorizontalValue(final int value
) {
456 if (hScroller
== null) {
459 hScroller
.setValue(value
);
464 * Get the increment for clicking on an arrow on the horizontal
467 * @return the increment value
469 public int getHorizontalSmallChange() {
470 if (hScroller
== null) {
473 return hScroller
.getSmallChange();
478 * Set the increment for clicking on an arrow on the horizontal
481 * @param smallChange the new increment value
483 public void setHorizontalSmallChange(final int smallChange
) {
484 if (hScroller
== null) {
487 hScroller
.setSmallChange(smallChange
);
492 * Get the increment for clicking in the bar between the box and an
493 * arrow on the horizontal scrollbar.
495 * @return the increment value
497 public int getHorizontalBigChange() {
498 if (hScroller
== null) {
501 return hScroller
.getBigChange();
506 * Set the increment for clicking in the bar between the box and an
507 * arrow on the horizontal scrollbar.
509 * @param bigChange the new increment value
511 public void setHorizontalBigChange(final int bigChange
) {
512 if (hScroller
== null) {
515 hScroller
.setBigChange(bigChange
);
520 * Perform a small step change left.
522 public void horizontalDecrement() {
523 if (hScroller
== null) {
526 hScroller
.decrement();
531 * Perform a small step change right.
533 public void horizontalIncrement() {
534 if (hScroller
== null) {
537 hScroller
.increment();
542 * Perform a big step change left.
544 public void bigHorizontalDecrement() {
545 if (hScroller
== null) {
548 hScroller
.bigDecrement();
553 * Perform a big step change right.
555 public void bigHorizontalIncrement() {
556 if (hScroller
== null) {
559 hScroller
.bigIncrement();
564 * Go to the left edge of the horizontal scroller.
566 public void toLeft() {
567 if (hScroller
== null) {
575 * Go to the right edge of the horizontal scroller.
577 public void toRight() {
578 if (hScroller
== null) {
586 * Go to the top-left edge of the horizontal and vertical scrollers.
588 public void toHome() {
589 if (hScroller
!= null) {
592 if (vScroller
!= null) {
598 * Go to the bottom-right edge of the horizontal and vertical scrollers.
600 public void toEnd() {
601 if (hScroller
!= null) {
604 if (vScroller
!= null) {
605 vScroller
.toBottom();