/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* Part of the Processing project - http://processing.org Copyright (c) 2004-06 Ben Fry and Casey Reas Copyright (c) 2001-04 Massachusetts Institute of Technology This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package processing.app; import java.awt.*; import java.io.*; import java.util.*; import processing.app.syntax.*; import processing.core.*; /** * Storage class for theme settings. This was separated from the Preferences * class for 1.0 so that the coloring wouldn't conflict with previous releases * and to make way for future ability to customize. */ public class Theme { /** Copy of the defaults in case the user mangles a preference. */ static HashMap<String,String> defaults; /** Table of attributes/values for the theme. */ static HashMap<String,String> table = new HashMap<String,String>();; static protected void init() { try { load(Base.getLibStream("theme/theme.txt")); } catch (Exception te) { Base.showError(null, "Could not read color theme settings.\n" + "You'll need to reinstall Wiring.", 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 = new HashMap<String, String>(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 table.get(attribute); } static public String getDefault(String 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(); } 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 parsed; } static public void setColor(String attr, Color what) { set(attr, "#" + PApplet.hex(what.getRGB() & 0xffffff, 6)); } static public Font getFont(String attr) { boolean replace = false; String value = get(attr); if (value == null) { value = getDefault(attr); replace = true; } String[] pieces = PApplet.split(value, ','); if (pieces.length != 3) { value = getDefault(attr); pieces = PApplet.split(value, ','); 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) { set(attr, value); } 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 = 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); return new SyntaxStyle(color, italic, bold, underlined); } }