| 1 | /* |
| 2 | * Jexer - Java Text User Interface |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (C) 2019 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.bits.CellAttributes; |
| 32 | import jexer.bits.GraphicsChars; |
| 33 | import jexer.event.TKeypressEvent; |
| 34 | import jexer.event.TMouseEvent; |
| 35 | import static jexer.TKeypress.*; |
| 36 | |
| 37 | /** |
| 38 | * TSpinner implements a simple up/down spinner. |
| 39 | */ |
| 40 | public class TSpinner extends TWidget { |
| 41 | |
| 42 | // ------------------------------------------------------------------------ |
| 43 | // Variables -------------------------------------------------------------- |
| 44 | // ------------------------------------------------------------------------ |
| 45 | |
| 46 | /** |
| 47 | * The action to perform when the user clicks on the up arrow. |
| 48 | */ |
| 49 | private TAction upAction = null; |
| 50 | |
| 51 | /** |
| 52 | * The action to perform when the user clicks on the down arrow. |
| 53 | */ |
| 54 | private TAction downAction = null; |
| 55 | |
| 56 | // ------------------------------------------------------------------------ |
| 57 | // Constructors ----------------------------------------------------------- |
| 58 | // ------------------------------------------------------------------------ |
| 59 | |
| 60 | /** |
| 61 | * Public constructor. |
| 62 | * |
| 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 |
| 68 | * pressed |
| 69 | */ |
| 70 | public TSpinner(final TWidget parent, final int x, final int y, |
| 71 | final TAction upAction, final TAction downAction) { |
| 72 | |
| 73 | // Set parent and window |
| 74 | super(parent, x, y, 2, 1); |
| 75 | |
| 76 | this.upAction = upAction; |
| 77 | this.downAction = downAction; |
| 78 | } |
| 79 | |
| 80 | // ------------------------------------------------------------------------ |
| 81 | // Event handlers --------------------------------------------------------- |
| 82 | // ------------------------------------------------------------------------ |
| 83 | |
| 84 | /** |
| 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) |
| 92 | && (mouse.getX() == getWidth() - 2) |
| 93 | ) { |
| 94 | return true; |
| 95 | } |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Returns true if the mouse is currently on the down arrow. |
| 101 | * |
| 102 | * @param mouse mouse event |
| 103 | * @return true if the mouse is currently on the down arrow |
| 104 | */ |
| 105 | private boolean mouseOnDownArrow(final TMouseEvent mouse) { |
| 106 | if ((mouse.getY() == 0) |
| 107 | && (mouse.getX() == getWidth() - 1) |
| 108 | ) { |
| 109 | return true; |
| 110 | } |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Handle mouse checkbox presses. |
| 116 | * |
| 117 | * @param mouse mouse button down event |
| 118 | */ |
| 119 | @Override |
| 120 | public void onMouseDown(final TMouseEvent mouse) { |
| 121 | if ((mouseOnUpArrow(mouse)) && (mouse.isMouse1())) { |
| 122 | up(); |
| 123 | } else if ((mouseOnDownArrow(mouse)) && (mouse.isMouse1())) { |
| 124 | down(); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Handle keystrokes. |
| 130 | * |
| 131 | * @param keypress keystroke event |
| 132 | */ |
| 133 | @Override |
| 134 | public void onKeypress(final TKeypressEvent keypress) { |
| 135 | if (keypress.equals(kbUp)) { |
| 136 | up(); |
| 137 | return; |
| 138 | } |
| 139 | if (keypress.equals(kbDown)) { |
| 140 | down(); |
| 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 | |
| 152 | /** |
| 153 | * Draw the spinner arrows. |
| 154 | */ |
| 155 | @Override |
| 156 | public void draw() { |
| 157 | CellAttributes spinnerColor; |
| 158 | |
| 159 | if (isAbsoluteActive()) { |
| 160 | spinnerColor = getTheme().getColor("tspinner.active"); |
| 161 | } else { |
| 162 | spinnerColor = getTheme().getColor("tspinner.inactive"); |
| 163 | } |
| 164 | |
| 165 | putCharXY(getWidth() - 2, 0, GraphicsChars.UPARROW, spinnerColor); |
| 166 | putCharXY(getWidth() - 1, 0, GraphicsChars.DOWNARROW, spinnerColor); |
| 167 | } |
| 168 | |
| 169 | // ------------------------------------------------------------------------ |
| 170 | // TSpinner --------------------------------------------------------------- |
| 171 | // ------------------------------------------------------------------------ |
| 172 | |
| 173 | /** |
| 174 | * Perform the "up" action. |
| 175 | */ |
| 176 | private void up() { |
| 177 | if (upAction != null) { |
| 178 | upAction.DO(this); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Perform the "down" action. |
| 184 | */ |
| 185 | private void down() { |
| 186 | if (downAction != null) { |
| 187 | downAction.DO(this); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | } |