DevFaqGetNameOfProjectGroup

How to get the name of the active project group

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.

See http://bits.netbeans.org/dev/javadoc/org-netbeans-modules-projectuiapi/org/netbeans/api/project/ui/OpenProjects.html#getActiveProjectGroup()

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;
    }

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo