Commit | Line | Data |
---|---|---|
daa4106c | 1 | /* |
30d336cc KL |
2 | * Jexer - Java Text User Interface |
3 | * | |
e16dda65 | 4 | * The MIT License (MIT) |
30d336cc | 5 | * |
a69ed767 | 6 | * Copyright (C) 2019 Kevin Lamonte |
30d336cc | 7 | * |
e16dda65 KL |
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: | |
30d336cc | 14 | * |
e16dda65 KL |
15 | * The above copyright notice and this permission notice shall be included in |
16 | * all copies or substantial portions of the Software. | |
30d336cc | 17 | * |
e16dda65 KL |
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. | |
30d336cc KL |
25 | * |
26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
27 | * @version 1 | |
28 | */ | |
29 | package jexer; | |
30 | ||
31 | import jexer.bits.CellAttributes; | |
00691e80 | 32 | import jexer.bits.MnemonicString; |
9f613a0c | 33 | import jexer.bits.StringUtils; |
30d336cc KL |
34 | |
35 | /** | |
00691e80 KL |
36 | * TLabel implements a simple label, with an optional mnemonic hotkey action |
37 | * associated with it. | |
30d336cc | 38 | */ |
051e2913 | 39 | public class TLabel extends TWidget { |
30d336cc | 40 | |
d36057df KL |
41 | // ------------------------------------------------------------------------ |
42 | // Variables -------------------------------------------------------------- | |
43 | // ------------------------------------------------------------------------ | |
44 | ||
30d336cc | 45 | /** |
00691e80 | 46 | * The shortcut and label. |
30d336cc | 47 | */ |
00691e80 KL |
48 | private MnemonicString mnemonic; |
49 | ||
50 | /** | |
51 | * The action to perform when the mnemonic shortcut is pressed. | |
52 | */ | |
53 | private TAction action; | |
30d336cc KL |
54 | |
55 | /** | |
56 | * Label color. | |
57 | */ | |
58 | private String colorKey; | |
59 | ||
051e2913 KL |
60 | /** |
61 | * If true, use the window's background color. | |
62 | */ | |
63 | private boolean useWindowBackground = true; | |
64 | ||
d36057df KL |
65 | // ------------------------------------------------------------------------ |
66 | // Constructors ----------------------------------------------------------- | |
67 | // ------------------------------------------------------------------------ | |
68 | ||
30d336cc KL |
69 | /** |
70 | * Public constructor, using the default "tlabel" for colorKey. | |
71 | * | |
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 | |
76 | */ | |
77 | public TLabel(final TWidget parent, final String text, final int x, | |
78 | final int y) { | |
79 | ||
80 | this(parent, text, x, y, "tlabel"); | |
81 | } | |
82 | ||
00691e80 KL |
83 | /** |
84 | * Public constructor, using the default "tlabel" for colorKey. | |
85 | * | |
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 | |
91 | */ | |
92 | public TLabel(final TWidget parent, final String text, final int x, | |
93 | final int y, final TAction action) { | |
94 | ||
95 | this(parent, text, x, y, "tlabel", action); | |
96 | } | |
97 | ||
30d336cc KL |
98 | /** |
99 | * Public constructor. | |
100 | * | |
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 | |
106 | */ | |
107 | public TLabel(final TWidget parent, final String text, final int x, | |
108 | final int y, final String colorKey) { | |
109 | ||
051e2913 KL |
110 | this(parent, text, x, y, colorKey, true); |
111 | } | |
112 | ||
00691e80 KL |
113 | /** |
114 | * Public constructor. | |
115 | * | |
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 | |
122 | */ | |
123 | public TLabel(final TWidget parent, final String text, final int x, | |
124 | final int y, final String colorKey, final TAction action) { | |
125 | ||
126 | this(parent, text, x, y, colorKey, true, action); | |
127 | } | |
128 | ||
051e2913 KL |
129 | /** |
130 | * Public constructor. | |
131 | * | |
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 | |
138 | */ | |
139 | public TLabel(final TWidget parent, final String text, final int x, | |
140 | final int y, final String colorKey, final boolean useWindowBackground) { | |
141 | ||
00691e80 KL |
142 | this(parent, text, x, y, colorKey, useWindowBackground, null); |
143 | } | |
144 | ||
145 | /** | |
146 | * Public constructor. | |
147 | * | |
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 | |
155 | */ | |
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) { | |
159 | ||
30d336cc | 160 | // Set parent and window |
9f613a0c | 161 | super(parent, false, x, y, StringUtils.width(text), 1); |
30d336cc | 162 | |
00691e80 | 163 | mnemonic = new MnemonicString(text); |
30d336cc | 164 | this.colorKey = colorKey; |
051e2913 | 165 | this.useWindowBackground = useWindowBackground; |
00691e80 | 166 | this.action = action; |
30d336cc KL |
167 | } |
168 | ||
d36057df KL |
169 | // ------------------------------------------------------------------------ |
170 | // TWidget ---------------------------------------------------------------- | |
171 | // ------------------------------------------------------------------------ | |
172 | ||
d8dc8aea KL |
173 | /** |
174 | * Override TWidget's height: we can only set height at construction | |
175 | * time. | |
176 | * | |
177 | * @param height new widget height (ignored) | |
178 | */ | |
179 | @Override | |
180 | public void setHeight(final int height) { | |
181 | // Do nothing | |
182 | } | |
183 | ||
30d336cc KL |
184 | /** |
185 | * Draw a static label. | |
186 | */ | |
2ce6dab2 KL |
187 | @Override |
188 | public void draw() { | |
30d336cc KL |
189 | // Setup my color |
190 | CellAttributes color = new CellAttributes(); | |
00691e80 | 191 | CellAttributes mnemonicColor = new CellAttributes(); |
30d336cc | 192 | color.setTo(getTheme().getColor(colorKey)); |
00691e80 | 193 | mnemonicColor.setTo(getTheme().getColor("tlabel.mnemonic")); |
051e2913 KL |
194 | if (useWindowBackground) { |
195 | CellAttributes background = getWindow().getBackground(); | |
196 | color.setBackColor(background.getBackColor()); | |
00691e80 KL |
197 | mnemonicColor.setBackColor(background.getBackColor()); |
198 | } | |
199 | putStringXY(0, 0, mnemonic.getRawLabel(), color); | |
3fe82fa7 KL |
200 | if (mnemonic.getScreenShortcutIdx() >= 0) { |
201 | putCharXY(mnemonic.getScreenShortcutIdx(), 0, | |
00691e80 | 202 | mnemonic.getShortcut(), mnemonicColor); |
051e2913 | 203 | } |
30d336cc KL |
204 | } |
205 | ||
d36057df KL |
206 | // ------------------------------------------------------------------------ |
207 | // TLabel ----------------------------------------------------------------- | |
208 | // ------------------------------------------------------------------------ | |
209 | ||
210 | /** | |
00691e80 | 211 | * Get label raw text. |
d36057df KL |
212 | * |
213 | * @return label text | |
214 | */ | |
215 | public String getLabel() { | |
00691e80 KL |
216 | return mnemonic.getRawLabel(); |
217 | } | |
218 | ||
219 | /** | |
220 | * Get the mnemonic string for this label. | |
221 | * | |
222 | * @return mnemonic string | |
223 | */ | |
224 | public MnemonicString getMnemonic() { | |
225 | return mnemonic; | |
d36057df KL |
226 | } |
227 | ||
228 | /** | |
229 | * Set label text. | |
230 | * | |
231 | * @param label new label text | |
232 | */ | |
233 | public void setLabel(final String label) { | |
00691e80 KL |
234 | mnemonic = new MnemonicString(label); |
235 | } | |
236 | ||
1dac6b8d KL |
237 | /** |
238 | * Get the label color. | |
239 | * | |
36bad4f9 | 240 | * @return the ColorTheme key color to use for foreground text |
1dac6b8d KL |
241 | */ |
242 | public String getColorKey() { | |
243 | return colorKey; | |
244 | } | |
245 | ||
246 | /** | |
247 | * Set the label color. | |
248 | * | |
249 | * @param colorKey ColorTheme key color to use for foreground text | |
250 | */ | |
251 | public void setColorKey(final String colorKey) { | |
252 | this.colorKey = colorKey; | |
253 | } | |
254 | ||
00691e80 KL |
255 | /** |
256 | * Act as though the mnemonic shortcut was pressed. | |
257 | */ | |
258 | public void dispatch() { | |
259 | if (action != null) { | |
a524aa2e | 260 | action.DO(this); |
00691e80 | 261 | } |
d36057df KL |
262 | } |
263 | ||
30d336cc | 264 | } |