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