Commit | Line | Data |
---|---|---|
daa4106c | 1 | /* |
7272e49f KL |
2 | * Jexer - Java Text User Interface |
3 | * | |
e16dda65 | 4 | * The MIT License (MIT) |
7272e49f | 5 | * |
a69ed767 | 6 | * Copyright (C) 2019 Kevin Lamonte |
7272e49f | 7 | * |
e16dda65 KL |
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: | |
7272e49f | 14 | * |
e16dda65 KL |
15 | * The above copyright notice and this permission notice shall be included in |
16 | * all copies or substantial portions of the Software. | |
7272e49f | 17 | * |
e16dda65 KL |
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. | |
7272e49f KL |
25 | * |
26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
27 | * @version 1 | |
28 | */ | |
29 | package jexer; | |
30 | ||
31 | import jexer.bits.CellAttributes; | |
32 | import jexer.bits.GraphicsChars; | |
33 | import jexer.event.TKeypressEvent; | |
34 | import jexer.event.TMouseEvent; | |
051e2913 | 35 | import static jexer.TKeypress.*; |
7272e49f KL |
36 | |
37 | /** | |
382bc294 | 38 | * TSpinner implements a simple up/down spinner. |
7272e49f | 39 | */ |
051e2913 | 40 | public class TSpinner extends TWidget { |
7272e49f | 41 | |
d36057df KL |
42 | // ------------------------------------------------------------------------ |
43 | // Variables -------------------------------------------------------------- | |
44 | // ------------------------------------------------------------------------ | |
45 | ||
7272e49f | 46 | /** |
051e2913 | 47 | * The action to perform when the user clicks on the up arrow. |
7272e49f | 48 | */ |
051e2913 | 49 | private TAction upAction = null; |
7272e49f KL |
50 | |
51 | /** | |
051e2913 | 52 | * The action to perform when the user clicks on the down arrow. |
7272e49f | 53 | */ |
051e2913 | 54 | private TAction downAction = null; |
7272e49f | 55 | |
d36057df KL |
56 | // ------------------------------------------------------------------------ |
57 | // Constructors ----------------------------------------------------------- | |
58 | // ------------------------------------------------------------------------ | |
59 | ||
7272e49f KL |
60 | /** |
61 | * Public constructor. | |
62 | * | |
63 | * @param parent parent widget | |
64 | * @param x column relative to parent | |
65 | * @param y row relative to parent | |
051e2913 KL |
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 | |
68 | * pressed | |
7272e49f | 69 | */ |
051e2913 KL |
70 | public TSpinner(final TWidget parent, final int x, final int y, |
71 | final TAction upAction, final TAction downAction) { | |
7272e49f KL |
72 | |
73 | // Set parent and window | |
051e2913 | 74 | super(parent, x, y, 2, 1); |
7272e49f | 75 | |
051e2913 KL |
76 | this.upAction = upAction; |
77 | this.downAction = downAction; | |
7272e49f KL |
78 | } |
79 | ||
d36057df KL |
80 | // ------------------------------------------------------------------------ |
81 | // Event handlers --------------------------------------------------------- | |
82 | // ------------------------------------------------------------------------ | |
83 | ||
7272e49f | 84 | /** |
051e2913 KL |
85 | * Returns true if the mouse is currently on the up arrow. |
86 | * | |
87 | * @param mouse mouse event | |
88 | * @return true if the mouse is currently on the up arrow | |
89 | */ | |
90 | private boolean mouseOnUpArrow(final TMouseEvent mouse) { | |
91 | if ((mouse.getY() == 0) | |
e23ea538 | 92 | && (mouse.getX() == getWidth() - 2) |
051e2913 KL |
93 | ) { |
94 | return true; | |
95 | } | |
96 | return false; | |
97 | } | |
98 | ||
99 | /** | |
100 | * Returns true if the mouse is currently on the down arrow. | |
7272e49f KL |
101 | * |
102 | * @param mouse mouse event | |
051e2913 | 103 | * @return true if the mouse is currently on the down arrow |
7272e49f | 104 | */ |
051e2913 | 105 | private boolean mouseOnDownArrow(final TMouseEvent mouse) { |
00d2622b | 106 | if ((mouse.getY() == 0) |
e23ea538 | 107 | && (mouse.getX() == getWidth() - 1) |
d36057df | 108 | ) { |
7272e49f KL |
109 | return true; |
110 | } | |
111 | return false; | |
112 | } | |
113 | ||
d36057df KL |
114 | /** |
115 | * Handle mouse checkbox presses. | |
116 | * | |
117 | * @param mouse mouse button down event | |
118 | */ | |
119 | @Override | |
120 | public void onMouseDown(final TMouseEvent mouse) { | |
051e2913 KL |
121 | if ((mouseOnUpArrow(mouse)) && (mouse.isMouse1())) { |
122 | up(); | |
123 | } else if ((mouseOnDownArrow(mouse)) && (mouse.isMouse1())) { | |
124 | down(); | |
d36057df KL |
125 | } |
126 | } | |
127 | ||
128 | /** | |
129 | * Handle keystrokes. | |
130 | * | |
131 | * @param keypress keystroke event | |
132 | */ | |
133 | @Override | |
134 | public void onKeypress(final TKeypressEvent keypress) { | |
051e2913 KL |
135 | if (keypress.equals(kbUp)) { |
136 | up(); | |
137 | return; | |
138 | } | |
139 | if (keypress.equals(kbDown)) { | |
140 | down(); | |
d36057df KL |
141 | return; |
142 | } | |
143 | ||
144 | // Pass to parent for the things we don't care about. | |
145 | super.onKeypress(keypress); | |
146 | } | |
147 | ||
148 | // ------------------------------------------------------------------------ | |
149 | // TWidget ---------------------------------------------------------------- | |
150 | // ------------------------------------------------------------------------ | |
151 | ||
7272e49f | 152 | /** |
051e2913 | 153 | * Draw the spinner arrows. |
7272e49f KL |
154 | */ |
155 | @Override | |
156 | public void draw() { | |
051e2913 | 157 | CellAttributes spinnerColor; |
7272e49f | 158 | |
7c870d89 | 159 | if (isAbsoluteActive()) { |
051e2913 | 160 | spinnerColor = getTheme().getColor("tspinner.active"); |
7272e49f | 161 | } else { |
051e2913 | 162 | spinnerColor = getTheme().getColor("tspinner.inactive"); |
7272e49f KL |
163 | } |
164 | ||
a69ed767 KL |
165 | putCharXY(getWidth() - 2, 0, GraphicsChars.UPARROW, spinnerColor); |
166 | putCharXY(getWidth() - 1, 0, GraphicsChars.DOWNARROW, spinnerColor); | |
7272e49f KL |
167 | } |
168 | ||
d36057df | 169 | // ------------------------------------------------------------------------ |
051e2913 | 170 | // TSpinner --------------------------------------------------------------- |
d36057df KL |
171 | // ------------------------------------------------------------------------ |
172 | ||
7272e49f | 173 | /** |
051e2913 | 174 | * Perform the "up" action. |
7272e49f | 175 | */ |
051e2913 KL |
176 | private void up() { |
177 | if (upAction != null) { | |
178 | upAction.DO(); | |
179 | } | |
7272e49f KL |
180 | } |
181 | ||
182 | /** | |
051e2913 | 183 | * Perform the "down" action. |
7272e49f | 184 | */ |
051e2913 KL |
185 | private void down() { |
186 | if (downAction != null) { | |
187 | downAction.DO(); | |
188 | } | |
7272e49f KL |
189 | } |
190 | ||
191 | } |