Merge branch 'master' of https://github.com/klamonte/jexer
[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.SortedMap;
38 import java.util.StringTokenizer;
39 import java.util.TreeMap;
40
41 /**
42 * ColorTheme is a collection of colors keyed by string. A default theme is
43 * also provided that matches the blue-and-white theme used by Turbo Vision.
44 */
45 public final class ColorTheme {
46
47 /**
48 * The current theme colors.
49 */
50 private SortedMap<String, CellAttributes> colors;
51
52 /**
53 * Public constructor sets the theme to the default.
54 */
55 public ColorTheme() {
56 colors = new TreeMap<String, CellAttributes>();
57 setDefaultTheme();
58 }
59
60 /**
61 * Retrieve the CellAttributes for a named theme color.
62 *
63 * @param name theme color name, e.g. "twindow.border"
64 * @return color associated with name, e.g. bold yellow on blue
65 */
66 public CellAttributes getColor(final String name) {
67 CellAttributes attr = (CellAttributes) colors.get(name);
68 return attr;
69 }
70
71 /**
72 * Set the color for a named theme color.
73 *
74 * @param name theme color name, e.g. "twindow.border"
75 * @param color the new color to associate with name, e.g. bold yellow on
76 * blue
77 */
78 public void setColor(final String name, final CellAttributes color) {
79 colors.put(name, color);
80 }
81
82 /**
83 * Save the color theme mappings to an ASCII file.
84 *
85 * @param filename file to write to
86 * @throws IOException if the I/O fails
87 */
88 public void save(final String filename) throws IOException {
89 FileWriter file = new FileWriter(filename);
90 for (String key: colors.keySet()) {
91 CellAttributes color = getColor(key);
92 file.write(String.format("%s = %s\n", key, color));
93 }
94 file.close();
95 }
96
97 /**
98 * Read color theme mappings from an ASCII file.
99 *
100 * @param filename file to read from
101 * @throws IOException if the I/O fails
102 */
103 public void load(final String filename) throws IOException {
104 BufferedReader reader = new BufferedReader(new FileReader(filename));
105 String line = reader.readLine();
106 for (; line != null; line = reader.readLine()) {
107 String key;
108 String bold;
109 String foreColor;
110 String backColor;
111
112 // Look for lines that resemble:
113 // "key = blah on blah"
114 // "key = bold blah on blah"
115 StringTokenizer tokenizer = new StringTokenizer(line);
116 key = tokenizer.nextToken();
117 if (!tokenizer.nextToken().equals("=")) {
118 // Skip this line
119 continue;
120 }
121 bold = tokenizer.nextToken();
122 if (!bold.toLowerCase().equals("bold")) {
123 // "key = blah on blah"
124 foreColor = bold;
125 } else {
126 // "key = bold blah on blah"
127 foreColor = tokenizer.nextToken().toLowerCase();
128 }
129 if (!tokenizer.nextToken().toLowerCase().equals("on")) {
130 // Skip this line
131 continue;
132 }
133 backColor = tokenizer.nextToken().toLowerCase();
134
135 CellAttributes color = new CellAttributes();
136 if (bold.equals("bold")) {
137 color.setBold(true);
138 }
139 color.setForeColor(Color.getColor(foreColor));
140 color.setBackColor(Color.getColor(backColor));
141 colors.put(key, color);
142 }
143 // All done.
144 reader.close();
145 }
146
147 /**
148 * Sets to defaults that resemble the Borland IDE colors.
149 */
150 public void setDefaultTheme() {
151 CellAttributes color;
152
153 // TWindow border
154 color = new CellAttributes();
155 color.setForeColor(Color.WHITE);
156 color.setBackColor(Color.BLUE);
157 color.setBold(true);
158 colors.put("twindow.border", color);
159
160 // TWindow background
161 color = new CellAttributes();
162 color.setForeColor(Color.YELLOW);
163 color.setBackColor(Color.BLUE);
164 color.setBold(true);
165 colors.put("twindow.background", color);
166
167 // TWindow border - inactive
168 color = new CellAttributes();
169 color.setForeColor(Color.BLACK);
170 color.setBackColor(Color.BLUE);
171 color.setBold(true);
172 colors.put("twindow.border.inactive", color);
173
174 // TWindow background - inactive
175 color = new CellAttributes();
176 color.setForeColor(Color.YELLOW);
177 color.setBackColor(Color.BLUE);
178 color.setBold(true);
179 colors.put("twindow.background.inactive", color);
180
181 // TWindow border - modal
182 color = new CellAttributes();
183 color.setForeColor(Color.WHITE);
184 color.setBackColor(Color.WHITE);
185 color.setBold(true);
186 colors.put("twindow.border.modal", color);
187
188 // TWindow background - modal
189 color = new CellAttributes();
190 color.setForeColor(Color.BLACK);
191 color.setBackColor(Color.WHITE);
192 color.setBold(false);
193 colors.put("twindow.background.modal", color);
194
195 // TWindow border - modal + inactive
196 color = new CellAttributes();
197 color.setForeColor(Color.BLACK);
198 color.setBackColor(Color.WHITE);
199 color.setBold(true);
200 colors.put("twindow.border.modal.inactive", color);
201
202 // TWindow background - modal + inactive
203 color = new CellAttributes();
204 color.setForeColor(Color.BLACK);
205 color.setBackColor(Color.WHITE);
206 color.setBold(false);
207 colors.put("twindow.background.modal.inactive", color);
208
209 // TWindow border - during window movement - modal
210 color = new CellAttributes();
211 color.setForeColor(Color.GREEN);
212 color.setBackColor(Color.WHITE);
213 color.setBold(true);
214 colors.put("twindow.border.modal.windowmove", color);
215
216 // TWindow border - during window movement
217 color = new CellAttributes();
218 color.setForeColor(Color.GREEN);
219 color.setBackColor(Color.BLUE);
220 color.setBold(true);
221 colors.put("twindow.border.windowmove", color);
222
223 // TWindow background - during window movement
224 color = new CellAttributes();
225 color.setForeColor(Color.YELLOW);
226 color.setBackColor(Color.BLUE);
227 color.setBold(false);
228 colors.put("twindow.background.windowmove", color);
229
230 // TApplication background
231 color = new CellAttributes();
232 color.setForeColor(Color.BLUE);
233 color.setBackColor(Color.WHITE);
234 color.setBold(false);
235 colors.put("tapplication.background", color);
236
237 // TButton text
238 color = new CellAttributes();
239 color.setForeColor(Color.BLACK);
240 color.setBackColor(Color.GREEN);
241 color.setBold(false);
242 colors.put("tbutton.inactive", color);
243 color = new CellAttributes();
244 color.setForeColor(Color.WHITE);
245 color.setBackColor(Color.GREEN);
246 color.setBold(true);
247 colors.put("tbutton.active", color);
248 color = new CellAttributes();
249 color.setForeColor(Color.BLACK);
250 color.setBackColor(Color.WHITE);
251 color.setBold(true);
252 colors.put("tbutton.disabled", color);
253 color = new CellAttributes();
254 color.setForeColor(Color.YELLOW);
255 color.setBackColor(Color.GREEN);
256 color.setBold(true);
257 colors.put("tbutton.mnemonic", color);
258 color = new CellAttributes();
259 color.setForeColor(Color.YELLOW);
260 color.setBackColor(Color.GREEN);
261 color.setBold(true);
262 colors.put("tbutton.mnemonic.highlighted", color);
263
264 // TLabel text
265 color = new CellAttributes();
266 color.setForeColor(Color.WHITE);
267 color.setBackColor(Color.BLUE);
268 color.setBold(true);
269 colors.put("tlabel", color);
270
271 // TText text
272 color = new CellAttributes();
273 color.setForeColor(Color.WHITE);
274 color.setBackColor(Color.BLACK);
275 color.setBold(false);
276 colors.put("ttext", color);
277
278 // TField text
279 color = new CellAttributes();
280 color.setForeColor(Color.WHITE);
281 color.setBackColor(Color.BLUE);
282 color.setBold(false);
283 colors.put("tfield.inactive", color);
284 color = new CellAttributes();
285 color.setForeColor(Color.YELLOW);
286 color.setBackColor(Color.BLACK);
287 color.setBold(true);
288 colors.put("tfield.active", color);
289
290 // TCheckbox
291 color = new CellAttributes();
292 color.setForeColor(Color.WHITE);
293 color.setBackColor(Color.BLUE);
294 color.setBold(false);
295 colors.put("tcheckbox.inactive", color);
296 color = new CellAttributes();
297 color.setForeColor(Color.YELLOW);
298 color.setBackColor(Color.BLACK);
299 color.setBold(true);
300 colors.put("tcheckbox.active", color);
301
302
303 // TRadioButton
304 color = new CellAttributes();
305 color.setForeColor(Color.WHITE);
306 color.setBackColor(Color.BLUE);
307 color.setBold(false);
308 colors.put("tradiobutton.inactive", color);
309 color = new CellAttributes();
310 color.setForeColor(Color.YELLOW);
311 color.setBackColor(Color.BLACK);
312 color.setBold(true);
313 colors.put("tradiobutton.active", color);
314
315 // TRadioGroup
316 color = new CellAttributes();
317 color.setForeColor(Color.WHITE);
318 color.setBackColor(Color.BLUE);
319 color.setBold(false);
320 colors.put("tradiogroup.inactive", color);
321 color = new CellAttributes();
322 color.setForeColor(Color.YELLOW);
323 color.setBackColor(Color.BLUE);
324 color.setBold(true);
325 colors.put("tradiogroup.active", color);
326
327 // TMenu
328 color = new CellAttributes();
329 color.setForeColor(Color.BLACK);
330 color.setBackColor(Color.WHITE);
331 color.setBold(false);
332 colors.put("tmenu", color);
333 color = new CellAttributes();
334 color.setForeColor(Color.BLACK);
335 color.setBackColor(Color.GREEN);
336 color.setBold(false);
337 colors.put("tmenu.highlighted", color);
338 color = new CellAttributes();
339 color.setForeColor(Color.RED);
340 color.setBackColor(Color.WHITE);
341 color.setBold(false);
342 colors.put("tmenu.mnemonic", color);
343 color = new CellAttributes();
344 color.setForeColor(Color.RED);
345 color.setBackColor(Color.GREEN);
346 color.setBold(false);
347 colors.put("tmenu.mnemonic.highlighted", color);
348 color = new CellAttributes();
349 color.setForeColor(Color.BLACK);
350 color.setBackColor(Color.WHITE);
351 color.setBold(true);
352 colors.put("tmenu.disabled", color);
353
354 // TProgressBar
355 color = new CellAttributes();
356 color.setForeColor(Color.BLUE);
357 color.setBackColor(Color.BLUE);
358 color.setBold(true);
359 colors.put("tprogressbar.complete", color);
360 color = new CellAttributes();
361 color.setForeColor(Color.WHITE);
362 color.setBackColor(Color.BLUE);
363 color.setBold(false);
364 colors.put("tprogressbar.incomplete", color);
365
366 // THScroller / TVScroller
367 color = new CellAttributes();
368 color.setForeColor(Color.CYAN);
369 color.setBackColor(Color.BLUE);
370 color.setBold(false);
371 colors.put("tscroller.bar", color);
372 color = new CellAttributes();
373 color.setForeColor(Color.BLUE);
374 color.setBackColor(Color.CYAN);
375 color.setBold(false);
376 colors.put("tscroller.arrows", color);
377
378 // TTreeView
379 color = new CellAttributes();
380 color.setForeColor(Color.WHITE);
381 color.setBackColor(Color.BLUE);
382 color.setBold(false);
383 colors.put("ttreeview", color);
384 color = new CellAttributes();
385 color.setForeColor(Color.GREEN);
386 color.setBackColor(Color.BLUE);
387 color.setBold(true);
388 colors.put("ttreeview.expandbutton", color);
389 color = new CellAttributes();
390 color.setForeColor(Color.BLACK);
391 color.setBackColor(Color.CYAN);
392 color.setBold(false);
393 colors.put("ttreeview.selected", color);
394 color = new CellAttributes();
395 color.setForeColor(Color.RED);
396 color.setBackColor(Color.BLUE);
397 color.setBold(false);
398 colors.put("ttreeview.unreadable", color);
399 color = new CellAttributes();
400 color.setForeColor(Color.BLACK);
401 color.setBackColor(Color.BLUE);
402 color.setBold(true);
403 colors.put("ttreeview.inactive", color);
404
405 // TText text
406 color = new CellAttributes();
407 color.setForeColor(Color.WHITE);
408 color.setBackColor(Color.BLUE);
409 color.setBold(false);
410 colors.put("tdirectorylist", color);
411 color = new CellAttributes();
412 color.setForeColor(Color.BLACK);
413 color.setBackColor(Color.CYAN);
414 color.setBold(false);
415 colors.put("tdirectorylist.selected", color);
416 color = new CellAttributes();
417 color.setForeColor(Color.BLACK);
418 color.setBackColor(Color.CYAN);
419 color.setBold(false);
420 colors.put("tdirectorylist.unreadable", color);
421 color = new CellAttributes();
422 color.setForeColor(Color.BLACK);
423 color.setBackColor(Color.BLUE);
424 color.setBold(true);
425 colors.put("tdirectorylist.inactive", color);
426
427 // TEditor
428 color = new CellAttributes();
429 color.setForeColor(Color.WHITE);
430 color.setBackColor(Color.BLACK);
431 color.setBold(false);
432 colors.put("teditor", color);
433
434 }
435
436 }