Example usage for java.util.prefs Preferences userRoot

List of usage examples for java.util.prefs Preferences userRoot

Introduction

In this page you can find the example usage for java.util.prefs Preferences userRoot.

Prototype

public static Preferences userRoot() 

Source Link

Document

Returns the root preference node for the calling user.

Usage

From source file:PreferenceExample.java

public static void main(String args[]) throws Exception {
    Preferences prefsRoot = Preferences.userRoot();
    Preferences myPrefs = prefsRoot.node("PreferenceExample");

    myPrefs.put("A", "a");
    myPrefs.put("B", "b");
    myPrefs.put("C", "c");

    System.out.print("Node's children: ");
    for (String s : myPrefs.childrenNames()) {
        System.out.print(s + "");
    }/*from  w  w  w. j  a  va  2 s  . c om*/

}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    // Get the system root
    Preferences prefs = Preferences.systemRoot();

    // Get the user root
    prefs = Preferences.userRoot();

    // The name of a root is ""
    String name = prefs.name();/* w w  w  .  j  a  va 2  s . com*/

    // The parent of a root is null
    Preferences parent = prefs.parent();

    // The absolute path of a root is "/"
    String path = prefs.absolutePath();
}

From source file:Main.java

public static void main(String[] args) {
    String PREF_KEY = "org.username";
    // Write Preferences information to HKCU (HKEY_CURRENT_USER),HKCU\Software\JavaSoft\Prefs\
    Preferences userPref = Preferences.userRoot();
    userPref.put(PREF_KEY, "a");

    System.out.println("Preferences = " + userPref.get(PREF_KEY, PREF_KEY + " was not found."));

    // Write Preferences information to HKLM (HKEY_LOCAL_MACHINE),HKLM\Software\JavaSoft\Prefs\
    Preferences systemPref = Preferences.systemRoot();
    systemPref.put(PREF_KEY, "b");

    System.out.println("Preferences = " + systemPref.get(PREF_KEY, PREF_KEY + " was not found."));
}

From source file:PlanetPrefs.java

