001: /*
002: * @(#)Preferences.java 1.0 September 17, 2005
003: *
004: * Copyright (c) 2005 Werner Randelshofer
005: * Staldenmattweg 2, Immensee, CH-6405, Switzerland.
006: * All rights reserved.
007: *
008: * This software is the confidential and proprietary information of
009: * Werner Randelshofer. ("Confidential Information"). You shall not
010: * disclose such Confidential Information and shall use it only in
011: * accordance with the terms of the license agreement you entered into
012: * with Werner Randelshofer.
013: */
014:
015: package com.jidesoft.plaf.aqua;
016:
017: import com.jidesoft.utils.SecurityUtils;
018:
019: import java.io.File;
020: import java.io.FileReader;
021: import java.io.IOException;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.logging.Logger;
025:
026: /**
027: * Utility class for accessing Mac OS X System Preferences.
028: *
029: * @author Werner Randelshofer
030: * @version 1.0 September 17, 2005 Created.
031: */
032: class AquaPreferences {
033: private static final Logger LOGGER = Logger
034: .getLogger(AquaPreferences.class.getName());
035:
036: private static HashMap prefs;
037:
038: /**
039: * Creates a new instance.
040: */
041: public AquaPreferences() {
042: }
043:
044: public static String getString(String key) {
045: return (String) get(key);
046: }
047:
048: public static Object get(String key) {
049: if (prefs == null) {
050: prefs = new HashMap();
051: loadGlobalPreferences();
052: }
053: //System.out.println("Preferences.get("+key+"):"+prefs.get(key));
054: return prefs.get(key);
055: }
056:
057: private static void loadGlobalPreferences() {
058: // Load Mac OS X global preferences
059: // --------------------------------
060:
061: // Fill preferences with default values, in case we fail to read them
062:
063: // Appearance: "1"=Blue, "6"=Graphite
064: prefs.put("AppleAquaColorVariant", "1");
065: // Highlight Color: (RGB float values)
066: prefs.put("AppleHighlightColor", "0.709800 0.835300 1.000000");
067: // Collation order: (Language code)
068: prefs.put("AppleCollationOrder", "en");
069: // Place scroll arrows: "Single"=At top and bottom, "DoubleMax"=Together
070: prefs.put("AppleScrollBarVariant", "DoubleMax");
071: // Click in the scroll bar to: "true"=Jump to here, "false"=Jump to next page
072: prefs.put("AppleScrollerPagingBehavior", "false");
073:
074: File globalPrefsFile = new File(SecurityUtils.getProperty(
075: "user.home", "")
076: + "/Library/Preferences/.GlobalPreferences.plist");
077: try {
078: XMLElement xml = readPList(globalPrefsFile);
079: for (Iterator i0 = xml.iterateChildren(); i0.hasNext();) {
080: XMLElement xml1 = (XMLElement) i0.next();
081:
082: String key = null;
083: for (Iterator i1 = xml1.iterateChildren(); i1.hasNext();) {
084: XMLElement xml2 = (XMLElement) i1.next();
085: if (xml2.getName().equals("key")) {
086: key = xml2.getContent();
087: } else {
088: if (key != null) {
089: //System.out.println("Preferences "+key+"="+xml2.getContent());
090: prefs.put(key, xml2.getContent());
091: }
092: key = null;
093: }
094: }
095: }
096: } catch (IOException e) {
097: LOGGER
098: .warning("AquaPreferences failed to load Mac OS X global system preferences - "
099: + e.getLocalizedMessage());
100: } catch (Exception e) {
101: LOGGER
102: .warning("AquaPreferences failed to load Mac OS X global system preferences - "
103: + e.getLocalizedMessage());
104: }
105: }
106:
107: /**
108: * Reads the specified PList file and returns it as an XMLElement.
109: * This method can deal with XML encoded and binary encoded PList files.
110: */
111: private static XMLElement readPList(File plistFile)
112: throws IOException {
113: FileReader reader = null;
114: XMLElement xml = null;
115: try {
116: reader = new FileReader(plistFile);
117: xml = new XMLElement(new HashMap(), false, false);
118: try {
119: xml.parseFromReader(reader);
120: } catch (XMLParseException e) {
121: xml = new BinaryPListParser().parse(plistFile);
122: }
123: } finally {
124: if (reader != null) {
125: reader.close();
126: }
127: }
128: return xml;
129: }
130: }
|