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
.TMouseEvent
;
32 import jexer
.event
.TResizeEvent
;
35 * TScrollableWindow is a convenience superclass for windows that have
38 public class TScrollableWindow
extends TWindow
implements Scrollable
{
40 // ------------------------------------------------------------------------
41 // Variables --------------------------------------------------------------
42 // ------------------------------------------------------------------------
45 * The horizontal scrollbar.
47 protected THScroller hScroller
= null;
50 * The vertical scrollbar.
52 protected TVScroller vScroller
= null;
54 // ------------------------------------------------------------------------
55 // Constructors -----------------------------------------------------------
56 // ------------------------------------------------------------------------
59 * Public constructor. Window will be located at (0, 0).
61 * @param application TApplication that manages this window
62 * @param title window title, will be centered along the top border
63 * @param width width of window
64 * @param height height of window
66 public TScrollableWindow(final TApplication application
, final String title
,
67 final int width
, final int height
) {
69 super(application
, title
, width
, height
);
73 * Public constructor. Window will be located at (0, 0).
75 * @param application TApplication that manages this window
76 * @param title window title, will be centered along the top border
77 * @param width width of window
78 * @param height height of window
79 * @param flags bitmask of RESIZABLE, CENTERED, or MODAL
81 public TScrollableWindow(final TApplication application
, final String title
,
82 final int width
, final int height
, final int flags
) {
84 super(application
, title
, width
, height
, flags
);
90 * @param application TApplication that manages this window
91 * @param title window title, will be centered along the top border
92 * @param x column relative to parent
93 * @param y row relative to parent
94 * @param width width of window
95 * @param height height of window
97 public TScrollableWindow(final TApplication application
, final String title
,
98 final int x
, final int y
, final int width
, final int height
) {
100 super(application
, title
, x
, y
, width
, height
);
104 * Public constructor.
106 * @param application TApplication that manages this window
107 * @param title window title, will be centered along the top border
108 * @param x column relative to parent
109 * @param y row relative to parent
110 * @param width width of window
111 * @param height height of window
112 * @param flags mask of RESIZABLE, CENTERED, or MODAL
114 public TScrollableWindow(final TApplication application
, final String title
,
115 final int x
, final int y
, final int width
, final int height
,
118 super(application
, title
, x
, y
, width
, height
, flags
);
121 // ------------------------------------------------------------------------
122 // TWindow ----------------------------------------------------------------
123 // ------------------------------------------------------------------------
126 * Handle window/screen resize events.
128 * @param event resize event
131 public void onResize(final TResizeEvent event
) {
132 if (event
.getType() == TResizeEvent
.Type
.WIDGET
) {
137 super.onResize(event
);
145 public void maximize() {
151 * Restore (unmaximize) window.
154 public void restore() {
159 // ------------------------------------------------------------------------
160 // TScrollableWindow ------------------------------------------------------
161 // ------------------------------------------------------------------------
164 * Place the scrollbars on the edge of this widget, and adjust bigChange
165 * to match the new size. This is called by onResize().
167 protected void placeScrollbars() {
168 if (hScroller
!= null) {
169 hScroller
.setY(getHeight() - 2);
170 hScroller
.setWidth(getWidth() - hScroller
.getX() - 3);
171 hScroller
.setBigChange(getWidth() - hScroller
.getX() - 3);
173 if (vScroller
!= null) {
174 vScroller
.setX(getWidth() - 2);
175 vScroller
.setHeight(getHeight() - 2);
176 vScroller
.setBigChange(getHeight() - 2);
181 * Recompute whatever data is displayed by this widget.
183 public void reflowData() {
184 // Default: nothing to do
188 * Get the horizontal scrollbar, or null if this Viewport does not
189 * support horizontal scrolling.
191 * @return the horizontal scrollbar
193 public THScroller
getHorizontalScroller() {
198 * Get the vertical scrollbar, or null if this Viewport does not support
199 * vertical scrolling.
201 * @return the vertical scrollbar
203 public TVScroller
getVerticalScroller() {
208 * Get the value that corresponds to being on the top edge of the
209 * vertical scroll bar.
211 * @return the scroll value
213 public int getTopValue() {
214 if (vScroller
== null) {
217 return vScroller
.getTopValue();
222 * Set the value that corresponds to being on the top edge of the
223 * vertical scroll bar.
225 * @param topValue the new scroll value
227 public void setTopValue(final int topValue
) {
228 if (vScroller
== null) {
231 vScroller
.setTopValue(topValue
);
236 * Get the value that corresponds to being on the bottom edge of the
237 * vertical scroll bar.
239 * @return the scroll value
241 public int getBottomValue() {
242 if (vScroller
== null) {
245 return vScroller
.getBottomValue();
250 * Set the value that corresponds to being on the bottom edge of the
251 * vertical scroll bar.
253 * @param bottomValue the new scroll value
255 public void setBottomValue(final int bottomValue
) {
256 if (vScroller
== null) {
259 vScroller
.setBottomValue(bottomValue
);
264 * Get current value of the vertical scroll.
266 * @return the scroll value
268 public int getVerticalValue() {
269 if (vScroller
== null) {
272 return vScroller
.getValue();
277 * Set current value of the vertical scroll.
279 * @param value the new scroll value
281 public void setVerticalValue(final int value
) {
282 if (vScroller
== null) {
285 vScroller
.setValue(value
);
290 * Get the increment for clicking on an arrow on the vertical scrollbar.
292 * @return the increment value
294 public int getVerticalSmallChange() {
295 if (vScroller
== null) {
298 return vScroller
.getSmallChange();
303 * Set the increment for clicking on an arrow on the vertical scrollbar.
305 * @param smallChange the new increment value
307 public void setVerticalSmallChange(final int smallChange
) {
308 if (vScroller
== null) {
311 vScroller
.setSmallChange(smallChange
);
316 * Get the increment for clicking in the bar between the box and an
317 * arrow on the vertical scrollbar.
319 * @return the increment value
321 public int getVerticalBigChange() {
322 if (vScroller
== null) {
325 return vScroller
.getBigChange();
330 * Set the increment for clicking in the bar between the box and an
331 * arrow on the vertical scrollbar.
333 * @param bigChange the new increment value
335 public void setVerticalBigChange(final int bigChange
) {
336 if (vScroller
== null) {
339 vScroller
.setBigChange(bigChange
);
344 * Perform a small step change up.
346 public void verticalDecrement() {
347 if (vScroller
== null) {
350 vScroller
.decrement();
355 * Perform a small step change down.
357 public void verticalIncrement() {
358 if (vScroller
== null) {
361 vScroller
.increment();
366 * Perform a big step change up.
368 public void bigVerticalDecrement() {
369 if (vScroller
== null) {
372 vScroller
.bigDecrement();
377 * Perform a big step change down.
379 public void bigVerticalIncrement() {
380 if (vScroller
== null) {
383 vScroller
.bigIncrement();
388 * Go to the top edge of the vertical scroller.
390 public void toTop() {
391 if (vScroller
== null) {
399 * Go to the bottom edge of the vertical scroller.
401 public void toBottom() {
402 if (vScroller
== null) {
405 vScroller
.toBottom();
410 * Get the value that corresponds to being on the left edge of the
411 * horizontal scroll bar.
413 * @return the scroll value
415 public int getLeftValue() {
416 if (hScroller
== null) {
419 return hScroller
.getLeftValue();
424 * Set the value that corresponds to being on the left edge of the
425 * horizontal scroll bar.
427 * @param leftValue the new scroll value
429 public void setLeftValue(final int leftValue
) {
430 if (hScroller
== null) {
433 hScroller
.setLeftValue(leftValue
);
438 * Get the value that corresponds to being on the right edge of the
439 * horizontal scroll bar.
441 * @return the scroll value
443 public int getRightValue() {
444 if (hScroller
== null) {
447 return hScroller
.getRightValue();
452 * Set the value that corresponds to being on the right edge of the
453 * horizontal scroll bar.
455 * @param rightValue the new scroll value
457 public void setRightValue(final int rightValue
) {
458 if (hScroller
== null) {
461 hScroller
.setRightValue(rightValue
);
466 * Get current value of the horizontal scroll.
468 * @return the scroll value
470 public int getHorizontalValue() {
471 if (hScroller
== null) {
474 return hScroller
.getValue();
479 * Set current value of the horizontal scroll.
481 * @param value the new scroll value
483 public void setHorizontalValue(final int value
) {
484 if (hScroller
== null) {
487 hScroller
.setValue(value
);
492 * Get the increment for clicking on an arrow on the horizontal
495 * @return the increment value
497 public int getHorizontalSmallChange() {
498 if (hScroller
== null) {
501 return hScroller
.getSmallChange();
506 * Set the increment for clicking on an arrow on the horizontal
509 * @param smallChange the new increment value
511 public void setHorizontalSmallChange(final int smallChange
) {
512 if (hScroller
== null) {
515 hScroller
.setSmallChange(smallChange
);
520 * Get the increment for clicking in the bar between the box and an
521 * arrow on the horizontal scrollbar.
523 * @return the increment value
525 public int getHorizontalBigChange() {
526 if (hScroller
== null) {
529 return hScroller
.getBigChange();
534 * Set the increment for clicking in the bar between the box and an
535 * arrow on the horizontal scrollbar.
537 * @param bigChange the new increment value
539 public void setHorizontalBigChange(final int bigChange
) {
540 if (hScroller
== null) {
543 hScroller
.setBigChange(bigChange
);
548 * Perform a small step change left.
550 public void horizontalDecrement() {
551 if (hScroller
== null) {
554 hScroller
.decrement();
559 * Perform a small step change right.
561 public void horizontalIncrement() {
562 if (hScroller
== null) {
565 hScroller
.increment();
570 * Perform a big step change left.
572 public void bigHorizontalDecrement() {
573 if (hScroller
== null) {
576 hScroller
.bigDecrement();
581 * Perform a big step change right.
583 public void bigHorizontalIncrement() {
584 if (hScroller
== null) {
587 hScroller
.bigIncrement();
592 * Go to the left edge of the horizontal scroller.
594 public void toLeft() {
595 if (hScroller
== null) {
603 * Go to the right edge of the horizontal scroller.
605 public void toRight() {
606 if (hScroller
== null) {
614 * Go to the top-left edge of the horizontal and vertical scrollers.
616 public void toHome() {
617 if (hScroller
!= null) {
620 if (vScroller
!= null) {
626 * Go to the bottom-right edge of the horizontal and vertical scrollers.
628 public void toEnd() {
629 if (hScroller
!= null) {
632 if (vScroller
!= null) {
633 vScroller
.toBottom();
638 * Check if a mouse press/release/motion event coordinate is over the
639 * vertical scrollbar.
641 * @param mouse a mouse-based event
642 * @return whether or not the mouse is on the scrollbar
644 protected final boolean mouseOnVerticalScroller(final TMouseEvent mouse
) {
645 if (vScroller
== null) {
648 if ((mouse
.getAbsoluteX() == vScroller
.getAbsoluteX())
649 && (mouse
.getAbsoluteY() >= vScroller
.getAbsoluteY())
650 && (mouse
.getAbsoluteY() < vScroller
.getAbsoluteY() +
651 vScroller
.getHeight())
659 * Check if a mouse press/release/motion event coordinate is over the
660 * horizontal scrollbar.
662 * @param mouse a mouse-based event
663 * @return whether or not the mouse is on the scrollbar
665 protected final boolean mouseOnHorizontalScroller(final TMouseEvent mouse
) {
666 if (hScroller
== null) {
669 if ((mouse
.getAbsoluteY() == hScroller
.getAbsoluteY())
670 && (mouse
.getAbsoluteX() >= hScroller
.getAbsoluteX())
671 && (mouse
.getAbsoluteX() < hScroller
.getAbsoluteX() +
672 hScroller
.getWidth())