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
.MnemonicString
;
35 * TLabel implements a simple label, with an optional mnemonic hotkey action
38 public class TLabel
extends TWidget
{
40 // ------------------------------------------------------------------------
41 // Variables --------------------------------------------------------------
42 // ------------------------------------------------------------------------
45 * The shortcut and label.
47 private MnemonicString mnemonic
;
50 * The action to perform when the mnemonic shortcut is pressed.
52 private TAction action
;
57 private String colorKey
;
60 * If true, use the window's background color.
62 private boolean useWindowBackground
= true;
64 // ------------------------------------------------------------------------
65 // Constructors -----------------------------------------------------------
66 // ------------------------------------------------------------------------
69 * Public constructor, using the default "tlabel" for colorKey.
71 * @param parent parent widget
72 * @param text label on the screen
73 * @param x column relative to parent
74 * @param y row relative to parent
76 public TLabel(final TWidget parent
, final String text
, final int x
,
79 this(parent
, text
, x
, y
, "tlabel");
83 * Public constructor, using the default "tlabel" for colorKey.
85 * @param parent parent widget
86 * @param text label on the screen
87 * @param x column relative to parent
88 * @param y row relative to parent
89 * @param action to call when shortcut is pressed
91 public TLabel(final TWidget parent
, final String text
, final int x
,
92 final int y
, final TAction action
) {
94 this(parent
, text
, x
, y
, "tlabel", action
);
100 * @param parent parent widget
101 * @param text label on the screen
102 * @param x column relative to parent
103 * @param y row relative to parent
104 * @param colorKey ColorTheme key color to use for foreground text
106 public TLabel(final TWidget parent
, final String text
, final int x
,
107 final int y
, final String colorKey
) {
109 this(parent
, text
, x
, y
, colorKey
, true);
113 * Public constructor.
115 * @param parent parent widget
116 * @param text label on the screen
117 * @param x column relative to parent
118 * @param y row relative to parent
119 * @param colorKey ColorTheme key color to use for foreground text
120 * @param action to call when shortcut is pressed
122 public TLabel(final TWidget parent
, final String text
, final int x
,
123 final int y
, final String colorKey
, final TAction action
) {
125 this(parent
, text
, x
, y
, colorKey
, true, action
);
129 * Public constructor.
131 * @param parent parent widget
132 * @param text label on the screen
133 * @param x column relative to parent
134 * @param y row relative to parent
135 * @param colorKey ColorTheme key color to use for foreground text
136 * @param useWindowBackground if true, use the window's background color
138 public TLabel(final TWidget parent
, final String text
, final int x
,
139 final int y
, final String colorKey
, final boolean useWindowBackground
) {
141 this(parent
, text
, x
, y
, colorKey
, useWindowBackground
, null);
145 * Public constructor.
147 * @param parent parent widget
148 * @param text label on the screen
149 * @param x column relative to parent
150 * @param y row relative to parent
151 * @param colorKey ColorTheme key color to use for foreground text
152 * @param useWindowBackground if true, use the window's background color
153 * @param action to call when shortcut is pressed
155 public TLabel(final TWidget parent
, final String text
, final int x
,
156 final int y
, final String colorKey
, final boolean useWindowBackground
,
157 final TAction action
) {
159 // Set parent and window
160 super(parent
, false, x
, y
, text
.length(), 1);
162 mnemonic
= new MnemonicString(text
);
163 this.colorKey
= colorKey
;
164 this.useWindowBackground
= useWindowBackground
;
165 this.action
= action
;
168 // ------------------------------------------------------------------------
169 // TWidget ----------------------------------------------------------------
170 // ------------------------------------------------------------------------
173 * Draw a static label.
178 CellAttributes color
= new CellAttributes();
179 CellAttributes mnemonicColor
= new CellAttributes();
180 color
.setTo(getTheme().getColor(colorKey
));
181 mnemonicColor
.setTo(getTheme().getColor("tlabel.mnemonic"));
182 if (useWindowBackground
) {
183 CellAttributes background
= getWindow().getBackground();
184 color
.setBackColor(background
.getBackColor());
185 mnemonicColor
.setBackColor(background
.getBackColor());
187 putStringXY(0, 0, mnemonic
.getRawLabel(), color
);
188 if (mnemonic
.getShortcutIdx() >= 0) {
189 putCharXY(mnemonic
.getShortcutIdx(), 0,
190 mnemonic
.getShortcut(), mnemonicColor
);
194 // ------------------------------------------------------------------------
195 // TLabel -----------------------------------------------------------------
196 // ------------------------------------------------------------------------
199 * Get label raw text.
203 public String
getLabel() {
204 return mnemonic
.getRawLabel();
208 * Get the mnemonic string for this label.
210 * @return mnemonic string
212 public MnemonicString
getMnemonic() {
219 * @param label new label text
221 public void setLabel(final String label
) {
222 mnemonic
= new MnemonicString(label
);
226 * Act as though the mnemonic shortcut was pressed.
228 public void dispatch() {
229 if (action
!= null) {