From 2420f903afc54a5bcf61f1bdd5e3dfada2fab5b2 Mon Sep 17 00:00:00 2001 From: Kevin Lamonte Date: Sun, 8 Mar 2015 21:21:48 -0400 Subject: [PATCH] ColorTheme load/save --- demos/Demo1.java | 12 ++++ src/jexer/bits/CellAttributes.java | 2 +- src/jexer/bits/ColorTheme.java | 99 ++++++++++++++++-------------- 3 files changed, 67 insertions(+), 46 deletions(-) diff --git a/demos/Demo1.java b/demos/Demo1.java index 94f0e9f..6f4869a 100644 --- a/demos/Demo1.java +++ b/demos/Demo1.java @@ -30,12 +30,24 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA */ + +import jexer.bits.*; import jexer.TApplication; /** * The demo application itself. */ class DemoApplication extends TApplication { + /** + * Public constructor + */ + public DemoApplication() { + try { + ColorTheme theme = new ColorTheme(); + } catch (Exception e) { + e.printStackTrace(); + } + } } /** diff --git a/src/jexer/bits/CellAttributes.java b/src/jexer/bits/CellAttributes.java index 5bfa847..7dce699 100644 --- a/src/jexer/bits/CellAttributes.java +++ b/src/jexer/bits/CellAttributes.java @@ -160,7 +160,7 @@ public class CellAttributes { * @param color "red", "blue", etc. * @return Color.RED, Color.BLUE, etc. */ - static private Color colorFromString(String color) { + static public Color colorFromString(String color) { switch (color.toLowerCase()) { case "black": return Color.BLACK; diff --git a/src/jexer/bits/ColorTheme.java b/src/jexer/bits/ColorTheme.java index e1f5b77..ff78af4 100644 --- a/src/jexer/bits/ColorTheme.java +++ b/src/jexer/bits/ColorTheme.java @@ -32,9 +32,13 @@ */ package jexer.bits; -import java.util.HashMap; -import java.util.Map; - +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.util.SortedMap; +import java.util.StringTokenizer; +import java.util.TreeMap; /** * ColorTheme is a collection of colors keyed by string. @@ -44,7 +48,15 @@ public class ColorTheme { /** * The current theme colors */ - private Map colors; + private SortedMap colors; + + /** + * Public constructor. + */ + public ColorTheme() { + colors = new TreeMap(); + setDefaultTheme(); + } /** * Retrieve the CellAttributes by name. @@ -62,14 +74,13 @@ public class ColorTheme { * * @param filename file to write to */ - public void save(String filename) { - /* - auto file = File(filename, "wt"); - foreach (string key; colors.keys.sort) { - CellAttributes color = colors[key]; - file.writefln("%s = %s", key, color); + public void save(String filename) throws IOException { + FileWriter file = new FileWriter(filename); + for (String key: colors.keySet()) { + CellAttributes color = getColor(key); + file.write(String.format("%s = %s\n", key, color)); } - */ + file.close(); } /** @@ -77,45 +88,51 @@ public class ColorTheme { * * @param filename file to read from */ - public void load(String filename) { - /* - string text = std.file.readText!(string)(filename); - foreach (line; std.string.splitLines!(string)(text)) { - string key; - string bold; - string foreColor; - string on; - string backColor; - auto tokenCount = formattedRead(line, "%s = %s %s %s %s", - &key, &bold, &foreColor, &on, &backColor); - if (tokenCount == 4) { - std.stdio.stderr.writefln("1 %s = %s %s %s %s", - key, bold, foreColor, on, backColor); - + public void load(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; + + // 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; - backColor = on; - bold = ""; - } else if (tokenCount == 5) { - // "key = bold blah on blah" - std.stdio.stderr.writefln("2 %s = %s %s %s %s", - key, bold, foreColor, on, backColor); } else { - // Unknown line, skip this one + // "key = bold blah on blah" + foreColor = tokenizer.nextToken().toLowerCase(); + } + if (!tokenizer.nextToken().toLowerCase().equals("on")) { + // Skip this line continue; } + backColor = tokenizer.nextToken().toLowerCase(); + CellAttributes color = new CellAttributes(); - if (bold == "bold") { + if (bold.equals("bold")) { color.bold = true; } color.foreColor = CellAttributes.colorFromString(foreColor); color.backColor = CellAttributes.colorFromString(backColor); - colors[key] = color; + colors.put(key, color); } - */ } - /// Sets to defaults that resemble the Borland IDE colors. + /** + * Sets to defaults that resemble the Borland IDE colors. + */ public void setDefaultTheme() { CellAttributes color; @@ -400,14 +417,6 @@ public class ColorTheme { color.bold = false; colors.put("teditor", color); - } - /** - * Public constructor. - */ - public ColorTheme() { - setDefaultTheme(); - } } - -- 2.27.0