KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > NbPreferences


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.util;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.PrintStream JavaDoc;
25 import java.util.logging.Level JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27 import java.util.prefs.Preferences JavaDoc;
28 import org.netbeans.modules.openide.util.PreferencesProvider;
29
30 /**
31  * Provides an implementation of the Preferences API which may be backed by
32  * a NetBeans-specific implementation.
33  * @see <a HREF="doc-files/preferences.html">Preferences API in NetBeans</a>
34  * @since org.openide.util 7.4
35  * @author Radek Matous
36  */

37 public final class NbPreferences {
38     private static PreferencesProvider PREFS_IMPL;
39     
40     private NbPreferences() {}
41     
42     /**
43      * Returns user preference node . {@link Preferences#absolutePath} of such
44      * a node depends whether class provided as a parameter was loaded as a part of any module
45      * or not. If so, then absolute path corresponds to slashified code name base of module.
46      * If not, then absolute path corresponds to class's package.
47      *
48      * @param cls the class for which a user preference node is desired.
49      * @return the user preference node
50      */

51     public static Preferences JavaDoc forModule(Class JavaDoc cls) {
52           if (PREFS_IMPL == null) {
53                 PREFS_IMPL = getPreferencesProvider();
54           }
55           return PREFS_IMPL.preferencesForModule(cls);
56     }
57     
58     /**
59      * Returns the root preference node.
60      *
61      * @return the root preference node.
62      */

63     public static Preferences JavaDoc root() {
64           if (PREFS_IMPL == null) {
65                 PREFS_IMPL = getPreferencesProvider();
66           }
67           return PREFS_IMPL.preferencesRoot();
68     }
69          
70     private static PreferencesProvider getPreferencesProvider() {
71         PreferencesProvider retval = Lookup.getDefault().lookup(PreferencesProvider.class);
72         if (retval == null) {
73              retval = new PreferencesProvider() {
74                   public Preferences JavaDoc preferencesForModule(Class JavaDoc cls) {
75                        return Preferences.userNodeForPackage(cls);
76                   }
77
78                   public Preferences JavaDoc preferencesRoot() {
79                        return Preferences.userRoot();
80                   }
81              };
82              // Avoided warning in case it is set
83
//(e.g. from NbTestCase - org.netbeans.junit.internal.MemoryPreferencesFactory).
84
String JavaDoc prefsFactory = System.getProperty("java.util.prefs.PreferencesFactory");//NOI18N
85
if (!"org.netbeans.junit.internal.MemoryPreferencesFactory".equals(prefsFactory)) {//NOI18N
86
Logger JavaDoc logger = Logger.getLogger(NbPreferences.class.getName());
87                  ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
88                  new Exception JavaDoc().printStackTrace(new PrintStream JavaDoc(bos));
89                  logger.log(prefsFactory == null ? Level.WARNING : Level.FINE,
90                          "NetBeans implementation of Preferences not found: " + bos.toString() );//NOI18N
91
}
92         }
93         return retval;
94     }
95 }
96
Popular Tags