public static void main(String args[]) {
    String names[] = { "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto" };
    int moons[] = { 0, 0, 1, 2, 16, 18, 21, 8, 1 };

    Preferences prefs = Preferences.userRoot().node("/MasteringJava/Chap17");

    for (int i = 0, n = names.length; i < n; i++) {
        prefs.putInt(names[i], moons[i]);
    }// ww w. j a va  2 s.  c om

    try {
        String keys[] = prefs.keys();
        for (int i = 0, n = keys.length; i < n; i++) {
            System.out.println(keys[i] + ": " + prefs.getInt(keys[i], 0));
        }
    } catch (BackingStoreException e) {
        System.err.println("Unable to read backing store: " + e);
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    // System preference nodes

    // Use a Class
    Preferences prefs = Preferences.systemNodeForPackage(java.lang.String.class);

    // Use an absolute path
    prefs = Preferences.systemRoot().node("/java/lang/String");

    // Use a relative path
    prefs = Preferences.systemRoot().node("/javax/swing");
    prefs = prefs.node("text/html");

    // User preference nodes

    // Use a class
    prefs = Preferences.userNodeForPackage(Main.class);

    // Use an absolute path
    prefs = Preferences.userRoot().node("/com/mycompany");

    // Use a relative path
    prefs = Preferences.userRoot().node("/javax/swing");
    prefs = prefs.node("text/html");
}

From source file:PreferenceExample.java

public static void main(String args[]) {
    PreferenceExample pe = new PreferenceExample();
    Preferences prefsRoot = Preferences.userRoot();
    Preferences myPrefs = prefsRoot.node("PreferenceExample");

    try {//from   w ww .j  a v  a 2 s . c  om
        pe.setSomeProperties(myPrefs);
        pe.printInformation(myPrefs);
        pe.exportToFile(myPrefs, "prefs.xml");
    } catch (BackingStoreException bse) {
        System.out.println("Problem with accessing the backing store\n" + bse);
        bse.printStackTrace();
    }
}

From source file:org.streamsource.streamflow.statistic.web.Dao.java

private static void initializeDataSource() {
    Preferences preference = Preferences.userRoot()
            .node("/streamsource/streamflow/StreamflowServer/streamflowds/properties");
    String dbUrl = preference.get("url", "n/a");
    String dbUser = preference.get("username", "n/a");
    String dbPwd = preference.get("password", "n/a");
    String dbDriver = preference.get("driver", "n/a");

    dbVendor = preference.get("dbVendor", "mysql");

    ds = new BasicDataSource();
    ds.setDriverClassName(dbDriver);//  w w w .  ja va  2s.  c o m
    ds.setUrl(dbUrl);
    ds.setUsername(dbUser);
    ds.setPassword(dbPwd);

    bDataSourceInitialized = true;
}

From source file:de.maklerpoint.office.Tags.Tags.java

/**
 * Entfernt den Tag und ldt die Liste neu
 * @param tagName /*from  ww  w . j  a  va2  s . com*/
 */
public static void removeTag(String tagName) {
    Preferences prefs = Preferences.userRoot().node(Tags.class.getName());
    String taglist = prefs.get("tagListe", "Standard,ffentlich,Wichtig,Dienstlich,Persnlich");

    String[] result = taglist.split(",");

    if (result == null) {
        return;
    }

    String[] clean = (String[]) ArrayUtils.removeElement(result, tagName);
    prefs.put("tagListe", ArrayStringTools.arrayToString(clean, ","));

    InitializeTags.loadTags();
}

From source file:de.maklerpoint.office.Tags.Tags.java

/**
 * //from   w  ww .  j a  va  2  s .c  o  m
 * @param tag 
 */
public static void addTag(TagObj tag) {
    Preferences prefs = Preferences.userRoot().node(Tags.class.getName());
    String taglist = prefs.get("tagListe", "Standard,ffentlich,Wichtig,Dienstlich,Persnlich");

    String[] result = taglist.split(",");

    if (result == null) {
        return;
    }

    String[] clean = (String[]) ArrayUtils.add(result, tag.getName());
    prefs.put("tagListe", ArrayStringTools.arrayToString(clean, ","));

    prefs.put("tag" + tag.getName(), tag.getTagColor() + "," + tag.getFontColor());

    InitializeTags.loadTags();
}

From source file:com.mindquarry.desktop.util.AutostartUtilities.java

public static void setAutostart(boolean autostart, Set<String> targetPatterns) {
    // check if we are on a Windows platform, otherwise skip processing
    boolean windows = SVNFileUtil.isWindows;
    if (!windows) {
        log.debug("not setting autostart, os is not windows: " + System.getProperty("os.name"));
        return;/*from   ww  w  . j a  v  a  2s.c  om*/
    }
    log.debug("setting autostart, os: " + System.getProperty("os.name"));
    // registry variables

    // create registry method objects
    final Preferences userRoot = Preferences.userRoot();
    final Class<? extends Preferences> clz = userRoot.getClass();

    try {
        // define methods for registry manipulation
        final Method mOpenKey = clz.getDeclaredMethod("openKey", //$NON-NLS-1$
                new Class[] { byte[].class, int.class, int.class });
        mOpenKey.setAccessible(true);

        final Method mCloseKey = clz.getDeclaredMethod("closeKey", //$NON-NLS-1$
                new Class[] { int.class });
        mCloseKey.setAccessible(true);

        final Method mWinRegSetValue = clz.getDeclaredMethod("WindowsRegSetValueEx", //$NON-NLS-1$
                new Class[] { int.class, byte[].class, byte[].class });
        mWinRegSetValue.setAccessible(true);

        final Method mWinRegDeleteValue = clz.getDeclaredMethod("WindowsRegDeleteValue", //$NON-NLS-1$
                new Class[] { int.class, byte[].class });
        mWinRegDeleteValue.setAccessible(true);

        // open registry key
        Integer hSettings = (Integer) mOpenKey.invoke(userRoot, new Object[] { toByteArray(AUTOSTART_KEY),
                new Integer(KEY_ALL_ACCESS), new Integer(KEY_ALL_ACCESS) });

        // check autostart settings
        if (autostart) {
            // find classpath entry for desktop client JAR:
            String path = JarUtilities.getJar(targetPatterns);
            // write autostart value
            mWinRegSetValue.invoke(userRoot,
                    new Object[] { hSettings, toByteArray(AUTOSTART_ENTRY), toByteArray(path) });
        } else {
            // delete autostart entry
            mWinRegDeleteValue.invoke(userRoot, new Object[] { hSettings, toByteArray(AUTOSTART_ENTRY) });
        }
        // close registry key
        mCloseKey.invoke(Preferences.userRoot(), new Object[] { hSettings });
    } catch (Exception e) {
        // TODO: throw exception and catch it outside to display an error dialog
        log.error("Error while writing registry entries.", e); //$NON-NLS-1$
    }
}