Java Code Examples for org.netbeans.api.project.ProjectUtils#getSources()

The following examples show how to use org.netbeans.api.project.ProjectUtils#getSources() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClientDataObject.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void init(FileObject fo, ClientDataLoader loader) {
    // added ValidateXMLCookie
    InputSource in = DataObjectAdapters.inputSource(this);
    CheckXMLCookie checkCookie = new CheckXMLSupport(in);
    getCookieSet().add(checkCookie);
    ValidateXMLCookie validateCookie = new ValidateXMLSupport(in);
    getCookieSet().add(validateCookie);
    
    Project project = FileOwnerQuery.getOwner(getPrimaryFile());
    if (project != null) {
        Sources sources = ProjectUtils.getSources(project);
        sources.addChangeListener(this);
    }
    refreshSourceFolders();
    addPropertyChangeListener(this);
}
 
Example 2
Source File: ProjectMenuItem.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private Set<VCSFileProxy> getRootFilesForProjectNodes (Node[] nodes) {
    Set<VCSFileProxy> rootFiles = new HashSet<VCSFileProxy>(nodes.length);
    for (int i = 0; i < nodes.length; i++) {
        Node node = nodes[i];
        Project project =  node.getLookup().lookup(Project.class);
        if (project != null) {
            Sources sources = ProjectUtils.getSources(project);
            SourceGroup[] sourceGroups = sources.getSourceGroups(Sources.TYPE_GENERIC);
            for (int j = 0; j < sourceGroups.length; j++) {
                SourceGroup sourceGroup = sourceGroups[j];
                FileObject srcRootFo = sourceGroup.getRootFolder();
                VCSFileProxy rootFile = VCSFileProxy.createFileProxy(srcRootFo);
                if (rootFile == null) {
                    continue;
                }
                rootFiles.add(rootFile);
            }
            continue;
        }
    }
    return rootFiles;
}
 
Example 3
Source File: GenerateCodeDialog.java    From jeddict with Apache License 2.0 6 votes vote down vote up
private void populateSourceFolderCombo(ProjectInfo projectInfo) {
    ArrayList<SourceGroup> srcRoots = new ArrayList<>();
    int index = 0;
    FileObject sfo = projectInfo.getSourceGroup() != null ? projectInfo.getSourceGroup().getRootFolder() : null;
    if (projectInfo.getProject() != null) {
        Sources sources = ProjectUtils.getSources(projectInfo.getProject());
        if (sources != null) {
            SourceGroup[] srcGrps = sources.getSourceGroups(SOURCES_TYPE_JAVA);
            if (srcGrps != null) {
                for (SourceGroup srcGrp : srcGrps) {
                    if (srcGrp != null) {
                        srcRoots.add(srcGrp);
                        if (srcGrp.getRootFolder() != null && srcGrp.getRootFolder().equals(sfo)) {
                            index = srcRoots.size() - 1;
                        }
                    }
                }
            }
        }
    }

    if (srcRoots.size() > 0) {
        projectInfo.setSourceGroup(srcRoots.get(index));
    }
}
 
Example 4
Source File: DefaultPlugin.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Finds a Java source group the given file belongs to.
 * 
 * @param  file  {@literal FileObject} to find a {@literal SourceGroup} for
 * @return  the found {@literal SourceGroup}, or {@literal null} if the given
 *          file does not belong to any Java source group
 */
private static SourceGroup findSourceGroup(FileObject file) {
    final Project project = FileOwnerQuery.getOwner(file);
    if (project == null) {
        return null;
    }

    Sources src = ProjectUtils.getSources(project);
    SourceGroup[] srcGrps = src.getSourceGroups(SOURCES_TYPE_JAVA);
    for (SourceGroup srcGrp : srcGrps) {
        FileObject rootFolder = srcGrp.getRootFolder();
        if (((file == rootFolder) || FileUtil.isParentOf(rootFolder, file)) 
                && srcGrp.contains(file)) {
            return srcGrp;
        }
    }
    return null;
}
 
Example 5
Source File: ResourceLibraryIterator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private FileObject getNearestContractsParent(Project project) {
    WebModule wm = WebModule.getWebModule(project.getProjectDirectory());
    if (wm != null) {
        // web application
        projectType = ProjectType.WEB;
        if (wm.getDocumentBase() != null) {
            return wm.getDocumentBase();
        }
    } else {
        // j2se library
        projectType = ProjectType.J2SE;
        Sources sources = ProjectUtils.getSources(project);
        SourceGroup[] sourceGroups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
        for (SourceGroup sourceGroup : sourceGroups) {
            FileObject metaInf = sourceGroup.getRootFolder().getFileObject(META_INF);
            if (metaInf != null) {
                return metaInf;
            }
        }
        if (sourceGroups.length > 0) {
            return sourceGroups[0].getRootFolder();
        }
    }

    // fallback
    return project.getProjectDirectory();
}
 
