mirror of https://github.com/noisymime/Arduino.git
Refactoring of Theme class
This commit is contained in:
parent
026dd50d87
commit
479b974fe1
|
@ -1,5 +1,3 @@
|
||||||
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Part of the Processing project - http://processing.org
|
Part of the Processing project - http://processing.org
|
||||||
|
|
||||||
|
@ -23,14 +21,14 @@
|
||||||
|
|
||||||
package processing.app;
|
package processing.app;
|
||||||
|
|
||||||
import java.awt.*;
|
|
||||||
import java.io.*;
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
import processing.app.syntax.*;
|
|
||||||
import processing.core.*;
|
|
||||||
import static processing.app.I18n._;
|
import static processing.app.I18n._;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.Font;
|
||||||
|
import java.awt.SystemColor;
|
||||||
|
|
||||||
|
import processing.app.helpers.PreferencesMap;
|
||||||
|
import processing.app.syntax.SyntaxStyle;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Storage class for theme settings. This was separated from the Preferences
|
* Storage class for theme settings. This was separated from the Preferences
|
||||||
|
@ -40,165 +38,110 @@ import static processing.app.I18n._;
|
||||||
public class Theme {
|
public class Theme {
|
||||||
|
|
||||||
/** Copy of the defaults in case the user mangles a preference. */
|
/** Copy of the defaults in case the user mangles a preference. */
|
||||||
static HashMap<String,String> defaults;
|
static PreferencesMap defaults;
|
||||||
/** Table of attributes/values for the theme. */
|
/** Table of attributes/values for the theme. */
|
||||||
static HashMap<String,String> table = new HashMap<String,String>();;
|
static PreferencesMap table = new PreferencesMap();
|
||||||
|
|
||||||
|
|
||||||
static protected void init() {
|
static protected void init() {
|
||||||
try {
|
try {
|
||||||
load(Base.getLibStream("theme/theme.txt"));
|
table.load(Base.getLibStream("theme/theme.txt"));
|
||||||
} catch (Exception te) {
|
} catch (Exception te) {
|
||||||
Base.showError(null, _("Could not read color theme settings.\n" +
|
Base.showError(null, _("Could not read color theme settings.\n" +
|
||||||
"You'll need to reinstall Arduino."), te);
|
"You'll need to reinstall Arduino."), te);
|
||||||
}
|
}
|
||||||
|
|
||||||
// check for platform-specific properties in the defaults
|
|
||||||
String platformExt = "." + Base.getPlatformName();
|
|
||||||
int platformExtLength = platformExt.length();
|
|
||||||
for (String key : table.keySet()) {
|
|
||||||
if (key.endsWith(platformExt)) {
|
|
||||||
// this is a key specific to a particular platform
|
|
||||||
String actualKey = key.substring(0, key.length() - platformExtLength);
|
|
||||||
String value = get(key);
|
|
||||||
table.put(actualKey, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// other things that have to be set explicitly for the defaults
|
// other things that have to be set explicitly for the defaults
|
||||||
setColor("run.window.bgcolor", SystemColor.control);
|
setColor("run.window.bgcolor", SystemColor.control);
|
||||||
|
|
||||||
// clone the hash table
|
// clone the hash table
|
||||||
defaults = (HashMap<String, String>) table.clone();
|
defaults = new PreferencesMap(table);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static protected void load(InputStream input) throws IOException {
|
|
||||||
String[] lines = PApplet.loadStrings(input);
|
|
||||||
for (String line : lines) {
|
|
||||||
if ((line.length() == 0) ||
|
|
||||||
(line.charAt(0) == '#')) continue;
|
|
||||||
|
|
||||||
// this won't properly handle = signs being in the text
|
|
||||||
int equals = line.indexOf('=');
|
|
||||||
if (equals != -1) {
|
|
||||||
String key = line.substring(0, equals).trim();
|
|
||||||
String value = line.substring(equals + 1).trim();
|
|
||||||
table.put(key, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static public String get(String attribute) {
|
static public String get(String attribute) {
|
||||||
return (String) table.get(attribute);
|
return table.get(attribute);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static public String getDefault(String attribute) {
|
static public String getDefault(String attribute) {
|
||||||
return (String) defaults.get(attribute);
|
return defaults.get(attribute);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static public void set(String attribute, String value) {
|
static public void set(String attribute, String value) {
|
||||||
table.put(attribute, value);
|
table.put(attribute, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static public boolean getBoolean(String attribute) {
|
static public boolean getBoolean(String attribute) {
|
||||||
String value = get(attribute);
|
return new Boolean(get(attribute));
|
||||||
return (new Boolean(value)).booleanValue();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static public void setBoolean(String attribute, boolean value) {
|
static public void setBoolean(String attribute, boolean value) {
|
||||||
set(attribute, value ? "true" : "false");
|
set(attribute, value ? "true" : "false");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static public int getInteger(String attribute) {
|
static public int getInteger(String attribute) {
|
||||||
return Integer.parseInt(get(attribute));
|
return Integer.parseInt(get(attribute));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static public void setInteger(String key, int value) {
|
static public void setInteger(String key, int value) {
|
||||||
set(key, String.valueOf(value));
|
set(key, String.valueOf(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static public Color getColor(String name) {
|
static public Color getColor(String name) {
|
||||||
Color parsed = null;
|
return parseColor(get(name));
|
||||||
String s = get(name);
|
|
||||||
if ((s != null) && (s.indexOf("#") == 0)) {
|
|
||||||
try {
|
|
||||||
int v = Integer.parseInt(s.substring(1), 16);
|
|
||||||
parsed = new Color(v);
|
|
||||||
} catch (Exception e) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return parsed;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static public void setColor(String attr, Color what) {
|
static public void setColor(String attr, Color what) {
|
||||||
set(attr, "#" + PApplet.hex(what.getRGB() & 0xffffff, 6));
|
set(attr, "#" + String.format("%06x", what.getRGB() & 0xffffff));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static public Font getFont(String attr) {
|
static public Font getFont(String attr) {
|
||||||
boolean replace = false;
|
|
||||||
String value = get(attr);
|
String value = get(attr);
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
//System.out.println("reset 1");
|
|
||||||
value = getDefault(attr);
|
value = getDefault(attr);
|
||||||
replace = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
String[] pieces = PApplet.split(value, ',');
|
|
||||||
if (pieces.length != 3) {
|
|
||||||
value = getDefault(attr);
|
|
||||||
//System.out.println("reset 2 for " + attr);
|
|
||||||
pieces = PApplet.split(value, ',');
|
|
||||||
//PApplet.println(pieces);
|
|
||||||
replace = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
String name = pieces[0];
|
|
||||||
int style = Font.PLAIN; // equals zero
|
|
||||||
if (pieces[1].indexOf("bold") != -1) {
|
|
||||||
style |= Font.BOLD;
|
|
||||||
}
|
|
||||||
if (pieces[1].indexOf("italic") != -1) {
|
|
||||||
style |= Font.ITALIC;
|
|
||||||
}
|
|
||||||
int size = PApplet.parseInt(pieces[2], 12);
|
|
||||||
Font font = new Font(name, style, size);
|
|
||||||
|
|
||||||
// replace bad font with the default
|
|
||||||
if (replace) {
|
|
||||||
//System.out.println(attr + " > " + value);
|
|
||||||
//setString(attr, font.getName() + ",plain," + font.getSize());
|
|
||||||
set(attr, value);
|
set(attr, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return font;
|
String[] split = value.split(",");
|
||||||
|
if (split.length != 3) {
|
||||||
|
value = getDefault(attr);
|
||||||
|
set(attr, value);
|
||||||
|
split = value.split(",");
|
||||||
|
}
|
||||||
|
|
||||||
|
String name = split[0];
|
||||||
|
int style = Font.PLAIN;
|
||||||
|
if (split[1].contains("bold"))
|
||||||
|
style |= Font.BOLD;
|
||||||
|
if (split[1].contains("italic"))
|
||||||
|
style |= Font.ITALIC;
|
||||||
|
int size = 12; // Default
|
||||||
|
try {
|
||||||
|
// (parseDouble handle numbers with decimals too)
|
||||||
|
size = (int) Double.parseDouble(split[2]);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
}
|
||||||
|
return new Font(name, style, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static public SyntaxStyle getStyle(String what) {
|
static public SyntaxStyle getStyle(String what) {
|
||||||
String str = get("editor." + what + ".style");
|
String split[] = get("editor." + what + ".style").split(",");
|
||||||
|
|
||||||
StringTokenizer st = new StringTokenizer(str, ",");
|
Color color = parseColor(split[0]);
|
||||||
|
|
||||||
String s = st.nextToken();
|
String style = split[1];
|
||||||
if (s.indexOf("#") == 0) s = s.substring(1);
|
boolean bold = style.contains("bold");
|
||||||
Color color = new Color(Integer.parseInt(s, 16));
|
boolean italic = style.contains("italic");
|
||||||
|
boolean underlined = style.contains("underlined");
|
||||||
s = st.nextToken();
|
|
||||||
boolean bold = (s.indexOf("bold") != -1);
|
|
||||||
boolean italic = (s.indexOf("italic") != -1);
|
|
||||||
boolean underlined = (s.indexOf("underlined") != -1);
|
|
||||||
|
|
||||||
return new SyntaxStyle(color, italic, bold, underlined);
|
return new SyntaxStyle(color, italic, bold, underlined);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Color parseColor(String v) {
|
||||||
|
try {
|
||||||
|
if (v.indexOf("#") == 0)
|
||||||
|
v = v.substring(1);
|
||||||
|
return new Color(Integer.parseInt(v, 16));
|
||||||
|
} catch (Exception e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue