Maven NBM Quickstart 6.9

Revision as of 09:42, 8 July 2010 by Kganfield (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)

This document is a draft. The current version of this document is NetBeans Platform Quick Start Using Maven

This document demonstrates how to create a NetBeans Platform Application using Maven and NetBeans IDE 6.9. This document is based on NetBeans Platform Quick Start

Contents


Requirements for This tutorial

You need the following software:

  • NetBeans IDE 6.9

Prerequisites

This document assumes you have some basic knowledge of, or programming experience with, the following technologies:

  • Java Programming
  • NetBeans IDE

Before starting this tutorial you may want to familiarize yourself with the following documentation.

Creating the NetBeans Platform Application Project

In this section you use the New Project wizard to create a NetBeans Platform Application from a Maven archetype. In this section you will also modify the sample platform module that is generated by the New Project wizard.

Creating the Maven Platform Application Project

To create the platform application:

  1. Choose File > New Project (Ctrl-Shift-N). Select Maven NetBeans Application from the Maven category. Click Next.
  2. Type MavenPlatformWordApp for the Project Name and set the project location. Click Next.
  3. Select Create Module Project and type MavenPlatformWordApp-WordEngine for the module name. Click Finish.

File:Maven-nbm-newprojectwizard1.png

When you click Finish, by default the IDE creates the following Maven project types.

  • NetBeans Platform Application
    This project is a container project for the Platform application and lists the modules to include and the location of the project's repositories. This project does not contain any sources. The IDE generates the modules containing the sources and resources in this folder.
  • NetBeans Platform based application
    This project specifies the artifacts (sources) needed for compiling the application. This project specifies the details of the required dependencies (IDE artifacts, module artifacts) in the pom.xml file.
  • NetBeans Module
    This project is the first module of the platform application.
  • Platform application branding resources
    This project contains the resources used for branding the application.

For all Maven projects, the POM is located under the Project Files node.

File:Maven-nbm-projectswindow1.png


If you look at the pom.xml file for the module you see that the project has the artifactId MavenPlatformWordApp-WordEngine.

<parent>
      <artifactId>MavenPlatformWordApp</artifactId>
      <groupId>com.mycompany</groupId>
      <version>1.0-SNAPSHOT</version>
  </parent>
  <artifactId>MavenPlatformWordApp-WordEngine</artifactId>
  <packaging>nbm</packaging>  
  <version>1.0-SNAPSHOT</version>
  <name>MavenPlatformWordApp-WordEngine NetBeans Module</name>

To build a NetBeans module you need to use the nbm-maven-plugin. If you look at the POM for the WordEngine module, you can see that the IDE automatically specified the plugin to use to build the project as a NetBeans module.

<plugin>
       <groupId>org.codehaus.mojo</groupId>
       <artifactId>nbm-maven-plugin</artifactId>
       <extensions>true</extensions>
</plugin>

For more information on the NetBeans plugin, see the


If you look at the pom.xml file for the NetBeans Platform Application, you can see that MavenPlatformWordApp-WordEngine is listed as one of the modules in the application.

<modules>
   <module>MavenPlatformWordApp-WordEngine</module>
   <module>branding</module>
   <module>application</module>
</modules>

If you look at the pom.xml file for the NetBeans Platform based application, you can see that the MavenPlatformWordApp-WordEngine artifact is listed as a required dependency for compiling the application.

<dependency>
   <groupId>com.mycompany</groupId>
   <artifactId>MavenPlatformWordApp-WordEngine</artifactId>
   <version>1.0-SNAPSHOT</version>
</dependency>

Modifying the WordEngine Module

In this section you will modify the WordEngine module to add a window component and a button and text area.

Adding a Window Component to the WordEngine Module

In this exercise you will use a wizard to add a Window Component to the module.

  1. Right-click MavenPlatformWordApp - WordEngine NetBeans Module in the Projects window and choose New > Other to open the New File dialog.
  2. Select Window in the Module Development category. Click Next.
  3. Select output in the Window Position dropdown list. Click Next.
  4. Type Text in the Class Name Prefix field. Click Finish.
    The wizard displays a list of the files that will be created and the files that will be modified.


