Java Code Examples for java.util.prefs.Preferences#nodeExists()

The following examples show how to use java.util.prefs.Preferences#nodeExists() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FontUtilities.java    From constellation with Apache License 2.0 6 votes vote down vote up
/**
 * Return the user's default font size.
 * <p>
 * This retrieves the font size specified in Setup &rarr; Options &rarr;
 * Miscellaneous &rarr; Output &rarr; Font Size. The default if not
 * specified is 11.
 *
 * @return The user's default font size.
 */
public static int getOutputFontSize() {
    int fontSize;

    try {
        final Preferences p = NbPreferences.root();
        if (p.nodeExists(ApplicationPreferenceKeys.OUTPUT2_PREFERENCE)) {
            final String fontSizePreference = p.node(ApplicationPreferenceKeys.OUTPUT2_PREFERENCE).get(ApplicationPreferenceKeys.OUTPUT2_FONT_SIZE, ApplicationPreferenceKeys.OUTPUT2_FONT_SIZE_DEFAULT);
            fontSize = Integer.parseInt(fontSizePreference);
        } else {
            fontSize = UIManager.getFont(SWING_FONT).getSize();
        }
    } catch (final BackingStoreException | NumberFormatException ex) {
        fontSize = Integer.parseInt(ApplicationPreferenceKeys.OUTPUT2_FONT_SIZE_DEFAULT);
        LOGGER.severe(ex.getLocalizedMessage());
    }

    LOGGER.log(Level.FINE, "Font size is {0}", fontSize);
    return fontSize;
}
 
Example 2
Source File: PreferencesPlaceholderConfigurer.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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
 */
protected String resolvePlaceholder(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);
	}
}
 
Example 3
Source File: ProxyPreferencesImplTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void checkNotContains(Preferences prefs, String[] tree, String prefsId) throws BackingStoreException {
    for(String s : tree) {
        int equalIdx = s.lastIndexOf('=');
        assertTrue(equalIdx != -1);
        String value = s.substring(equalIdx + 1);

        String key;
        String nodePath;
        int slashIdx = s.lastIndexOf('/', equalIdx);
        if (slashIdx != -1) {
            key = s.substring(slashIdx + 1, equalIdx);
            nodePath = s.substring(0, slashIdx);
        } else {
            key = s.substring(0, equalIdx);
            nodePath = "";
        }

        if (prefs.nodeExists(nodePath)) {
            Preferences node = prefs.node(nodePath);
            String realValue = node.get(key, null);
            if (realValue != null && realValue.equals(value)) {
                fail(prefsId + ", '" + nodePath + "' node contains key '" + key + "' = '" + realValue + "'");
            }
        }
    }
}
 
Example 4
Source File: ProxyPreferencesImplTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void checkNotContains(Preferences prefs, String[] tree, String prefsId) throws BackingStoreException {
    for(String s : tree) {
        int equalIdx = s.lastIndexOf('=');
        assertTrue(equalIdx != -1);
        String value = s.substring(equalIdx + 1);

        String key;
        String nodePath;
        int slashIdx = s.lastIndexOf('/', equalIdx);
        if (slashIdx != -1) {
            key = s.substring(slashIdx + 1, equalIdx);
            nodePath = s.substring(0, slashIdx);
        } else {
            key = s.substring(0, equalIdx);
            nodePath = "";
        }

        if (prefs.nodeExists(nodePath)) {
            Preferences node = prefs.node(nodePath);
            String realValue = node.get(key, null);
            if (realValue != null && realValue.equals(value)) {
                fail(prefsId + ", '" + nodePath + "' node contains key '" + key + "' = '" + realValue + "'");
            }
        }
    }
}
 
Example 5
Source File: ApplicationSettings.java    From keystore-explorer with GNU General Public License v3.0 6 votes vote down vote up
private ApplicationSettings() {

		// one-time conversion from old to new preferences location:
		Preferences root = Preferences.userRoot();
		try {
			// if preferences exist under /net/sf/keystore_explorer but not under /org/kse ...
			if (root.nodeExists(PREFS_NODE_OLD) && !root.nodeExists(PREFS_NODE)) {

				// ... then copy settings from old to new subtree
				Preferences prefsOld = root.node(PREFS_NODE_OLD);
				Preferences prefsNew = root.node(PREFS_NODE);

				for (String key : prefsOld.keys()) {
					prefsNew.put(key, prefsOld.get(key, ""));
				}

				prefsNew.flush();
			}
		} catch (BackingStoreException e) {
			// ignore errors here
		}

		load();
	}
 
