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
.TMouseEvent
;
32 import jexer
.event
.TResizeEvent
;
35 * TScrollableWindow is a convenience superclass for windows that have
38 public class TScrollableWindow
extends TWindow
implements Scrollable
{
41 * The horizontal scrollbar.
43 protected THScroller hScroller
= null;
46 * The vertical scrollbar.
48 protected TVScroller vScroller
= null;
51 * Place the scrollbars on the edge of this widget, and adjust bigChange
52 * to match the new size. This is called by onResize().
54 protected void placeScrollbars() {
55 if (hScroller
!= null) {
56 hScroller
.setY(getHeight() - 2);
57 hScroller
.setWidth(getWidth() - hScroller
.getX() - 3);
58 hScroller
.setBigChange(getWidth() - hScroller
.getX() - 3);
60 if (vScroller
!= null) {
61 vScroller
.setX(getWidth() - 2);
62 vScroller
.setHeight(getHeight() - 2);
63 vScroller
.setBigChange(getHeight() - 2);
68 * Recompute whatever data is displayed by this widget.
70 public void reflowData() {
71 // Default: nothing to do
75 * Handle window/screen resize events.
77 * @param event resize event
80 public void onResize(final TResizeEvent event
) {
81 if (event
.getType() == TResizeEvent
.Type
.WIDGET
) {
86 super.onResize(event
);
91 * Public constructor. Window will be located at (0, 0).
93 * @param application TApplication that manages this window
94 * @param title window title, will be centered along the top border
95 * @param width width of window
96 * @param height height of window
98 public TScrollableWindow(final TApplication application
, final String title
,
99 final int width
, final int height
) {
101 super(application
, title
, width
, height
);
105 * Public constructor. Window will be located at (0, 0).
107 * @param application TApplication that manages this window
108 * @param title window title, will be centered along the top border
109 * @param width width of window
110 * @param height height of window
111 * @param flags bitmask of RESIZABLE, CENTERED, or MODAL
113 public TScrollableWindow(final TApplication application
, final String title
,
114 final int width
, final int height
, final int flags
) {
116 super(application
, title
, width
, height
, flags
);
120 * Public constructor.
122 * @param application TApplication that manages this window
123 * @param title window title, will be centered along the top border
124 * @param x column relative to parent
125 * @param y row relative to parent
126 * @param width width of window
127 * @param height height of window
129 public TScrollableWindow(final TApplication application
, final String title
,
130 final int x
, final int y
, final int width
, final int height
) {
132 super(application
, title
, x
, y
, width
, height
);
136 * Public constructor.
138 * @param application TApplication that manages this window
139 * @param title window title, will be centered along the top border
140 * @param x column relative to parent
141 * @param y row relative to parent
142 * @param width width of window
143 * @param height height of window
144 * @param flags mask of RESIZABLE, CENTERED, or MODAL
146 public TScrollableWindow(final TApplication application
, final String title
,
147 final int x
, final int y
, final int width
, final int height
,
150 super(application
, title
, x
, y
, width
, height
, flags
);
154 * Get the horizontal scrollbar, or null if this Viewport does not
155 * support horizontal scrolling.
157 * @return the horizontal scrollbar
159 public THScroller
getHorizontalScroller() {
164 * Get the vertical scrollbar, or null if this Viewport does not support
165 * vertical scrolling.
167 * @return the vertical scrollbar
169 public TVScroller
getVerticalScroller() {
174 * Get the value that corresponds to being on the top edge of the
175 * vertical scroll bar.
177 * @return the scroll value
179 public int getTopValue() {
180 if (vScroller
== null) {
183 return vScroller
.getTopValue();
188 * Set the value that corresponds to being on the top edge of the
189 * vertical scroll bar.
191 * @param topValue the new scroll value
193 public void setTopValue(final int topValue
) {
194 if (vScroller
== null) {
197 vScroller
.setTopValue(topValue
);
202 * Get the value that corresponds to being on the bottom edge of the
203 * vertical scroll bar.
205 * @return the scroll value
207 public int getBottomValue() {
208 if (vScroller
== null) {
211 return vScroller
.getBottomValue();
216 * Set the value that corresponds to being on the bottom edge of the
217 * vertical scroll bar.
219 * @param bottomValue the new scroll value
221 public void setBottomValue(final int bottomValue
) {
222 if (vScroller
== null) {
225 vScroller
.setBottomValue(bottomValue
);
230 * Get current value of the vertical scroll.
232 * @return the scroll value
234 public int getVerticalValue() {
235 if (vScroller
== null) {
238 return vScroller
.getValue();
243 * Set current value of the vertical scroll.
245 * @param value the new scroll value
247 public void setVerticalValue(final int value
) {
248 if (vScroller
== null) {
251 vScroller
.setValue(value
);
256 * Get the increment for clicking on an arrow on the vertical scrollbar.
258 * @return the increment value
260 public int getVerticalSmallChange() {
261 if (vScroller
== null) {
264 return vScroller
.getSmallChange();
269 * Set the increment for clicking on an arrow on the vertical scrollbar.
271 * @param smallChange the new increment value
273 public void setVerticalSmallChange(final int smallChange
) {
274 if (vScroller
== null) {
277 vScroller
.setSmallChange(smallChange
);
282 * Get the increment for clicking in the bar between the box and an
283 * arrow on the vertical scrollbar.
285 * @return the increment value
287 public int getVerticalBigChange() {
288 if (vScroller
== null) {
291 return vScroller
.getBigChange();
296 * Set the increment for clicking in the bar between the box and an
297 * arrow on the vertical scrollbar.
299 * @param bigChange the new increment value
301 public void setVerticalBigChange(final int bigChange
) {
302 if (vScroller
== null) {
305 vScroller
.setBigChange(bigChange
);
310 * Perform a small step change up.
312 public void verticalDecrement() {
313 if (vScroller
== null) {
316 vScroller
.decrement();
321 * Perform a small step change down.
323 public void verticalIncrement() {
324 if (vScroller
== null) {
327 vScroller
.increment();
332 * Perform a big step change up.
334 public void bigVerticalDecrement() {
335 if (vScroller
== null) {
338 vScroller
.bigDecrement();
343 * Perform a big step change down.
345 public void bigVerticalIncrement() {
346 if (vScroller
== null) {
349 vScroller
.bigIncrement();
354 * Go to the top edge of the vertical scroller.
356 public void toTop() {
357 if (vScroller
== null) {
365 * Go to the bottom edge of the vertical scroller.
367 public void toBottom() {
368 if (vScroller
== null) {
371 vScroller
.toBottom();
376 * Get the value that corresponds to being on the left edge of the
377 * horizontal scroll bar.
379 * @return the scroll value
381 public int getLeftValue() {
382 if (hScroller
== null) {
385 return hScroller
.getLeftValue();
390 * Set the value that corresponds to being on the left edge of the
391 * horizontal scroll bar.
393 * @param leftValue the new scroll value
395 public void setLeftValue(final int leftValue
) {
396 if (hScroller
== null) {
399 hScroller
.setLeftValue(leftValue
);
404 * Get the value that corresponds to being on the right edge of the
405 * horizontal scroll bar.
407 * @return the scroll value
409 public int getRightValue() {
410 if (hScroller
== null) {
413 return hScroller
.getRightValue();
418 * Set the value that corresponds to being on the right edge of the
419 * horizontal scroll bar.
421 * @param rightValue the new scroll value
423 public void setRightValue(final int rightValue
) {
424 if (hScroller
== null) {
427 hScroller
.setRightValue(rightValue
);
432 * Get current value of the horizontal scroll.
434 * @return the scroll value
436 public int getHorizontalValue() {
437 if (hScroller
== null) {
440 return hScroller
.getValue();
445 * Set current value of the horizontal scroll.
447 * @param value the new scroll value
449 public void setHorizontalValue(final int value
) {
450 if (hScroller
== null) {
453 hScroller
.setValue(value
);
458 * Get the increment for clicking on an arrow on the horizontal
461 * @return the increment value
463 public int getHorizontalSmallChange() {
464 if (hScroller
== null) {
467 return hScroller
.getSmallChange();
472 * Set the increment for clicking on an arrow on the horizontal
475 * @param smallChange the new increment value
477 public void setHorizontalSmallChange(final int smallChange
) {
478 if (hScroller
== null) {
481 hScroller
.setSmallChange(smallChange
);
486 * Get the increment for clicking in the bar between the box and an
487 * arrow on the horizontal scrollbar.
489 * @return the increment value
491 public int getHorizontalBigChange() {
492 if (hScroller
== null) {
495 return hScroller
.getBigChange();
500 * Set the increment for clicking in the bar between the box and an
501 * arrow on the horizontal scrollbar.
503 * @param bigChange the new increment value
505 public void setHorizontalBigChange(final int bigChange
) {
506 if (hScroller
== null) {
509 hScroller
.setBigChange(bigChange
);
514 * Perform a small step change left.
516 public void horizontalDecrement() {
517 if (hScroller
== null) {
520 hScroller
.decrement();
525 * Perform a small step change right.
527 public void horizontalIncrement() {
528 if (hScroller
== null) {
531 hScroller
.increment();
536 * Perform a big step change left.
538 public void bigHorizontalDecrement() {
539 if (hScroller
== null) {
542 hScroller
.bigDecrement();
547 * Perform a big step change right.
549 public void bigHorizontalIncrement() {
550 if (hScroller
== null) {
553 hScroller
.bigIncrement();
558 * Go to the left edge of the horizontal scroller.
560 public void toLeft() {
561 if (hScroller
== null) {
569 * Go to the right edge of the horizontal scroller.
571 public void toRight() {
572 if (hScroller
== null) {
580 * Go to the top-left edge of the horizontal and vertical scrollers.
582 public void toHome() {
583 if (hScroller
!= null) {
586 if (vScroller
!= null) {
592 * Go to the bottom-right edge of the horizontal and vertical scrollers.
594 public void toEnd() {
595 if (hScroller
!= null) {
598 if (vScroller
!= null) {
599 vScroller
.toBottom();
604 * Check if a mouse press/release/motion event coordinate is over the
605 * vertical scrollbar.
607 * @param mouse a mouse-based event
608 * @return whether or not the mouse is on the scrollbar
610 protected final boolean mouseOnVerticalScroller(final TMouseEvent mouse
) {
611 if (vScroller
== null) {
614 if ((mouse
.getAbsoluteX() == vScroller
.getAbsoluteX())
615 && (mouse
.getAbsoluteY() >= vScroller
.getAbsoluteY())
616 && (mouse
.getAbsoluteY() < vScroller
.getAbsoluteY() +
617 vScroller
.getHeight())
625 * Check if a mouse press/release/motion event coordinate is over the
626 * horizontal scrollbar.
628 * @param mouse a mouse-based event
629 * @return whether or not the mouse is on the scrollbar
631 protected final boolean mouseOnHorizontalScroller(final TMouseEvent mouse
) {
632 if (hScroller
== null) {
635 if ((mouse
.getAbsoluteY() == hScroller
.getAbsoluteY())
636 && (mouse
.getAbsoluteX() >= hScroller
.getAbsoluteX())
637 && (mouse
.getAbsoluteX() < hScroller
.getAbsoluteX() +
638 hScroller
.getWidth())