Expose width/height in TApplication constructor, attempt on ECMA48
[nikiroo-utils.git] / src / jexer / bits / ColorTheme.java
index 5ed503266f92dd58bbfa815726d13df943a97276..baf7685b4ef2331a2cb07a41e67700bc745a4c00 100644 (file)
@@ -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;
@@ -114,47 +115,84 @@ 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));
+    }
+
+    /**
+     * 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();
+        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"
-            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();
-            }
-            if (!tokenizer.nextToken().toLowerCase().equals("on")) {
-                // Skip this line
+            //     "key = blink bold blah on blah"
+            //     "key = bold blink blah on blah"
+            //     "key = blink blah on blah"
+            if (line.indexOf('=') == -1) {
+                // Invalid line.
                 continue;
             }
-            backColor = tokenizer.nextToken().toLowerCase();
-
-            CellAttributes color = new CellAttributes();
-            if (bold.equals("bold")) {
-                color.setBold(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.
-        reader.close();
+        bufferedReader.close();
     }
 
     /**
@@ -240,12 +278,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();
@@ -284,7 +322,7 @@ public final class ColorTheme {
         // TText text
         color = new CellAttributes();
         color.setForeColor(Color.WHITE);
-        color.setBackColor(Color.BLACK);
+        color.setBackColor(Color.BLUE);
         color.setBold(false);
         colors.put("ttext", color);
 
@@ -312,7 +350,6 @@ public final class ColorTheme {
         color.setBold(true);
         colors.put("tcheckbox.active", color);
 
-
         // TRadioButton
         color = new CellAttributes();
         color.setForeColor(Color.WHITE);
@@ -410,9 +447,12 @@ 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);
 
         // TList
@@ -432,9 +472,12 @@ 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);
 
         // TStatusBar
@@ -457,10 +500,20 @@ 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);
 
     }
 
+    /**
+     * Make human-readable description of this Cell.
+     *
+     * @return displayable String
+     */
+    @Override
+    public String toString() {
+        return colors.toString();
+    }
+
 }