Example 6
Source File: PreferencesPlaceholderConfigurer.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * 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);
	}
}
 
Example 7
Source File: PreferencesPlaceholderConfigurer.java    From blog_demos with Apache License 2.0 6 votes vote down vote up
/**
 * 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
 */
protected String resolvePlaceholder(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);
	}
}
 
Example 8
Source File: PreferencesPlaceholderConfigurer.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * 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);
	}
}
 
Example 9
Source File: PreferenceUtilites.java    From constellation with Apache License 2.0 6 votes vote down vote up
/**
 * Add a PreferenceChangeListener to a specified preference node.
 * <p>
 * For example, to listen for font size changes, listen to
 * "org/netbeans/core/output2".
 *
 * @param preferenceNode The preference node to listen to.
 * @param pcl A PreferenceChangeListener
 *
 * @return True if the addPreferenceChangeListener() worked, false
 * otherwise.
 */
public static boolean addPreferenceChangeListener(final String preferenceNode, final PreferenceChangeListener pcl) {
    try {
        Preferences p = NbPreferences.root();
        if (p.nodeExists(preferenceNode)) {
            p = p.node(preferenceNode);
            p.addPreferenceChangeListener(pcl);

            return true;
        }
    } catch (BackingStoreException ex) {
        Exceptions.printStackTrace(ex);
    }

    return false;
}
 
Example 10
Source File: InputContext.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private AWTKeyStroke getInputMethodSelectionKeyStroke(Preferences root) {
    try {
        if (root.nodeExists(inputMethodSelectionKeyPath)) {
            Preferences node = root.node(inputMethodSelectionKeyPath);
            int keyCode = node.getInt(inputMethodSelectionKeyCodeName, KeyEvent.VK_UNDEFINED);
            if (keyCode != KeyEvent.VK_UNDEFINED) {
                int modifiers = node.getInt(inputMethodSelectionKeyModifiersName, 0);
                return AWTKeyStroke.getAWTKeyStroke(keyCode, modifiers);
            }
        }
    } catch (BackingStoreException bse) {
    }

    return null;
}
 
Example 11
Source File: InputContext.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private AWTKeyStroke getInputMethodSelectionKeyStroke(Preferences root) {
    try {
        if (root.nodeExists(inputMethodSelectionKeyPath)) {
            Preferences node = root.node(inputMethodSelectionKeyPath);
            int keyCode = node.getInt(inputMethodSelectionKeyCodeName, KeyEvent.VK_UNDEFINED);
            if (keyCode != KeyEvent.VK_UNDEFINED) {
                int modifiers = node.getInt(inputMethodSelectionKeyModifiersName, 0);
                return AWTKeyStroke.getAWTKeyStroke(keyCode, modifiers);
            }
        }
    } catch (BackingStoreException bse) {
    }

    return null;
}
 
Example 12
Source File: InputContext.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private AWTKeyStroke getInputMethodSelectionKeyStroke(Preferences root) {
    try {
        if (root.nodeExists(inputMethodSelectionKeyPath)) {
            Preferences node = root.node(inputMethodSelectionKeyPath);
            int keyCode = node.getInt(inputMethodSelectionKeyCodeName, KeyEvent.VK_UNDEFINED);
            if (keyCode != KeyEvent.VK_UNDEFINED) {
                int modifiers = node.getInt(inputMethodSelectionKeyModifiersName, 0);
                return AWTKeyStroke.getAWTKeyStroke(keyCode, modifiers);
            }
        }
    } catch (BackingStoreException bse) {
    }

    return null;
}
 