Example 6
Source File: ReactRestControllerWizardIterator.java    From nb-springboot with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(WizardDescriptor wizard) {
    this.wizard = wizard;
    wizard.putProperty(WIZ_CRUD_METHODS, false);
    wizard.putProperty(WIZ_ERROR_HANDLING, 0);
    Project project = Templates.getProject(wizard);
    Sources src = ProjectUtils.getSources(project);
    SourceGroup[] groups = src.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
    panel = JavaTemplates.createPackageChooser(project, groups, new RestControllerWizardPanel1(), true);
    // force creation of visual part
    JComponent cmp = (JComponent) panel.getComponent();
    cmp.putClientProperty(WizardDescriptor.PROP_CONTENT_SELECTED_INDEX, 0);
    cmp.putClientProperty(WizardDescriptor.PROP_CONTENT_DATA, TemplateUtils.createSteps(wizard, new String[]{cmp.getName()}));
}
 
Example 7
Source File: MavenFileLocator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private ClassPath getProjectClasspath(Project p) {
    ClassPath result;
    ClassPathProvider cpp = p.getLookup().lookup(ClassPathProvider.class);
    Set<FileObject> roots = new HashSet<FileObject>();
    Sources sources = ProjectUtils.getSources(p);
    if (sources != null) {
        SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
        for (SourceGroup group : groups) {
            roots.add(group.getRootFolder());
        }
    }

    Set<ClassPath> setCP = new HashSet<ClassPath>();
    if (cpp != null) {
        for (FileObject file : roots) {
            ClassPath path = cpp.findClassPath(file, ClassPath.COMPILE);
            setCP.add(path);
        }
    }

    for (ClassPath cp : setCP) {
        FileObject[] rootsCP = cp.getRoots();
        for (FileObject fo : rootsCP) {
            FileObject[] aaa = SourceForBinaryQuery.findSourceRoots(fo.toURL()).getRoots();
            roots.addAll(Arrays.asList(aaa));
        }
    }
    JavaPlatform platform = p.getLookup().lookup(ActiveJ2SEPlatformProvider.class).getJavaPlatform();

    if (platform != null) {
        roots.addAll(Arrays.asList(platform.getSourceFolders().getRoots()));
    }
    result = ClassPathSupport.createClassPath(roots.toArray(new FileObject[roots.size()]));
    return result;
}
 
Example 8
Source File: Utils.java    From nb-springboot with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves the execute {@code ClassPath} object for the given project.
 *
 * @param proj the project
 * @return found ClassPath object or null
 */
public static ClassPath execClasspathForProj(Project proj) {
    Sources srcs = ProjectUtils.getSources(proj);
    SourceGroup[] srcGroups = srcs.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
    if (srcGroups.length > 0) {
        return ClassPath.getClassPath(srcGroups[0].getRootFolder(), ClassPath.EXECUTE);
    } else {
        logger.log(WARNING, "No sources found for project: {0}", new Object[]{proj.toString()});
    }
    return null;
}
 
Example 9
Source File: RunSeleniumAction.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@CheckForNull
private FileObject[] lookupSeleniumTestOnly(Lookup context) {
    Collection<? extends FileObject> fileObjects = context.lookupAll(FileObject.class);
    if (fileObjects.isEmpty()) {
        return null;
    }
    Project p = null;
    Iterator<? extends FileObject> iterator = fileObjects.iterator();
    while (iterator.hasNext()) {
        FileObject fo = iterator.next();
        Project project = FileOwnerQuery.getOwner(fo);
        if (project == null) {
            return null;
        }
        if(p == null) {
            p = project;
        }
        if(!p.equals(project)) { // selected FileObjects belong to different projects
            return null;
        }
        Sources sources = ProjectUtils.getSources(project);
        SourceGroup[] sourceGroups = sources.getSourceGroups(WebClientProjectConstants.SOURCES_TYPE_HTML5_TEST_SELENIUM);
        if (sourceGroups.length != 1) { // no Selenium Tests Folder set yet
            return null;
        }
        FileObject rootFolder = sourceGroups[0].getRootFolder();
        if (!FileUtil.isParentOf(rootFolder, fo)) { // file in not under Selenium Tests Folder
            return null;
        }
    }
    return fileObjects.toArray(new FileObject[fileObjects.size()]);
}
 
