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
.bits
.CellAttributes
;
32 import jexer
.bits
.GraphicsChars
;
33 import jexer
.event
.TKeypressEvent
;
34 import jexer
.event
.TMouseEvent
;
35 import static jexer
.TKeypress
.*;
38 * TSpinner implements a simple up/down spinner. Values can be numer
40 public class TSpinner
extends TWidget
{
42 // ------------------------------------------------------------------------
43 // Variables --------------------------------------------------------------
44 // ------------------------------------------------------------------------
47 * The action to perform when the user clicks on the up arrow.
49 private TAction upAction
= null;
52 * The action to perform when the user clicks on the down arrow.
54 private TAction downAction
= null;
56 // ------------------------------------------------------------------------
57 // Constructors -----------------------------------------------------------
58 // ------------------------------------------------------------------------
63 * @param parent parent widget
64 * @param x column relative to parent
65 * @param y row relative to parent
66 * @param upAction action to call when the up arrow is clicked or pressed
67 * @param downAction action to call when the down arrow is clicked or
70 public TSpinner(final TWidget parent
, final int x
, final int y
,
71 final TAction upAction
, final TAction downAction
) {
73 // Set parent and window
74 super(parent
, x
, y
, 2, 1);
76 this.upAction
= upAction
;
77 this.downAction
= downAction
;
80 // ------------------------------------------------------------------------
81 // Event handlers ---------------------------------------------------------
82 // ------------------------------------------------------------------------
85 * Returns true if the mouse is currently on the up arrow.
87 * @param mouse mouse event
88 * @return true if the mouse is currently on the up arrow
90 private boolean mouseOnUpArrow(final TMouseEvent mouse
) {
91 if ((mouse
.getY() == 0)
92 && (mouse
.getX() == getWidth() - 2)
100 * Returns true if the mouse is currently on the down arrow.
102 * @param mouse mouse event
103 * @return true if the mouse is currently on the down arrow
105 private boolean mouseOnDownArrow(final TMouseEvent mouse
) {
106 if ((mouse
.getY() == 0)
107 && (mouse
.getX() == getWidth() - 1)
115 * Handle mouse checkbox presses.
117 * @param mouse mouse button down event
120 public void onMouseDown(final TMouseEvent mouse
) {
121 if ((mouseOnUpArrow(mouse
)) && (mouse
.isMouse1())) {
123 } else if ((mouseOnDownArrow(mouse
)) && (mouse
.isMouse1())) {
131 * @param keypress keystroke event
134 public void onKeypress(final TKeypressEvent keypress
) {
135 if (keypress
.equals(kbUp
)) {
139 if (keypress
.equals(kbDown
)) {
144 // Pass to parent for the things we don't care about.
145 super.onKeypress(keypress
);
148 // ------------------------------------------------------------------------
149 // TWidget ----------------------------------------------------------------
150 // ------------------------------------------------------------------------
153 * Draw the spinner arrows.
157 CellAttributes spinnerColor
;
159 if (isAbsoluteActive()) {
160 spinnerColor
= getTheme().getColor("tspinner.active");
162 spinnerColor
= getTheme().getColor("tspinner.inactive");
165 putCharXY(getWidth() - 2, 0, GraphicsChars
.UPARROW
, spinnerColor
);
166 putCharXY(getWidth() - 1, 0, GraphicsChars
.DOWNARROW
, spinnerColor
);
169 // ------------------------------------------------------------------------
170 // TSpinner ---------------------------------------------------------------
171 // ------------------------------------------------------------------------
174 * Perform the "up" action.
177 if (upAction
!= null) {
183 * Perform the "down" action.
185 private void down() {
186 if (downAction
!= null) {