NetBeans Platform File Type Tutorial Using Maven
This tutorial needs a review. You can edit it in GitHub following these contribution guidelines. |
This document demonstrates how to create a NetBeans module from a Maven archetype and build and install the module in NetBeans IDE or any other application on the NetBeans Platform. In this tutorial, you will create a module project that when installed enables you to recognize a new type of file in your projects. The module also creates a menu action that is available when the user right-click a file conforming to the new file type.
This document is based on the Ant-based NetBeans File Type Tutorial and can be used to learn some of the differences between using Ant and Maven to develop NetBeans modules. After you understand the differences, you can easily proceed through other tutorials on the NetBeans Platform Learning Trail.
You do not need to download Maven because it is bundled with the IDE. Optionally, use your own download of Maven, in which case use the Options window to configure your Maven settings. |
Before starting this tutorial you may want to familiarize yourself with the following introductory Maven documentation:
Creating the Module Project
In this section you create a NetBeans module project from a Maven archetype.
-
Open the New Project wizard and choose NetBeans Module in the Maven category:
Click Next.
-
Type AbcFileTypeSupport in Project Name and specify a location for storing the project:
Click Next. Specify the NetBeans API version you want to use. Click Finish.
When you click Finish, the IDE creates the AbcFileTypeSupport project:
If you look in the POM, shown above, you can see that Maven will use the maven-jar-plugin
to build the JAR and the nbm-maven-plugin
will package the JAR as a NetBeans Module ( nbm
).
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>AbcFileTypeSupport</artifactId>
<version>1.0-SNAPSHOT</version>
*<packaging>nbm</packaging>*
<name>AbcFileTypeSupport</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<!--
Repository hosting NetBeans modules, especially APIs.
Versions are based on IDE releases, e.g.: RELEASE691
To create your own repository, use: nbm:populate-repository
-->
<repository>
<id>netbeans</id>
<name>NetBeans</name>
<url>http://bits.netbeans.org/maven2/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-api-annotations-common</artifactId>
<version>RELEASE71-BETA</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
*<artifactId>nbm-maven-plugin</artifactId>*
<version>3.6</version>
<extensions>true</extensions>
</plugin>
<plugin>
<!-- NetBeans 6.9+ requires JDK 6 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
*<artifactId>maven-jar-plugin</artifactId>*
<version>2.3.1</version>
<configuration>
<!-- to have the jar plugin pickup the nbm generated manifest -->
<useDefaultManifestFile>true</useDefaultManifestFile>
</configuration>
</plugin>
</plugins>
</build>
Recognizing the File Type
In this section you will use the New File Type wizard to create the files for recognizing a new file type named .abc
.
-
Right-click the project node in the Projects window and choose New > File Type. Click Next.
-
In the File Recognition panel, type text/x-abc for the MIME Type and .abc .ABC for the Filename Extension:
Click Next.
-
Type Abc as the Class Name Prefix. Click Browse and select a 16x16 pixel image file as the new file type’s icon.
If you don’t have an image available, just save this image abc16.png
() to your system and select the image after clicking Browse.
Click Finish.
When you click Finish, the IDE creates the files shown below:
For more details on the files that the IDE created, see What is a DataObject and the section on Recognizing Abc Files in the Ant-Based File Type Integration Tutorial.
Building and Running the Project
To install the module, you first need to build and run it.
-
Right-click the project node and choose Build.
-
Right-click the project node and choose Run. The IDE launches with the new module installed. To confirm that the new module is working correctly, create a new project and then use the New File wizard to create an
abc
file:
For example, you can create a simple Java application and then open the New File wizard and choose the Empty Abc file type in the Other category.
When you create the new file, specify a source package if you want to see the file in the Projects window. By default the wizard for the new file type will create the file at the root level of the project.
-
After you create the new abc file you can see that the file is displayed in the Projects window with the icon for the file type. If you open the file in the editor you can see that the contents of the new file were generated from the file template.
Click the "Visual" tab and you will see the GUI panel created by the New File Type wizard:
You can see that your file type is now recognized by the application. In the next section, we’ll create a new action for our file type, which will be available when the user right-click on a file conforming to the type in the Projects window.
Adding an Action for the File Type
In this section you will add an action that can be invoked from the popup menu when the user right-clicks the node of a file conforming to your new file type.
-
Right-click the AbcFileTypeSupport project and choose New > Action. In the Action Type panel, select Conditionally Enabled and type AbcDataObject for the Cookie Class, which specifies the type that needs to be in the Lookup for the Action to be enabled:
Click Next.
-
Select Edit in the Category drop-down list and deselect Global Menu Item. Select File Type Context Menu Item and select text/x-abc in the Content Type drop-down list:
Click Next.
-
Type MyAction as the Class Name and My Action as the Display Name:
Click Finish.
When you click Finish, MyAction.java
is created in the com.mycompany.abcfiletype
source package:
package com.mycompany.abcfiletypesupport;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionRegistration;
import org.openide.util.NbBundle.Messages;
@ActionID(category = "Edit",
id = "com.mycompany.abcfiletypesupport.MyAction")
@ActionRegistration(displayName = "#CTL_MyAction")
@ActionReferences({
@ActionReference(path = "Loaders/text/x-abc/Actions", position = 0)
})
@Messages("CTL_MyAction=My Action")
public final class MyAction implements ActionListener {
private final AbcDataObject context;
public MyAction(AbcDataObject context) {
this.context = context;
}
public void actionPerformed(ActionEvent ev) {
// TODO use context
}
}
-
Let’s now make the Action a bit more meaningful. We’ll add some code for the action. In this example you will add some code that uses
DialogDisplayer
to open a dialog box when the action is invoked from the popup menu.
Modify the actionPerformed(ActionEvent ev)
method in MyAction.java
to open a dialog when My Action is invoked.
@Override
public void actionPerformed(ActionEvent ev) {
*FileObject f = context.getPrimaryFile();
String displayName = FileUtil.getFileDisplayName(f);
String msg = "This file is " + displayName + ".";
NotifyDescriptor nd = new NotifyDescriptor.Message(msg);
DialogDisplayer.getDefault().notify(nd);*
}
-
Fix the missing import statements (Ctrl-Shift-I) and then confirm that your import statements are as shown below:
package com.mycompany.abcfiletypesupport;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionRegistration;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.util.NbBundle.Messages;
@ActionID(category = "Edit",
id = "com.mycompany.abcfiletypesupport.MyAction")
@ActionRegistration(displayName = "#CTL_MyAction")
@ActionReferences({
@ActionReference(path = "Loaders/text/x-abc/Actions", position = 0)
})
@Messages("CTL_MyAction=My Action")
public final class MyAction implements ActionListener {
private final AbcDataObject context;
public MyAction(AbcDataObject context) {
this.context = context;
}
@Override
public void actionPerformed(ActionEvent ev) {
FileObject f = context.getPrimaryFile();
String displayName = FileUtil.getFileDisplayName(f);
String msg = "This file is " + displayName + ".";
NotifyDescriptor nd = new NotifyDescriptor.Message(msg);
DialogDisplayer.getDefault().notify(nd);
}
}
-
You can now try out the module to confirm that the new action works correctly.
Note. To run the module you will first need to clean and build the module.
When you right-click on a node of the abc
file type you will see that My Action is one of the items in the popup menu.
This tutorial demonstrated how to create and run a NetBeans module that you created from a Maven Archetype. You also learned a little about how to work with file types, but for more details you should look at the Ant-Based NetBeans File Type Tutorial. For more examples on how to build NetBeans Platform applications and modules, see the tutorials listed in the NetBeans Platform Learning Trail.
See Also
For more information about creating and developing applications, see the following resources.
If you have any questions about the NetBeans Platform, feel free to write to the mailing list, users@netbeans.apache.org, or view the NetBeans Platform mailing list archive.