| 1 | package processing.app.helpers; |
| 2 | |
| 3 | import java.awt.Color; |
| 4 | import java.awt.Font; |
| 5 | |
| 6 | public abstract class PreferencesHelper { |
| 7 | |
| 8 | // /** |
| 9 | // * Create a Color with the value of the specified key. The format of the color |
| 10 | // * should be an hexadecimal number of 6 digit, eventually prefixed with a '#'. |
| 11 | // * |
| 12 | // * @param name |
| 13 | // * @return A Color object or <b>null</b> if the key is not found or the format |
| 14 | // * is wrong |
| 15 | // */ |
| 16 | // static public Color getColor(PreferencesMap prefs, String name) { |
| 17 | // Color parsed = parseColor(prefs.get(name)); |
| 18 | // if (parsed != null) |
| 19 | // return parsed; |
| 20 | // return Color.GRAY; // set a default |
| 21 | // } |
| 22 | // |
| 23 | // |
| 24 | // static public void setColor(PreferencesMap prefs, String attr, Color what) { |
| 25 | // putColor(prefs, attr, what); |
| 26 | // } |
| 27 | // |
| 28 | // |
| 29 | // static public Font getFontWithDefault(PreferencesMap prefs, PreferencesMap defaults, String attr) { |
| 30 | // Font font = getFont(prefs, attr); |
| 31 | // if (font == null) { |
| 32 | // String value = defaults.get(attr); |
| 33 | // prefs.put(attr, value); |
| 34 | // font = getFont(prefs, attr); |
| 35 | // } |
| 36 | // return font; |
| 37 | // } |
| 38 | // |
| 39 | // static public SyntaxStyle getStyle(PreferencesMap prefs, String what) { |
| 40 | // String str = prefs.get("editor." + what + ".style"); |
| 41 | // |
| 42 | // StringTokenizer st = new StringTokenizer(str, ","); |
| 43 | // |
| 44 | // String s = st.nextToken(); |
| 45 | // if (s.indexOf("#") == 0) s = s.substring(1); |
| 46 | // Color color = Color.DARK_GRAY; |
| 47 | // try { |
| 48 | // color = new Color(Integer.parseInt(s, 16)); |
| 49 | // } catch (Exception e) { } |
| 50 | // |
| 51 | // s = st.nextToken(); |
| 52 | // boolean bold = (s.indexOf("bold") != -1); |
| 53 | // boolean italic = (s.indexOf("italic") != -1); |
| 54 | // boolean underlined = (s.indexOf("underlined") != -1); |
| 55 | // |
| 56 | // return new SyntaxStyle(color, italic, bold, underlined); |
| 57 | // } |
| 58 | |
| 59 | /** |
| 60 | * Set the value of the specified key based on the Color passed as parameter. |
| 61 | * |
| 62 | * @param attr |
| 63 | * @param color |
| 64 | */ |
| 65 | public static void putColor(PreferencesMap prefs, String attr, Color color) { |
| 66 | prefs.put(attr, "#" + String.format("%06x", color.getRGB() & 0xffffff)); |
| 67 | } |
| 68 | |
| 69 | public static Color parseColor(String v) { |
| 70 | try { |
| 71 | if (v.indexOf("#") == 0) |
| 72 | v = v.substring(1); |
| 73 | return new Color(Integer.parseInt(v, 16)); |
| 74 | } catch (Exception e) { |
| 75 | return null; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | public static Font getFont(PreferencesMap prefs, String key) { |
| 80 | String value = prefs.get(key); |
| 81 | if (value == null) |
| 82 | return null; |
| 83 | String[] split = value.split(","); |
| 84 | if (split.length != 3) |
| 85 | return null; |
| 86 | |
| 87 | String name = split[0]; |
| 88 | int style = Font.PLAIN; |
| 89 | if (split[1].contains("bold")) |
| 90 | style |= Font.BOLD; |
| 91 | if (split[1].contains("italic")) |
| 92 | style |= Font.ITALIC; |
| 93 | int size; |
| 94 | try { |
| 95 | // ParseDouble handle numbers with decimals too |
| 96 | size = (int) Double.parseDouble(split[2]); |
| 97 | if (size < 1) // Do not allow negative or zero size |
| 98 | throw new NumberFormatException(); |
| 99 | } catch (NumberFormatException e) { |
| 100 | // for wrong formatted size pick the default |
| 101 | size = 12; |
| 102 | } |
| 103 | return new Font(name, style, size); |
| 104 | } |
| 105 | } |