Example 10
Source File: Analyzer.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static Collection<? extends FileObject> toAnalyze(Lookup l) {
    Set<FileObject> result = new LinkedHashSet<FileObject>();

    for (FileObject fo : l.lookupAll(FileObject.class)) {
        if (fo.getMIMEType().equals(JAVA_MIME_TYPE)) {
            result.add(fo);
        }
        if (fo.isFolder()) {
            if (containsJavaFiles(fo)) {
                result.add(fo);
            }
        }
    }

    for (DataObject od : l.lookupAll(DataObject.class)) {
        FileObject primaryFile = od.getPrimaryFile();
        if (primaryFile.getMIMEType().equals(JAVA_MIME_TYPE)) {
            result.add(primaryFile);
        }
        if (primaryFile.isFolder()) {
            if (containsJavaFiles(primaryFile)) {
                result.add(primaryFile);
            }
        }
    }
    
    for (Project p : l.lookupAll(Project.class)) {
        Sources s = ProjectUtils.getSources(p);
        
        for (SourceGroup sg : s.getSourceGroups("java")) { // NOI18N
            result.add(sg.getRootFolder());
        }
    }
    
    return result;
}
 
Example 11
Source File: NewProjectIterator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void createProjectZip(OutputStream target, Project source) throws IOException {
    Sources srcs = ProjectUtils.getSources(source); // #63247: don't use lookup directly
    // assuming we got 1-sized array, should be enforced by UI.
    SourceGroup[] grps = srcs.getSourceGroups(Sources.TYPE_GENERIC);
    SourceGroup group = grps[0];
    Collection<FileObject> files = new ArrayList<FileObject>();
    collectFiles(group.getRootFolder(), files,
            SharabilityQuery.getSharability(group.getRootFolder()));
    createZipFile(target, group.getRootFolder(), files);
}
 
Example 12
Source File: TestAction.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 */
static SourceGroup getSourceGroup(FileObject file, Project prj) {
    Sources src = ProjectUtils.getSources(prj);
    SourceGroup[] srcGrps = src.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
    for (SourceGroup srcGrp : srcGrps) {
        FileObject rootFolder = srcGrp.getRootFolder();
        if (((file == rootFolder) || FileUtil.isParentOf(rootFolder, file)) 
                && srcGrp.contains(file)) {
            return srcGrp;
        }
    }
    return null;
}
 
Example 13
Source File: FreeformSourcesTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testSources() throws Exception {
    Sources s = ProjectUtils.getSources(simple);
    SourceGroup[] groups = s.getSourceGroups(Sources.TYPE_GENERIC);
    assertEquals("one generic group", 1, groups.length);
    assertEquals("right root folder", simple.getProjectDirectory(), groups[0].getRootFolder());
    assertEquals("right display name", "Simple Freeform Project", groups[0].getDisplayName());
    groups = s.getSourceGroups("java");
    assertEquals("two Java groups", 2, groups.length);
    assertEquals("right root folder #1", simple.getProjectDirectory().getFileObject("src"), groups[0].getRootFolder());
    assertEquals("right display name #1", "Main Sources", groups[0].getDisplayName());
    assertEquals("right root folder #2", simple.getProjectDirectory().getFileObject("antsrc"), groups[1].getRootFolder());
    assertEquals("right display name #2", "Ant Task Sources", groups[1].getDisplayName());
}
 
Example 14
Source File: FacesConfigIterator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static ClassPath getCompileClasspath(Project project) {
    ClassPathProvider cpp = project.getLookup().lookup(ClassPathProvider.class);
    Sources sources = ProjectUtils.getSources(project);
    if (sources == null) {
        return null;
    }

    SourceGroup[] sourceGroups = sources.getSourceGroups("java"); //NOII18N
    if (sourceGroups.length > 0) {
        return cpp.findClassPath(sourceGroups[0].getRootFolder(), ClassPath.COMPILE);
    }
    return null;
}
 
Example 15
Source File: ProjectsRootNode.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void setProjectFiles(Project project) {
    Sources sources = ProjectUtils.getSources(project);  // returns singleton
    if (sourcesListener == null) {
        sourcesListener = WeakListeners.change(this, sources);
        sources.addChangeListener(sourcesListener);
    }
    setGroups(Arrays.asList(sources.getSourceGroups(Sources.TYPE_GENERIC)), project.getProjectDirectory());
}
 