Example 13
Source File: InputContext.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private AWTKeyStroke getInputMethodSelectionKeyStroke(Preferences root) {
    try {
        if (root.nodeExists(inputMethodSelectionKeyPath)) {
            Preferences node = root.node(inputMethodSelectionKeyPath);
            int keyCode = node.getInt(inputMethodSelectionKeyCodeName, KeyEvent.VK_UNDEFINED);
            if (keyCode != KeyEvent.VK_UNDEFINED) {
                int modifiers = node.getInt(inputMethodSelectionKeyModifiersName, 0);
                return AWTKeyStroke.getAWTKeyStroke(keyCode, modifiers);
            }
        }
    } catch (BackingStoreException bse) {
    }

    return null;
}
 
Example 14
Source File: InputContext.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private AWTKeyStroke getInputMethodSelectionKeyStroke(Preferences root) {
    try {
        if (root.nodeExists(inputMethodSelectionKeyPath)) {
            Preferences node = root.node(inputMethodSelectionKeyPath);
            int keyCode = node.getInt(inputMethodSelectionKeyCodeName, KeyEvent.VK_UNDEFINED);
            if (keyCode != KeyEvent.VK_UNDEFINED) {
                int modifiers = node.getInt(inputMethodSelectionKeyModifiersName, 0);
                return AWTKeyStroke.getAWTKeyStroke(keyCode, modifiers);
            }
        }
    } catch (BackingStoreException bse) {
    }

    return null;
}
 
Example 15
Source File: FormattingSettingsFromNbPreferences.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void afterLoad(Map<String, TypedValue> map, MimePath mimePath, String profile, boolean defaults) throws IOException {
    if (defaults || mimePath.size() != 1 || !affectedMimeTypes.containsKey(mimePath.getPath())) {
        return;
    }

    try {
        Preferences nbprefs = getNbPreferences(mimePath.getPath());
        if (nbprefs != null && nbprefs.nodeExists("CodeStyle/default")) { //NOI18N
            Preferences codestyle = nbprefs.node("CodeStyle/default"); //NOI18N
            for(String key : codestyle.keys()) {
                if (!map.containsKey(key)) {
                    TypedValue typedValue = guessTypedValue(codestyle.get(key, null));
                    if (typedValue != null) {
                        map.put(key, typedValue);
                        if (LOG.isLoggable(Level.FINE)) {
                            LOG.fine("Injecting '" + key + "' = '" + typedValue.getValue() //NOI18N
                                + "' (" + typedValue.getJavaType() + ") for '" + mimePath.getPath() + "'"); //NOI18N
                        }
                    }
                }
            }
        }
    } catch (BackingStoreException bse) {
        // ignore
        LOG.log(Level.FINE, null, bse);
    }
}
 
Example 16
Source File: InputContext.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private AWTKeyStroke getInputMethodSelectionKeyStroke(Preferences root) {
    try {
        if (root.nodeExists(inputMethodSelectionKeyPath)) {
            Preferences node = root.node(inputMethodSelectionKeyPath);
            int keyCode = node.getInt(inputMethodSelectionKeyCodeName, KeyEvent.VK_UNDEFINED);
            if (keyCode != KeyEvent.VK_UNDEFINED) {
                int modifiers = node.getInt(inputMethodSelectionKeyModifiersName, 0);
                return AWTKeyStroke.getAWTKeyStroke(keyCode, modifiers);
            }
        }
    } catch (BackingStoreException bse) {
    }

    return null;
}
 
Example 17
Source File: InputContext.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private AWTKeyStroke getInputMethodSelectionKeyStroke(Preferences root) {
    try {
        if (root.nodeExists(inputMethodSelectionKeyPath)) {
            Preferences node = root.node(inputMethodSelectionKeyPath);
            int keyCode = node.getInt(inputMethodSelectionKeyCodeName, KeyEvent.VK_UNDEFINED);
            if (keyCode != KeyEvent.VK_UNDEFINED) {
                int modifiers = node.getInt(inputMethodSelectionKeyModifiersName, 0);
                return AWTKeyStroke.getAWTKeyStroke(keyCode, modifiers);
            }
        }
    } catch (BackingStoreException bse) {
    }

    return null;
}
 
