2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2017 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 java
.io
.BufferedReader
;
32 import java
.io
.FileReader
;
33 import java
.io
.FileWriter
;
34 import java
.io
.IOException
;
35 import java
.io
.Reader
;
36 import java
.util
.ArrayList
;
37 import java
.util
.List
;
39 import java
.util
.SortedMap
;
40 import java
.util
.StringTokenizer
;
41 import java
.util
.TreeMap
;
44 * ColorTheme is a collection of colors keyed by string. A default theme is
45 * also provided that matches the blue-and-white theme used by Turbo Vision.
47 public final class ColorTheme
{
50 * The current theme colors.
52 private SortedMap
<String
, CellAttributes
> colors
;
55 * Public constructor sets the theme to the default.
58 colors
= new TreeMap
<String
, CellAttributes
>();
63 * Retrieve the CellAttributes for a named theme color.
65 * @param name theme color name, e.g. "twindow.border"
66 * @return color associated with name, e.g. bold yellow on blue
68 public CellAttributes
getColor(final String name
) {
69 CellAttributes attr
= (CellAttributes
) colors
.get(name
);
74 * Retrieve all the names in the theme.
76 * @return a list of names
78 public List
<String
> getColorNames() {
79 Set
<String
> keys
= colors
.keySet();
80 List
<String
> names
= new ArrayList
<String
>(keys
.size());
86 * Set the color for a named theme color.
88 * @param name theme color name, e.g. "twindow.border"
89 * @param color the new color to associate with name, e.g. bold yellow on
92 public void setColor(final String name
, final CellAttributes color
) {
93 colors
.put(name
, color
);
97 * Save the color theme mappings to an ASCII file.
99 * @param filename file to write to
100 * @throws IOException if the I/O fails
102 public void save(final String filename
) throws IOException
{
103 FileWriter file
= new FileWriter(filename
);
104 for (String key
: colors
.keySet()) {
105 CellAttributes color
= getColor(key
);
106 file
.write(String
.format("%s = %s\n", key
, color
));
112 * Read color theme mappings from an ASCII file.
114 * @param filename file to read from
115 * @throws IOException if the I/O fails
117 public void load(final String filename
) throws IOException
{
118 load(new FileReader(filename
));
122 * Set a color based on a text string. Color text string is of the form:
123 * <code>[ bold ] [ blink ] { foreground on background }</code>
125 * @param key the color key string
126 * @param text the text string
128 public void setColorFromString(final String key
, final String text
) {
129 boolean bold
= false;
130 boolean blink
= false;
135 StringTokenizer tokenizer
= new StringTokenizer(text
);
136 token
= tokenizer
.nextToken();
137 while (token
.equals("bold") || token
.equals("blink")) {
138 if (token
.equals("bold")) {
140 token
= tokenizer
.nextToken();
142 if (token
.equals("blink")) {
144 token
= tokenizer
.nextToken();
148 // What's left is "blah on blah"
149 foreColor
= token
.toLowerCase();
151 if (!tokenizer
.nextToken().toLowerCase().equals("on")) {
155 backColor
= tokenizer
.nextToken().toLowerCase();
157 CellAttributes color
= new CellAttributes();
162 color
.setBlink(true);
164 color
.setForeColor(Color
.getColor(foreColor
));
165 color
.setBackColor(Color
.getColor(backColor
));
166 colors
.put(key
, color
);
170 * Read color theme mappings from a Reader. The reader is closed at the
173 * @param reader the reader to read from
174 * @throws IOException if the I/O fails
176 public void load(final Reader reader
) throws IOException
{
177 BufferedReader bufferedReader
= new BufferedReader(reader
);
178 String line
= bufferedReader
.readLine();
179 for (; line
!= null; line
= bufferedReader
.readLine()) {
180 // Look for lines that resemble:
181 // "key = blah on blah"
182 // "key = bold blah on blah"
183 // "key = blink bold blah on blah"
184 // "key = bold blink blah on blah"
185 // "key = blink blah on blah"
186 if (line
.indexOf('=') == -1) {
190 String key
= line
.substring(0, line
.indexOf(':')).trim();
191 String text
= line
.substring(line
.indexOf(':') + 1);
192 setColorFromString(key
, text
);
195 bufferedReader
.close();
199 * Sets to defaults that resemble the Borland IDE colors.
201 public void setDefaultTheme() {
202 CellAttributes color
;
205 color
= new CellAttributes();
206 color
.setForeColor(Color
.WHITE
);
207 color
.setBackColor(Color
.BLUE
);
209 colors
.put("twindow.border", color
);
211 // TWindow background
212 color
= new CellAttributes();
213 color
.setForeColor(Color
.YELLOW
);
214 color
.setBackColor(Color
.BLUE
);
216 colors
.put("twindow.background", color
);
218 // TWindow border - inactive
219 color
= new CellAttributes();
220 color
.setForeColor(Color
.BLACK
);
221 color
.setBackColor(Color
.BLUE
);
223 colors
.put("twindow.border.inactive", color
);
225 // TWindow background - inactive
226 color
= new CellAttributes();
227 color
.setForeColor(Color
.YELLOW
);
228 color
.setBackColor(Color
.BLUE
);
230 colors
.put("twindow.background.inactive", color
);
232 // TWindow border - modal
233 color
= new CellAttributes();
234 color
.setForeColor(Color
.WHITE
);
235 color
.setBackColor(Color
.WHITE
);
237 colors
.put("twindow.border.modal", color
);
239 // TWindow background - modal
240 color
= new CellAttributes();
241 color
.setForeColor(Color
.BLACK
);
242 color
.setBackColor(Color
.WHITE
);
243 color
.setBold(false);
244 colors
.put("twindow.background.modal", color
);
246 // TWindow border - modal + inactive
247 color
= new CellAttributes();
248 color
.setForeColor(Color
.BLACK
);
249 color
.setBackColor(Color
.WHITE
);
251 colors
.put("twindow.border.modal.inactive", color
);
253 // TWindow background - modal + inactive
254 color
= new CellAttributes();
255 color
.setForeColor(Color
.BLACK
);
256 color
.setBackColor(Color
.WHITE
);
257 color
.setBold(false);
258 colors
.put("twindow.background.modal.inactive", color
);
260 // TWindow border - during window movement - modal
261 color
= new CellAttributes();
262 color
.setForeColor(Color
.GREEN
);
263 color
.setBackColor(Color
.WHITE
);
265 colors
.put("twindow.border.modal.windowmove", color
);
267 // TWindow border - during window movement
268 color
= new CellAttributes();
269 color
.setForeColor(Color
.GREEN
);
270 color
.setBackColor(Color
.BLUE
);
272 colors
.put("twindow.border.windowmove", color
);
274 // TWindow background - during window movement
275 color
= new CellAttributes();
276 color
.setForeColor(Color
.YELLOW
);
277 color
.setBackColor(Color
.BLUE
);
278 color
.setBold(false);
279 colors
.put("twindow.background.windowmove", color
);
281 // TDesktop background
282 color
= new CellAttributes();
283 color
.setForeColor(Color
.BLUE
);
284 color
.setBackColor(Color
.WHITE
);
285 color
.setBold(false);
286 colors
.put("tdesktop.background", color
);
289 color
= new CellAttributes();
290 color
.setForeColor(Color
.BLACK
);
291 color
.setBackColor(Color
.GREEN
);
292 color
.setBold(false);
293 colors
.put("tbutton.inactive", color
);
294 color
= new CellAttributes();
295 color
.setForeColor(Color
.CYAN
);
296 color
.setBackColor(Color
.GREEN
);
298 colors
.put("tbutton.active", color
);
299 color
= new CellAttributes();
300 color
.setForeColor(Color
.BLACK
);
301 color
.setBackColor(Color
.WHITE
);
303 colors
.put("tbutton.disabled", color
);
304 color
= new CellAttributes();
305 color
.setForeColor(Color
.YELLOW
);
306 color
.setBackColor(Color
.GREEN
);
308 colors
.put("tbutton.mnemonic", color
);
309 color
= new CellAttributes();
310 color
.setForeColor(Color
.YELLOW
);
311 color
.setBackColor(Color
.GREEN
);
313 colors
.put("tbutton.mnemonic.highlighted", color
);
316 color
= new CellAttributes();
317 color
.setForeColor(Color
.WHITE
);
318 color
.setBackColor(Color
.BLUE
);
320 colors
.put("tlabel", color
);
323 color
= new CellAttributes();
324 color
.setForeColor(Color
.WHITE
);
325 color
.setBackColor(Color
.BLUE
);
326 color
.setBold(false);
327 colors
.put("ttext", color
);
330 color
= new CellAttributes();
331 color
.setForeColor(Color
.WHITE
);
332 color
.setBackColor(Color
.BLUE
);
333 color
.setBold(false);
334 colors
.put("tfield.inactive", color
);
335 color
= new CellAttributes();
336 color
.setForeColor(Color
.YELLOW
);
337 color
.setBackColor(Color
.BLACK
);
339 colors
.put("tfield.active", color
);
342 color
= new CellAttributes();
343 color
.setForeColor(Color
.WHITE
);
344 color
.setBackColor(Color
.BLUE
);
345 color
.setBold(false);
346 colors
.put("tcheckbox.inactive", color
);
347 color
= new CellAttributes();
348 color
.setForeColor(Color
.YELLOW
);
349 color
.setBackColor(Color
.BLACK
);
351 colors
.put("tcheckbox.active", color
);
354 color
= new CellAttributes();
355 color
.setForeColor(Color
.WHITE
);
356 color
.setBackColor(Color
.BLUE
);
357 color
.setBold(false);
358 colors
.put("tradiobutton.inactive", color
);
359 color
= new CellAttributes();
360 color
.setForeColor(Color
.YELLOW
);
361 color
.setBackColor(Color
.BLACK
);
363 colors
.put("tradiobutton.active", color
);
366 color
= new CellAttributes();
367 color
.setForeColor(Color
.WHITE
);
368 color
.setBackColor(Color
.BLUE
);
369 color
.setBold(false);
370 colors
.put("tradiogroup.inactive", color
);
371 color
= new CellAttributes();
372 color
.setForeColor(Color
.YELLOW
);
373 color
.setBackColor(Color
.BLUE
);
375 colors
.put("tradiogroup.active", color
);
378 color
= new CellAttributes();
379 color
.setForeColor(Color
.BLACK
);
380 color
.setBackColor(Color
.WHITE
);
381 color
.setBold(false);
382 colors
.put("tmenu", color
);
383 color
= new CellAttributes();
384 color
.setForeColor(Color
.BLACK
);
385 color
.setBackColor(Color
.GREEN
);
386 color
.setBold(false);
387 colors
.put("tmenu.highlighted", color
);
388 color
= new CellAttributes();
389 color
.setForeColor(Color
.RED
);
390 color
.setBackColor(Color
.WHITE
);
391 color
.setBold(false);
392 colors
.put("tmenu.mnemonic", color
);
393 color
= new CellAttributes();
394 color
.setForeColor(Color
.RED
);
395 color
.setBackColor(Color
.GREEN
);
396 color
.setBold(false);
397 colors
.put("tmenu.mnemonic.highlighted", color
);
398 color
= new CellAttributes();
399 color
.setForeColor(Color
.BLACK
);
400 color
.setBackColor(Color
.WHITE
);
402 colors
.put("tmenu.disabled", color
);
405 color
= new CellAttributes();
406 color
.setForeColor(Color
.BLUE
);
407 color
.setBackColor(Color
.BLUE
);
409 colors
.put("tprogressbar.complete", color
);
410 color
= new CellAttributes();
411 color
.setForeColor(Color
.WHITE
);
412 color
.setBackColor(Color
.BLUE
);
413 color
.setBold(false);
414 colors
.put("tprogressbar.incomplete", color
);
416 // THScroller / TVScroller
417 color
= new CellAttributes();
418 color
.setForeColor(Color
.CYAN
);
419 color
.setBackColor(Color
.BLUE
);
420 color
.setBold(false);
421 colors
.put("tscroller.bar", color
);
422 color
= new CellAttributes();
423 color
.setForeColor(Color
.BLUE
);
424 color
.setBackColor(Color
.CYAN
);
425 color
.setBold(false);
426 colors
.put("tscroller.arrows", color
);
429 color
= new CellAttributes();
430 color
.setForeColor(Color
.WHITE
);
431 color
.setBackColor(Color
.BLUE
);
432 color
.setBold(false);
433 colors
.put("ttreeview", color
);
434 color
= new CellAttributes();
435 color
.setForeColor(Color
.GREEN
);
436 color
.setBackColor(Color
.BLUE
);
438 colors
.put("ttreeview.expandbutton", color
);
439 color
= new CellAttributes();
440 color
.setForeColor(Color
.BLACK
);
441 color
.setBackColor(Color
.CYAN
);
442 color
.setBold(false);
443 colors
.put("ttreeview.selected", color
);
444 color
= new CellAttributes();
445 color
.setForeColor(Color
.RED
);
446 color
.setBackColor(Color
.BLUE
);
447 color
.setBold(false);
448 colors
.put("ttreeview.unreadable", color
);
449 color
= new CellAttributes();
450 // color.setForeColor(Color.BLACK);
451 // color.setBackColor(Color.BLUE);
452 // color.setBold(true);
453 color
.setForeColor(Color
.WHITE
);
454 color
.setBackColor(Color
.BLUE
);
455 color
.setBold(false);
456 colors
.put("ttreeview.inactive", color
);
459 color
= new CellAttributes();
460 color
.setForeColor(Color
.WHITE
);
461 color
.setBackColor(Color
.BLUE
);
462 color
.setBold(false);
463 colors
.put("tlist", color
);
464 color
= new CellAttributes();
465 color
.setForeColor(Color
.BLACK
);
466 color
.setBackColor(Color
.CYAN
);
467 color
.setBold(false);
468 colors
.put("tlist.selected", color
);
469 color
= new CellAttributes();
470 color
.setForeColor(Color
.BLACK
);
471 color
.setBackColor(Color
.CYAN
);
472 color
.setBold(false);
473 colors
.put("tlist.unreadable", color
);
474 color
= new CellAttributes();
475 // color.setForeColor(Color.BLACK);
476 // color.setBackColor(Color.BLUE);
477 // color.setBold(true);
478 color
.setForeColor(Color
.WHITE
);
479 color
.setBackColor(Color
.BLUE
);
480 color
.setBold(false);
481 colors
.put("tlist.inactive", color
);
484 color
= new CellAttributes();
485 color
.setForeColor(Color
.BLACK
);
486 color
.setBackColor(Color
.WHITE
);
487 color
.setBold(false);
488 colors
.put("tstatusbar.text", color
);
489 color
= new CellAttributes();
490 color
.setForeColor(Color
.RED
);
491 color
.setBackColor(Color
.WHITE
);
492 color
.setBold(false);
493 colors
.put("tstatusbar.button", color
);
494 color
= new CellAttributes();
495 color
.setForeColor(Color
.WHITE
);
496 color
.setBackColor(Color
.BLUE
);
497 color
.setBold(false);
498 colors
.put("tstatusbar.selected", color
);
501 color
= new CellAttributes();
502 color
.setForeColor(Color
.WHITE
);
503 color
.setBackColor(Color
.BLUE
);
504 color
.setBold(false);
505 colors
.put("teditor", color
);
510 * Make human-readable description of this Cell.
512 * @return displayable String
515 public String
toString() {
516 return colors
.toString();