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

The following examples show how to use java.util.prefs.Preferences#childrenNames() . 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: ProjectManager.java    From markdown-writer-fx with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private static List<File> getRecentProjects() {
	Preferences state = getProjectsState();
	ArrayList<File> projects = new ArrayList<>();

	try {
		String[] childrenNames = state.childrenNames();
		for (String childName : childrenNames) {
			Preferences child = state.node(childName);
			String path = child.get(KEY_PATH, null);
			if (path != null)
				projects.add(new File(path));
		}
	} catch (BackingStoreException ex) {
		// ignore
		ex.printStackTrace();
	}

	return projects;
}
 
Example 2
Source File: SysSettings.java    From elexis-3-core with Eclipse Public License 1.0 6 votes vote down vote up
private void loadTree(Preferences root, String path){
	try {
		String[] subnodes = root.childrenNames();
		path = path.replaceFirst("^/", "");
		for (int s = 0; s < subnodes.length; s++) {
			Preferences sub = root.node(subnodes[s]);
			loadTree(sub, path + "/" + subnodes[s]);
		}
		String[] keys = root.keys();
		for (int i = 0; i < keys.length; i++) {
			if (path.equals(""))
				set(keys[i], root.get(keys[i], ""));
			else
				set(path + "/" + keys[i], root.get(keys[i], ""));
		}
	} catch (Exception ex) {
		ExHandler.handle(ex);
	}
	
}
 
Example 3
Source File: ConfigurationsManager.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void init() {
    Preferences prefs = getConfigurationsRoot();
    try {
        for (String kid:prefs.childrenNames()) {
            if (kid.startsWith(RULE_PREFIX)) {
                Preferences p = prefs.node(kid);
                String displayName = p.get("display.name", "unknown");
                create(kid.substring(RULE_PREFIX.length()), displayName);
            }
        }
    } catch (BackingStoreException ex) {
        Exceptions.printStackTrace(ex);
    }
    if (configs.isEmpty()) {
        create("default", NbBundle.getMessage(ConfigurationsManager.class, "DN_Default"));
    }
    prefs.putInt(KEY_CONFIGURATIONS_VERSION, CURRENT_CONFIGURATIONS_VERSION);
}
 
Example 4
Source File: ProxyPreferencesImplTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void checkEquals(String msg, Preferences expected, Preferences test) throws BackingStoreException {
    assertEquals("Won't compare two Preferences with different absolutePath", expected.absolutePath(), test.absolutePath());
    
    // check the keys and their values
    for(String key : expected.keys()) {
        String expectedValue = expected.get(key, null);
        assertNotNull(msg + "; Expected:" + expected.absolutePath() + " has no '" + key + "'", expectedValue);
        
        String value = test.get(key, null);
        assertNotNull(msg + "; Test:" + test.absolutePath() + " has no '" + key + "'", value);
        assertEquals(msg + "; Test:" + test.absolutePath() + "/" + key + " has wrong value", expectedValue, value);
    }

    // check the children
    for(String child : expected.childrenNames()) {
        assertTrue(msg + "; Expected:" + expected.absolutePath() + " has no '" + child + "' subnode", expected.nodeExists(child));
        Preferences expectedChild = expected.node(child);

        assertTrue(msg + "; Test:" + test.absolutePath() + " has no '" + child + "' subnode", test.nodeExists(child));
        Preferences testChild = test.node(child);

        checkEquals(msg, expectedChild, testChild);
    }
}
 
Example 5
Source File: InstancePropertiesManager.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Returns all existing properties created in the given namespace.
 *
 * @param namespace string identifying the namespace
 * @return list of all existing properties created in the given namespace
 */