Example 18
Source File: FontUtilities.java    From constellation with Apache License 2.0 5 votes vote down vote up
/**
 * Set the default font size as a preference if its not already defined.
 * <p>
 * Top Components listen for changes in
 * ApplicationPreferenceKeys.OUTPUT2_PREFERENCE and if its not defined then
 * the listener will not be registered. This initialise method is called
 * when the application starts to make sure a preference is defined.
 */
public static synchronized void initialiseFontPreferenceOnFirstUse() {
    final Preferences p = NbPreferences.root();
    try {
        if (!p.nodeExists(ApplicationPreferenceKeys.OUTPUT2_PREFERENCE)) {
            p.node(ApplicationPreferenceKeys.OUTPUT2_PREFERENCE).put(ApplicationPreferenceKeys.OUTPUT2_FONT_SIZE, ApplicationPreferenceKeys.OUTPUT2_FONT_SIZE_DEFAULT);
        }
    } catch (final BackingStoreException ex) {
        Exceptions.printStackTrace(ex);
    }
}
 
Example 19
Source File: QueriesCache.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@NonNull
private Map<URL,T> loadRoots() {
    synchronized(lck) {
       if (cache == null) {
           Map<URL,T> result = new HashMap<URL, T>();
           final Preferences root = NbPreferences.forModule(QueriesCache.class);
           final String folder = clazz.getSimpleName();
           try {
               if (root.nodeExists(folder)) {
                   final Preferences node = root.node(folder);
                   Map<URL,List<URL>> bindings = new HashMap<URL, List<URL>>();
                   for (String key : node.keys()) {
                       final String value = node.get(key, null);
                       if (value != null) {
                           final URL binUrl = getURL(key);
                           List<URL> binding = bindings.get(binUrl);
                           if(binding == null) {
                               binding = new ArrayList<URL>();
                               bindings.put(binUrl,binding);
                           }
                           binding.add(new URL(value));
                       }
                   }
                   for (Map.Entry<URL,List<URL>> e : bindings.entrySet()) {
                       final T instance = clazz.newInstance();
                       instance.update(e.getValue());
                       result.put(e.getKey(), instance);
                   }
               }
           } catch (BackingStoreException bse) {
               Exceptions.printStackTrace(bse);
           } catch (MalformedURLException mue) {
               Exceptions.printStackTrace(mue);
           } catch (InstantiationException ie) {
               Exceptions.printStackTrace(ie);
           } catch (IllegalAccessException iae) {
               Exceptions.printStackTrace(iae);
           }
           cache = result;
       }
       return cache;
    }
}
 
Example 20
Source File: Configuration.java    From wpcleaner with Apache License 2.0 4 votes vote down vote up
/**
 * Move a child.
 * 
 * @param oldParent Old parent node.
 * @param newParent New parent node.
 * @param childName Child name.
 * @return True if the child has been completely moved.
 * @throws BackingStoreException
 */
private boolean moveChild(
    Preferences oldParent, Preferences newParent, String childName)
    throws BackingStoreException {
  if ((oldParent == null) || (childName == null)) {
    return true;
  }
  if (!oldParent.nodeExists(childName)) {
    return true;
  }
  if (newParent == null) {
    return false;
  }
  Preferences oldChild = oldParent.node(childName);
  Preferences newChild = newParent.node(childName);

  // Move keys
  String[] keyNames = oldChild.keys();
  if (keyNames != null) {
    for (String keyName : keyNames) {
      String value = oldChild.get(keyName, null);
      if (value != null) {
        newChild.put(keyName, value);
      }
      oldChild.remove(keyName);
    }
  }

  // Move children
  String[] childNames2 = oldChild.childrenNames();
  if (childNames2 != null) {
    for (String childName2 : childNames2) {
      moveChild(oldChild, newChild, childName2);
    }
  }

  // Clean up
  boolean ok = false;
  newChild.flush();
  keyNames = oldChild.keys();
  childNames2 = oldChild.childrenNames();
  if (((keyNames == null) || (keyNames.length == 0)) ||
      ((childNames2 == null) || (childNames2.length == 0))) {
    oldChild.removeNode();
    ok = true;
  }
  oldChild.flush();
  return ok;
}