2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2017 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 static jexer
.TKeypress
.kbSpace
;
32 import jexer
.bits
.CellAttributes
;
33 import jexer
.bits
.GraphicsChars
;
34 import jexer
.event
.TKeypressEvent
;
35 import jexer
.event
.TMouseEvent
;
38 * TCheckBox implements an on/off checkbox.
40 public class TCheckBox
extends TWidget
{
42 // ------------------------------------------------------------------------
43 // Variables --------------------------------------------------------------
44 // ------------------------------------------------------------------------
47 * Checkbox state, true means checked.
49 private boolean checked
= false;
52 * Label for this checkbox.
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 label label to display next to (right of) the checkbox
67 * @param checked initial check state
69 public TCheckBox(final TWidget parent
, final int x
, final int y
,
70 final String label
, final boolean checked
) {
72 // Set parent and window
73 super(parent
, x
, y
, label
.length() + 4, 1);
76 this.checked
= checked
;
78 setCursorVisible(true);
82 // ------------------------------------------------------------------------
83 // Event handlers ---------------------------------------------------------
84 // ------------------------------------------------------------------------
87 * Returns true if the mouse is currently on the checkbox.
89 * @param mouse mouse event
90 * @return true if the mouse is currently on the checkbox
92 private boolean mouseOnCheckbox(final TMouseEvent mouse
) {
93 if ((mouse
.getY() == 0)
94 && (mouse
.getX() >= 0)
95 && (mouse
.getX() <= 2)
103 * Handle mouse checkbox presses.
105 * @param mouse mouse button down event
108 public void onMouseDown(final TMouseEvent mouse
) {
109 if ((mouseOnCheckbox(mouse
)) && (mouse
.isMouse1())) {
118 * @param keypress keystroke event
121 public void onKeypress(final TKeypressEvent keypress
) {
122 if (keypress
.equals(kbSpace
)) {
127 // Pass to parent for the things we don't care about.
128 super.onKeypress(keypress
);
131 // ------------------------------------------------------------------------
132 // TWidget ----------------------------------------------------------------
133 // ------------------------------------------------------------------------
136 * Draw a checkbox with label.
140 CellAttributes checkboxColor
;
142 if (isAbsoluteActive()) {
143 checkboxColor
= getTheme().getColor("tcheckbox.active");
145 checkboxColor
= getTheme().getColor("tcheckbox.inactive");
148 getScreen().putCharXY(0, 0, '[', checkboxColor
);
150 getScreen().putCharXY(1, 0, GraphicsChars
.CHECK
, checkboxColor
);
152 getScreen().putCharXY(1, 0, ' ', checkboxColor
);
154 getScreen().putCharXY(2, 0, ']', checkboxColor
);
155 getScreen().putStringXY(4, 0, label
, checkboxColor
);
158 // ------------------------------------------------------------------------
159 // TCheckBox --------------------------------------------------------------
160 // ------------------------------------------------------------------------
165 * @return if true, this is checked
167 public boolean isChecked() {
174 * @param checked new checked value.
176 public void setChecked(final boolean checked
) {
177 this.checked
= checked
;