public List<InstanceProperties> getProperties(String namespace) {
    Preferences prefs = NbPreferences.forModule(InstancePropertiesManager.class);

    try {
        prefs = prefs.node(namespace);
        prefs.flush();

        List<InstanceProperties> allProperties = new ArrayList<InstanceProperties>();
        synchronized (this) {
            for (String id : prefs.childrenNames()) {
                Preferences child = prefs.node(id);
                InstanceProperties props = cache.get(child);
                if (props == null) {
                    props = new DefaultInstanceProperties(id, this, child);
                    cache.put(child, props);
                }
                allProperties.add(props);
            }
        }
        return allProperties;
    } catch (BackingStoreException ex) {
        LOGGER.log(Level.INFO, null, ex);
        throw new IllegalStateException(ex);
    }
}
 
Example 6
Source File: MarginallyCleverPreferencesHelper.java    From Robot-Overlord-App with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param args command line arguments.
 */
@SuppressWarnings("deprecation")
public static void main(String[] args) throws BackingStoreException {
  final Preferences machinesPreferenceNode = PreferencesHelper.getPreferenceNode(PreferencesHelper.MakelangeloPreferenceKey.MACHINES);
  Log.message("node name: "+ machinesPreferenceNode.name());
  final boolean wereThereCommandLineArguments = args.length > 0;
  if (wereThereCommandLineArguments) {
    final boolean wasSaveFileFlagFound = wasSearchKeyFoundInArray(SAVE_FILE_FLAG, args);
    if (wasSaveFileFlagFound) {
      final File preferencesFile = MarginallyCleverPreferencesFileFactory.getXmlPreferencesFile();
      try (final OutputStream fileOutputStream = new FileOutputStream(preferencesFile)) {
        PreferencesHelper.getPreferenceNode(PreferencesHelper.MakelangeloPreferenceKey.LEGACY_MAKELANGELO_ROOT).exportSubtree(fileOutputStream);
      } catch (IOException e) {
        Log.error(e.getMessage());
      }
    }
    final boolean wasPurgeFlagFound = wasSearchKeyFoundInArray(PURGE_FLAG, args);
    if (wasPurgeFlagFound) {
      final String[] childrenPreferenceNodeNames = machinesPreferenceNode.childrenNames();
      purgeMachineNamesThatAreLessThanZero(machinesPreferenceNode, childrenPreferenceNodeNames);
    }
  }
}
 
Example 7
Source File: ProxyPreferencesTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void removeAllKidsAndKeys(Preferences prefs) throws BackingStoreException {
    for(String kid : prefs.childrenNames()) {
        prefs.node(kid).removeNode();
    }
    for(String key : prefs.keys()) {
        prefs.remove(key);
    }
}
 
Example 8
Source File: Debug.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void removeAll(Preferences prefs, boolean delete) throws BackingStoreException {

    String[] kidName = prefs.childrenNames();
    for (String aKidName : kidName) {
      Preferences pkid = prefs.node(aKidName);
      removeAll(pkid, true);
    }

    if (delete)
      prefs.removeNode();
    else
      prefs.clear();
  }
 
Example 9
Source File: CredentialsStore.java    From swift-explorer with Apache License 2.0 5 votes vote down vote up
/**
 * lists the available credentials for this user.
 * @return the credentials.
 */
public List<Credentials> getAvailableCredentials() {
    List<Credentials> results = new ArrayList<Credentials>();
    try {
        Preferences prefs = Preferences.userNodeForPackage(CredentialsStore.class);
        for (String node : prefs.childrenNames()) {
            results.add(toCredentials(prefs.node(node)));
        }
    } catch (BackingStoreException ex) {
        throw new RuntimeException(ex);
    }
    return results;
}
 
Example 10
Source File: ProxyPreferencesTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void dump(Preferences prefs, String prefsId) throws BackingStoreException {
    for(String key : prefs.keys()) {
        System.out.println(prefsId + ", " + prefs.absolutePath() + "/" + key + "=" + prefs.get(key, null));
    }
    for(String child : prefs.childrenNames()) {
        dump(prefs.node(child), prefsId);
    }
}
 
