NetBeans Platform File Type Tutorial Using Maven

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.

Contents

Content on this page applies to NetBeans IDE 7.1

To follow this tutorial, you need the software and resources listed in the following table.

Software or Resource Version Required
NetBeans IDE version 7.1
Java Developer Kit (JDK) version 7 or above

Note: 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.

  1. Open the New Project wizard and choose NetBeans Module in the Maven category:

    Module wizard 1

    Click Next.

  2. Type AbcFileTypeSupport in Project Name and specify a location for storing the project:

    Module wizard 2

    Click Next. Specify the NetBeans API version you want to use. Click Finish.

When you click Finish, the IDE creates the AbcFileTypeSupport project:

Module wizard 4

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.

  1. Right-click the project node in the Projects window and choose New > File Type. Click Next.

  2. In the File Recognition panel, type text/x-abc for the MIME Type and .abc .ABC for the Filename Extension:

    Module wizard 1

    Click Next.

  3. 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.png16x16 icon image ) to your system and select the image after clicking Browse.

    Module wizard 1

    Click Finish.

When you click Finish, the IDE creates the files shown below:

Module wizard 1

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.

  1. Right-click the project node and choose Build.

  2. 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:

    Module wizard 1

    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.

  3. 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.

    Module wizard 1

    Click the "Visual" tab and you will see the GUI panel created by the New File Type wizard:

    Module wizard 1

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.

  1. 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:

    Module wizard 1

    Click Next.

  2. 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:

    Module wizard 1

    Click Next.

  3. Type MyAction as the Class Name and My Action as the Display Name:

    Module wizard 1

    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
        }
        
    }
  4. 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);
    }
  5. 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);
        }
        
    }
  6. 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.

    Module wizard 1

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, dev@platform.netbeans.org, or view the NetBeans Platform mailing list archive.

Project Features

Project Links

About this Project

Platform was started in November 2009, is owned by Antonin Nebuzelsky, and has 138 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20140418.2d69abc). © 2013, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
 
 
Close
loading
Please Confirm
Close