When you click Finish, in the Projects window you can see that the IDE generated the class TextTopComponent.java in com.mycompany.wordengine under Source Packages. The IDE also generated additional resource files in com.mycompany.wordengine under Other Sources. In this exercise you will only edit TextTopComponent.java so you can close the other files.

You can view the structure of the project in the Files window. To compile a Maven project, only source files can be located under Source Packages (src/main/java directory in the Files window). Resource files (e.g., XML files) need to be located under Other Sources (src/main/resources directory in the Files window).


Modifying the Text Window Component

You will now add the text area and button elements to the window component by performing the following steps.

  1. Click the Design tab of TextTopComponent.java in the editor.
  2. Drag and drop a button and a text area from the Palette onto the window.
  3. Right-click the text area and choose Change Variable Name, and then type text as the name. You will use the name when accessing the component from your code.
  4. Set the text of the button to "Filter!"
  5. Double-click the button to open the event handler method for the button in the source code editor. The method is created automatically when you double-click the button element.
  6. Modify the body of the method to the following code.
    You can use the code completion in the editor to help you type the code.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
   String s = text.getText();
   s = s.toUpperCase();
   text.setText(s);
}

File:Maven-nbm-textopcomponent.png


Testing the Application

  1. Right-click the project node of the MavenPlatformWordApp NetBeans Platform based application and choose Build with Dependencies.
  2. Right-click the project node of the MavenPlatformWordApp NetBeans Platform based application and choose Run.

When you Run the application, the IDE launches the NetBeans Platform application. To test the application, perform the following steps:

  1. Choose Window > Text from the main menu or the platform application.
  2. Type some characters in the text area. Click Filter!

When you click Filter!, the characters that you typed are changed to upper case and displayed in the text area.

Updating the Branding Elements

The branding module specifies the branding resources that are used when building the Platform application. The branding dialog enables you to easily specify the name of the application, the splash screen and the application icon and to modify the values of text elements.

Specifying the Name of the NetBeans Platform Application

When you create the Platform application from the Platform application archetype, the default name for the application is foo.

In this exercise you will use the branding wizard to modify the branding resources module to specify the name of the application.

  1. Right-click the Platform application branding resources and choose Branding.
  2. In the Basic tab, modify the Application Title to My Maven Platform Word App.

Click OK.

Replacing the Splash Screen

In this exercise you will replace the default splash image. By default the branding module generated by the IDE contains an image that is displayed when the platform application starts. You can replace this with a different image by performing the following steps.

  1. Right-click the Platform application branding resources and choose Branding.
  2. In the Splash Screen tab, specify an image to use as the splash screen by clicking the Browse button next to the default splash screen image and locating the image you want to use.

Click OK.

For example, you can copy the image below to your local system and specify the image in the Branding dialog. File:Splash.gif


Testing the Application

  1. Right-click the project node of the MavenPlatformWordApp NetBeans Platform based application and choose Build with Dependencies.
  2. Right-click the project node of the MavenPlatformWordApp NetBeans Platform based application and choose Run.

Adding the TextFilter Module to the Platform Application

In this exercise you will create the module TextFilter for the platform application and add the module as a dependency to the application.

Creating the TextFilter Platform Module

In this exercise you will create a new module for the Platform application. After you create the module you need to add the module as a dependency of the application.

To create the module:

  1. Choose File > New Project (Ctrl-Shift-N).
  2. Select the Maven NetBeans Module archetype from the Maven category. Click Next.
  3. Type TextFilter for the Project Name.
  4. Click Browse to set the Project Location and locate the MavenPlatformWordApp directory. Click Finish.

File:Maven-nbm-moduleprojectlocation.png

When you click Finish, the IDE creates the module and opens the module project TextFilter NetBeans Module in the Projects window.

Adding the TextFilter Module as a Dependency of MavenPlatformWordApp