Example 16
Source File: EntityWizardDescriptor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isValid() {
    // XXX add the following checks
    // p.getName = valid NmToken
    // p.getName not already in module
    if (wizardDescriptor == null) {
        return true;
    }
    
    Sources sources=ProjectUtils.getSources(project);
    SourceGroup groups[]=sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
    if(groups == null || groups.length == 0) {
        wizardDescriptor.putProperty(WizardDescriptor.PROP_ERROR_MESSAGE,
                NbBundle.getMessage(EntityWizardDescriptor.class,"ERR_JavaSourceGroup")); //NOI18N
        return false;
    }
    
    
    if (SourceLevelChecker.isSourceLevel14orLower(project)) {
        wizardDescriptor.putProperty(WizardDescriptor.PROP_ERROR_MESSAGE,
                NbBundle.getMessage(EntityWizardDescriptor.class, "ERR_NeedProperSourceLevel")); // NOI18N
        return false;
    }
    if (p.getPrimaryKeyClassName().trim().equals("")) { //NOI18N
        wizardDescriptor.putProperty(WizardDescriptor.PROP_ERROR_MESSAGE, NbBundle.getMessage(EntityWizardDescriptor.class,"ERR_PrimaryKeyNotEmpty")); //NOI18N
        return false;
    }
    
    wizardDescriptor.putProperty(WizardDescriptor.PROP_ERROR_MESSAGE, " "); //NOI18N
    return true;
}
 
Example 17
Source File: AbstractGroovyActionProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private String[] getTargetNames(String command, Lookup context, Properties p) {
    if (supportedActions.keySet().contains(command)) {
        if (command.equals(COMMAND_TEST)) {
            return setupTestAll(p);
        }

        FileObject[] testSources = findTestSources(context);
        if (testSources != null) {
            if (command.equals(COMMAND_RUN_SINGLE) || command.equals(COMMAND_TEST_SINGLE)) {
                 return setupTestSingle(p, testSources);
            } else if (command.equals(COMMAND_DEBUG_SINGLE) || (command.equals(COMMAND_DEBUG_TEST_SINGLE))) {
                return setupDebugTestSingle(p, testSources);
            } else if (command.equals(COMMAND_COMPILE_SINGLE)) {
                return setupCompileSingle(p, testSources);
            }
        } else {
            FileObject file = findSources(context)[0];
            Sources sources = ProjectUtils.getSources(project);
            SourceGroup[] sourceGroups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
            String clazz = FileUtil.getRelativePath(getRoot(sourceGroups, file), file);
            p.setProperty("javac.includes", clazz); // NOI18N
            // Convert foo/FooTest.java -> foo.FooTest
            if (clazz.endsWith(".groovy")) { // NOI18N
                clazz = clazz.substring(0, clazz.length() - 7);
            }
            clazz = clazz.replace('/','.');

            String[] targets = loadTargetsFromConfig().get(command);
            if (command.equals(COMMAND_RUN_SINGLE)) {
                p.setProperty("run.class", clazz); // NOI18N
            } else if (command.equals(COMMAND_DEBUG_SINGLE)) {
                p.setProperty("debug.class", clazz); // NOI18N
            } else if (command.equals(COMMAND_COMPILE_SINGLE)) {
                p.setProperty("compile.class", clazz); // NOI18N
            }
            return getTargetNamesForCommand(targets, command);
        }
    }
    return new String[0];
}
 
Example 18
Source File: HibernateWebModuleExtender.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Set<FileObject> extend(WebModule webModule) {
    Project enclosingProject = Util.getEnclosingProjectFromWebModule(webModule);

    // when there is no enclosing project found empty set is returned
    if (enclosingProject!=null) {
        Sources sources = ProjectUtils.getSources(enclosingProject);
        try {
            SourceGroup[] javaSourceGroup = sources.getSourceGroups(
                    JavaProjectConstants.SOURCES_TYPE_RESOURCES);
            if (javaSourceGroup == null || javaSourceGroup.length == 0) {
                javaSourceGroup = sources.getSourceGroups(
                        JavaProjectConstants.SOURCES_TYPE_JAVA);
            }
            if (javaSourceGroup != null && javaSourceGroup.length != 0) {
                FileObject targetFolder = javaSourceGroup[0].getRootFolder();
                CreateHibernateConfiguration createHibernateConfiguration =
                        new CreateHibernateConfiguration(targetFolder, enclosingProject);
                targetFolder.getFileSystem().runAtomicAction(createHibernateConfiguration);

                return createHibernateConfiguration.getCreatedFiles();
            }
        } catch (Exception ex) {
            Exceptions.printStackTrace(ex);
        }
    }
    return Collections.EMPTY_SET;
}
 
