Java tutorial
/* * license-updater - Copyright (c) 2012 MSF. All rights reserved. * * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General * Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ package br.msf.commons.netbeans.util; import br.msf.commons.util.CollectionUtils; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import org.apache.commons.lang.ArrayUtils; import org.netbeans.api.java.classpath.ClassPath; import org.netbeans.api.java.project.JavaProjectConstants; import org.netbeans.api.java.source.ClasspathInfo; import org.netbeans.api.project.FileOwnerQuery; import org.netbeans.api.project.Project; import org.netbeans.api.project.SourceGroup; import org.netbeans.api.project.Sources; import org.netbeans.api.project.ui.OpenProjects; import org.openide.filesystems.FileObject; import org.openide.loaders.DataObject; import org.openide.util.Lookup; import org.openide.util.Utilities; /** * <p>TODO * * @author Marcius da Silva da Fonseca (sf.marcius@gmail.com) * @version 1.0 * @since license-updater-1.0 */ public abstract class ProjectUtils { public static Project getMainProject() { return OpenProjects.getDefault().getMainProject(); } public static Project getSelectedProject() { return getSelectedProject(Utilities.actionsGlobalContext()); } public static Project getSelectedProject(final Lookup context) { Lookup.Result<Project> projectLookup = context.lookupResult(Project.class); Project p = (projectLookup.allInstances().isEmpty()) ? null : projectLookup.allInstances().iterator().next(); if (p == null) { DataObject packageNode = PackageUtils.getSelectedPackage(context); if (packageNode != null) { p = FileOwnerQuery.getOwner(packageNode.getPrimaryFile()); } } return p; } public static boolean hasOpenProjects() { return CollectionUtils.isNotEmpty(getOpenProjects()); } public static Collection<Project> getOpenProjects() { Project[] open = OpenProjects.getDefault().getOpenProjects(); return ArrayUtils.isEmpty(open) ? CollectionUtils.EMPTY_COLLECTION : Arrays.asList(open); } public static ClassPath getSourceClassPath(final Project project) { Sources sources = org.netbeans.api.project.ProjectUtils.getSources(project); SourceGroup srcGroup = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA)[0]; return ClassPath.getClassPath(srcGroup.getRootFolder(), ClassPath.SOURCE); } public static ClasspathInfo getSourceClassPathInfo(final Project project) { Sources sources = org.netbeans.api.project.ProjectUtils.getSources(project); SourceGroup srcGroup = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA)[0]; return ClasspathInfo.create(srcGroup.getRootFolder()); } public static Collection<JavaPackage> getSourcePackages(final Project project, final boolean includeRootPackage) { return PackageUtils.getPackages(getSourceClassPath(project), includeRootPackage); } public static Collection<FileObject> getProjectFiles(final Project project, final FileType... types) { Collection<FileObject> fileObjects = new ArrayList<FileObject>(); Sources srcs = org.netbeans.api.project.ProjectUtils.getSources(project); SourceGroup[] srcGrps = srcs.getSourceGroups(Sources.TYPE_GENERIC); for (SourceGroup srcGrp : srcGrps) { fileObjects.addAll(FileObjectUtils.getFiles(srcGrp.getRootFolder(), true, types)); } return fileObjects; } }