How to get the name of the active project group ?

Apache NetBeans Wiki Index

Note: These pages are being reviewed.

Variant I: "use OpenProjects API" (since NB7.3)

org.netbeans.api.project.ui.OpenProjects.getDefault().getActiveProjectGroup().getName()

This approach uses a public API which is known to be stable for future versions. Since 7.3.

Variant II: "direct access to properties"-hack

Note: this is rather a hack. It is not guaranteed that this will work for newer NetBeans versions. But this approach is known to work at least with NB 6.9.1 to 7.3.

    /**
     *
     * @return name of the current project group or null
     */
    public String getActiveProjectGroup() {
	Preferences groupNode = getPreferences("org/netbeans/modules/projectui/groups");
	if (null != groupNode) {
	    final String groupId = groupNode.get("active", null);
	    if (null != groupId) {
		final Preferences groupPref = getPreferences("org/netbeans/modules/projectui/groups/" + groupId);
		if (null != groupPref) {
		    final String activeProjectGroup = groupPref.get("name", null);
		    return activeProjectGroup;
		}
	    }
	}
	return null;
    }

    /**
     * Get the preference for the given node path.
     *
     * @param path configuration path like "org/netbeans/modules/projectui"
     * @return {@link Preferences} or null
     */
    private Preferences getPreferences(String path) {
	try {
	    if (NbPreferences.root().nodeExists(path)) {
		return NbPreferences.root().node(path);
	    }
	} catch (BackingStoreException ex) {
	    Exceptions.printStackTrace(ex);
	}
	return null;
    }