2 * Jexer - Java Text User Interface
4 * License: LGPLv3 or later
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.
10 * Copyright (C) 2015 Kevin Lamonte
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.
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.
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
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
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
;
40 import java
.util
.SortedMap
;
41 import java
.util
.StringTokenizer
;
42 import java
.util
.TreeMap
;
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.
48 public final class ColorTheme
{
51 * The current theme colors.
53 private SortedMap
<String
, CellAttributes
> colors
;
56 * Public constructor sets the theme to the default.
59 colors
= new TreeMap
<String
, CellAttributes
>();
64 * Retrieve the CellAttributes for a named theme color.
66 * @param name theme color name, e.g. "twindow.border"
67 * @return color associated with name, e.g. bold yellow on blue
69 public CellAttributes
getColor(final String name
) {
70 CellAttributes attr
= (CellAttributes
) colors
.get(name
);
75 * Retrieve all the names in the theme.
77 * @return a list of names
79 public List
<String
> getColorNames() {
80 Set
<String
> keys
= colors
.keySet();
81 List
<String
> names
= new ArrayList
<String
>(keys
.size());
87 * Set the color for a named theme color.
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
93 public void setColor(final String name
, final CellAttributes color
) {
94 colors
.put(name
, color
);
98 * Save the color theme mappings to an ASCII file.
100 * @param filename file to write to
101 * @throws IOException if the I/O fails
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
));
113 * Read color theme mappings from an ASCII file.
115 * @param filename file to read from
116 * @throws IOException if the I/O fails
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()) {
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("=")) {
136 bold
= tokenizer
.nextToken();
137 if (!bold
.toLowerCase().equals("bold")) {
138 // "key = blah on blah"
141 // "key = bold blah on blah"
142 foreColor
= tokenizer
.nextToken().toLowerCase();
144 if (!tokenizer
.nextToken().toLowerCase().equals("on")) {
148 backColor
= tokenizer
.nextToken().toLowerCase();
150 CellAttributes color
= new CellAttributes();
151 if (bold
.equals("bold")) {
154 color
.setForeColor(Color
.getColor(foreColor
));
155 color
.setBackColor(Color
.getColor(backColor
));
156 colors
.put(key
, color
);
163 * Sets to defaults that resemble the Borland IDE colors.
165 public void setDefaultTheme() {
166 CellAttributes color
;
169 color
= new CellAttributes();
170 color
.setForeColor(Color
.WHITE
);
171 color
.setBackColor(Color
.BLUE
);
173 colors
.put("twindow.border", color
);
175 // TWindow background
176 color
= new CellAttributes();
177 color
.setForeColor(Color
.YELLOW
);
178 color
.setBackColor(Color
.BLUE
);
180 colors
.put("twindow.background", color
);
182 // TWindow border - inactive
183 color
= new CellAttributes();
184 color
.setForeColor(Color
.BLACK
);
185 color
.setBackColor(Color
.BLUE
);
187 colors
.put("twindow.border.inactive", color
);
189 // TWindow background - inactive
190 color
= new CellAttributes();
191 color
.setForeColor(Color
.YELLOW
);
192 color
.setBackColor(Color
.BLUE
);
194 colors
.put("twindow.background.inactive", color
);
196 // TWindow border - modal
197 color
= new CellAttributes();
198 color
.setForeColor(Color
.WHITE
);
199 color
.setBackColor(Color
.WHITE
);
201 colors
.put("twindow.border.modal", color
);
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
);
210 // TWindow border - modal + inactive
211 color
= new CellAttributes();
212 color
.setForeColor(Color
.BLACK
);
213 color
.setBackColor(Color
.WHITE
);
215 colors
.put("twindow.border.modal.inactive", color
);
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
);
224 // TWindow border - during window movement - modal
225 color
= new CellAttributes();
226 color
.setForeColor(Color
.GREEN
);
227 color
.setBackColor(Color
.WHITE
);
229 colors
.put("twindow.border.modal.windowmove", color
);
231 // TWindow border - during window movement
232 color
= new CellAttributes();
233 color
.setForeColor(Color
.GREEN
);
234 color
.setBackColor(Color
.BLUE
);
236 colors
.put("twindow.border.windowmove", color
);
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
);
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
);
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
);
262 colors
.put("tbutton.active", color
);
263 color
= new CellAttributes();
264 color
.setForeColor(Color
.BLACK
);
265 color
.setBackColor(Color
.WHITE
);
267 colors
.put("tbutton.disabled", color
);
268 color
= new CellAttributes();
269 color
.setForeColor(Color
.YELLOW
);
270 color
.setBackColor(Color
.GREEN
);
272 colors
.put("tbutton.mnemonic", color
);
273 color
= new CellAttributes();
274 color
.setForeColor(Color
.YELLOW
);
275 color
.setBackColor(Color
.GREEN
);
277 colors
.put("tbutton.mnemonic.highlighted", color
);
280 color
= new CellAttributes();
281 color
.setForeColor(Color
.WHITE
);
282 color
.setBackColor(Color
.BLUE
);
284 colors
.put("tlabel", color
);
287 color
= new CellAttributes();
288 color
.setForeColor(Color
.WHITE
);
289 color
.setBackColor(Color
.BLACK
);
290 color
.setBold(false);
291 colors
.put("ttext", color
);
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
);
303 colors
.put("tfield.active", color
);
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
);
315 colors
.put("tcheckbox.active", color
);
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
);
328 colors
.put("tradiobutton.active", color
);
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
);
340 colors
.put("tradiogroup.active", color
);
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
);
367 colors
.put("tmenu.disabled", color
);
370 color
= new CellAttributes();
371 color
.setForeColor(Color
.BLUE
);
372 color
.setBackColor(Color
.BLUE
);
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
);
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
);
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
);
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
);
418 colors
.put("ttreeview.inactive", color
);
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
);
440 colors
.put("tlist.inactive", color
);
443 color
= new CellAttributes();
444 color
.setForeColor(Color
.WHITE
);
445 color
.setBackColor(Color
.BLACK
);
446 color
.setBold(false);
447 colors
.put("teditor", color
);