In this exercise you add the TextFilter module as a dependency of the the MavenPlatformWordApp application.

  1. Right-click the Libraries node of the MavenPlatformWordApp - NetBeans Platform based application project and choose Add Dependency.
  2. Click the Open Projects tab in the Add Dependency dialog.
  3. Select the TextFilter NetBeans Module module. Click OK.

File:Maven-nbm-add-dependency-dialog-openprojects.png

When you click OK, the IDE adds the module as a dependency of the project. If you expand the Libraries node, you can see that the module is added to the list of dependencies.

If you open the POM for MavenPlatformWordApp - NetBeans Platform based application, you can see that the IDE added the following lines inside the dependencies element in the POM.

<dependency>
   <groupId>${project.groupId}</groupId>
   <artifactId>TextFilter</artifactId>
   <version>${project.version}</version>
</dependency>

The IDE also modifies the POM of the MavenPlatformWordApp - NetBeans Platform Application project to add the TextFilter module to the list of modules that are included in the application.

<modules>
    <module>module1</module>
    <module>branding</module>
    <module>application</module>
    <module>TextFilter</module>
</modules>

Adding an Interface to the TextFilter Module

In this exercise you will add an interface.

  1. Right-click the "TextFilter" module and choose New > Java Interface.
  2. Type TextFilter as the Class Name.
  3. Select com.mycompany.textfilter in the Package dropdown list. Click Finish.
  4. Modify the class to add the following code. Save your changes.
package com.mycompany.textfilter;

public interface TextFilter {
    String process(String s);
}

Making the TextFilter Module Public

In this exercise you will make the contents of the com.mycompany source package public so that other modules can access the methods. To declare the package as public, you will modify select the packages to be made public in project's Properties dialog box. the configuration element of nbm-maven-plugin in the POM to add the package to the list of packages that are exported as public by the plugin.

For more information, see nbm-maven-plugin manifest

  1. Right-click the TextFilter module and choose Properties.
  2. Select the Public Packages category in the Project Properties dialog.
  3. Select the com.mycompany.textfilter package. Click OK.

When you click OK, the IDE modifies the project POM to modify the configuration element of the nbm-maven-plugin artifact to add the following.

<publicPackages>
   <publicPackage>com.mycompany.*</publicPackage>
</publicPackages>

The POM entry should now contain the following:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>nbm-maven-plugin</artifactId>
    <version>3.2-SNAPSHOT</version>
    <extensions>true</extensions>
    <configuration>
                    <publicPackages>
                        <publicPackage>com.mycompany.textfilter</publicPackage>
                    </publicPackages>
    </configuration>
</plugin>



Adding the MyFilter Module to the Platform Application

In this exercise you will create the module MyFilter. You will add the TextFilter module as a dependency of MyFilter.

Creating the MyFilter Platform Module

In this exercise you will create another module for the Platform application. You will perform the same steps that you performed when creating the TextFilter module.

  1. Choose File > New Project (Ctrl-Shift-N). Select Maven Project from the Maven category. Click Next.
  2. Select Maven NetBeans Module Archetype. Click Next.
  3. Type MyFilter for the Project Name.
  4. Click Browse to set the Project Location and locate the MavenPlatformWordApp directory. Click Finish.
  5. Add the MyFilter module as a dependency of the MavenPlatformWordApp - NetBeans Platform based application project.

Adding the TextFilter Module as a Dependency of MyFilter

In this exercise you add the TextFilter module as a dependency of the the MyFilter module.

  1. Right-click the Libraries node of the MyFilter project and choose Add Dependency.
  2. Click the Open Projects tab in the Add Dependency dialog.
  3. Select the TextFilter module. Click OK.

Modifying the MyFilter Module

In this exercise you will add an interface.

  1. Right-click the MyFilter module and choose New > Java Class.
  2. Type UpperCaseFilter as the Class Name.
  3. Select com.mycompany.myfilter in the Package dropdown list. Click Finish.
  4. Modify the class to add the following code. Save your changes.
package com.mycompany.myfilter;

import com.mycompany.textfilter.TextFilter;
import org.openide.util.lookup.ServiceProvider;

@ServiceProvider(service=TextFilter.class)
public class UpperCaseFilter implements TextFilter {
    
