Made Compiler and PdePreprocessor independent from Preferences.

Created a class PreferencesData to manage all parameters except the ones for the GUI.
Removed GUI parameters management from ParametersMap.
Created ParametersHelper class to help with GUI parameters management.
Used ParametersHelper in Themes.
This commit is contained in:
Claudio Indellicati 2014-04-06 00:29:20 +02:00 committed by Cristian Maglie
parent 54f3f538f2
commit b61f2a419f
7 changed files with 378 additions and 292 deletions

View File

@ -38,15 +38,7 @@ import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.MissingResourceException;
import java.util.StringTokenizer;
import javax.swing.Box;
import javax.swing.JButton;
@ -58,10 +50,9 @@ import javax.swing.JTextField;
import javax.swing.KeyStroke;
import processing.app.helpers.FileUtils;
import processing.app.helpers.PreferencesHelper;
import processing.app.helpers.PreferencesMap;
import processing.app.syntax.SyntaxStyle;
import processing.core.PApplet;
import processing.core.PConstants;
/**
@ -93,7 +84,7 @@ import processing.core.PConstants;
*/
public class Preferences {
static final String PREFS_FILE = "preferences.txt";
static final String PREFS_FILE = PreferencesData.PREFS_FILE;
class Language {
Language(String _name, String _originalName, String _isoCode) {
@ -238,72 +229,12 @@ public class Preferences {
Editor editor;
// data model
static PreferencesMap defaults;
static PreferencesMap prefs = new PreferencesMap();
static File preferencesFile;
static boolean doSave = true;
static protected void init(File file) {
if (file != null)
preferencesFile = file;
else
preferencesFile = Base.getSettingsFile(Preferences.PREFS_FILE);
// start by loading the defaults, in case something
// important was deleted from the user prefs
try {
prefs.load(Base.getLibStream("preferences.txt"));
} catch (IOException e) {
Base.showError(null, _("Could not read default settings.\n" +
"You'll need to reinstall Arduino."), e);
}
// set some runtime constants (not saved on preferences file)
File hardwareFolder = Base.getHardwareFolder();
prefs.put("runtime.ide.path", hardwareFolder.getParentFile().getAbsolutePath());
prefs.put("runtime.ide.version", "" + Base.REVISION);
// clone the hash table
defaults = new PreferencesMap(prefs);
if (preferencesFile.exists()) {
// load the previous preferences file
try {
prefs.load(preferencesFile);
} catch (IOException ex) {
Base.showError(_("Error reading preferences"),
I18n.format(_("Error reading the preferences file. "
+ "Please delete (or move)\n"
+ "{0} and restart Arduino."),
preferencesFile.getAbsolutePath()), ex);
}
}
// load the I18n module for internationalization
try {
I18n.init(get("editor.languages.current"));
} catch (MissingResourceException e) {
I18n.init("en");
set("editor.languages.current", "en");
}
// set some other runtime constants (not saved on preferences file)
set("runtime.os", PConstants.platformNames[PApplet.platform]);
PreferencesData.init(file);
// other things that have to be set explicitly for the defaults
setColor("run.window.bgcolor", SystemColor.control);
fixPreferences();
}
private static void fixPreferences() {
String baud = get("serial.debug_rate");
if ("14400".equals(baud) || "28800".equals(baud) || "38400".equals(baud)) {
set("serial.debug_rate", "9600");
}
PreferencesHelper.putColor(PreferencesData.prefs, "run.window.bgcolor", SystemColor.control);
}
@ -380,7 +311,7 @@ public class Preferences {
label = new JLabel(_("Editor language: "));
box.add(label);
comboLanguage = new JComboBox(languages);
String currentLanguage = Preferences.get("editor.languages.current");
String currentLanguage = PreferencesData.get("editor.languages.current");
for (Language language : languages) {
if (language.isoCode.equals(currentLanguage))
comboLanguage.setSelectedItem(language);
@ -507,11 +438,11 @@ public class Preferences {
right = Math.max(right, left + d.width);
top += d.height; // + GUI_SMALL;
label = new JLabel(preferencesFile.getAbsolutePath());
label = new JLabel(PreferencesData.preferencesFile.getAbsolutePath());
final JLabel clickable = label;
label.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
Base.openFolder(preferencesFile.getParentFile());
Base.openFolder(PreferencesData.preferencesFile.getParentFile());
}
public void mouseEntered(MouseEvent e) {
@ -630,19 +561,19 @@ public class Preferences {
*/
protected void applyFrame() {
// put each of the settings into the table
setBoolean("build.verbose", verboseCompilationBox.isSelected());
setBoolean("upload.verbose", verboseUploadBox.isSelected());
setBoolean("editor.linenumbers", displayLineNumbersBox.isSelected());
setBoolean("upload.verify", verifyUploadBox.isSelected());
setBoolean("editor.save_on_verify", saveVerifyUploadBox.isSelected());
PreferencesData.setBoolean("build.verbose", verboseCompilationBox.isSelected());
PreferencesData.setBoolean("upload.verbose", verboseUploadBox.isSelected());
PreferencesData.setBoolean("editor.linenumbers", displayLineNumbersBox.isSelected());
PreferencesData.setBoolean("upload.verify", verifyUploadBox.isSelected());
PreferencesData.setBoolean("editor.save_on_verify", saveVerifyUploadBox.isSelected());
// setBoolean("sketchbook.closing_last_window_quits",
// closingLastQuitsBox.isSelected());
//setBoolean("sketchbook.prompt", sketchPromptBox.isSelected());
//setBoolean("sketchbook.auto_clean", sketchCleanBox.isSelected());
// if the sketchbook path has changed, rebuild the menus
String oldPath = get("sketchbook.path");
String oldPath = PreferencesData.get("sketchbook.path");
String newPath = sketchbookLocationField.getText();
if (newPath.isEmpty()) {
if (Base.getPortableFolder() == null)
@ -652,12 +583,12 @@ public class Preferences {
}
if (!newPath.equals(oldPath)) {
editor.base.rebuildSketchbookMenus();
set("sketchbook.path", newPath);
PreferencesData.set("sketchbook.path", newPath);
}
setBoolean("editor.external", externalEditorBox.isSelected());
setBoolean("update.check", checkUpdatesBox.isSelected());
setBoolean("editor.save_on_verify", saveVerifyUploadBox.isSelected());
PreferencesData.setBoolean("editor.external", externalEditorBox.isSelected());
PreferencesData.setBoolean("update.check", checkUpdatesBox.isSelected());
PreferencesData.setBoolean("editor.save_on_verify", saveVerifyUploadBox.isSelected());
/*
// was gonna use this to check memory settings,
@ -674,24 +605,24 @@ public class Preferences {
String newSizeText = fontSizeField.getText();
try {
int newSize = Integer.parseInt(newSizeText.trim());
String pieces[] = PApplet.split(get("editor.font"), ',');
String pieces[] = PApplet.split(PreferencesData.get("editor.font"), ',');
pieces[2] = String.valueOf(newSize);
set("editor.font", PApplet.join(pieces, ','));
PreferencesData.set("editor.font", PApplet.join(pieces, ','));
} catch (Exception e) {
System.err.println(I18n.format(_("ignoring invalid font size {0}"), newSizeText));
}
if (autoAssociateBox != null) {
setBoolean("platform.auto_file_type_associations",
PreferencesData.setBoolean("platform.auto_file_type_associations",
autoAssociateBox.isSelected());
}
setBoolean("editor.update_extension", updateExtensionBox.isSelected());
PreferencesData.setBoolean("editor.update_extension", updateExtensionBox.isSelected());
// adds the selected language to the preferences file
Language newLanguage = (Language) comboLanguage.getSelectedItem();
set("editor.languages.current", newLanguage.isoCode);
PreferencesData.set("editor.languages.current", newLanguage.isoCode);
editor.applyPreferences();
}
@ -701,10 +632,10 @@ public class Preferences {
this.editor = editor;
// set all settings entry boxes to their actual status
verboseCompilationBox.setSelected(getBoolean("build.verbose"));
verboseUploadBox.setSelected(getBoolean("upload.verbose"));
displayLineNumbersBox.setSelected(getBoolean("editor.linenumbers"));
verifyUploadBox.setSelected(getBoolean("upload.verify"));
verboseCompilationBox.setSelected(PreferencesData.getBoolean("build.verbose"));
verboseUploadBox.setSelected(PreferencesData.getBoolean("upload.verbose"));
displayLineNumbersBox.setSelected(PreferencesData.getBoolean("editor.linenumbers"));
verifyUploadBox.setSelected(PreferencesData.getBoolean("upload.verify"));
//closingLastQuitsBox.
// setSelected(getBoolean("sketchbook.closing_last_window_quits"));
@ -714,200 +645,95 @@ public class Preferences {
// setSelected(getBoolean("sketchbook.auto_clean"));
sketchbookLocationField.
setText(get("sketchbook.path"));
setText(PreferencesData.get("sketchbook.path"));
externalEditorBox.
setSelected(getBoolean("editor.external"));
setSelected(PreferencesData.getBoolean("editor.external"));
checkUpdatesBox.
setSelected(getBoolean("update.check"));
setSelected(PreferencesData.getBoolean("update.check"));
saveVerifyUploadBox.
setSelected(getBoolean("editor.save_on_verify"));
setSelected(PreferencesData.getBoolean("editor.save_on_verify"));
if (autoAssociateBox != null) {
autoAssociateBox.
setSelected(getBoolean("platform.auto_file_type_associations"));
setSelected(PreferencesData.getBoolean("platform.auto_file_type_associations"));
}
updateExtensionBox.setSelected(get("editor.update_extension") == null ||
getBoolean("editor.update_extension"));
updateExtensionBox.setSelected(PreferencesData.get("editor.update_extension") == null ||
PreferencesData.getBoolean("editor.update_extension"));
dialog.setVisible(true);
}
// .................................................................
static public String[] loadStrings(InputStream input) {
try {
BufferedReader reader =
new BufferedReader(new InputStreamReader(input, "UTF-8"));
String lines[] = new String[100];
int lineCount = 0;
String line = null;
while ((line = reader.readLine()) != null) {
if (lineCount == lines.length) {
String temp[] = new String[lineCount << 1];
System.arraycopy(lines, 0, temp, 0, lineCount);
lines = temp;
}
lines[lineCount++] = line;
}
reader.close();
if (lineCount == lines.length) {
return lines;
}
// resize array to appropriate amount for these lines
String output[] = new String[lineCount];
System.arraycopy(lines, 0, output, 0, lineCount);
return output;
} catch (IOException e) {
e.printStackTrace();
//throw new RuntimeException("Error inside loadStrings()");
}
return null;
}
// .................................................................
static protected void save() {
if (!doSave) return;
// try {
// on startup, don't worry about it
// this is trying to update the prefs for who is open
// before Preferences.init() has been called.
if (preferencesFile == null) return;
// Fix for 0163 to properly use Unicode when writing preferences.txt
PrintWriter writer = PApplet.createWriter(preferencesFile);
String[] keys = prefs.keySet().toArray(new String[0]);
Arrays.sort(keys);
for (String key: keys) {
if (key.startsWith("runtime."))
continue;
writer.println(key + "=" + prefs.get(key));
}
writer.flush();
writer.close();
PreferencesData.save();
}
// .................................................................
static public String get(String attribute) {
return prefs.get(attribute);
return PreferencesData.get(attribute);
}
static public String get(String attribute, String defaultValue) {
String value = get(attribute);
return (value == null) ? defaultValue : value;
return PreferencesData.get(attribute, defaultValue);
}
public static boolean has(String key) {
return prefs.containsKey(key);
return PreferencesData.has(key);
}
public static void remove(String key) {
prefs.remove(key);
}
static public String getDefault(String attribute) {
return defaults.get(attribute);
PreferencesData.remove(key);
}
static public void set(String attribute, String value) {
prefs.put(attribute, value);
}
static public void unset(String attribute) {
prefs.remove(attribute);
PreferencesData.set(attribute, value);
}
static public boolean getBoolean(String attribute) {
return prefs.getBoolean(attribute);
return PreferencesData.getBoolean(attribute);
}
static public void setBoolean(String attribute, boolean value) {
prefs.putBoolean(attribute, value);
PreferencesData.setBoolean(attribute, value);
}
static public int getInteger(String attribute) {
return Integer.parseInt(get(attribute));
return PreferencesData.getInteger(attribute);
}
static public void setInteger(String key, int value) {
set(key, String.valueOf(value));
}
static public Color getColor(String name) {
Color parsed = prefs.getColor(name);
if (parsed != null)
return parsed;
return Color.GRAY; // set a default
}
static public void setColor(String attr, Color what) {
prefs.putColor(attr, what);
PreferencesData.setInteger(key, value);
}
static public Font getFont(String attr) {
Font font = prefs.getFont(attr);
Font font = PreferencesHelper.getFont(PreferencesData.prefs, attr);
if (font == null) {
String value = defaults.get(attr);
prefs.put(attr, value);
font = prefs.getFont(attr);
String value = PreferencesData.defaults.get(attr);
PreferencesData.prefs.put(attr, value);
font = PreferencesHelper.getFont(PreferencesData.prefs, attr);
}
return font;
}
static public SyntaxStyle getStyle(String what) {
String str = get("editor." + what + ".style");
StringTokenizer st = new StringTokenizer(str, ",");
String s = st.nextToken();
if (s.indexOf("#") == 0) s = s.substring(1);
Color color = Color.DARK_GRAY;
try {
color = new Color(Integer.parseInt(s, 16));
} catch (Exception e) { }
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);
}
// get a copy of the Preferences
static public PreferencesMap getMap()
{
return new PreferencesMap(prefs);
return PreferencesData.getMap();
}
// Decide wether changed preferences will be saved. When value is
// false, Preferences.save becomes a no-op.
static public void setDoSave(boolean value)
{
doSave = value;
PreferencesData.setDoSave(value);
}
}

View File

@ -0,0 +1,214 @@
package processing.app;
import static processing.app.I18n._;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.MissingResourceException;
import processing.app.helpers.PreferencesMap;
import processing.core.PApplet;
import processing.core.PConstants;
public class PreferencesData {
static final String PREFS_FILE = "preferences.txt";
// data model
static PreferencesMap defaults;
static PreferencesMap prefs = new PreferencesMap();
static File preferencesFile;
static boolean doSave = true;
static public void init(File file) {
if (file != null)
preferencesFile = file;
else
preferencesFile = Base.getSettingsFile(Preferences.PREFS_FILE);
// start by loading the defaults, in case something
// important was deleted from the user prefs
try {
prefs.load(Base.getLibStream("preferences.txt"));
} catch (IOException e) {
Base.showError(null, _("Could not read default settings.\n" +
"You'll need to reinstall Arduino."), e);
}
// set some runtime constants (not saved on preferences file)
File hardwareFolder = Base.getHardwareFolder();
prefs.put("runtime.ide.path", hardwareFolder.getParentFile().getAbsolutePath());
prefs.put("runtime.ide.version", "" + Base.REVISION);
// clone the hash table
defaults = new PreferencesMap(prefs);
if (preferencesFile.exists()) {
// load the previous preferences file
try {
prefs.load(preferencesFile);
} catch (IOException ex) {
Base.showError(_("Error reading preferences"),
I18n.format(_("Error reading the preferences file. "
+ "Please delete (or move)\n"
+ "{0} and restart Arduino."),
preferencesFile.getAbsolutePath()), ex);
}
}
// load the I18n module for internationalization
try {
I18n.init(get("editor.languages.current"));
} catch (MissingResourceException e) {
I18n.init("en");
set("editor.languages.current", "en");
}
// set some other runtime constants (not saved on preferences file)
set("runtime.os", PConstants.platformNames[PApplet.platform]);
fixPreferences();
}
private static void fixPreferences() {
String baud = get("serial.debug_rate");
if ("14400".equals(baud) || "28800".equals(baud) || "38400".equals(baud)) {
set("serial.debug_rate", "9600");
}
}
static public String[] loadStrings(InputStream input) {
try {
BufferedReader reader =
new BufferedReader(new InputStreamReader(input, "UTF-8"));
String lines[] = new String[100];
int lineCount = 0;
String line = null;
while ((line = reader.readLine()) != null) {
if (lineCount == lines.length) {
String temp[] = new String[lineCount << 1];
System.arraycopy(lines, 0, temp, 0, lineCount);
lines = temp;
}
lines[lineCount++] = line;
}
reader.close();
if (lineCount == lines.length) {
return lines;
}
// resize array to appropriate amount for these lines
String output[] = new String[lineCount];
System.arraycopy(lines, 0, output, 0, lineCount);
return output;
} catch (IOException e) {
e.printStackTrace();
//throw new RuntimeException("Error inside loadStrings()");
}
return null;
}
static protected void save() {
if (!doSave)
return;
// on startup, don't worry about it
// this is trying to update the prefs for who is open
// before Preferences.init() has been called.
if (preferencesFile == null) return;
// Fix for 0163 to properly use Unicode when writing preferences.txt
PrintWriter writer = PApplet.createWriter(preferencesFile);
String[] keys = prefs.keySet().toArray(new String[0]);
Arrays.sort(keys);
for (String key: keys) {
if (key.startsWith("runtime."))
continue;
writer.println(key + "=" + prefs.get(key));
}
writer.flush();
writer.close();
}
// .................................................................
static public String get(String attribute) {
return prefs.get(attribute);
}
static public String get(String attribute, String defaultValue) {
String value = get(attribute);
return (value == null) ? defaultValue : value;
}
public static boolean has(String key) {
return prefs.containsKey(key);
}
public static void remove(String key) {
prefs.remove(key);
}
static public String getDefault(String attribute) {
return defaults.get(attribute);
}
static public void set(String attribute, String value) {
prefs.put(attribute, value);
}
static public void unset(String attribute) {
prefs.remove(attribute);
}
static public boolean getBoolean(String attribute) {
return prefs.getBoolean(attribute);
}
static public void setBoolean(String attribute, boolean value) {
prefs.putBoolean(attribute, value);
}
static public int getInteger(String attribute) {
return Integer.parseInt(get(attribute));
}
static public void setInteger(String key, int value) {
set(key, String.valueOf(value));
}
// get a copy of the Preferences
static public PreferencesMap getMap()
{
return new PreferencesMap(prefs);
}
// Decide wether changed preferences will be saved. When value is
// false, Preferences.save becomes a no-op.
static public void setDoSave(boolean value)
{
doSave = value;
}
}

View File

@ -27,6 +27,7 @@ import java.awt.Color;
import java.awt.Font;
import java.awt.SystemColor;
import processing.app.helpers.PreferencesHelper;
import processing.app.helpers.PreferencesMap;
import processing.app.syntax.SyntaxStyle;
@ -86,19 +87,19 @@ public class Theme {
}
static public Color getColor(String name) {
return PreferencesMap.parseColor(get(name));
return PreferencesHelper.parseColor(get(name));
}
static public void setColor(String attr, Color color) {
table.putColor(attr, color);
PreferencesHelper.putColor(table, attr, color);
}
static public Font getFont(String attr) {
Font font = table.getFont(attr);
Font font = PreferencesHelper.getFont(table, attr);
if (font == null) {
String value = getDefault(attr);
set(attr, value);
font = table.getFont(attr);
font = PreferencesHelper.getFont(table, attr);
}
return font;
}
@ -106,7 +107,7 @@ public class Theme {
static public SyntaxStyle getStyle(String what) {
String split[] = get("editor." + what + ".style").split(",");
Color color = PreferencesMap.parseColor(split[0]);
Color color = PreferencesHelper.parseColor(split[0]);
String style = split[1];
boolean bold = style.contains("bold");

View File

@ -38,7 +38,7 @@ import java.util.Map;
import processing.app.Base;
import processing.app.I18n;
import processing.app.Preferences;
import processing.app.PreferencesData;
import processing.app.SketchCode;
import processing.app.SketchData;
import processing.app.helpers.FileUtils;
@ -49,6 +49,7 @@ import processing.app.helpers.filefilters.OnlyDirs;
import processing.app.packages.Library;
import processing.app.packages.LibraryList;
import processing.app.preproc.PdePreprocessor;
import processing.core.PApplet;
public class Compiler implements MessageConsumer {
@ -104,7 +105,7 @@ public class Compiler implements MessageConsumer {
public boolean compile(boolean _verbose) throws RunnerException {
preprocess(prefs.get("build.path"));
verbose = _verbose || Preferences.getBoolean("build.verbose");
verbose = _verbose || PreferencesData.getBoolean("build.verbose");
sketchIsCompiled = false;
objectFiles = new ArrayList<File>();
@ -203,7 +204,7 @@ public class Compiler implements MessageConsumer {
// Merge all the global preference configuration in order of priority
PreferencesMap p = new PreferencesMap();
p.putAll(Preferences.getMap());
p.putAll(PreferencesData.getMap());
if (corePlatform != null)
p.putAll(corePlatform.getPreferences());
p.putAll(targetPlatform.getPreferences());

View File

@ -0,0 +1,103 @@
package processing.app.helpers;
import java.awt.Color;
import java.awt.Font;
public abstract class PreferencesHelper {
// /**
// * Create a Color with the value of the specified key. The format of the color
// * should be an hexadecimal number of 6 digit, eventually prefixed with a '#'.
// *
// * @param name
// * @return A Color object or <b>null</b> if the key is not found or the format
// * is wrong
// */
// static public Color getColor(PreferencesMap prefs, String name) {
// Color parsed = parseColor(prefs.get(name));
// if (parsed != null)
// return parsed;
// return Color.GRAY; // set a default
// }
//
//
// static public void setColor(PreferencesMap prefs, String attr, Color what) {
// putColor(prefs, attr, what);
// }
//
//
// static public Font getFontWithDefault(PreferencesMap prefs, PreferencesMap defaults, String attr) {
// Font font = getFont(prefs, attr);
// if (font == null) {
// String value = defaults.get(attr);
// prefs.put(attr, value);
// font = getFont(prefs, attr);
// }
// return font;
// }
//
// static public SyntaxStyle getStyle(PreferencesMap prefs, String what) {
// String str = prefs.get("editor." + what + ".style");
//
// StringTokenizer st = new StringTokenizer(str, ",");
//
// String s = st.nextToken();
// if (s.indexOf("#") == 0) s = s.substring(1);
// Color color = Color.DARK_GRAY;
// try {
// color = new Color(Integer.parseInt(s, 16));
// } catch (Exception e) { }
//
// 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);
// }
/**
* Set the value of the specified key based on the Color passed as parameter.
*
* @param attr
* @param color
*/
public static void putColor(PreferencesMap prefs, String attr, Color color) {
prefs.put(attr, "#" + String.format("%06x", color.getRGB() & 0xffffff));
}
public 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;
}
}
public static Font getFont(PreferencesMap prefs, String key) {
String value = prefs.get(key);
if (value == null)
return null;
String[] split = value.split(",");
if (split.length != 3)
return null;
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;
try {
// ParseDouble handle numbers with decimals too
size = (int) Double.parseDouble(split[2]);
} catch (NumberFormatException e) {
// for wrong formatted size pick the default
size = 12;
}
return new Font(name, style, size);
}
}

View File

@ -21,8 +21,6 @@
*/
package processing.app.helpers;
import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@ -324,61 +322,4 @@ public class PreferencesMap extends LinkedHashMap<String, String> {
return new Boolean(prev);
}
/**
* Create a Color with the value of the specified key. The format of the color
* should be an hexadecimal number of 6 digit, eventually prefixed with a '#'.
*
* @param name
* @return A Color object or <b>null</b> if the key is not found or the format
* is wrong
*/
public Color getColor(String name) {
return parseColor(get(name));
}
/**
* Set the value of the specified key based on the Color passed as parameter.
*
* @param attr
* @param color
*/
public void putColor(String attr, Color color) {
put(attr, "#" + String.format("%06x", color.getRGB() & 0xffffff));
}
public 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;
}
}
public Font getFont(String key) {
String value = get(key);
if (value == null)
return null;
String[] split = value.split(",");
if (split.length != 3)
return null;
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;
try {
// ParseDouble handle numbers with decimals too
size = (int) Double.parseDouble(split[2]);
} catch (NumberFormatException e) {
// for wrong formatted size pick the default
size = 12;
}
return new Font(name, style, size);
}
}

View File

@ -90,7 +90,7 @@ public class PdePreprocessor {
program = scrubComments(program);
// If there are errors, an exception is thrown and this fxn exits.
if (Preferences.getBoolean("preproc.substitute_unicode")) {
if (PreferencesData.getBoolean("preproc.substitute_unicode")) {
program = substituteUnicode(program);
}