Example 11
Source File: ProxyPreferencesImplTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void removeAllKidsAndKeys(Preferences prefs) throws BackingStoreException {
    for(String kid : prefs.childrenNames()) {
        prefs.node(kid).removeNode();
    }
    for(String key : prefs.keys()) {
        prefs.remove(key);
    }
}
 
Example 12
Source File: ListPreferencesNodes.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
private static void printNode( Preferences node, String indent )
    throws BackingStoreException
{
    System.out.print( indent );
    String name = node.name();
    if( "".equals( name ) )
    {
        name = "/";
    }

    System.out.print( name );
    String[] nodes = node.keys();
    if( nodes.length > 0 )
    {
        System.out.print( "  { " );
        boolean first = true;
        for( String key : nodes )
        {
            if( !first )
            {
                System.out.print( ", " );
            }
            first = false;
            System.out.print( key );
        }
        System.out.print( " }" );
    }
    System.out.println();
    for( String childName : node.childrenNames() )
    {
        Preferences child = node.node( childName );
        printNode( child, indent + "  " );
    }
}
 
Example 13
Source File: ProxyPreferencesImplTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void dump(Preferences prefs, String prefsId) throws BackingStoreException {
    for(String key : prefs.keys()) {
        System.out.println(prefsId + ", " + prefs.absolutePath() + "/" + key + "=" + prefs.get(key, null));
    }
    for(String child : prefs.childrenNames()) {
        dump(prefs.node(child), prefsId);
    }
}
 
Example 14
Source File: FormattingCustomizerPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void deepCopy(Preferences from, Preferences to) throws BackingStoreException {
    for(String kid : from.childrenNames()) {
        Preferences fromKid = from.node(kid);
        Preferences toKid = to.node(kid);
        deepCopy(fromKid, toKid);
    }
    for(String key : from.keys()) {
        String value = from.get(key, null);
        if (value == null) continue;

        Class type = guessType(value);
        if (Integer.class == type) {
            to.putInt(key, from.getInt(key, -1));
        } else if (Long.class == type) {
            to.putLong(key, from.getLong(key, -1L));
        } else if (Float.class == type) {
            to.putFloat(key, from.getFloat(key, -1f));
        } else if (Double.class == type) {
            to.putDouble(key, from.getDouble(key, -1D));
        } else if (Boolean.class == type) {
            to.putBoolean(key, from.getBoolean(key, false));
        } else if (String.class == type) {
            to.put(key, value);
        } else /* byte [] */ {
            to.putByteArray(key, from.getByteArray(key, new byte [0]));
        }
    }
}
 
Example 15
Source File: FormattingCustomizerPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void removeAllKidsAndKeys(Preferences prefs) throws BackingStoreException {
    for(String kid : prefs.childrenNames()) {
        // remove just the keys otherwise node listeners won't survive
        removeAllKidsAndKeys(prefs.node(kid));
    }
    for(String key : prefs.keys()) {
        prefs.remove(key);
    }
}
 
Example 16
Source File: ProxyPreferencesImplTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void removeAllKidsAndKeys(Preferences prefs) throws BackingStoreException {
    for(String kid : prefs.childrenNames()) {
        prefs.node(kid).removeNode();
    }
    for(String key : prefs.keys()) {
        prefs.remove(key);
    }
}
 
Example 17
Source File: AdjustConfigurationPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public ModifiedPreferences(ModifiedPreferences parent, String name, Preferences node) {
    this(parent, name); // NOI18N
    try {
        for (java.lang.String key : node.keys()) {
            put(key, node.get(key, null));
        }
        for (String child : node.childrenNames()) {
            subNodes.put(child, new ModifiedPreferences(this, node.name(), node.node(child)));
        }
    }
    catch (BackingStoreException ex) {
        Exceptions.printStackTrace(ex);
    }
}
 
