X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2Fbits%2FColorTheme.java;h=ffba4d472cc67c36ebda4114e503ba1db545c719;hb=12b90437b5f22c2ae6e9b9b14c3b62b60f6143e5;hp=d3a468f416dd3d2986282f7b53653d097ffc4a90;hpb=2ce6dab2bbd951e6d0f09f94759efda5ee4b65ac;p=nikiroo-utils.git diff --git a/src/jexer/bits/ColorTheme.java b/src/jexer/bits/ColorTheme.java index d3a468f..ffba4d4 100644 --- a/src/jexer/bits/ColorTheme.java +++ b/src/jexer/bits/ColorTheme.java @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (C) 2016 Kevin Lamonte + * Copyright (C) 2019 Kevin Lamonte * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -32,6 +32,7 @@ import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; +import java.io.Reader; import java.util.ArrayList; import java.util.List; import java.util.Set; @@ -43,13 +44,21 @@ import java.util.TreeMap; * ColorTheme is a collection of colors keyed by string. A default theme is * also provided that matches the blue-and-white theme used by Turbo Vision. */ -public final class ColorTheme { +public class ColorTheme { + + // ------------------------------------------------------------------------ + // Variables -------------------------------------------------------------- + // ------------------------------------------------------------------------ /** * The current theme colors. */ private SortedMap colors; + // ------------------------------------------------------------------------ + // Constructors ----------------------------------------------------------- + // ------------------------------------------------------------------------ + /** * Public constructor sets the theme to the default. */ @@ -58,6 +67,10 @@ public final class ColorTheme { setDefaultTheme(); } + // ------------------------------------------------------------------------ + // ColorTheme ------------------------------------------------------------- + // ------------------------------------------------------------------------ + /** * Retrieve the CellAttributes for a named theme color. * @@ -65,7 +78,7 @@ public final class ColorTheme { * @return color associated with name, e.g. bold yellow on blue */ public CellAttributes getColor(final String name) { - CellAttributes attr = (CellAttributes) colors.get(name); + CellAttributes attr = colors.get(name); return attr; } @@ -114,47 +127,116 @@ public final class ColorTheme { * @throws IOException if the I/O fails */ public void load(final String filename) throws IOException { - BufferedReader reader = new BufferedReader(new FileReader(filename)); - String line = reader.readLine(); - for (; line != null; line = reader.readLine()) { - String key; - String bold; - String foreColor; - String backColor; + load(new FileReader(filename)); + } - // Look for lines that resemble: - // "key = blah on blah" - // "key = bold blah on blah" - StringTokenizer tokenizer = new StringTokenizer(line); - key = tokenizer.nextToken(); - if (!tokenizer.nextToken().equals("=")) { - // Skip this line - continue; - } - bold = tokenizer.nextToken(); - if (!bold.toLowerCase().equals("bold")) { - // "key = blah on blah" - foreColor = bold; - } else { - // "key = bold blah on blah" - foreColor = tokenizer.nextToken().toLowerCase(); + /** + * Set a color based on a text string. Color text string is of the form: + * [ bold ] [ blink ] { foreground on background } + * + * @param key the color key string + * @param text the text string + */ + public void setColorFromString(final String key, final String text) { + boolean bold = false; + boolean blink = false; + String foreColor; + String backColor; + String token; + + StringTokenizer tokenizer = new StringTokenizer(text); + token = tokenizer.nextToken(); + + if (token.toLowerCase().equals("rgb:")) { + // Foreground + int foreColorRGB = -1; + try { + foreColorRGB = Integer.parseInt(tokenizer.nextToken(), 16); + } catch (NumberFormatException e) { + // Default to white on black + foreColorRGB = 0xFFFFFF; } + + // "on" if (!tokenizer.nextToken().toLowerCase().equals("on")) { - // Skip this line - continue; + // Invalid line. + return; } - backColor = tokenizer.nextToken().toLowerCase(); - CellAttributes color = new CellAttributes(); - if (bold.equals("bold")) { - color.setBold(true); + // Background + int backColorRGB = -1; + try { + backColorRGB = Integer.parseInt(tokenizer.nextToken(), 16); + } catch (NumberFormatException e) { + backColorRGB = 0; } - color.setForeColor(Color.getColor(foreColor)); - color.setBackColor(Color.getColor(backColor)); + + CellAttributes color = new CellAttributes(); + color.setForeColorRGB(foreColorRGB); + color.setBackColorRGB(backColorRGB); colors.put(key, color); + return; + } + + while (token.equals("bold") || token.equals("blink")) { + if (token.equals("bold")) { + bold = true; + token = tokenizer.nextToken(); + } + if (token.equals("blink")) { + blink = true; + token = tokenizer.nextToken(); + } + } + + // What's left is "blah on blah" + foreColor = token.toLowerCase(); + + if (!tokenizer.nextToken().toLowerCase().equals("on")) { + // Invalid line. + return; + } + backColor = tokenizer.nextToken().toLowerCase(); + + CellAttributes color = new CellAttributes(); + if (bold) { + color.setBold(true); + } + if (blink) { + color.setBlink(true); + } + color.setForeColor(Color.getColor(foreColor)); + color.setBackColor(Color.getColor(backColor)); + colors.put(key, color); + } + + /** + * Read color theme mappings from a Reader. The reader is closed at the + * end. + * + * @param reader the reader to read from + * @throws IOException if the I/O fails + */ + public void load(final Reader reader) throws IOException { + BufferedReader bufferedReader = new BufferedReader(reader); + String line = bufferedReader.readLine(); + for (; line != null; line = bufferedReader.readLine()) { + // Look for lines that resemble: + // "key = blah on blah" + // "key = bold blah on blah" + // "key = blink bold blah on blah" + // "key = bold blink blah on blah" + // "key = blink blah on blah" + if (line.indexOf('=') == -1) { + // Invalid line. + continue; + } + String key = line.substring(0, line.indexOf(':')).trim(); + String text = line.substring(line.indexOf(':') + 1); + setColorFromString(key, text); } // All done. - reader.close(); + bufferedReader.close(); } /** @@ -240,12 +322,12 @@ public final class ColorTheme { color.setBold(false); colors.put("twindow.background.windowmove", color); - // TApplication background + // TDesktop background color = new CellAttributes(); color.setForeColor(Color.BLUE); color.setBackColor(Color.WHITE); color.setBold(false); - colors.put("tapplication.background", color); + colors.put("tdesktop.background", color); // TButton text color = new CellAttributes(); @@ -280,27 +362,32 @@ public final class ColorTheme { color.setBackColor(Color.BLUE); color.setBold(true); colors.put("tlabel", color); + color = new CellAttributes(); + color.setForeColor(Color.YELLOW); + color.setBackColor(Color.BLUE); + color.setBold(true); + colors.put("tlabel.mnemonic", color); // TText text color = new CellAttributes(); color.setForeColor(Color.WHITE); - color.setBackColor(Color.BLACK); + color.setBackColor(Color.BLUE); color.setBold(false); colors.put("ttext", color); // TField text color = new CellAttributes(); - color.setForeColor(Color.WHITE); - color.setBackColor(Color.BLUE); + color.setForeColor(Color.BLACK); + color.setBackColor(Color.WHITE); color.setBold(false); colors.put("tfield.inactive", color); color = new CellAttributes(); - color.setForeColor(Color.YELLOW); - color.setBackColor(Color.BLACK); - color.setBold(true); + color.setForeColor(Color.BLACK); + color.setBackColor(Color.CYAN); + color.setBold(false); colors.put("tfield.active", color); - // TCheckbox + // TCheckBox color = new CellAttributes(); color.setForeColor(Color.WHITE); color.setBackColor(Color.BLUE); @@ -311,7 +398,67 @@ public final class ColorTheme { color.setBackColor(Color.BLACK); color.setBold(true); colors.put("tcheckbox.active", color); + color = new CellAttributes(); + color.setForeColor(Color.YELLOW); + color.setBackColor(Color.BLUE); + color.setBold(true); + colors.put("tcheckbox.mnemonic", color); + color = new CellAttributes(); + color.setForeColor(Color.RED); + color.setBackColor(Color.BLACK); + color.setBold(true); + colors.put("tcheckbox.mnemonic.highlighted", color); + + // TComboBox + color = new CellAttributes(); + color.setForeColor(Color.BLACK); + color.setBackColor(Color.WHITE); + color.setBold(false); + colors.put("tcombobox.inactive", color); + color = new CellAttributes(); + color.setForeColor(Color.BLUE); + color.setBackColor(Color.CYAN); + color.setBold(false); + colors.put("tcombobox.active", color); + + // TSpinner + color = new CellAttributes(); + color.setForeColor(Color.BLACK); + color.setBackColor(Color.WHITE); + color.setBold(false); + colors.put("tspinner.inactive", color); + color = new CellAttributes(); + color.setForeColor(Color.BLUE); + color.setBackColor(Color.CYAN); + color.setBold(false); + colors.put("tspinner.active", color); + // TCalendar + color = new CellAttributes(); + color.setForeColor(Color.WHITE); + color.setBackColor(Color.BLUE); + color.setBold(false); + colors.put("tcalendar.background", color); + color = new CellAttributes(); + color.setForeColor(Color.WHITE); + color.setBackColor(Color.BLUE); + color.setBold(false); + colors.put("tcalendar.day", color); + color = new CellAttributes(); + color.setForeColor(Color.RED); + color.setBackColor(Color.WHITE); + color.setBold(false); + colors.put("tcalendar.day.selected", color); + color = new CellAttributes(); + color.setForeColor(Color.BLUE); + color.setBackColor(Color.CYAN); + color.setBold(false); + colors.put("tcalendar.arrow", color); + color = new CellAttributes(); + color.setForeColor(Color.WHITE); + color.setBackColor(Color.BLUE); + color.setBold(true); + colors.put("tcalendar.title", color); // TRadioButton color = new CellAttributes(); @@ -324,6 +471,16 @@ public final class ColorTheme { color.setBackColor(Color.BLACK); color.setBold(true); colors.put("tradiobutton.active", color); + color = new CellAttributes(); + color.setForeColor(Color.YELLOW); + color.setBackColor(Color.BLUE); + color.setBold(true); + colors.put("tradiobutton.mnemonic", color); + color = new CellAttributes(); + color.setForeColor(Color.RED); + color.setBackColor(Color.BLACK); + color.setBold(true); + colors.put("tradiobutton.mnemonic.highlighted", color); // TRadioGroup color = new CellAttributes(); @@ -410,10 +567,18 @@ public final class ColorTheme { color.setBold(false); colors.put("ttreeview.unreadable", color); color = new CellAttributes(); - color.setForeColor(Color.BLACK); + // color.setForeColor(Color.BLACK); + // color.setBackColor(Color.BLUE); + // color.setBold(true); + color.setForeColor(Color.WHITE); color.setBackColor(Color.BLUE); - color.setBold(true); + color.setBold(false); colors.put("ttreeview.inactive", color); + color = new CellAttributes(); + color.setForeColor(Color.BLACK); + color.setBackColor(Color.WHITE); + color.setBold(false); + colors.put("ttreeview.selected.inactive", color); // TList color = new CellAttributes(); @@ -432,10 +597,18 @@ public final class ColorTheme { color.setBold(false); colors.put("tlist.unreadable", color); color = new CellAttributes(); - color.setForeColor(Color.BLACK); + // color.setForeColor(Color.BLACK); + // color.setBackColor(Color.BLUE); + // color.setBold(true); + color.setForeColor(Color.WHITE); color.setBackColor(Color.BLUE); - color.setBold(true); + color.setBold(false); colors.put("tlist.inactive", color); + color = new CellAttributes(); + color.setForeColor(Color.BLACK); + color.setBackColor(Color.WHITE); + color.setBold(false); + colors.put("tlist.selected.inactive", color); // TStatusBar color = new CellAttributes(); @@ -457,10 +630,59 @@ public final class ColorTheme { // TEditor color = new CellAttributes(); color.setForeColor(Color.WHITE); - color.setBackColor(Color.BLACK); + color.setBackColor(Color.BLUE); color.setBold(false); colors.put("teditor", color); + // TTable + color = new CellAttributes(); + color.setForeColor(Color.WHITE); + color.setBackColor(Color.BLUE); + color.setBold(false); + colors.put("ttable.inactive", color); + color = new CellAttributes(); + color.setForeColor(Color.BLACK); + color.setBackColor(Color.CYAN); + color.setBold(false); + colors.put("ttable.active", color); + color = new CellAttributes(); + color.setForeColor(Color.YELLOW); + color.setBackColor(Color.CYAN); + color.setBold(true); + colors.put("ttable.selected", color); + color = new CellAttributes(); + color.setForeColor(Color.BLACK); + color.setBackColor(Color.WHITE); + color.setBold(false); + colors.put("ttable.label", color); + color = new CellAttributes(); + color.setForeColor(Color.BLUE); + color.setBackColor(Color.WHITE); + color.setBold(false); + colors.put("ttable.label.selected", color); + color = new CellAttributes(); + color.setForeColor(Color.WHITE); + color.setBackColor(Color.BLUE); + color.setBold(false); + colors.put("ttable.border", color); + + // TSplitPane + color = new CellAttributes(); + color.setForeColor(Color.WHITE); + color.setBackColor(Color.BLUE); + color.setBold(false); + colors.put("tsplitpane", color); + + } + + /** + * Make human-readable description of this Cell. + * + * @return displayable String + */ + @Override + public String toString() { + return colors.toString(); } }