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; |
30d336cc KL |
33 | |
34 | /** | |
00691e80 KL |
35 | * TLabel implements a simple label, with an optional mnemonic hotkey action |
36 | * associated with it. | |
30d336cc | 37 | */ |
051e2913 | 38 | public class TLabel extends TWidget { |
30d336cc | 39 | |
d36057df KL |
40 | // ------------------------------------------------------------------------ |
41 | // Variables -------------------------------------------------------------- | |
42 | // ------------------------------------------------------------------------ | |
43 | ||
30d336cc | 44 | /** |
00691e80 | 45 | * The shortcut and label. |
30d336cc | 46 | */ |
00691e80 KL |
47 | private MnemonicString mnemonic; |
48 | ||
49 | /** | |
50 | * The action to perform when the mnemonic shortcut is pressed. | |
51 | */ | |
52 | private TAction action; | |
30d336cc KL |
53 | |
54 | /** | |
55 | * Label color. | |
56 | */ | |
57 | private String colorKey; | |
58 | ||
051e2913 KL |
59 | /** |
60 | * If true, use the window's background color. | |
61 | */ | |
62 | private boolean useWindowBackground = true; | |
63 | ||
d36057df KL |
64 | // ------------------------------------------------------------------------ |
65 | // Constructors ----------------------------------------------------------- | |
66 | // ------------------------------------------------------------------------ | |
67 | ||
30d336cc KL |
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 | ||
00691e80 KL |
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 | ||
30d336cc KL |
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 | ||
051e2913 KL |
109 | this(parent, text, x, y, colorKey, true); |
110 | } | |
111 | ||
00691e80 KL |
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 | ||
051e2913 KL |
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 | ||
00691e80 KL |
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 | ||
30d336cc | 159 | // Set parent and window |
a83fea2b | 160 | super(parent, false, x, y, text.length(), 1); |
30d336cc | 161 | |
00691e80 | 162 | mnemonic = new MnemonicString(text); |
30d336cc | 163 | this.colorKey = colorKey; |
051e2913 | 164 | this.useWindowBackground = useWindowBackground; |
00691e80 | 165 | this.action = action; |
30d336cc KL |
166 | } |
167 | ||
d36057df KL |
168 | // ------------------------------------------------------------------------ |
169 | // TWidget ---------------------------------------------------------------- | |
170 | // ------------------------------------------------------------------------ | |
171 | ||
30d336cc KL |
172 | /** |
173 | * Draw a static label. | |
174 | */ | |
2ce6dab2 KL |
175 | @Override |
176 | public void draw() { | |
30d336cc KL |
177 | // Setup my color |
178 | CellAttributes color = new CellAttributes(); | |
00691e80 | 179 | CellAttributes mnemonicColor = new CellAttributes(); |
30d336cc | 180 | color.setTo(getTheme().getColor(colorKey)); |
00691e80 | 181 | mnemonicColor.setTo(getTheme().getColor("tlabel.mnemonic")); |
051e2913 KL |
182 | if (useWindowBackground) { |
183 | CellAttributes background = getWindow().getBackground(); | |
184 | color.setBackColor(background.getBackColor()); | |
00691e80 KL |
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); | |
051e2913 | 191 | } |
30d336cc KL |
192 | } |
193 | ||
d36057df KL |
194 | // ------------------------------------------------------------------------ |
195 | // TLabel ----------------------------------------------------------------- | |
196 | // ------------------------------------------------------------------------ | |
197 | ||
198 | /** | |
00691e80 | 199 | * Get label raw text. |
d36057df KL |
200 | * |
201 | * @return label text | |
202 | */ | |
203 | public String getLabel() { | |
00691e80 KL |
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; | |
d36057df KL |
214 | } |
215 | ||
216 | /** | |
217 | * Set label text. | |
218 | * | |
219 | * @param label new label text | |
220 | */ | |
221 | public void setLabel(final String label) { | |
00691e80 KL |
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 | } | |
d36057df KL |
232 | } |
233 | ||
30d336cc | 234 | } |