Example 18
Source File: GroupsManager.java    From netbeans with Apache License 2.0 5 votes vote down vote up
List<DocumentGroupImpl> getGroups() {
    Preferences prefs = getPreferences();
    try {
        String[] names = prefs.childrenNames();
        ArrayList<DocumentGroupImpl> res = new ArrayList<>(names.length);
        for( String name : names ) {
            res.add(createGroup(name));
        }
        Collections.sort(res);
        return res;
    } catch( BackingStoreException e ) {
        LOG.log(Level.INFO, null, e);
    }
    return Collections.emptyList();
}
 
Example 19
Source File: CIOptions.java    From nb-ci-plugin with GNU General Public License v2.0 5 votes vote down vote up
public List<CIEntry> getEntries(final CIEntry.Type type) {
    List<CIEntry> entries = new LinkedList<CIEntry>();

    try {
        String nodePath = getNodePath(type);

        if (getPreferences().nodeExists(nodePath)) {
            Preferences parent = getPreferences().node(nodePath);
            String[] childrenNames = parent.childrenNames();

            for (String childName : childrenNames) {
                Preferences child = parent.node(childName);

                String path = child.get(KEY_ENTRY_PATH, null);
                String branch = child.get(KEY_ENTRY_BRANCH, null);
                String version = child.get(KEY_ENTRY_VERSION, null);

                entries.add(new CIEntry(type, childName, path, branch, version));
            }
        }
    } catch (BackingStoreException ex) {
        // TODO needs to implment it.
        Exceptions.printStackTrace(ex);
    }

    return entries;
}
 
Example 20
Source File: FilePreferencesXmlSupport.java    From phoebus with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Put the preferences in the specified Preferences node into the specified XML
 * element which is assumed to represent a node in the specified XML document
 * which is assumed to conform to PREFS_DTD. If subTree is true, create children
 * of the specified XML node conforming to all of the children of the specified
 * Preferences node and recurse.
 *
 * @throws BackingStoreException
 *             if it is not possible to read the preferences or children out of
 *             the specified preferences node.
 */
private static void putPreferencesInXml(Element elt, Document doc, Preferences prefs, boolean subTree)
        throws BackingStoreException {
    Preferences[] kidsCopy = null;
    String[] kidNames = null;

    // Node is locked to export its contents and get a
    // copy of children, then lock is released,
    // and, if subTree = true, recursive calls are made on children

    // to remove a node we need an exclusive lock

    // to remove a node we need an exclusive lock
    if (!((FileSystemPreferences) prefs).lockFile(false))
        throw (new BackingStoreException("Couldn't get file lock."));
    try {
        // Check if this node was concurrently removed. If yes
        // remove it from XML Document and return.
        if (((FileSystemPreferences) prefs).isRemoved()) {
            elt.getParentNode().removeChild(elt);
            return;
        }
        // Put map in xml element
        String[] keys = prefs.keys();
        Element map = (Element) elt.appendChild(doc.createElement("map"));
        for (int i = 0; i < keys.length; i++) {
            Element entry = (Element) map.appendChild(doc.createElement("entry"));
            entry.setAttribute("key", keys[i]);
            // NEXT STATEMENT THROWS NULL PTR EXC INSTEAD OF ASSERT FAIL
            entry.setAttribute("value", prefs.get(keys[i], null));
        }
        // Recurse if appropriate
        if (subTree) {
            /* Get a copy of kids while lock is held */
            kidNames = prefs.childrenNames();
            kidsCopy = new Preferences[kidNames.length];
            for (int i = 0; i < kidNames.length; i++)
                kidsCopy[i] = prefs.node(kidNames[i]);
        }
    } finally {
        ((FileSystemPreferences) prefs).unlockFile();
    }
    if (subTree) {
        for (int i = 0; i < kidNames.length; i++) {
            Element xmlKid = (Element) elt.appendChild(doc.createElement("node"));
            xmlKid.setAttribute("name", kidNames[i]);
            putPreferencesInXml(xmlKid, doc, kidsCopy[i], subTree);
        }
    }
}