Package br.msf.commons.netbeans.util

Source Code of br.msf.commons.netbeans.util.PackageUtils

/*
* 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 java.util.Collection;
import java.util.TreeSet;
import javax.swing.ComboBoxModel;
import javax.swing.ListCellRenderer;
import org.netbeans.api.java.classpath.ClassPath;
import org.netbeans.api.java.project.JavaProjectConstants;
import org.netbeans.api.project.Project;
import org.netbeans.spi.java.project.support.ui.PackageView;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
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 PackageUtils {

    public static DataObject getSelectedPackage() {
        return getSelectedPackage(Utilities.actionsGlobalContext());
    }

    public static DataObject getSelectedPackage(final Lookup context) {
        Lookup.Result<DataObject> packageLookup = context.lookupResult(DataObject.class);
        return (packageLookup.allInstances().isEmpty()) ? null : packageLookup.allInstances().iterator().next();
    }

    public static boolean containsPackage(ClassPath classPath, String fullPackageName) {
        for (FileObject cpRoot : classPath.getRoots()) {
            FileObject fo = FileObjectUtils.getPackageFileObject(cpRoot, fullPackageName);
            if (fo != null) {
                return true;
            }
        }
        return false;
    }

    public static Collection<JavaPackage> getPackages(final ClassPath classPath, final boolean includeRootPackage) {
        Collection<JavaPackage> packages = new TreeSet<JavaPackage>();
        for (FileObject root : classPath.getRoots()) {
            addPackages(classPath, root, includeRootPackage, packages);
//            JavaPackage pack = new JavaPackage(classPath, root);
//            if ((pack.isRoot() && includeRootPackage) || !pack.hasSubPackage() || pack.hasFileChildren()) {
//                packages.add(pack);
//            }
//            for (FileObject subPkg : pack.getChildren()) {
//                addPackages(classPath, subPkg, packages);
//            }
        }
        return packages;
    }

    private static void addPackages(final ClassPath classPath,
                                    final FileObject rootPackage,
                                    final boolean includeRootPackage,
                                    final Collection<JavaPackage> packCollection) {
        if (FileObjectUtils.isValidDir(rootPackage)) {
            final JavaPackage pack = new JavaPackage(classPath, rootPackage);
            if ((pack.isRoot() && includeRootPackage) || !pack.hasSubPackage() || pack.hasFileChildren()) {
                packCollection.add(pack);
            }
            for (FileObject subPkg : rootPackage.getChildren()) {
                addPackages(classPath, subPkg, includeRootPackage, packCollection);
            }
        }
    }

    public static ComboBoxModel getPackageComboModel(Project project) {
        return PackageView.createListView((org.netbeans.api.project.ProjectUtils.getSources(project)).getSourceGroups(
                JavaProjectConstants.SOURCES_TYPE_JAVA)[0]);
    }

    public static ListCellRenderer getPackageComboRenderer() {
        return PackageView.listRenderer();
    }
}
TOP

Related Classes of br.msf.commons.netbeans.util.PackageUtils

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.