retrofit from gjexer
[nikiroo-utils.git] / src / jexer / TLabel.java
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.MnemonicString;
33
34 /**
35 * TLabel implements a simple label, with an optional mnemonic hotkey action
36 * associated with it.
37 */
38 public class TLabel extends TWidget {
39
40 // ------------------------------------------------------------------------
41 // Variables --------------------------------------------------------------
42 // ------------------------------------------------------------------------
43
44 /**
45 * The shortcut and label.
46 */
47 private MnemonicString mnemonic;
48
49 /**
50 * The action to perform when the mnemonic shortcut is pressed.
51 */
52 private TAction action;
53
54 /**
55 * Label color.
56 */
57 private String colorKey;
58
59 /**
60 * If true, use the window's background color.
61 */
62 private boolean useWindowBackground = true;
63
64 // ------------------------------------------------------------------------
65 // Constructors -----------------------------------------------------------
66 // ------------------------------------------------------------------------
67
68 /**
69 * Public constructor, using the default "tlabel" for colorKey.
70 *
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
75 */
76 public TLabel(final TWidget parent, final String text, final int x,
77 final int y) {
78
79 this(parent, text, x, y, "tlabel");
80 }
81
82 /**
83 * Public constructor, using the default "tlabel" for colorKey.
84 *
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
90 */
91 public TLabel(final TWidget parent, final String text, final int x,
92 final int y, final TAction action) {
93
94 this(parent, text, x, y, "tlabel", action);
95 }
96
97 /**
98 * Public constructor.
99 *
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
105 */
106 public TLabel(final TWidget parent, final String text, final int x,
107 final int y, final String colorKey) {
108
109 this(parent, text, x, y, colorKey, true);
110 }
111
112 /**
113 * Public constructor.
114 *
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
121 */
122 public TLabel(final TWidget parent, final String text, final int x,
123 final int y, final String colorKey, final TAction action) {
124
125 this(parent, text, x, y, colorKey, true, action);
126 }
127
128 /**
129 * Public constructor.
130 *
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
137 */
138 public TLabel(final TWidget parent, final String text, final int x,
139 final int y, final String colorKey, final boolean useWindowBackground) {
140
141 this(parent, text, x, y, colorKey, useWindowBackground, null);
142 }
143
144 /**
145 * Public constructor.
146 *
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
154 */
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) {
158
159 // Set parent and window
160 super(parent, false, x, y, text.length(), 1);
161
162 mnemonic = new MnemonicString(text);
163 this.colorKey = colorKey;
164 this.useWindowBackground = useWindowBackground;
165 this.action = action;
166 }
167
168 // ------------------------------------------------------------------------
169 // TWidget ----------------------------------------------------------------
170 // ------------------------------------------------------------------------
171
172 /**
173 * Draw a static label.
174 */
175 @Override
176 public void draw() {
177 // Setup my color
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());
186 }
187 putStringXY(0, 0, mnemonic.getRawLabel(), color);
188 if (mnemonic.getShortcutIdx() >= 0) {
189 putCharXY(mnemonic.getShortcutIdx(), 0,
190 mnemonic.getShortcut(), mnemonicColor);
191 }
192 }
193
194 // ------------------------------------------------------------------------
195 // TLabel -----------------------------------------------------------------
196 // ------------------------------------------------------------------------
197
198 /**
199 * Get label raw text.
200 *
201 * @return label text
202 */
203 public String getLabel() {
204 return mnemonic.getRawLabel();
205 }
206
207 /**
208 * Get the mnemonic string for this label.
209 *
210 * @return mnemonic string
211 */
212 public MnemonicString getMnemonic() {
213 return mnemonic;
214 }
215
216 /**
217 * Set label text.
218 *
219 * @param label new label text
220 */
221 public void setLabel(final String label) {
222 mnemonic = new MnemonicString(label);
223 }
224
225 /**
226 * Act as though the mnemonic shortcut was pressed.
227 */
228 public void dispatch() {
229 if (action != null) {
230 action.DO();
231 }
232 }
233
234 }