eaa351cb12d7b2d5e86011ddb17bd24f04f04395
[nikiroo-utils.git] / src / jexer / bits / ColorTheme.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * License: LGPLv3 or later
5 *
6 * This module is licensed under the GNU Lesser General Public License
7 * Version 3. Please see the file "COPYING" in this directory for more
8 * information about the GNU Lesser General Public License Version 3.
9 *
10 * Copyright (C) 2015 Kevin Lamonte
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 3 of
15 * the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this program; if not, see
24 * http://www.gnu.org/licenses/, or write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
26 * 02110-1301 USA
27 *
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 * @version 1
30 */
31 package jexer.bits;
32
33 import java.io.BufferedReader;
34 import java.io.FileReader;
35 import java.io.FileWriter;
36 import java.io.IOException;
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.Set;
40 import java.util.SortedMap;
41 import java.util.StringTokenizer;
42 import java.util.TreeMap;
43
44 /**
45 * ColorTheme is a collection of colors keyed by string. A default theme is
46 * also provided that matches the blue-and-white theme used by Turbo Vision.
47 */
48 public final class ColorTheme {
49
50 /**
51 * The current theme colors.
52 */
53 private SortedMap<String, CellAttributes> colors;
54
55 /**
56 * Public constructor sets the theme to the default.
57 */
58 public ColorTheme() {
59 colors = new TreeMap<String, CellAttributes>();
60 setDefaultTheme();
61 }
62
63 /**
64 * Retrieve the CellAttributes for a named theme color.
65 *
66 * @param name theme color name, e.g. "twindow.border"
67 * @return color associated with name, e.g. bold yellow on blue
68 */
69 public CellAttributes getColor(final String name) {
70 CellAttributes attr = (CellAttributes) colors.get(name);
71 return attr;
72 }
73
74 /**
75 * Retrieve all the names in the theme.
76 *
77 * @return a list of names
78 */
79 public List<String> getColorNames() {
80 Set<String> keys = colors.keySet();
81 List<String> names = new ArrayList<String>(keys.size());
82 names.addAll(keys);
83 return names;
84 }
85
86 /**
87 * Set the color for a named theme color.
88 *
89 * @param name theme color name, e.g. "twindow.border"
90 * @param color the new color to associate with name, e.g. bold yellow on
91 * blue
92 */
93 public void setColor(final String name, final CellAttributes color) {
94 colors.put(name, color);
95 }
96
97 /**
98 * Save the color theme mappings to an ASCII file.
99 *
100 * @param filename file to write to
101 * @throws IOException if the I/O fails
102 */
103 public void save(final String filename) throws IOException {
104 FileWriter file = new FileWriter(filename);
105 for (String key: colors.keySet()) {
106 CellAttributes color = getColor(key);
107 file.write(String.format("%s = %s\n", key, color));
108 }
109 file.close();
110 }
111
112 /**
113 * Read color theme mappings from an ASCII file.
114 *
115 * @param filename file to read from
116 * @throws IOException if the I/O fails
117 */
118 public void load(final String filename) throws IOException {
119 BufferedReader reader = new BufferedReader(new FileReader(filename));
120 String line = reader.readLine();
121 for (; line != null; line = reader.readLine()) {
122 String key;
123 String bold;
124 String foreColor;
125 String backColor;
126
127 // Look for lines that resemble:
128 // "key = blah on blah"
129 // "key = bold blah on blah"
130 StringTokenizer tokenizer = new StringTokenizer(line);
131 key = tokenizer.nextToken();
132 if (!tokenizer.nextToken().equals("=")) {
133 // Skip this line
134 continue;
135 }
136 bold = tokenizer.nextToken();
137 if (!bold.toLowerCase().equals("bold")) {
138 // "key = blah on blah"
139 foreColor = bold;
140 } else {
141 // "key = bold blah on blah"
142 foreColor = tokenizer.nextToken().toLowerCase();
143 }
144 if (!tokenizer.nextToken().toLowerCase().equals("on")) {
145 // Skip this line
146 continue;
147 }
148 backColor = tokenizer.nextToken().toLowerCase();
149
150 CellAttributes color = new CellAttributes();
151 if (bold.equals("bold")) {
152 color.setBold(true);
153 }
154 color.setForeColor(Color.getColor(foreColor));
155 color.setBackColor(Color.getColor(backColor));
156 colors.put(key, color);
157 }
158 // All done.
159 reader.close();
160 }
161
162 /**
163 * Sets to defaults that resemble the Borland IDE colors.
164 */
165 public void setDefaultTheme() {
166 CellAttributes color;
167
168 // TWindow border
169 color = new CellAttributes();
170 color.setForeColor(Color.WHITE);
171 color.setBackColor(Color.BLUE);
172 color.setBold(true);
173 colors.put("twindow.border", color);
174
175 // TWindow background
176 color = new CellAttributes();
177 color.setForeColor(Color.YELLOW);
178 color.setBackColor(Color.BLUE);
179 color.setBold(true);
180 colors.put("twindow.background", color);
181
182 // TWindow border - inactive
183 color = new CellAttributes();
184 color.setForeColor(Color.BLACK);
185 color.setBackColor(Color.BLUE);
186 color.setBold(true);
187 colors.put("twindow.border.inactive", color);
188
189 // TWindow background - inactive
190 color = new CellAttributes();
191 color.setForeColor(Color.YELLOW);
192 color.setBackColor(Color.BLUE);
193 color.setBold(true);
194 colors.put("twindow.background.inactive", color);
195
196 // TWindow border - modal
197 color = new CellAttributes();
198 color.setForeColor(Color.WHITE);
199 color.setBackColor(Color.WHITE);
200 color.setBold(true);
201 colors.put("twindow.border.modal", color);
202
203 // TWindow background - modal
204 color = new CellAttributes();
205 color.setForeColor(Color.BLACK);
206 color.setBackColor(Color.WHITE);
207 color.setBold(false);
208 colors.put("twindow.background.modal", color);
209
210 // TWindow border - modal + inactive
211 color = new CellAttributes();
212 color.setForeColor(Color.BLACK);
213 color.setBackColor(Color.WHITE);
214 color.setBold(true);
215 colors.put("twindow.border.modal.inactive", color);
216
217 // TWindow background - modal + inactive
218 color = new CellAttributes();
219 color.setForeColor(Color.BLACK);
220 color.setBackColor(Color.WHITE);
221 color.setBold(false);
222 colors.put("twindow.background.modal.inactive", color);
223
224 // TWindow border - during window movement - modal
225 color = new CellAttributes();
226 color.setForeColor(Color.GREEN);
227 color.setBackColor(Color.WHITE);
228 color.setBold(true);
229 colors.put("twindow.border.modal.windowmove", color);
230
231 // TWindow border - during window movement
232 color = new CellAttributes();
233 color.setForeColor(Color.GREEN);
234 color.setBackColor(Color.BLUE);
235 color.setBold(true);
236 colors.put("twindow.border.windowmove", color);
237
238 // TWindow background - during window movement
239 color = new CellAttributes();
240 color.setForeColor(Color.YELLOW);
241 color.setBackColor(Color.BLUE);
242 color.setBold(false);
243 colors.put("twindow.background.windowmove", color);
244
245 // TApplication background
246 color = new CellAttributes();
247 color.setForeColor(Color.BLUE);
248 color.setBackColor(Color.WHITE);
249 color.setBold(false);
250 colors.put("tapplication.background", color);
251
252 // TButton text
253 color = new CellAttributes();
254 color.setForeColor(Color.BLACK);
255 color.setBackColor(Color.GREEN);
256 color.setBold(false);
257 colors.put("tbutton.inactive", color);
258 color = new CellAttributes();
259 color.setForeColor(Color.CYAN);
260 color.setBackColor(Color.GREEN);
261 color.setBold(true);
262 colors.put("tbutton.active", color);
263 color = new CellAttributes();
264 color.setForeColor(Color.BLACK);
265 color.setBackColor(Color.WHITE);
266 color.setBold(true);
267 colors.put("tbutton.disabled", color);
268 color = new CellAttributes();
269 color.setForeColor(Color.YELLOW);
270 color.setBackColor(Color.GREEN);
271 color.setBold(true);
272 colors.put("tbutton.mnemonic", color);
273 color = new CellAttributes();
274 color.setForeColor(Color.YELLOW);
275 color.setBackColor(Color.GREEN);
276 color.setBold(true);
277 colors.put("tbutton.mnemonic.highlighted", color);
278
279 // TLabel text
280 color = new CellAttributes();
281 color.setForeColor(Color.WHITE);
282 color.setBackColor(Color.BLUE);
283 color.setBold(true);
284 colors.put("tlabel", color);
285
286 // TText text
287 color = new CellAttributes();
288 color.setForeColor(Color.WHITE);
289 color.setBackColor(Color.BLACK);
290 color.setBold(false);
291 colors.put("ttext", color);
292
293 // TField text
294 color = new CellAttributes();
295 color.setForeColor(Color.WHITE);
296 color.setBackColor(Color.BLUE);
297 color.setBold(false);
298 colors.put("tfield.inactive", color);
299 color = new CellAttributes();
300 color.setForeColor(Color.YELLOW);
301 color.setBackColor(Color.BLACK);
302 color.setBold(true);
303 colors.put("tfield.active", color);
304
305 // TCheckbox
306 color = new CellAttributes();
307 color.setForeColor(Color.WHITE);
308 color.setBackColor(Color.BLUE);
309 color.setBold(false);
310 colors.put("tcheckbox.inactive", color);
311 color = new CellAttributes();
312 color.setForeColor(Color.YELLOW);
313 color.setBackColor(Color.BLACK);
314 color.setBold(true);
315 colors.put("tcheckbox.active", color);
316
317
318 // TRadioButton
319 color = new CellAttributes();
320 color.setForeColor(Color.WHITE);
321 color.setBackColor(Color.BLUE);
322 color.setBold(false);
323 colors.put("tradiobutton.inactive", color);
324 color = new CellAttributes();
325 color.setForeColor(Color.YELLOW);
326 color.setBackColor(Color.BLACK);
327 color.setBold(true);
328 colors.put("tradiobutton.active", color);
329
330 // TRadioGroup
331 color = new CellAttributes();
332 color.setForeColor(Color.WHITE);
333 color.setBackColor(Color.BLUE);
334 color.setBold(false);
335 colors.put("tradiogroup.inactive", color);
336 color = new CellAttributes();
337 color.setForeColor(Color.YELLOW);
338 color.setBackColor(Color.BLUE);
339 color.setBold(true);
340 colors.put("tradiogroup.active", color);
341
342 // TMenu
343 color = new CellAttributes();
344 color.setForeColor(Color.BLACK);
345 color.setBackColor(Color.WHITE);
346 color.setBold(false);
347 colors.put("tmenu", color);
348 color = new CellAttributes();
349 color.setForeColor(Color.BLACK);
350 color.setBackColor(Color.GREEN);
351 color.setBold(false);
352 colors.put("tmenu.highlighted", color);
353 color = new CellAttributes();
354 color.setForeColor(Color.RED);
355 color.setBackColor(Color.WHITE);
356 color.setBold(false);
357 colors.put("tmenu.mnemonic", color);
358 color = new CellAttributes();
359 color.setForeColor(Color.RED);
360 color.setBackColor(Color.GREEN);
361 color.setBold(false);
362 colors.put("tmenu.mnemonic.highlighted", color);
363 color = new CellAttributes();
364 color.setForeColor(Color.BLACK);
365 color.setBackColor(Color.WHITE);
366 color.setBold(true);
367 colors.put("tmenu.disabled", color);
368
369 // TProgressBar
370 color = new CellAttributes();
371 color.setForeColor(Color.BLUE);
372 color.setBackColor(Color.BLUE);
373 color.setBold(true);
374 colors.put("tprogressbar.complete", color);
375 color = new CellAttributes();
376 color.setForeColor(Color.WHITE);
377 color.setBackColor(Color.BLUE);
378 color.setBold(false);
379 colors.put("tprogressbar.incomplete", color);
380
381 // THScroller / TVScroller
382 color = new CellAttributes();
383 color.setForeColor(Color.CYAN);
384 color.setBackColor(Color.BLUE);
385 color.setBold(false);
386 colors.put("tscroller.bar", color);
387 color = new CellAttributes();
388 color.setForeColor(Color.BLUE);
389 color.setBackColor(Color.CYAN);
390 color.setBold(false);
391 colors.put("tscroller.arrows", color);
392
393 // TTreeView
394 color = new CellAttributes();
395 color.setForeColor(Color.WHITE);
396 color.setBackColor(Color.BLUE);
397 color.setBold(false);
398 colors.put("ttreeview", color);
399 color = new CellAttributes();
400 color.setForeColor(Color.GREEN);
401 color.setBackColor(Color.BLUE);
402 color.setBold(true);
403 colors.put("ttreeview.expandbutton", color);
404 color = new CellAttributes();
405 color.setForeColor(Color.BLACK);
406 color.setBackColor(Color.CYAN);
407 color.setBold(false);
408 colors.put("ttreeview.selected", color);
409 color = new CellAttributes();
410 color.setForeColor(Color.RED);
411 color.setBackColor(Color.BLUE);
412 color.setBold(false);
413 colors.put("ttreeview.unreadable", color);
414 color = new CellAttributes();
415 color.setForeColor(Color.BLACK);
416 color.setBackColor(Color.BLUE);
417 color.setBold(true);
418 colors.put("ttreeview.inactive", color);
419
420 // TList
421 color = new CellAttributes();
422 color.setForeColor(Color.WHITE);
423 color.setBackColor(Color.BLUE);
424 color.setBold(false);
425 colors.put("tlist", color);
426 color = new CellAttributes();
427 color.setForeColor(Color.BLACK);
428 color.setBackColor(Color.CYAN);
429 color.setBold(false);
430 colors.put("tlist.selected", color);
431 color = new CellAttributes();
432 color.setForeColor(Color.BLACK);
433 color.setBackColor(Color.CYAN);
434 color.setBold(false);
435 colors.put("tlist.unreadable", color);
436 color = new CellAttributes();
437 color.setForeColor(Color.BLACK);
438 color.setBackColor(Color.BLUE);
439 color.setBold(true);
440 colors.put("tlist.inactive", color);
441
442 // TEditor
443 color = new CellAttributes();
444 color.setForeColor(Color.WHITE);
445 color.setBackColor(Color.BLACK);
446 color.setBold(false);
447 colors.put("teditor", color);
448
449 }
450
451 }