Example 19
Source File: WebProjectClassPathModifierTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testRemoveRoots() throws Exception {
    File f = new File(getDataDir().getAbsolutePath(), "projects/WebApplication1");
    FileObject projdir = FileUtil.toFileObject(f);
    WebProject webProject = (WebProject) ProjectManager.getDefault().findProject(projdir);
    
    Sources sources = ProjectUtils.getSources(webProject);
    SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
    FileObject srcJava = webProject.getSourceRoots().getRoots()[0];
    assertEquals("We should edit sources", "${src.dir}", groups[0].getName());
    String classPathProperty = webProject.getClassPathProvider().getPropertyName(groups[0], ClassPath.COMPILE)[0];
    
    AntProjectHelper helper = webProject.getAntProjectHelper();
    
    // create src folder
    final String srcFolder = "srcFolder";
    File folder = new File(getDataDir().getAbsolutePath(), srcFolder);
    if (folder.exists()) {
        folder.delete();
    }
    FileUtil.createFolder(folder);
    URL[] cpRoots = new URL[]{folder.toURL()};
    
    // init
    EditableProperties props = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
    String cpProperty = props.getProperty(classPathProperty);
    boolean alreadyOnCp = cpProperty.indexOf(srcFolder) != -1;
    //assertFalse("srcFolder should not be on cp", alreadyInCp);
    
    // add
    boolean addRoots = ProjectClassPathModifier.addRoots(cpRoots, srcJava, ClassPath.COMPILE);
    // we do not check this - it can be already on cp (tests are created only before the 1st test starts)
    if (!alreadyOnCp) {
        assertTrue(addRoots);
    }
    props = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
    cpProperty = props.getProperty(classPathProperty);
    assertTrue("srcFolder should be on cp", cpProperty.indexOf(srcFolder) != -1);
    
    // simulate #113390
    folder.delete();
    assertFalse("srcFolder should not exist.", folder.exists());
    
    // remove
    boolean removeRoots = ProjectClassPathModifier.removeRoots(cpRoots, srcJava, ClassPath.COMPILE);
    assertTrue(removeRoots);
    props = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
    cpProperty = props.getProperty(classPathProperty);
    assertTrue("srcFolder should not be on cp", cpProperty.indexOf(srcFolder) == -1);
}
 
Example 20
Source File: CreateElement.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Gets the possible sourceGroups based on the current file.
 * <ul>
 * <li>If it is a file from src/main/java it will return only
 * src/main/java.</li>
 * <li>If it is a file from src/test/java it will return src/main/java AND
 * src/test/java. (src/test/java will have a higher prio than src/main/java)</li>
 * </ul>
 *
 * @param fileObject
 * @return map of sourceGroup and its hint-priority
 */
private static Map<SourceGroup, Integer> getPossibleSourceGroups(FileObject fileObject) {
    Boolean isInTestSources = isInTestSources(fileObject);
    if (null == isInTestSources) {
        return Collections.emptyMap();
    }

    Project p = FileOwnerQuery.getOwner(fileObject);
    if (null == p) {
        return Collections.emptyMap();
    }
    
    Sources src = ProjectUtils.getSources(p);
    SourceGroup[] sGroups = src.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
    
    SourceGroup sourceGroup = null;
    
    Set<FileObject> testRoots = new HashSet<>();
    SourceGroup linkedSources = null;
    
    for (SourceGroup sg : sGroups) {
        URL[] urls = UnitTestForSourceQuery.findUnitTests(sg.getRootFolder());
        for (URL u : urls) {
            FileObject r = URLMapper.findFileObject(u);
            if (r != null) {
                if (testRoots.add(r)) {
                    if (FileUtil.isParentOf(r, fileObject)) {
                        isInTestSources = true;
                        linkedSources = sg;
                    }
                }
            }
        }
        if (FileUtil.isParentOf(sg.getRootFolder(), fileObject)) {
            sourceGroup = sg;
        }
    }
    

    Map<SourceGroup, Integer> list = new HashMap<>();
    if (isInTestSources) {
        //in test sources (f.e. src/test/java) -> return main sources and test sources
        if (null != linkedSources) {
            list.put(linkedSources, PRIO_MAINSOURCEGROUP);
        }

        if (null != sourceGroup) {
            //test source group has a higher prio -> before main source group
            list.put(sourceGroup, PRIO_TESTSOURCEGROUP);
        }

    } else {
        //in sources (f.e. src/main/java) -> return only main sources
        if (null != sourceGroup) {
            list.put(sourceGroup, PRIO_MAINSOURCEGROUP);
        }
    }
    return list;
}