    public String process(String s) {
        return s.toUpperCase();
    }
}

Modifying the MavenPlatformWordApp application

In this exercise you will modify the event handler in the Text window component to load implementations of TextFilter.

  1. Add the TextFilter module as a dependency of the sample module.
  2. Open the TextTopComponent in the editor.
  3. Modify the button handler method to add the following:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        String s = text.getText();
        TextFilter filter = Lookup.getDefault().lookup(TextFilter.class);
        if (filter != null) {
	   s = filter.process(s);
        }
        text.setText(s);
    }

Save your changes.

Testing the Application

  1. Right-click the project node of the MavenPlatformWordApp NetBeans Platform based application and choose Clean.
  2. Right-click the project node of the MavenPlatformWordApp NetBeans Platform based application and choose Build with Dependencies.
  3. Right-click the project node of the MavenPlatformWordApp NetBeans Platform based application and choose Run.


Adding LookupListener and InstanceContent to the Text Window Component

In this exercise you create a module that receives texts dynamically when the "Filter!" button is clicked.

  1. In the WordEngine module, modify the constructor of the "TextTopComponent" as follows:
public final class TextTopComponent extends TopComponent {
      private InstanceContent content;

      public TextTopComponent() {
          initComponents();
          setName(NbBundle.getMessage(TextTopComponent.class, "CTL_TextTopComponent"));
          setToolTipText(NbBundle.getMessage(TextTopComponent.class, "HINT_TextTopComponent"));
      //        setIcon(Utilities.loadImage(ICON_PATH, true));

          content = new InstanceContent();
          associateLookup(new AbstractLookup(content));

      }
  1. Modify the methos of the filter button to add the old value of the text to the InstanceContent object when the button is clicked.
     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
         String s = text.getText();
         TextFilter filter = Lookup.getDefault().lookup(TextFilter.class);
         if (filter != null) {
             content.add(s);
             s = filter.process(s);
         }
         text.setText(s);
     }

Creating the History Module

In this exercise you will create another module for the Platform application. You will perform the same steps that you performed when creating the TextFilter and MyFilter modules.

  1. Choose File > New Project (Ctrl-Shift-N). Select Maven Project from the Maven category. Click Next.
  2. Select Maven NetBeans Module Archetype. Click Next.
  3. Type History for the Project Name.
  4. Click Browse to set the Project Location and locate the MavenPlatformWordApp directory. Click Finish.
  5. Add the History module as a dependency of the MavenPlatformWordApp - NetBeans Platform based application project.


Creating a Window Component in the History Module

In this exercise you will use a wizard to add a Window Component to the module.

  1. Right-click History NetBeans Module in the Projects window and choose New > Other to open the New File dialog.
  2. Select Window in the Module Development category. Click Next.
  3. Select editor in the Window Position dropdown list. Click Next.
  4. Type History in the Class Name Prefix field. Click Finish.
    The wizard displays a list of the files that will be created and the files that will be modified.

Modifying the History Window Component

You will now add a text area element to the window component by performing the following steps.

  1. Click the Design tab of HistoryTopComponent.java in the editor.
  2. Drag and drop a text area from the Palette onto the window.
  3. Right-click the text area and choose Change Variable Name, and then type historyText as the name.
  4. Add the following code to the constructor of the HistoryTopComponent class so that it listens to the lookup of the String class of the current active window. It displays all retrieved String objects in the text area:
      private Lookup.Result result;

      public HistoryTopComponent() {
          initComponents();
          ...

          result = org.openide.util.Utilities.actionsGlobalContext().lookupResult(String.class);
          result.addLookupListener(new LookupListener() {
              public void resultChanged(LookupEvent e) {
                  historyText.setText(result.allInstances().toString());
              }
          });
      }

Testing the Application

  1. Right-click the project node of the MavenPlatformWordApp NetBeans Platform based application and choose Clean.
  2. Right-click the project node of the MavenPlatformWordApp NetBeans Platform based application and choose Build with Dependencies.
  3. Right-click the project node of the MavenPlatformWordApp NetBeans Platform based application and choose Run.

Bold text

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