| 1 | /* |
| 2 | * Jexer - Java Text User Interface |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (C) 2017 Kevin Lamonte |
| 7 | * |
| 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: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 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. |
| 25 | * |
| 26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] |
| 27 | * @version 1 |
| 28 | */ |
| 29 | package jexer; |
| 30 | |
| 31 | import jexer.event.TResizeEvent; |
| 32 | |
| 33 | /** |
| 34 | * TScrollableWindow is a convenience superclass for windows that have |
| 35 | * scrollbars. |
| 36 | */ |
| 37 | public class TScrollableWindow extends TWindow implements Scrollable { |
| 38 | |
| 39 | /** |
| 40 | * The horizontal scrollbar. |
| 41 | */ |
| 42 | protected THScroller hScroller = null; |
| 43 | |
| 44 | /** |
| 45 | * The vertical scrollbar. |
| 46 | */ |
| 47 | protected TVScroller vScroller = null; |
| 48 | |
| 49 | /** |
| 50 | * Place the scrollbars on the edge of this widget, and adjust bigChange |
| 51 | * to match the new size. This is called by onResize(). |
| 52 | */ |
| 53 | protected void placeScrollbars() { |
| 54 | if (hScroller != null) { |
| 55 | hScroller.setY(getHeight() - 2); |
| 56 | hScroller.setWidth(getWidth() - 3); |
| 57 | hScroller.setBigChange(getWidth() - 3); |
| 58 | } |
| 59 | if (vScroller != null) { |
| 60 | vScroller.setX(getWidth() - 2); |
| 61 | vScroller.setHeight(getHeight() - 2); |
| 62 | vScroller.setBigChange(getHeight() - 2); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Recompute whatever data is displayed by this widget. |
| 68 | */ |
| 69 | public void reflowData() { |
| 70 | // Default: nothing to do |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Handle window/screen resize events. |
| 75 | * |
| 76 | * @param event resize event |
| 77 | */ |
| 78 | @Override |
| 79 | public void onResize(final TResizeEvent event) { |
| 80 | if (event.getType() == TResizeEvent.Type.WIDGET) { |
| 81 | reflowData(); |
| 82 | placeScrollbars(); |
| 83 | return; |
| 84 | } else { |
| 85 | super.onResize(event); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Public constructor. Window will be located at (0, 0). |
| 91 | * |
| 92 | * @param application TApplication that manages this window |
| 93 | * @param title window title, will be centered along the top border |
| 94 | * @param width width of window |
| 95 | * @param height height of window |
| 96 | */ |
| 97 | public TScrollableWindow(final TApplication application, final String title, |
| 98 | final int width, final int height) { |
| 99 | |
| 100 | super(application, title, width, height); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Public constructor. Window will be located at (0, 0). |
| 105 | * |
| 106 | * @param application TApplication that manages this window |
| 107 | * @param title window title, will be centered along the top border |
| 108 | * @param width width of window |
| 109 | * @param height height of window |
| 110 | * @param flags bitmask of RESIZABLE, CENTERED, or MODAL |
| 111 | */ |
| 112 | public TScrollableWindow(final TApplication application, final String title, |
| 113 | final int width, final int height, final int flags) { |
| 114 | |
| 115 | super(application, title, width, height, flags); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Public constructor. |
| 120 | * |
| 121 | * @param application TApplication that manages this window |
| 122 | * @param title window title, will be centered along the top border |
| 123 | * @param x column relative to parent |
| 124 | * @param y row relative to parent |
| 125 | * @param width width of window |
| 126 | * @param height height of window |
| 127 | */ |
| 128 | public TScrollableWindow(final TApplication application, final String title, |
| 129 | final int x, final int y, final int width, final int height) { |
| 130 | |
| 131 | super(application, title, x, y, width, height); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Public constructor. |
| 136 | * |
| 137 | * @param application TApplication that manages this window |
| 138 | * @param title window title, will be centered along the top border |
| 139 | * @param x column relative to parent |
| 140 | * @param y row relative to parent |
| 141 | * @param width width of window |
| 142 | * @param height height of window |
| 143 | * @param flags mask of RESIZABLE, CENTERED, or MODAL |
| 144 | */ |
| 145 | public TScrollableWindow(final TApplication application, final String title, |
| 146 | final int x, final int y, final int width, final int height, |
| 147 | final int flags) { |
| 148 | |
| 149 | super(application, title, x, y, width, height, flags); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Get the horizontal scrollbar, or null if this Viewport does not |
| 154 | * support horizontal scrolling. |
| 155 | * |
| 156 | * @return the horizontal scrollbar |
| 157 | */ |
| 158 | public THScroller getHorizontalScroller() { |
| 159 | return hScroller; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Get the vertical scrollbar, or null if this Viewport does not support |
| 164 | * vertical scrolling. |
| 165 | * |
| 166 | * @return the vertical scrollbar |
| 167 | */ |
| 168 | public TVScroller getVerticalScroller() { |
| 169 | return vScroller; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Get the value that corresponds to being on the top edge of the |
| 174 | * vertical scroll bar. |
| 175 | * |
| 176 | * @return the scroll value |
| 177 | */ |
| 178 | public int getTopValue() { |
| 179 | if (vScroller == null) { |
| 180 | return 0; |
| 181 | } else { |
| 182 | return vScroller.getTopValue(); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Set the value that corresponds to being on the top edge of the |
| 188 | * vertical scroll bar. |
| 189 | * |
| 190 | * @param topValue the new scroll value |
| 191 | */ |
| 192 | public void setTopValue(final int topValue) { |
| 193 | if (vScroller == null) { |
| 194 | return; |
| 195 | } else { |
| 196 | vScroller.setTopValue(topValue); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Get the value that corresponds to being on the bottom edge of the |
| 202 | * vertical scroll bar. |
| 203 | * |
| 204 | * @return the scroll value |
| 205 | */ |
| 206 | public int getBottomValue() { |
| 207 | if (vScroller == null) { |
| 208 | return 0; |
| 209 | } else { |
| 210 | return vScroller.getBottomValue(); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Set the value that corresponds to being on the bottom edge of the |
| 216 | * vertical scroll bar. |
| 217 | * |
| 218 | * @param bottomValue the new scroll value |
| 219 | */ |
| 220 | public void setBottomValue(final int bottomValue) { |
| 221 | if (vScroller == null) { |
| 222 | return; |
| 223 | } else { |
| 224 | vScroller.setBottomValue(bottomValue); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Get current value of the vertical scroll. |
| 230 | * |
| 231 | * @return the scroll value |
| 232 | */ |
| 233 | public int getVerticalValue() { |
| 234 | if (vScroller == null) { |
| 235 | return 0; |
| 236 | } else { |
| 237 | return vScroller.getValue(); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Set current value of the vertical scroll. |
| 243 | * |
| 244 | * @param value the new scroll value |
| 245 | */ |
| 246 | public void setVerticalValue(final int value) { |
| 247 | if (vScroller == null) { |
| 248 | return; |
| 249 | } else { |
| 250 | vScroller.setValue(value); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Get the increment for clicking on an arrow on the vertical scrollbar. |
| 256 | * |
| 257 | * @return the increment value |
| 258 | */ |
| 259 | public int getVerticalSmallChange() { |
| 260 | if (vScroller == null) { |
| 261 | return 0; |
| 262 | } else { |
| 263 | return vScroller.getSmallChange(); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Set the increment for clicking on an arrow on the vertical scrollbar. |
| 269 | * |
| 270 | * @param smallChange the new increment value |
| 271 | */ |
| 272 | public void setVerticalSmallChange(final int smallChange) { |
| 273 | if (vScroller == null) { |
| 274 | return; |
| 275 | } else { |
| 276 | vScroller.setSmallChange(smallChange); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Get the increment for clicking in the bar between the box and an |
| 282 | * arrow on the vertical scrollbar. |
| 283 | * |
| 284 | * @return the increment value |
| 285 | */ |
| 286 | public int getVerticalBigChange() { |
| 287 | if (vScroller == null) { |
| 288 | return 0; |
| 289 | } else { |
| 290 | return vScroller.getBigChange(); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Set the increment for clicking in the bar between the box and an |
| 296 | * arrow on the vertical scrollbar. |
| 297 | * |
| 298 | * @param bigChange the new increment value |
| 299 | */ |
| 300 | public void setVerticalBigChange(final int bigChange) { |
| 301 | if (vScroller == null) { |
| 302 | return; |
| 303 | } else { |
| 304 | vScroller.setBigChange(bigChange); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Perform a small step change up. |
| 310 | */ |
| 311 | public void verticalDecrement() { |
| 312 | if (vScroller == null) { |
| 313 | return; |
| 314 | } else { |
| 315 | vScroller.decrement(); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Perform a small step change down. |
| 321 | */ |
| 322 | public void verticalIncrement() { |
| 323 | if (vScroller == null) { |
| 324 | return; |
| 325 | } else { |
| 326 | vScroller.increment(); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Perform a big step change up. |
| 332 | */ |
| 333 | public void bigVerticalDecrement() { |
| 334 | if (vScroller == null) { |
| 335 | return; |
| 336 | } else { |
| 337 | vScroller.bigDecrement(); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Perform a big step change down. |
| 343 | */ |
| 344 | public void bigVerticalIncrement() { |
| 345 | if (vScroller == null) { |
| 346 | return; |
| 347 | } else { |
| 348 | vScroller.bigIncrement(); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Go to the top edge of the vertical scroller. |
| 354 | */ |
| 355 | public void toTop() { |
| 356 | if (vScroller == null) { |
| 357 | return; |
| 358 | } else { |
| 359 | vScroller.toTop(); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Go to the bottom edge of the vertical scroller. |
| 365 | */ |
| 366 | public void toBottom() { |
| 367 | if (vScroller == null) { |
| 368 | return; |
| 369 | } else { |
| 370 | vScroller.toBottom(); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Get the value that corresponds to being on the left edge of the |
| 376 | * horizontal scroll bar. |
| 377 | * |
| 378 | * @return the scroll value |
| 379 | */ |
| 380 | public int getLeftValue() { |
| 381 | if (hScroller == null) { |
| 382 | return 0; |
| 383 | } else { |
| 384 | return hScroller.getLeftValue(); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * Set the value that corresponds to being on the left edge of the |
| 390 | * horizontal scroll bar. |
| 391 | * |
| 392 | * @param leftValue the new scroll value |
| 393 | */ |
| 394 | public void setLeftValue(final int leftValue) { |
| 395 | if (hScroller == null) { |
| 396 | return; |
| 397 | } else { |
| 398 | hScroller.setLeftValue(leftValue); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Get the value that corresponds to being on the right edge of the |
| 404 | * horizontal scroll bar. |
| 405 | * |
| 406 | * @return the scroll value |
| 407 | */ |
| 408 | public int getRightValue() { |
| 409 | if (hScroller == null) { |
| 410 | return 0; |
| 411 | } else { |
| 412 | return hScroller.getRightValue(); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Set the value that corresponds to being on the right edge of the |
| 418 | * horizontal scroll bar. |
| 419 | * |
| 420 | * @param rightValue the new scroll value |
| 421 | */ |
| 422 | public void setRightValue(final int rightValue) { |
| 423 | if (hScroller == null) { |
| 424 | return; |
| 425 | } else { |
| 426 | hScroller.setRightValue(rightValue); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Get current value of the horizontal scroll. |
| 432 | * |
| 433 | * @return the scroll value |
| 434 | */ |
| 435 | public int getHorizontalValue() { |
| 436 | if (hScroller == null) { |
| 437 | return 0; |
| 438 | } else { |
| 439 | return hScroller.getValue(); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Set current value of the horizontal scroll. |
| 445 | * |
| 446 | * @param value the new scroll value |
| 447 | */ |
| 448 | public void setHorizontalValue(final int value) { |
| 449 | if (hScroller == null) { |
| 450 | return; |
| 451 | } else { |
| 452 | hScroller.setValue(value); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * Get the increment for clicking on an arrow on the horizontal |
| 458 | * scrollbar. |
| 459 | * |
| 460 | * @return the increment value |
| 461 | */ |
| 462 | public int getHorizontalSmallChange() { |
| 463 | if (hScroller == null) { |
| 464 | return 0; |
| 465 | } else { |
| 466 | return hScroller.getSmallChange(); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * Set the increment for clicking on an arrow on the horizontal |
| 472 | * scrollbar. |
| 473 | * |
| 474 | * @param smallChange the new increment value |
| 475 | */ |
| 476 | public void setHorizontalSmallChange(final int smallChange) { |
| 477 | if (hScroller == null) { |
| 478 | return; |
| 479 | } else { |
| 480 | hScroller.setSmallChange(smallChange); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | /** |
| 485 | * Get the increment for clicking in the bar between the box and an |
| 486 | * arrow on the horizontal scrollbar. |
| 487 | * |
| 488 | * @return the increment value |
| 489 | */ |
| 490 | public int getHorizontalBigChange() { |
| 491 | if (hScroller == null) { |
| 492 | return 0; |
| 493 | } else { |
| 494 | return hScroller.getBigChange(); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | /** |
| 499 | * Set the increment for clicking in the bar between the box and an |
| 500 | * arrow on the horizontal scrollbar. |
| 501 | * |
| 502 | * @param bigChange the new increment value |
| 503 | */ |
| 504 | public void setHorizontalBigChange(final int bigChange) { |
| 505 | if (hScroller == null) { |
| 506 | return; |
| 507 | } else { |
| 508 | hScroller.setBigChange(bigChange); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | /** |
| 513 | * Perform a small step change left. |
| 514 | */ |
| 515 | public void horizontalDecrement() { |
| 516 | if (hScroller == null) { |
| 517 | return; |
| 518 | } else { |
| 519 | hScroller.decrement(); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * Perform a small step change right. |
| 525 | */ |
| 526 | public void horizontalIncrement() { |
| 527 | if (hScroller == null) { |
| 528 | return; |
| 529 | } else { |
| 530 | hScroller.increment(); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | /** |
| 535 | * Perform a big step change left. |
| 536 | */ |
| 537 | public void bigHorizontalDecrement() { |
| 538 | if (hScroller == null) { |
| 539 | return; |
| 540 | } else { |
| 541 | hScroller.bigDecrement(); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * Perform a big step change right. |
| 547 | */ |
| 548 | public void bigHorizontalIncrement() { |
| 549 | if (hScroller == null) { |
| 550 | return; |
| 551 | } else { |
| 552 | hScroller.bigIncrement(); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * Go to the left edge of the horizontal scroller. |
| 558 | */ |
| 559 | public void toLeft() { |
| 560 | if (hScroller == null) { |
| 561 | return; |
| 562 | } else { |
| 563 | hScroller.toLeft(); |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * Go to the right edge of the horizontal scroller. |
| 569 | */ |
| 570 | public void toRight() { |
| 571 | if (hScroller == null) { |
| 572 | return; |
| 573 | } else { |
| 574 | hScroller.toRight(); |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | /** |
| 579 | * Go to the top-left edge of the horizontal and vertical scrollers. |
| 580 | */ |
| 581 | public void toHome() { |
| 582 | if (hScroller != null) { |
| 583 | hScroller.toLeft(); |
| 584 | } |
| 585 | if (vScroller != null) { |
| 586 | vScroller.toTop(); |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | /** |
| 591 | * Go to the bottom-right edge of the horizontal and vertical scrollers. |
| 592 | */ |
| 593 | public void toEnd() { |
| 594 | if (hScroller != null) { |
| 595 | hScroller.toRight(); |
| 596 | } |
| 597 | if (vScroller != null) { |
| 598 | vScroller.toBottom(); |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | } |