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
.setX(Math
.min(Math
.max(0, getWidth() - 17), 17));
170 hScroller
.setY(getHeight() - 2);
171 hScroller
.setWidth(getWidth() - hScroller
.getX() - 3);
172 hScroller
.setBigChange(getWidth() - hScroller
.getX() - 3);
174 if (vScroller
!= null) {
175 vScroller
.setX(getWidth() - 2);
176 vScroller
.setHeight(getHeight() - 2);
177 vScroller
.setBigChange(getHeight() - 2);
182 * Recompute whatever data is displayed by this widget.
184 public void reflowData() {
185 // Default: nothing to do
189 * Get the horizontal scrollbar, or null if this Viewport does not
190 * support horizontal scrolling.
192 * @return the horizontal scrollbar
194 public THScroller
getHorizontalScroller() {
199 * Get the vertical scrollbar, or null if this Viewport does not support
200 * vertical scrolling.
202 * @return the vertical scrollbar
204 public TVScroller
getVerticalScroller() {
209 * Get the value that corresponds to being on the top edge of the
210 * vertical scroll bar.
212 * @return the scroll value
214 public int getTopValue() {
215 if (vScroller
== null) {
218 return vScroller
.getTopValue();
223 * Set the value that corresponds to being on the top edge of the
224 * vertical scroll bar.
226 * @param topValue the new scroll value
228 public void setTopValue(final int topValue
) {
229 if (vScroller
== null) {
232 vScroller
.setTopValue(topValue
);
237 * Get the value that corresponds to being on the bottom edge of the
238 * vertical scroll bar.
240 * @return the scroll value
242 public int getBottomValue() {
243 if (vScroller
== null) {
246 return vScroller
.getBottomValue();
251 * Set the value that corresponds to being on the bottom edge of the
252 * vertical scroll bar.
254 * @param bottomValue the new scroll value
256 public void setBottomValue(final int bottomValue
) {
257 if (vScroller
== null) {
260 vScroller
.setBottomValue(bottomValue
);
265 * Get current value of the vertical scroll.
267 * @return the scroll value
269 public int getVerticalValue() {
270 if (vScroller
== null) {
273 return vScroller
.getValue();
278 * Set current value of the vertical scroll.
280 * @param value the new scroll value
282 public void setVerticalValue(final int value
) {
283 if (vScroller
== null) {
286 vScroller
.setValue(value
);
291 * Get the increment for clicking on an arrow on the vertical scrollbar.
293 * @return the increment value
295 public int getVerticalSmallChange() {
296 if (vScroller
== null) {
299 return vScroller
.getSmallChange();
304 * Set the increment for clicking on an arrow on the vertical scrollbar.
306 * @param smallChange the new increment value
308 public void setVerticalSmallChange(final int smallChange
) {
309 if (vScroller
== null) {
312 vScroller
.setSmallChange(smallChange
);
317 * Get the increment for clicking in the bar between the box and an
318 * arrow on the vertical scrollbar.
320 * @return the increment value
322 public int getVerticalBigChange() {
323 if (vScroller
== null) {
326 return vScroller
.getBigChange();
331 * Set the increment for clicking in the bar between the box and an
332 * arrow on the vertical scrollbar.
334 * @param bigChange the new increment value
336 public void setVerticalBigChange(final int bigChange
) {
337 if (vScroller
== null) {
340 vScroller
.setBigChange(bigChange
);
345 * Perform a small step change up.
347 public void verticalDecrement() {
348 if (vScroller
== null) {
351 vScroller
.decrement();
356 * Perform a small step change down.
358 public void verticalIncrement() {
359 if (vScroller
== null) {
362 vScroller
.increment();
367 * Perform a big step change up.
369 public void bigVerticalDecrement() {
370 if (vScroller
== null) {
373 vScroller
.bigDecrement();
378 * Perform a big step change down.
380 public void bigVerticalIncrement() {
381 if (vScroller
== null) {
384 vScroller
.bigIncrement();
389 * Go to the top edge of the vertical scroller.
391 public void toTop() {
392 if (vScroller
== null) {
400 * Go to the bottom edge of the vertical scroller.
402 public void toBottom() {
403 if (vScroller
== null) {
406 vScroller
.toBottom();
411 * Get the value that corresponds to being on the left edge of the
412 * horizontal scroll bar.
414 * @return the scroll value
416 public int getLeftValue() {
417 if (hScroller
== null) {
420 return hScroller
.getLeftValue();
425 * Set the value that corresponds to being on the left edge of the
426 * horizontal scroll bar.
428 * @param leftValue the new scroll value
430 public void setLeftValue(final int leftValue
) {
431 if (hScroller
== null) {
434 hScroller
.setLeftValue(leftValue
);
439 * Get the value that corresponds to being on the right edge of the
440 * horizontal scroll bar.
442 * @return the scroll value
444 public int getRightValue() {
445 if (hScroller
== null) {
448 return hScroller
.getRightValue();
453 * Set the value that corresponds to being on the right edge of the
454 * horizontal scroll bar.
456 * @param rightValue the new scroll value
458 public void setRightValue(final int rightValue
) {
459 if (hScroller
== null) {
462 hScroller
.setRightValue(rightValue
);
467 * Get current value of the horizontal scroll.
469 * @return the scroll value
471 public int getHorizontalValue() {
472 if (hScroller
== null) {
475 return hScroller
.getValue();
480 * Set current value of the horizontal scroll.
482 * @param value the new scroll value
484 public void setHorizontalValue(final int value
) {
485 if (hScroller
== null) {
488 hScroller
.setValue(value
);
493 * Get the increment for clicking on an arrow on the horizontal
496 * @return the increment value
498 public int getHorizontalSmallChange() {
499 if (hScroller
== null) {
502 return hScroller
.getSmallChange();
507 * Set the increment for clicking on an arrow on the horizontal
510 * @param smallChange the new increment value
512 public void setHorizontalSmallChange(final int smallChange
) {
513 if (hScroller
== null) {
516 hScroller
.setSmallChange(smallChange
);
521 * Get the increment for clicking in the bar between the box and an
522 * arrow on the horizontal scrollbar.
524 * @return the increment value
526 public int getHorizontalBigChange() {
527 if (hScroller
== null) {
530 return hScroller
.getBigChange();
535 * Set the increment for clicking in the bar between the box and an
536 * arrow on the horizontal scrollbar.
538 * @param bigChange the new increment value
540 public void setHorizontalBigChange(final int bigChange
) {
541 if (hScroller
== null) {
544 hScroller
.setBigChange(bigChange
);
549 * Perform a small step change left.
551 public void horizontalDecrement() {
552 if (hScroller
== null) {
555 hScroller
.decrement();
560 * Perform a small step change right.
562 public void horizontalIncrement() {
563 if (hScroller
== null) {
566 hScroller
.increment();
571 * Perform a big step change left.
573 public void bigHorizontalDecrement() {
574 if (hScroller
== null) {
577 hScroller
.bigDecrement();
582 * Perform a big step change right.
584 public void bigHorizontalIncrement() {
585 if (hScroller
== null) {
588 hScroller
.bigIncrement();
593 * Go to the left edge of the horizontal scroller.
595 public void toLeft() {
596 if (hScroller
== null) {
604 * Go to the right edge of the horizontal scroller.
606 public void toRight() {
607 if (hScroller
== null) {
615 * Go to the top-left edge of the horizontal and vertical scrollers.
617 public void toHome() {
618 if (hScroller
!= null) {
621 if (vScroller
!= null) {
627 * Go to the bottom-right edge of the horizontal and vertical scrollers.
629 public void toEnd() {
630 if (hScroller
!= null) {
633 if (vScroller
!= null) {
634 vScroller
.toBottom();
639 * Check if a mouse press/release/motion event coordinate is over the
640 * vertical scrollbar.
642 * @param mouse a mouse-based event
643 * @return whether or not the mouse is on the scrollbar
645 protected final boolean mouseOnVerticalScroller(final TMouseEvent mouse
) {
646 if (vScroller
== null) {
649 if ((mouse
.getAbsoluteX() == vScroller
.getAbsoluteX())
650 && (mouse
.getAbsoluteY() >= vScroller
.getAbsoluteY())
651 && (mouse
.getAbsoluteY() < vScroller
.getAbsoluteY() +
652 vScroller
.getHeight())
660 * Check if a mouse press/release/motion event coordinate is over the
661 * horizontal scrollbar.
663 * @param mouse a mouse-based event
664 * @return whether or not the mouse is on the scrollbar
666 protected final boolean mouseOnHorizontalScroller(final TMouseEvent mouse
) {
667 if (hScroller
== null) {
670 if ((mouse
.getAbsoluteY() == hScroller
.getAbsoluteY())
671 && (mouse
.getAbsoluteX() >= hScroller
.getAbsoluteX())
672 && (mouse
.getAbsoluteX() < hScroller
.getAbsoluteX() +
673 hScroller
.getWidth())