X

Geertjan's Blog

  • June 22, 2013

How to Open Any Folder as a Project in the NetBeans Platform

Geertjan Wielenga
Product Manager

Typically, as described in the NetBeans Project Type Tutorial, you'll define a project type based on the presence of a file (e.g., "project.xml" or "customer.txt" or something like that) in a folder. I.e., if the file is there, then its parent, i.e., the folder that contains the file, is a project and should be opened in your application.

However, in some scenarios (as with the HTML5 project type introduced in NetBeans IDE 7.3), the user should be able to open absolutely any folder at all into the application. How to create a project type that is that liberal?

Here you go, the only condition that needs to be true is that the selected item in the "Open Project" dialog is a folder, as defined in the "isProject" method below. Nothing else. That's it. If you select a folder, it will be opened in your application, displaying absolutely everything as-is (since below there's no ProjectLogicalView defined):

import java.beans.PropertyChangeListener;
import java.io.IOException;
import javax.swing.Icon;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectInformation;
import org.netbeans.spi.project.ProjectFactory;
import org.netbeans.spi.project.ProjectState;
import org.openide.filesystems.FileObject;
import org.openide.loaders.DataFolder;
import org.openide.loaders.DataObjectNotFoundException;
import org.openide.nodes.FilterNode;
import org.openide.util.Exceptions;
import org.openide.util.ImageUtilities;
import org.openide.util.Lookup;
import org.openide.util.lookup.Lookups;
import org.openide.util.lookup.ServiceProvider;
@ServiceProvider(service = ProjectFactory.class)
public class FolderProjectFactory implements ProjectFactory {
@Override
public boolean isProject(FileObject projectDirectory) {
return DataFolder.findFolder(projectDirectory) != null;
}
@Override
public Project loadProject(FileObject dir, ProjectState state) throws IOException {
return isProject(dir) ? new FolderProject(dir) : null;
}
@Override
public void saveProject(Project prjct) throws IOException, ClassCastException {
// leave unimplemented for the moment
}
private class FolderProject implements Project {
private final FileObject projectDir;
private Lookup lkp;
private FolderProject(FileObject dir) {
this.projectDir = dir;
}
@Override
public FileObject getProjectDirectory() {
return projectDir;
}
@Override
public Lookup getLookup() {
if (lkp == null) {
lkp = Lookups.fixed(new Object[]{
new Info(),
});
}
return lkp;
}
private final class Info implements ProjectInformation {
@Override
public Icon getIcon() {
Icon icon = null;
try {
icon = ImageUtilities.image2Icon(
new FilterNode(DataFolder.find(
getProjectDirectory()).getNodeDelegate()).getIcon(1));
} catch (DataObjectNotFoundException ex) {
Exceptions.printStackTrace(ex);
}
return icon;
}
@Override
public String getName() {
return getProjectDirectory().getName();
}
@Override
public String getDisplayName() {
return getName();
}
@Override
public void addPropertyChangeListener(PropertyChangeListener pcl) {
//do nothing, won't change
}
@Override
public void removePropertyChangeListener(PropertyChangeListener pcl) {
//do nothing, won't change
}
@Override
public Project getProject() {
return FolderProject.this;
}
}
}
}

Even the ProjectInformation implementation really isn't needed at all, since it provides nothing more than the icon in the "Open Project" dialog, the rest (i.e., the display name in the "Open Project" dialog) is provided by default regardless of whether you have a ProjectInformation implementation or not.

Join the discussion

Comments ( 8 )
  • Tim Boudreau Saturday, June 22, 2013

    There's a little more to it to make something with a good user experience - the code above makes the IDE recognize *absolutely every* folder as a project.

    That's going to break functionality like Ctrl-Shift-1 to show a file in the Projects tab, and probably refactoring as well, for all projects in the IDE - anything that relies on finding the project that owns a Java source file in order to do something will find one of these projects instead.


  • Geertjan Saturday, June 22, 2013

    This is not for usage in the context of NetBeans IDE. This is for any project that uses the NetBeans Platform as a basis that would like to be able to open projects, i.e., you're not going to have Java source files, and refactoring, and Ctrl-Shift-1, in that scenario to worry about.


  • guest Friday, October 10, 2014

    Hi, I am new to NetBeans IDE. I want to import a Fortran project into NetBeans 8.0.1. The project was developed in Fortran 2008. I have tried to import all the files as zip but it did not import. Then I manually added all the files and folders in the project. When I compile it, its shows error that "Can't open module file 'moduledefs.mod' for reading at (1): No such file or directory nbproject"

    Is there any solution?


  • Geertjan Friday, October 10, 2014

    Can you point to the URL where it says NetBeans supports Fortran?


  • Tamil Vendhan Kanagaraju Wednesday, October 29, 2014

    Hi,

    I want to use your above code. I am new to NetBeans. Could you tell me how to use to above code in NetBeans to make it work?

    Thanks!


  • Geertjan Wednesday, October 29, 2014

    This is a pretty old blog entry. In NetBeans IDE 8 or 8.0.1, simply use the "HTML5 Application" project type in NetBeans IDE -- it does the same thing as the above.


  • guest Wednesday, January 4, 2017

    I did copy a netbeans java project from computer to pen drive now tell me how ta copy it to desired windows8 computer


  • guest Wednesday, March 29, 2017

    Funny thing is NetBeans IDE 8.2 cannot find the packages

    package org.netbeans.api.project does not exist

    Any clues?


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.