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
;
33 import jexer
.bits
.StringUtils
;
36 * TLabel implements a simple label, with an optional mnemonic hotkey action
39 public class TLabel
extends TWidget
{
41 // ------------------------------------------------------------------------
42 // Variables --------------------------------------------------------------
43 // ------------------------------------------------------------------------
46 * The shortcut and label.
48 private MnemonicString mnemonic
;
51 * The action to perform when the mnemonic shortcut is pressed.
53 private TAction action
;
58 private String colorKey
;
61 * If true, use the window's background color.
63 private boolean useWindowBackground
= true;
65 // ------------------------------------------------------------------------
66 // Constructors -----------------------------------------------------------
67 // ------------------------------------------------------------------------
70 * Public constructor, using the default "tlabel" for colorKey.
72 * @param parent parent widget
73 * @param text label on the screen
74 * @param x column relative to parent
75 * @param y row relative to parent
77 public TLabel(final TWidget parent
, final String text
, final int x
,
80 this(parent
, text
, x
, y
, "tlabel");
84 * Public constructor, using the default "tlabel" for colorKey.
86 * @param parent parent widget
87 * @param text label on the screen
88 * @param x column relative to parent
89 * @param y row relative to parent
90 * @param action to call when shortcut is pressed
92 public TLabel(final TWidget parent
, final String text
, final int x
,
93 final int y
, final TAction action
) {
95 this(parent
, text
, x
, y
, "tlabel", action
);
101 * @param parent parent widget
102 * @param text label on the screen
103 * @param x column relative to parent
104 * @param y row relative to parent
105 * @param colorKey ColorTheme key color to use for foreground text
107 public TLabel(final TWidget parent
, final String text
, final int x
,
108 final int y
, final String colorKey
) {
110 this(parent
, text
, x
, y
, colorKey
, true);
114 * Public constructor.
116 * @param parent parent widget
117 * @param text label on the screen
118 * @param x column relative to parent
119 * @param y row relative to parent
120 * @param colorKey ColorTheme key color to use for foreground text
121 * @param action to call when shortcut is pressed
123 public TLabel(final TWidget parent
, final String text
, final int x
,
124 final int y
, final String colorKey
, final TAction action
) {
126 this(parent
, text
, x
, y
, colorKey
, true, action
);
130 * Public constructor.
132 * @param parent parent widget
133 * @param text label on the screen
134 * @param x column relative to parent
135 * @param y row relative to parent
136 * @param colorKey ColorTheme key color to use for foreground text
137 * @param useWindowBackground if true, use the window's background color
139 public TLabel(final TWidget parent
, final String text
, final int x
,
140 final int y
, final String colorKey
, final boolean useWindowBackground
) {
142 this(parent
, text
, x
, y
, colorKey
, useWindowBackground
, null);
146 * Public constructor.
148 * @param parent parent widget
149 * @param text label on the screen
150 * @param x column relative to parent
151 * @param y row relative to parent
152 * @param colorKey ColorTheme key color to use for foreground text
153 * @param useWindowBackground if true, use the window's background color
154 * @param action to call when shortcut is pressed
156 public TLabel(final TWidget parent
, final String text
, final int x
,
157 final int y
, final String colorKey
, final boolean useWindowBackground
,
158 final TAction action
) {
160 // Set parent and window
161 super(parent
, false, x
, y
, StringUtils
.width(text
), 1);
163 mnemonic
= new MnemonicString(text
);
164 this.colorKey
= colorKey
;
165 this.useWindowBackground
= useWindowBackground
;
166 this.action
= action
;
169 // ------------------------------------------------------------------------
170 // TWidget ----------------------------------------------------------------
171 // ------------------------------------------------------------------------
174 * Draw a static label.
179 CellAttributes color
= new CellAttributes();
180 CellAttributes mnemonicColor
= new CellAttributes();
181 color
.setTo(getTheme().getColor(colorKey
));
182 mnemonicColor
.setTo(getTheme().getColor("tlabel.mnemonic"));
183 if (useWindowBackground
) {
184 CellAttributes background
= getWindow().getBackground();
185 color
.setBackColor(background
.getBackColor());
186 mnemonicColor
.setBackColor(background
.getBackColor());
188 putStringXY(0, 0, mnemonic
.getRawLabel(), color
);
189 if (mnemonic
.getScreenShortcutIdx() >= 0) {
190 putCharXY(mnemonic
.getScreenShortcutIdx(), 0,
191 mnemonic
.getShortcut(), mnemonicColor
);
195 // ------------------------------------------------------------------------
196 // TLabel -----------------------------------------------------------------
197 // ------------------------------------------------------------------------
200 * Get label raw text.
204 public String
getLabel() {
205 return mnemonic
.getRawLabel();
209 * Get the mnemonic string for this label.
211 * @return mnemonic string
213 public MnemonicString
getMnemonic() {
220 * @param label new label text
222 public void setLabel(final String label
) {
223 mnemonic
= new MnemonicString(label
);
227 * Get the label color.
229 * @return the ColorTheme key color to use for foreground text
231 public String
getColorKey() {
236 * Set the label color.
238 * @param colorKey ColorTheme key color to use for foreground text
240 public void setColorKey(final String colorKey
) {
241 this.colorKey
= colorKey
;
245 * Act as though the mnemonic shortcut was pressed.
247 public void dispatch() {
248 if (action
!= null) {