OpenProjectsProgramaticallyInNetBeansIDE

Tutorial | Open Projects Programatically in NetBeans

Doc Contributed by Kunal Modi

Contents

Introduction

Recently I was working on a NetBeans IDE module and had to open a NetBeans project programmatically. I looked up in the Projects API to find out. The exploring of the API was a fruitful one and I would like to share it with you.

Requirements

  1. Basic knowledge of Plugin Development, here's the quick start guide.
  2. Idea of NetBeans API's, Glossary etc.

What to expect at the end of the tutorial

A ToolBar button when pressed allows you to open projects in NetBeans.

Lets Start

There are two API which are related to the NetBeans Projects.

  1. Project API
  2. Project UI API

We will start with the Project API first because it was here where I found my answer. In order to understand well let up simulate the "Open Projects" Action in NetBeans IDE. At the end of this tutorial we should be able to have a Global button in the toolbar to open project(similar to the Open Project(cntrl + shift + O) Action). The process will help us understand how NetBeans Projects are Opened in the Window.

Creating the Module

  • We will create a new Module called "OpenProject".
  • To create a module refer to my previous tutorial where I have explained in detail on how to create a new Module.

Creating the Action

  • In order to simulate the "Open Project" button, let us now create an Action.
  • Right click the project node and select Action.
  • We will make an "Alwaya Enabled" action. So click next.

File:scr1_OpenProjectsProgramaticallyInNetBeansIDE.JPG

  • Keep the category as "File". We will just have a button in the Globar Toolbar. So check Global Toolbar Button and keep the category as Edit(Edit because we don't want to create a misunderstanding by keeping it in File category as there already exists the Open Project button in the File toolbar). Click Next.

File:scr2_OpenProjectsProgramaticallyInNetBeansIDE.JPG

  • Give the class name as "MyOpenProject", action name as "MyOpenProject" and a url to a 16X16 gif/png icon file.File:open_OpenProjectsProgramaticallyInNetBeansIDE.gif Click Finish.

File:scr3_OpenProjectsProgramaticallyInNetBeansIDE.JPG

  • A class called "MyOpenProject" will be automatically generated and the action will be registered in the XML layer.

Adding Dependencies

  • Since we will be using the Project API dependency, we need to add it to the module dependency library. To do so
  • Right Click the project node.
  • Go to properties
  • Go to Libraries
  • Select Add. One after the other add the following dependencies
  • Project API
  • Project UI API
  • File System API
  • The dependencies should look as follows.

File:scr4_OpenProjectsProgramaticallyInNetBeansIDE.JPG

  • Please Note: There is a shorter way to add Module Dependency. Right click on the library node under the project node and select "Add Module Dependency". Type in the module/classname you are looking for. Choose the appropriate one from the search results and click "Ok".

Code Content

  • Now we are set to write the performAction function. It will look like this.
public void performAction() {
                  try {
                  JFileChooser projectChooser = new JFileChooser();
                  projectChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                  projectChooser.showOpenDialog(null);
                  File projectToBeOpenedFile = projectChooser.getSelectedFile(); 
                  FileObject projectToBeOpened = FileUtil.toFileObject(projectToBeOpenedFile);
                  Project project = ProjectManager.getDefault().findProject(projectToBeOpened);
                  Project[[ | ]] array = new Project[1];
                  array[0] = project;
                  OpenProjects.getDefault().open(array, false);

                  } catch (IOException ex) {
                  Exceptions.printStackTrace(ex);
                  }
                  }

Code Explaination

  • Let us go through the code quickly.
JFileChooser projectChooser = new JFileChooser(); 
projectChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 
projectChooser.showOpenDialog(null);
  • This part of the code is a nomal JFileChosser where we restrict our selection to directories only.
File projectToBeOpenedFile = projectChooser.getSelectedFile(); 
FileObject projectToBeOpened = FileUtil.toFileObject(projectToBeOpenedFile);

The projectChooser now returns the java.io.File object of the selected project. This object is then converted to the NetBeans FileSystem by using the FileUtil class of the FileSystem API. This conversion returns a FileObject which the NetBeans equivalent of a file representation.

  • Now comes the crucial part of the code.
Project project = ProjectManager.getDefault().findProject(projectToBeOpened); 
Project[[ | ]] array = new Project[[1 | 1]]; array[0] = project; 
OpenProjects.getDefault().open(array, false);
  • We use the default instance of the ProjectManager class and use its
    findProject(org.openide.filesystems.fileobject)
    to find the project folder. This method returns a org.netbeans.api.project.Project. NetBeans uses this as an identifier for all of its projects. Now that we have the Project handle with us, we need to open it inside the NetBeans window. We can do this using the OpenProjects class(which belongs to the Project UI API). We get its default instance and use its {open(Project[] projects,boolean openSubprojects)} to open the project folder. But we notice here that the first argument takes an array of Project. So we create a Project array called array of size 1. We assign array[0] = project and pass array as the first parameter. The boolean value here asks if the user want to wants to open subprojects or not. We pass false here. So only the main project will be opened.
  • Now Run the module. You will be able to see your custom icon displaying "MyOpenProject".

File:scr5_OpenProjectsProgramaticallyInNetBeansIDE.JPG

  • Click it and a File Chooser will appear.
  • Select a project to open.

File:scr6_OpenProjectsProgramaticallyInNetBeansIDE.JPG

  • The project appears in the Project window.

File:scr7_OpenProjectsProgramaticallyInNetBeansIDE.JPG

Please Note

A lot more of Project related information can be obtained using these APIs. This tutorial just gives a basic start to explore these APIs further. Any remarks/suggestions/comments are welcome.

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo