/** * This implementation eagerly fetches the Preferences instances * for the required system and user tree nodes. */ @Override public void afterPropertiesSet() { if (this.systemTreePath != null) { this.systemPrefs = this.systemPrefs.node(this.systemTreePath); } if (this.userTreePath != null) { this.userPrefs = this.userPrefs.node(this.userTreePath); } }
/** * Resolve the given path and key against the given Preferences. * @param path the preferences path (placeholder part before '/') * @param key the preferences key (placeholder part after '/') * @param preferences the Preferences to resolve against * @return the value for the placeholder, or {@code null} if none found */ @Nullable protected String resolvePlaceholder(@Nullable String path, String key, Preferences preferences) { if (path != null) { // Do not create the node if it does not exist... try { if (preferences.nodeExists(path)) { return preferences.node(path).get(key, null); } else { return null; } } catch (BackingStoreException ex) { throw new BeanDefinitionStoreException("Cannot access specified node path [" + path + "]", ex); } } else { return preferences.get(key, null); } }
/** * This implementation eagerly fetches the Preferences instances * for the required system and user tree nodes. */ @Override public void afterPropertiesSet() { if (this.systemTreePath != null) { this.systemPrefs = this.systemPrefs.node(this.systemTreePath); } if (this.userTreePath != null) { this.userPrefs = this.userPrefs.node(this.userTreePath); } }
/** * Resolve the given path and key against the given Preferences. * @param path the preferences path (placeholder part before '/') * @param key the preferences key (placeholder part after '/') * @param preferences the Preferences to resolve against * @return the value for the placeholder, or {@code null} if none found */ @Nullable protected String resolvePlaceholder(@Nullable String path, String key, Preferences preferences) { if (path != null) { // Do not create the node if it does not exist... try { if (preferences.nodeExists(path)) { return preferences.node(path).get(key, null); } else { return null; } } catch (BackingStoreException ex) { throw new BeanDefinitionStoreException("Cannot access specified node path [" + path + "]", ex); } } else { return preferences.get(key, null); } }
Ini ini = new Ini(new File(filename)); java.util.prefs.Preferences prefs = new IniPreferences(ini); System.out.println("grumpy/homePage: " + prefs.node("grumpy").get("homePage", null));
/** * Retrieves data about he window settings from the previous session and applies * them to the stage. These settings include window size, position and if * it was maximized or not. * * @param stage the stage to apply these settings to * @param name the name under which we stored the settings */ public static void applyStoredDisplaySettings(Stage stage, String name){ try { if( PREFERENCES.nodeExists(name) ){ Preferences stagePreferences = PREFERENCES.node(name); boolean wasMaximized = stagePreferences.getBoolean(WINDOW_MAXIMIZED, false); if( wasMaximized ){ stage.setMaximized(true); } else { stage.setX( stagePreferences.getDouble(WINDOW_X_POS, DEFUALT_X)); stage.setY( stagePreferences.getDouble(WINDOW_Y_POS, DEFUALT_Y)); stage.setWidth( stagePreferences.getDouble(WINDOW_WIDTH, DEFUALT_WIDTH)); stage.setHeight(stagePreferences.getDouble(WINDOW_HEIGHT, DEFUALT_HEIHGT)); } } } catch (BackingStoreException ex) { LOGGER.error(ex, "Could not access preferences for window " + name); } }
Preferences prefs = Preferences.userRoot().node(getClass().getName()); JFileChooser chooser = new JFileChooser(prefs.get(LAST_USED_FOLDER, new File(".").getAbsolutePath())); int returnVal = chooser.showOpenDialog(null); if (returnVal == JFileChooser.APPROVE_OPTION) { // do something prefs.put(LAST_USED_FOLDER, chooser.getSelectedFile().getParent()); }
public String getPref(String section, String key) { try { if (prefs.nodeExists(section)) return prefs.node(section).get(key, null); } catch (BackingStoreException e) { e.printStackTrace(); } return null; }
private static void loadNode(Preferences prefs, Element node) { // load preferences NodeList children = selectNodeList(node, "node"); NodeList entries = selectNodeList(node, "map/entry"); int childNumber = children.getLength(); Preferences[] prefChildren = new Preferences[childNumber]; int entryNumber = entries.getLength(); synchronized (((AbstractPreferences) prefs).lock) { if (((AbstractPreferences) prefs).isRemoved()) { return; } for (int i = 0; i < entryNumber; i++) { Element entry = (Element) entries.item(i); String key = entry.getAttribute("key"); String value = entry.getAttribute("value"); prefs.put(key, value); } // get children preferences node for (int i = 0; i < childNumber; i++) { Element child = (Element) children.item(i); String name = child.getAttribute("name"); prefChildren[i] = prefs.node(name); } } // load children nodes after unlock for (int i = 0; i < childNumber; i++) { loadNode(prefChildren[i], (Element) children.item(i)); } }
/** * Returns the system preference node for the package of the given class. * The absolute path of the returned node is one slash followed by the given * class's full package name, replacing each period character ('.') with * a slash. For example, the absolute path of the preference associated with * the class Object would be "/java/lang". As a special case, the unnamed * package is associated with a preference node "/<unnamed>". This * method will create the node and its ancestors as needed. Any nodes created * by this method won't necessarily be persisted until the method {@code * flush()} is invoked. * * @param c * the given class. * @return the system preference node for the package of the given class. * @throws NullPointerException * if the given class is {@code null}. */ public static Preferences systemNodeForPackage (Class<?> c) { return factory.systemRoot().node(getNodeName(c)); }
/** * Returns the user preference node for the package of the given class. * The absolute path of the returned node is one slash followed by the given * class's full package name, replacing each period character ('.') with * a slash. For example, the absolute path of the preference associated with * the class Object would be "/java/lang". As a special case, the unnamed * package is associated with a preference node "/<unnamed>". This * method will create the node and its ancestors as needed. Any nodes created * by this method won't necessarily be persisted until the method {@code * flush()} is invoked. * * @param c * the given class. * @return the user preference node for the package of the given class. * @throws NullPointerException * if the given class is {@code null}. */ public static Preferences userNodeForPackage (Class<?> c) { return factory.userRoot().node(getNodeName(c)); }
Preferences prefs = Preferences.userRoot().node(preferencesKey); String[] keys = prefs.keys(); if (keys != null) {
@Test public void testPreferencesPlaceholderConfigurerWithPathInPlaceholder() { factory.registerBeanDefinition("tb", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "${mypath/myName}") .addPropertyValue("age", "${myAge}") .addPropertyValue("touchy", "${myotherpath/myTouchy}") .getBeanDefinition()); PreferencesPlaceholderConfigurer ppc = new PreferencesPlaceholderConfigurer(); Properties props = new Properties(); props.put("myAge", "99"); ppc.setProperties(props); ppc.setSystemTreePath("mySystemPath"); ppc.setUserTreePath("myUserPath"); Preferences.systemRoot().node("mySystemPath").node("mypath").put("myName", "myNameValue"); Preferences.systemRoot().node("mySystemPath/myotherpath").put("myTouchy", "myTouchyValue"); Preferences.userRoot().node("myUserPath/myotherpath").put("myTouchy", "myOtherTouchyValue"); ppc.afterPropertiesSet(); ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); assertEquals("myNameValue", tb.getName()); assertEquals(99, tb.getAge()); assertEquals("myOtherTouchyValue", tb.getTouchy()); Preferences.userRoot().node("myUserPath/myotherpath").remove("myTouchy"); Preferences.systemRoot().node("mySystemPath/myotherpath").remove("myTouchy"); Preferences.systemRoot().node("mySystemPath/mypath").remove("myName"); }
@Test public void testPreferencesPlaceholderConfigurerWithCustomTreePaths() { factory.registerBeanDefinition("tb", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "${myName}") .addPropertyValue("age", "${myAge}") .addPropertyValue("touchy", "${myTouchy}") .getBeanDefinition()); PreferencesPlaceholderConfigurer ppc = new PreferencesPlaceholderConfigurer(); Properties props = new Properties(); props.put("myAge", "99"); ppc.setProperties(props); ppc.setSystemTreePath("mySystemPath"); ppc.setUserTreePath("myUserPath"); Preferences.systemRoot().node("mySystemPath").put("myName", "myNameValue"); Preferences.systemRoot().node("mySystemPath").put("myTouchy", "myTouchyValue"); Preferences.userRoot().node("myUserPath").put("myTouchy", "myOtherTouchyValue"); ppc.afterPropertiesSet(); ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); assertEquals("myNameValue", tb.getName()); assertEquals(99, tb.getAge()); assertEquals("myOtherTouchyValue", tb.getTouchy()); Preferences.userRoot().node("myUserPath").remove("myTouchy"); Preferences.systemRoot().node("mySystemPath").remove("myTouchy"); Preferences.systemRoot().node("mySystemPath").remove("myName"); }
Preferences prefs = Preferences.userRoot().node(preferencesKey);
private static void exportSubTree(Preferences prefs, BufferedWriter out) throws BackingStoreException, IOException { String[] names = prefs.childrenNames(); if (names.length > 0) { for (int i = 0; i < names.length; i++) { Preferences child = prefs.node(names[i]); flushStartTag("node", new String[] { "name" }, new String[] { names[i] }, out); exportEntries(child, out); exportSubTree(child, out); flushEndTag("node", out); } } }
StreamDataSource cepstrumDataSource; prefs = Preferences.userRoot().node(PREFS_CONTEXT); filename = prefs.get(FILENAME_PREFERENCE, "untitled.raw"); file = new File(filename);
/** * Adds an OnCloseRequest handle to the window which will store the position, * size and maximized status of the window. * * @param stage the stage to read settings from * @param name the name under which we store the settings */ public static void applySaveOnCloseMethod(Stage stage, String name){ stage.setOnCloseRequest( ev -> { try { Preferences stagePreferences = PREFERENCES.node(name); if( stage.isMaximized() ){ stagePreferences.putBoolean(WINDOW_MAXIMIZED, true); } else { stagePreferences.putBoolean(WINDOW_MAXIMIZED, false); stagePreferences.putDouble(WINDOW_X_POS, stage.getX()); stagePreferences.putDouble(WINDOW_Y_POS, stage.getY()); stagePreferences.putDouble(WINDOW_WIDTH, stage.getWidth()); stagePreferences.putDouble(WINDOW_HEIGHT, stage.getHeight()); } stagePreferences.flush(); } catch (final BackingStoreException ex) { LOGGER.error(ex, "Could not flush preferences for window " + name); } }); }
/** * Set the general file path to the utility class and persist to JVM preferences. * @param path folder path to persist */ public static void set(String path) { PreferencesUtil.setPathFile(path); Preferences prefs = Preferences.userRoot().node(InjectionModel.class.getName()); prefs.put("pathFile", PreferencesUtil.pathFile); }
/** * This implementation eagerly fetches the Preferences instances * for the required system and user tree nodes. */ public void afterPropertiesSet() { this.systemPrefs = (this.systemTreePath != null) ? Preferences.systemRoot().node(this.systemTreePath) : Preferences.systemRoot(); this.userPrefs = (this.userTreePath != null) ? Preferences.userRoot().node(this.userTreePath) : Preferences.userRoot(); }