Skip to content

DevFaqGetNameOfProjectGroup

Antonio Vieiro edited this page Jan 25, 2018 · 1 revision

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.

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

Apache Migration Information

The content in this page was kindly donated by Oracle Corp. to the Apache Software Foundation.

This page was exported from http://wiki.netbeans.org/DevFaqGetNameOfProjectGroup , that was last modified by NetBeans user Markiewb on 2013-01-12T17:40:12Z.

NOTE: This document was automatically converted to the AsciiDoc format on 2018-01-26, and needs to be reviewed.

Clone this wiki locally