File: AquaPreferences.java

package info (click to toggle)
libjide-oss-java 3.6.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 12,888 kB
  • ctags: 11,533
  • sloc: java: 88,675; xml: 259; makefile: 33
file content (143 lines) | stat: -rw-r--r-- 4,965 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * @(#)Preferences.java  1.0  September 17, 2005
 *
 * Copyright (c) 2005 Werner Randelshofer
 * Staldenmattweg 2, Immensee, CH-6405, Switzerland.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Werner Randelshofer. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Werner Randelshofer.
 */

package com.jidesoft.plaf.aqua;

import com.jidesoft.plaf.UIDefaultsLookup;
import com.jidesoft.utils.SecurityUtils;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.logging.Logger;

/**
 * Utility class for accessing Mac OS X System Preferences.
 *
 * @author Werner Randelshofer
 * @version 1.0 September 17, 2005 Created.
 */
class AquaPreferences {
    private static final Logger LOGGER = Logger.getLogger(AquaPreferences.class.getName());
    private static HashMap prefs;

    /**
     * Creates a new instance.
     */
    public AquaPreferences() {
    }

    /**
     * Gets the preference value from Mac OS X's global preference. We added a way to override the setting. As long as
     * you add an entry to UIDefaults with "AquaPreference." prefix and the key, we will use the entry you added instead
     * of getting it from the system.
     *
     * @param key the key
     * @return the value.
     */
    public static String getString(String key) {
        String string = UIDefaultsLookup.getString("AquaPreference." + key);
        if (string != null) {
            return string;
        }
        return (String) get(key);
    }

    public static Object get(String key) {
        if (prefs == null) {
            prefs = new HashMap();
            loadGlobalPreferences();
        }
        //System.out.println("Preferences.get("+key+"):"+prefs.get(key));
        return prefs.get(key);
    }

    private static void loadGlobalPreferences() {
        // Load Mac OS X global preferences
        // --------------------------------

        // Fill preferences with default values, in case we fail to read them

        // Appearance: "1"=Blue, "6"=Graphite
        prefs.put("AppleAquaColorVariant", "1");
        // Highlight Color: (RGB float values)
        prefs.put("AppleHighlightColor", "0.709800 0.835300 1.000000");
        // Collation order: (Language code)
        prefs.put("AppleCollationOrder", "en");
        // Place scroll arrows: "Single"=At top and bottom, "DoubleMax"=Together
        prefs.put("AppleScrollBarVariant", "DoubleMax");
        // Click in the scroll bar to: "true"=Jump to here, "false"=Jump to next page
        prefs.put("AppleScrollerPagingBehavior", "false");

        File globalPrefsFile = new File(
                SecurityUtils.getProperty("user.home", "")
                        + "/Library/Preferences/.GlobalPreferences.plist"
        );
        try {
            XMLElement xml = readPList(globalPrefsFile);
            for (Iterator i0 = xml.iterateChildren(); i0.hasNext(); ) {
                XMLElement xml1 = (XMLElement) i0.next();

                String key = null;
                for (Iterator i1 = xml1.iterateChildren(); i1.hasNext(); ) {
                    XMLElement xml2 = (XMLElement) i1.next();
                    if (xml2.getName().equals("key")) {
                        key = xml2.getContent();
                    }
                    else {
                        if (key != null) {
                            //System.out.println("Preferences "+key+"="+xml2.getContent());
                            prefs.put(key, xml2.getContent());
                        }
                        key = null;
                    }
                }
            }
        }
        catch (IOException e) {
            LOGGER.warning("AquaPreferences failed to load Mac OS X global system preferences - " + e.getLocalizedMessage());
        }
        catch (Exception e) {
            LOGGER.warning("AquaPreferences failed to load Mac OS X global system preferences - " + e.getLocalizedMessage());
        }
    }

    /**
     * Reads the specified PList file and returns it as an XMLElement. This method can deal with XML encoded and binary
     * encoded PList files.
     */
    private static XMLElement readPList(File plistFile) throws IOException {
        FileReader reader = null;
        XMLElement xml = null;
        try {
            reader = new FileReader(plistFile);
            xml = new XMLElement(new HashMap(), false, false);
            try {
                xml.parseFromReader(reader);
            }
            catch (XMLParseException e) {
                xml = new BinaryPListParser().parse(plistFile);
            }
        }
        finally {
            if (reader != null) {
                reader.close();
            }
        }
        return xml;
    }
}