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
|
||||
|
||||
|
@ -23,14 +21,14 @@
|
|||
|
||||
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 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
|
||||
|
@ -40,165 +38,110 @@ import static processing.app.I18n._;
|
|||
public class Theme {
|
||||
|
||||
/** 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. */
|
||||
static HashMap<String,String> table = new HashMap<String,String>();;
|
||||
|
||||
static PreferencesMap table = new PreferencesMap();
|
||||
|
||||
static protected void init() {
|
||||
try {
|
||||
load(Base.getLibStream("theme/theme.txt"));
|
||||
table.load(Base.getLibStream("theme/theme.txt"));
|
||||
} catch (Exception te) {
|
||||
Base.showError(null, _("Could not read color theme settings.\n" +
|
||||
"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
|
||||
setColor("run.window.bgcolor", SystemColor.control);
|
||||
|
||||
// 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) {
|
||||
return (String) table.get(attribute);
|
||||
return table.get(attribute);
|
||||
}
|
||||
|
||||
|
||||
static public String getDefault(String attribute) {
|
||||
return (String) defaults.get(attribute);
|
||||
return defaults.get(attribute);
|
||||
}
|
||||
|
||||
|
||||
static public void set(String attribute, String value) {
|
||||
table.put(attribute, value);
|
||||
}
|
||||
|
||||
|
||||
static public boolean getBoolean(String attribute) {
|
||||
String value = get(attribute);
|
||||
return (new Boolean(value)).booleanValue();
|
||||
return new Boolean(get(attribute));
|
||||
}
|
||||
|
||||
|
||||
static public void setBoolean(String attribute, boolean value) {
|
||||
set(attribute, value ? "true" : "false");
|
||||
}
|
||||
|
||||
|
||||
static public int getInteger(String attribute) {
|
||||
return Integer.parseInt(get(attribute));
|
||||
}
|
||||
|
||||
|
||||
static public void setInteger(String key, int value) {
|
||||
set(key, String.valueOf(value));
|
||||
}
|
||||
|
||||
|
||||
static public Color getColor(String name) {
|
||||
Color parsed = null;
|
||||
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 parseColor(get(name));
|
||||
}
|
||||
}
|
||||
return parsed;
|
||||
}
|
||||
|
||||
|
||||
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) {
|
||||
boolean replace = false;
|
||||
String value = get(attr);
|
||||
if (value == null) {
|
||||
//System.out.println("reset 1");
|
||||
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);
|
||||
}
|
||||
|
||||
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) {
|
||||
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();
|
||||
if (s.indexOf("#") == 0) s = s.substring(1);
|
||||
Color color = new Color(Integer.parseInt(s, 16));
|
||||
|
||||
s = st.nextToken();
|
||||
boolean bold = (s.indexOf("bold") != -1);
|
||||
boolean italic = (s.indexOf("italic") != -1);
|
||||
boolean underlined = (s.indexOf("underlined") != -1);
|
||||
String style = split[1];
|
||||
boolean bold = style.contains("bold");
|
||||
boolean italic = style.contains("italic");
|
||||
boolean underlined = style.contains("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