Example usage for java.util.prefs Preferences node

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

Introduction

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

Prototype

public abstract Preferences node(String pathName);

Source Link

Document

Returns the named preference node in the same tree as this node, creating it and any of its ancestors if they do not already exist.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Preferences prefs = Preferences.userNodeForPackage(String.class);

    prefs.addNodeChangeListener(new NodeChangeListener() {
        public void childAdded(NodeChangeEvent evt) {
            Preferences parent = evt.getParent();
            Preferences child = evt.getChild();
        }/*from   w w  w . jav a2s  . co  m*/

        public void childRemoved(NodeChangeEvent evt) {
            Preferences parent = evt.getParent();
            Preferences child = evt.getChild();
        }
    });
    Preferences child = prefs.node("new node");
    child.removeNode();
    prefs.removeNode();
}

From source file:PrefsUtil.java

/**
 * Gets a Map from the preferences./*  w  ww  . jav  a 2  s  . c  o m*/
 */
public static Map getMap(Preferences preferences, String key) {
    return getMap(preferences.node(key));
}

From source file:PrefsUtil.java

/**
 * Gets a List from the preferences, starting with "0", then "1" etc
 *///ww w.  j  a  v a  2 s.  c  o m
public static List getList(Preferences preferences, String key) {
    return getList(preferences.node(key));
}

From source file:PrefsUtil.java

/**
 * Puts a list into the preferences./*w ww . j a  v  a 2  s  . c o m*/
 */
public static void putMap(Preferences preferences, Map map, String key) {
    putMap(preferences.node(key), map);
}

From source file:PrefsUtil.java

/**
 * Puts a list into the preferences starting with "0" then "1"
 *//*w ww. j a  v  a2 s .  c  o m*/
public static void putList(Preferences preferences, List list, String key) {
    putList(preferences.node(key), list);
}

From source file:PrefsUtil.java

/**
 * Clear all the node// ww w . j  a v  a  2  s .  co m
 */
public static void clear(Preferences preferences, String key) {
    try {
        if (preferences.nodeExists(key)) {
            preferences.node(key).clear();
        }
    } catch (BackingStoreException bse) {
        bse.printStackTrace();
    }
}

From source file:PrefsUtil.java

/**
 * Remove the node/*from   w w  w  .  j  a v  a 2 s .c  om*/
 */
public static void remove(Preferences preferences, String key) {
    try {
        if (preferences.nodeExists(key)) {
            preferences.node(key).removeNode();
        }
    } catch (BackingStoreException bse) {
        bse.printStackTrace();
    }
}

From source file:de.tbuchloh.kiskis.gui.common.Application.java

/**
 * @param prefs//  w  w  w .ja va 2  s  .c  o m
 *            is the location to store the program information
 * @param list
 *            is a list of Program objects
 */
public static void storePrograms(final Preferences prefs, final Collection list) {
    Preferences n = prefs.node(Settings.K_URL_ALLPROGS_NODE);
    try {
        n.removeNode();
    } catch (final BackingStoreException e) {
        throw new Error(e);
    }

    n = prefs.node(Settings.K_URL_ALLPROGS_NODE);
    int cnt = 0;
    for (final Iterator i = list.iterator(); i.hasNext();) {
        final Program p = (Program) i.next();
        final Preferences child = n.node(String.valueOf(cnt++));
        child.put(Settings.K_URL_PROG_PREFIX, p.getUrlRegex());
        child.put(Settings.K_URL_PROG_CMD, p.getCommand());
        LOG.debug("storing " + p); //$NON-NLS-1$
    }
    try {
        n.flush();
    } catch (final BackingStoreException e1) {
        throw new Error(e1);
    }
}

From source file:de.tbuchloh.kiskis.gui.common.Application.java

/**
 * @param prefs//  w w  w  .java 2 s .c  om
 *            the preferences object to load the program from
 * @return a list with Program-objects
 */
public static List<Program> loadPrograms(final Preferences prefs) {
    final List<Program> allProgs = new ArrayList<Program>();
    final Preferences n = prefs.node(Settings.K_URL_ALLPROGS_NODE);
    String[] progs;
    try {
        progs = n.childrenNames();
    } catch (final BackingStoreException e) {
        throw new Error(e);
    }
    for (final String prog : progs) {
        final Preferences child = n.node(prog);
        final String prefix = child.get(Settings.K_URL_PROG_PREFIX, null);
        final String cmd = child.get(Settings.K_URL_PROG_CMD, null);
        if (prefix == null || cmd == null) {
            LOG.error("The application " //$NON-NLS-1$
                    + prog + " is not specified correctly!"); //$NON-NLS-1$
            continue;
        }
        LOG.debug("command: " + cmd + " for prefix " + prefix); //$NON-NLS-1$ //$NON-NLS-2$
        allProgs.add(new Program(prefix, cmd));
    }

    if (progs.length == 0) {
        allProgs.add(new Program("http", DEFAULT_BROWSER + " " + URL_KEY));
    }
    return allProgs;
}

From source file:ec.nbdemetra.ui.Installer.java

public static void storeConfig(Collection<?> list, Preferences root) {
    Formatters.Formatter<Config> formatter = Config.xmlFormatter(false);
    for (IConfigurable o : Iterables.filter(list, IConfigurable.class)) {
        Config current = o.getConfig();//  w  w  w  .  j a va 2 s .c  o m
        Preferences domain = root.node(current.getDomain());
        InstallerStep.tryPut(domain, current.getName(), formatter, current);
    }
    try {
        root.flush();
    } catch (BackingStoreException ex) {
        LOGGER.warn("Can't flush storage", ex);
    }
}