Many changes:
[fanfix.git] / src / jexer / bits / ColorTheme.java
index e55398a8235f38b4677f2dc9cd81edf514cdf735..1c4670a76f3e04dd0297bd83d4915b8662149aa8 100644 (file)
@@ -44,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<String, CellAttributes> colors;
 
+    // ------------------------------------------------------------------------
+    // Constructors -----------------------------------------------------------
+    // ------------------------------------------------------------------------
+
     /**
      * Public constructor sets the theme to the default.
      */
@@ -59,6 +67,10 @@ public final class ColorTheme {
         setDefaultTheme();
     }
 
+    // ------------------------------------------------------------------------
+    // ColorTheme -------------------------------------------------------------
+    // ------------------------------------------------------------------------
+
     /**
      * Retrieve the CellAttributes for a named theme color.
      *
@@ -118,6 +130,85 @@ public final class ColorTheme {
         load(new FileReader(filename));
     }
 
+    /**
+     * Set a color based on a text string.  Color text string is of the form:
+     * <code>[ bold ] [ blink ] { foreground on background }</code>
+     *
+     * @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) {
+                e.printStackTrace();
+            }
+
+            // "on"
+            if (!tokenizer.nextToken().toLowerCase().equals("on")) {
+                // Invalid line.
+                return;
+            }
+
+            // Background
+            int backColorRGB = -1;
+            try {
+                backColorRGB = Integer.parseInt(tokenizer.nextToken(), 16);
+            } catch (NumberFormatException e) {
+                e.printStackTrace();
+            }
+
+            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.
@@ -129,56 +220,19 @@ public final class ColorTheme {
         BufferedReader bufferedReader = new BufferedReader(reader);
         String line = bufferedReader.readLine();
         for (; line != null; line = bufferedReader.readLine()) {
-            String key;
-            boolean bold = false;
-            boolean blink = false;
-            String foreColor;
-            String backColor;
-            String token;
-
             // 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"
-            StringTokenizer tokenizer = new StringTokenizer(line);
-            key = tokenizer.nextToken();
-            if (!tokenizer.nextToken().equals("=")) {
-                // Skip this line
+            if (line.indexOf('=') == -1) {
+                // Invalid line.
                 continue;
             }
-            token = tokenizer.nextToken();
-            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" or "blah"
-            foreColor = token.toLowerCase();
-
-            if (!tokenizer.nextToken().toLowerCase().equals("on")) {
-                // Skip this line
-                continue;
-            }
-            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);
+            String key = line.substring(0, line.indexOf(':')).trim();
+            String text = line.substring(line.indexOf(':') + 1);
+            setColorFromString(key, text);
         }
         // All done.
         bufferedReader.close();
@@ -327,7 +381,7 @@ public final class ColorTheme {
         color.setBold(true);
         colors.put("tfield.active", color);
 
-        // TCheckbox
+        // TCheckBox
         color = new CellAttributes();
         color.setForeColor(Color.WHITE);
         color.setBackColor(Color.BLUE);
@@ -339,6 +393,56 @@ public final class ColorTheme {
         color.setBold(true);
         colors.put("tcheckbox.active", 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();
@@ -437,10 +541,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();
@@ -459,10 +571,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();
@@ -490,4 +610,14 @@ public final class ColorTheme {
 
     }
 
+    /**
+     * Make human-readable description of this Cell.
+     *
+     * @return displayable String
+     */
+    @Override
+    public String toString() {
+        return colors.toString();
+    }
+
 }