| // |
| // Licensed to the Apache Software Foundation (ASF) under one |
| // or more contributor license agreements. See the NOTICE file |
| // distributed with this work for additional information |
| // regarding copyright ownership. The ASF licenses this file |
| // to you under the Apache License, Version 2.0 (the |
| // "License"); you may not use this file except in compliance |
| // with the License. You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, |
| // software distributed under the License is distributed on an |
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| // KIND, either express or implied. See the License for the |
| // specific language governing permissions and limitations |
| // under the License. |
| // |
| |
| = DevFaqGetNameOfProjectGroup |
| :jbake-type: wiki |
| :jbake-tags: wiki, devfaq, needsreview |
| :jbake-status: published |
| :keywords: Apache NetBeans wiki DevFaqGetNameOfProjectGroup |
| :description: Apache NetBeans wiki DevFaqGetNameOfProjectGroup |
| :toc: left |
| :toc-title: |
| :syntax: true |
| |
| = How to get the name of the active project group |
| |
| == Variant I: "use OpenProjects API" (since NB7.3) |
| |
| [source,java] |
| ---- |
| |
| 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 link:http://bits.netbeans.org/dev/javadoc/org-netbeans-modules-projectuiapi/org/netbeans/api/project/ui/OpenProjects.html#getActiveProjectGroup([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. |
| |
| [source,java] |
| ---- |
| |
| /** |
| * |
| * @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 link:http://wiki.netbeans.org/DevFaqGetNameOfProjectGroup[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-02-07, and needs to be reviewed. |