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 java
.util
.ArrayList
;
32 import java
.util
.List
;
34 import jexer
.bits
.CellAttributes
;
35 import jexer
.bits
.GraphicsChars
;
36 import jexer
.bits
.StringUtils
;
37 import jexer
.event
.TCommandEvent
;
38 import jexer
.event
.TKeypressEvent
;
39 import jexer
.event
.TMouseEvent
;
42 * TStatusBar implements a status line with clickable buttons.
44 public class TStatusBar
extends TWidget
{
46 // ------------------------------------------------------------------------
47 // Variables --------------------------------------------------------------
48 // ------------------------------------------------------------------------
51 * Remember mouse state.
53 private TMouseEvent mouse
;
56 * The text to display on the right side of the shortcut keys.
58 private String text
= null;
63 private List
<TStatusBarKey
> keys
= new ArrayList
<TStatusBarKey
>();
66 * A single shortcut key.
68 private class TStatusBarKey
{
71 * The keypress for this action.
76 * The command to issue.
86 * If true, the mouse is on this key.
88 public boolean selected
;
91 * The left edge coordinate to draw this key with.
96 * The width of this key on the screen.
98 * @return the number of columns this takes when drawn
101 return StringUtils
.width(this.label
) +
102 StringUtils
.width(this.key
.toString()) + 3;
106 * Add a key to this status bar.
108 * @param key the key to trigger on
109 * @param cmd the command event to issue when key is pressed or this
111 * @param label the label for this action
113 public TStatusBarKey(final TKeypress key
, final TCommand cmd
,
114 final String label
) {
123 // ------------------------------------------------------------------------
124 // Constructors -----------------------------------------------------------
125 // ------------------------------------------------------------------------
128 * Public constructor.
130 * @param window the window associated with this status bar
131 * @param text text for the bar on the bottom row
133 public TStatusBar(final TWindow window
, final String text
) {
135 // TStatusBar is a parentless widget, because TApplication handles
136 // its drawing and event routing directly.
137 super(null, false, 0, 0, StringUtils
.width(text
), 1);
144 * Public constructor.
146 * @param window the window associated with this status bar
148 public TStatusBar(final TWindow window
) {
152 // ------------------------------------------------------------------------
153 // Event handlers ---------------------------------------------------------
154 // ------------------------------------------------------------------------
159 * @param keypress keystroke event
160 * @return true if this keypress was consumed
162 public boolean statusBarKeypress(final TKeypressEvent keypress
) {
163 for (TStatusBarKey key
: keys
) {
164 if (keypress
.equals(key
.key
)) {
165 getApplication().postMenuEvent(new TCommandEvent(key
.cmd
));
173 * Returns true if the mouse is currently on the button.
175 * @param statusBarKey the status bar item
176 * @return if true the mouse is currently on the button
178 private boolean mouseOnShortcut(final TStatusBarKey statusBarKey
) {
180 && (mouse
.getAbsoluteY() == getApplication().getDesktopBottom())
181 && (mouse
.getAbsoluteX() >= statusBarKey
.x
)
182 && (mouse
.getAbsoluteX() < statusBarKey
.x
+ statusBarKey
.width())
190 * Handle mouse button presses.
192 * @param mouse mouse button event
193 * @return true if this mouse event was consumed
195 public boolean statusBarMouseDown(final TMouseEvent mouse
) {
198 for (TStatusBarKey key
: keys
) {
199 if ((mouseOnShortcut(key
)) && (mouse
.isMouse1())) {
208 * Handle mouse button releases.
210 * @param mouse mouse button release event
211 * @return true if this mouse event was consumed
213 public boolean statusBarMouseUp(final TMouseEvent mouse
) {
216 for (TStatusBarKey key
: keys
) {
217 if (key
.selected
&& mouse
.isMouse1()) {
218 key
.selected
= false;
220 // Dispatch the event
221 getApplication().postMenuEvent(new TCommandEvent(key
.cmd
));
229 * Handle mouse movements.
231 * @param mouse mouse motion event
233 public void statusBarMouseMotion(final TMouseEvent mouse
) {
236 for (TStatusBarKey key
: keys
) {
237 if (!mouseOnShortcut(key
)) {
238 key
.selected
= false;
243 // ------------------------------------------------------------------------
244 // TWidget ----------------------------------------------------------------
245 // ------------------------------------------------------------------------
252 CellAttributes barColor
= new CellAttributes();
253 barColor
.setTo(getTheme().getColor("tstatusbar.text"));
254 CellAttributes keyColor
= new CellAttributes();
255 keyColor
.setTo(getTheme().getColor("tstatusbar.button"));
256 CellAttributes selectedColor
= new CellAttributes();
257 selectedColor
.setTo(getTheme().getColor("tstatusbar.selected"));
259 // Status bar is weird. Its draw() method is called directly by
260 // TApplication after everything is drawn, and after
261 // Screen.resetClipping(). So at this point we are drawing in
262 // absolute coordinates, not relative to our TWindow.
263 int row
= getScreen().getHeight() - 1;
264 int width
= getScreen().getWidth();
266 hLineXY(0, row
, width
, ' ', barColor
);
269 for (TStatusBarKey key
: keys
) {
270 String keyStr
= key
.key
.toString();
272 putCharXY(col
++, row
, ' ', selectedColor
);
273 putStringXY(col
, row
, keyStr
, selectedColor
);
274 col
+= StringUtils
.width(keyStr
);
275 putCharXY(col
++, row
, ' ', selectedColor
);
276 putStringXY(col
, row
, key
.label
, selectedColor
);
277 col
+= StringUtils
.width(key
.label
);
278 putCharXY(col
++, row
, ' ', selectedColor
);
280 putCharXY(col
++, row
, ' ', barColor
);
281 putStringXY(col
, row
, keyStr
, keyColor
);
282 col
+= StringUtils
.width(keyStr
) + 1;
283 putStringXY(col
, row
, key
.label
, barColor
);
284 col
+= StringUtils
.width(key
.label
);
285 putCharXY(col
++, row
, ' ', barColor
);
288 if (text
.length() > 0) {
289 if (keys
.size() > 0) {
290 putCharXY(col
++, row
, GraphicsChars
.VERTICAL_BAR
, barColor
);
292 putCharXY(col
++, row
, ' ', barColor
);
293 putStringXY(col
, row
, text
, barColor
);
297 // ------------------------------------------------------------------------
298 // TStatusBar -------------------------------------------------------------
299 // ------------------------------------------------------------------------
302 * Add a key to this status bar.
304 * @param key the key to trigger on
305 * @param cmd the command event to issue when key is pressed or this item
307 * @param label the label for this action
309 public void addShortcutKeypress(final TKeypress key
, final TCommand cmd
,
310 final String label
) {
312 TStatusBarKey newKey
= new TStatusBarKey(key
, cmd
, label
);
313 if (keys
.size() > 0) {
314 TStatusBarKey oldKey
= keys
.get(keys
.size() - 1);
315 newKey
.x
= oldKey
.x
+ oldKey
.width();
321 * Set the text to display on the right side of the shortcut keys.
323 * @param text the new text
325 public void setText(final String text
) {