blob: cee947e56c35b2d2aa6a44a67c02193a098dc20c [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>NetBeans Platform Selection Tutorial Using Maven</title>
<link rel="stylesheet" type="text/css" href="https://netbeans.org/netbeans.css"/>
<meta name="AUDIENCE" content="NBUSER"/>
<meta name="TYPE" content="ARTICLE"/>
<meta name="EXPIRES" content="N"/>
<meta name="indexed" content="y"/>
<meta name="description" content="A short demonstration of how to use Maven to create and run a NetBeans Platform Module Suite."/>
<!-- Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. -->
<!-- Use is subject to license terms.-->
</head>
<body>
<h1>NetBeans Platform Selection Tutorial Using Maven</h1>
<p>This document demonstrates how to create a NetBeans Platform Module Suite from Maven archetypes
and build and install the suite in your installation of the IDE.
In this tutorial you will create a Maven module suite project that contains three NetBeans modules as sub-projects.
The Maven project that contains the sub-projects is a simple POM project that declares how the suite is compiled
and the installation target.
</p>
<p class="tips">This document is based on the Ant-based <a href="https://platform.netbeans.org/tutorials/nbm-selection-1.html">NetBeans Selection Management Tutorial</a>
and illustrates some of the differences between using Ant and Maven to develop NetBeans Platform module suites.
After you understand some of the differences, you can easily proceed through other
tutorials on the the <a href="https://netbeans.org/kb/trails/platform.html">NetBeans Platform Learning Trail</a>.</p>
<p><b>Contents</b></p>
<p><img src="../../images/articles/71/netbeans-stamp.png" class="stamp" width="114" height="114" alt="Content on this page applies to NetBeans IDE 7.1" title="Content on this page applies to NetBeans IDE 7.1"/></p>
<ul class="toc">
<li><a href="#01">Creating the NetBeans Platform Application</a></li>
<li><a href="#02">Creating an API</a></li>
<li><a href="#03">Listening to the Selection</a></li>
<li><a href="#04">Publishing a Selected Object</a></li>
<li><a href="#05">Building and Running the Application</a></li>
<li><a href="#06">Changing Selected Objects Dynamically</a></li>
</ul>
<p><b>To follow this tutorial, you need the software and resources listed in the following table.</b></p>
<table>
<tbody>
<tr>
<th class="tblheader" scope="col">Software or Resource</th>
<th class="tblheader" scope="col">Version Required</th>
</tr>
<tr>
<td class="tbltd1"><a href="http://download.netbeans.org/netbeans/6.9/beta/">NetBeans IDE</a></td>
<td class="tbltd1">version 7.1</td>
</tr>
<tr>
<td class="tbltd1"><a href="http://java.sun.com/javase/downloads/index.jsp">Java Developer Kit (JDK)</a></td>
<td class="tbltd1">version 6 or above</td>
</tr>
</tbody>
</table>
<p><strong class="notes">Note:</strong> 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.</p>
<p>Before starting this tutorial you may want to familiarize yourself with
the following introductory Maven documentation:</p>
<ul>
<li><a href="http://wiki.netbeans.org/MavenBestPractices">Best Practices for Maven in NetBeans IDE</a></li>
<li><a href="http://www.sonatype.com/books/maven-book/reference/introduction.html">Chapter 1. Introducing Apache Maven</a>
(from <a href="http://www.sonatype.com/books/maven-book/reference/public-book.html">Maven: The Definitive Guide</a>)</li>
</ul>
<!-- =================================================================== -->
<!-- +++++++++++++++ Configuring Maven +++++++++++++++++++++++++++++++++ -->
<!--
<h2><a name="config"></a>Using Maven with the IDE</h2>
<p>If this is your first Maven project you will want to familiarize yourself with the Maven configuration settings
and the Maven Repository Browser.</p>
<div class="indent">
<a name="config1"></a>
<h3>Configuring Maven Options</h3>
<p>You can use the Maven tab in the Options window to configure the behavior of Maven in the IDE and to check
that your configuration is correct.</p>
<ol>
<li>Select the Miscellaneous category in the Options window and click the Maven tab.</li>
<li>Specify the location of your local Maven installation (requires 2.0.9 or newer).</li>
<li>Confirm that the location of the local Maven repository is correct.</li>
<li>Click OK.</li>
</ol>
<p>In most cases, if your Maven configuration is typical the information in the Options window should already be correct.</p>
<p class="notes"><strong>Note.</strong> Maven support is activated as part of the Java SE feature set.
If the Maven tab is not available in the Options window, confirm that Java SE is activated by creating a Java application.</p>
<h3><a name="config2"></a>Viewing the Maven Repositories</h3>
<p>The artifacts that are used by Maven to build all your projects are stored in your local Maven repository.
When an artifact is declared as a project dependency, the artifact is downloaded to your local repository
from one of the registered remote repositories if it is not already installed.</p>
<p>The NetBeans repository and several well-known indexed Maven repositories are registered and listed in the Repository Browser window by default.
The NetBeans repository contains most of the public artifacts necessary for you to build your project.
You can use the Maven Repository Browser to view the contents of your local and remote repositories.
You can expand the Local Repository node to see the artifacts that are present locally.
The artifacts listed under the NetBeans repository nodes can be added as project dependencies, but not all of
them are present locally. They are only added to the Local Repository when they are declared as project
dependencies.</p>
<p>To open the Maven Repository Browser:</p>
<ul>
<li>Choose Window &gt; Other &gt; Maven Repository Browser from the main menu.<br/>
<img src="../../images/tutorials/maven-quickstart68/maven-nbm-netbeans-repo.png" alt="Screenshot of Maven Repository Browser" title="Screenshot of Maven Repository Browser" class="margin-around b-all" />
</li>
</ul>
</div>-->
<!-- =================================================================== -->
<!-- +++++++++++++++++++ Creating the Module Suite +++++++++++++++++++++ -->
<h2><a name="01"></a>Creating the NetBeans Platform Application</h2>
<p>In this section you use the New Project wizard to create a
NetBeans Platform Module Suite from a Maven archetype.
The wizard will create a POM project that will contain the module projects.
In the wizard you will also create a module as a sub-project of the suite.
</p>
<ol>
<li><p>Open the New Project wizard and select
NetBeans Application in the Maven category:</p>
<p><img src="../../images/tutorials/maven-suite/71/pic1.png" alt="Screenshot of Projects window" title="Screenshot of Projects window" class="margin-around b-all" />
</p>
<p>Click Next.</p>
</li>
<li><p>Type <strong>MavenSelectionSuite</strong> for the Project Name:</p>
<p><img src="../../images/tutorials/maven-suite/71/pic2.png" alt="Screenshot of Projects window" title="Screenshot of Projects window" class="margin-around b-all" />
</p>
<p>Click Next.</p>
</li>
<li><p>Choose the NetBeans API version
you want to use. Select Create Module Project and type <strong>MyAPI</strong> for
the module name:</p>
<p><img src="../../images/tutorials/maven-suite/71/pic3.png" alt="Screenshot of Projects window" title="Screenshot of Projects window" class="margin-around b-all" />
</p>
<p>Click Finish.</p>
</li>
</ol>
<p>When you click Finish, the IDE creates a
Maven reactor project, together with subprojects
for branding, NetBeans Platform application actions,
and the API module:</p>
<p><img src="../../images/tutorials/maven-suite/71/pic4.png" alt="Screenshot of Projects window" title="Screenshot of Projects window" />
</p>
<p>MavenSelectionSuite-parent is a reactor project which is a
container for sub-projects, in this case NetBeans Module projects.
The reactor project does not contain any source files.
The reactor's POM contains instructions for compiling the application:</p>
<pre class="examplecode">&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;groupId&gt;com.mycompany&lt;/groupId&gt;
&lt;artifactId&gt;MavenSelectionSuite-parent&lt;/artifactId&gt;
&lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
&lt;packaging&gt;pom&lt;/packaging&gt;
&lt;name&gt;MavenSelectionSuite-parent&lt;/name&gt;
&lt;repositories&gt;
&lt;!--
Repository hosting NetBeans modules, especially APIs.
Versions are based on IDE releases, e.g.: RELEASE691
To create your own repository, use: nbm:populate-repository
--&gt;
&lt;repository&gt;
&lt;id&gt;netbeans&lt;/id&gt;
&lt;name&gt;NetBeans&lt;/name&gt;
&lt;url&gt;http://bits.netbeans.org/maven2/&lt;/url&gt;
&lt;/repository&gt;
&lt;/repositories&gt;
&lt;dependencyManagement&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;junit&lt;/groupId&gt;
&lt;artifactId&gt;junit&lt;/artifactId&gt;
&lt;version&gt;4.8.2&lt;/version&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;/dependencyManagement&gt;
&lt;build&gt;
&lt;pluginManagement&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
&lt;artifactId&gt;nbm-maven-plugin&lt;/artifactId&gt;
&lt;version&gt;3.6&lt;/version&gt;
&lt;extensions&gt;true&lt;/extensions&gt;
&lt;configuration&gt;
&lt;brandingToken&gt;${brandingToken}&lt;/brandingToken&gt;
&lt;cluster&gt;${brandingToken}&lt;/cluster&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;plugin&gt;
&lt;!-- NetBeans 6.9+ requires JDK 6 --&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
&lt;version&gt;2.3.2&lt;/version&gt;
&lt;configuration&gt;
&lt;source&gt;1.6&lt;/source&gt;
&lt;target&gt;1.6&lt;/target&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
&lt;version&gt;2.3.1&lt;/version&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/pluginManagement&gt;
&lt;/build&gt;
&lt;modules&gt;
&lt;module&gt;branding&lt;/module&gt;
&lt;module&gt;application&lt;/module&gt;
&lt;module&gt;MyAPI&lt;/module&gt;
&lt;/modules&gt;
&lt;properties&gt;
&lt;netbeans.version&gt;RELEASE71-BETA&lt;/netbeans.version&gt;
&lt;brandingToken&gt;mavenselectionsuite&lt;/brandingToken&gt;
&lt;/properties&gt;
&lt;/project&gt;</pre>
<p>As you can see above, the reactor provides a
list of the modules that will be included when you build the project.
You can see that the MyAPI project is listed as one of these modules.</p>
<!-- =================================================================== -->
<!-- ++++++++++++++++++ Adding Class to MyAPI Module +++++++++++++++++++ -->
<h2><a name="02"></a>Creating an API</h2>
<p>You created the MyAPI module when you created the module suite, but now you need to
create a class in the module and expose the class to other modules.</p>
<div class="indent">
<a name="02a"></a>
<h3>Creating a Class in the MyAPI Module</h3>
<p>In this exercise you will create a simple class named <tt>Event</tt>.
Each instance of <tt>Event</tt> will be unique because the field <tt>index</tt>
is incremented by 1 each time a new instance of <tt>Event</tt> is created.</p>
<ol>
<li>In the <tt>com.mycompany.mavenselectionsuite</tt> package,
create a new Java class named <strong>Event</strong>.
</li>
<li>Modify the class to declare the following fields and getters:
<pre class="examplecode">package com.mycompany.mavenselectionsuite;
import java.util.Date;
public final class Event {
private final Date date = new Date();
private static int count = 0;
private final int index;
public Event() {
index = count++;
}
public Date getDate() {
return date;
}
public int getIndex() {
return index;
}
public String toString() {
return index + " - " + date;
}
}</pre>
</li>
</ol>
<a name="02b"></a>
<h3>Exposing the Package</h3>
<p>In this tutorial you will create additional modules that will need to access
the <tt>Event</tt> class.
In this exercise, you will make the contents of the package
that contains the <tt>Event</tt> class public so
that other modules can access it.
To declare the <tt>com.mycompany.mavenselectionsuite</tt> package as public, you will
modify the <tt>configuration</tt> element of <tt>nbm-maven-plugin</tt> in
the POM to specify the packages that are exported as public.
You can make the changes to the POM in the editor or by selecting the
packages to make public in the project's Properties window.</p>
<ol>
<li>Right-click the MyAPI project node and choose Properties to open the Properties window.</li>
<li><p>Select the <strong>com.mycompany.mavenselectionsuite</strong> package
in the <strong>Public Packages</strong> category:</p>
<p><img src="../../images/tutorials/maven-suite/71/pic5.png" alt="screenshot" title="screenshot" class="margin-around b-all" /></p>
<p>Click OK.</p>
<p>When you select a package to export, the IDE modifies the <tt>nbm-maven-plugin</tt>
element in the POM to specify the package:</p>
<pre class="examplecode">&lt;plugin&gt;
&lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
&lt;artifactId&gt;nbm-maven-plugin&lt;/artifactId&gt;
&lt;extensions&gt;true&lt;/extensions&gt;
&lt;configuration&gt;
&lt;publicPackages&gt;
<strong>&lt;publicPackage&gt;com.mycompany.mavenselectionsuite&lt;/publicPackage&gt;</strong>
&lt;/publicPackages&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;</pre></li>
<li><p>Right-click the project and choose Build. When you build the project,
the <tt>nbm-maven-plugin</tt> will generate a manifest header in
the <tt>MANIFEST.MF</tt>
of the JAR, to specify the public package:</p>
<pre class="examplecode">Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: geertjan
Build-Jdk: 1.7.0
OpenIDE-Module-Localizing-Bundle: com/mycompany/mavenselectionsuite/Bu
ndle.properties
OpenIDE-Module-Specification-Version: 1.0
OpenIDE-Module-Implementation-Version: 1.0-20111222
OpenIDE-Module-Build-Version: 201112221054
OpenIDE-Module: com.mycompany.MyAPI
<b>OpenIDE-Module-Public-Packages: com.mycompany.mavenselectionsuite.*</b>
OpenIDE-Module-Requires: org.openide.modules.ModuleFormat1
OpenIDE-Module-Display-Category: com.mycompany
OpenIDE-Module-Name: MyAPI
OpenIDE-Module-Short-Description: &lt;undefined&gt;
OpenIDE-Module-Long-Description: &lt;undefined&gt;
OpenIDE-Module-Module-Dependencies: org.netbeans.api.annotations.commo
n/1 > 1.10.1</pre>
<p class="tips">For more information, see the <a href="http://mojo.codehaus.org/nbm-maven-plugin/manifest-mojo.html#publicPackages">nbm-maven-plugin manifest documentation</a>.</p>
</li>
</ol>
</div>
<!-- =================================================================== -->
<!-- ++++++++++++++++++ Creating the MyViewer Module +++++++++++++++++++ -->
<h2><a name="03"></a>Listening to the Selection</h2>
<p>In this section you will create a new module named MyViewer and add a window component and two text fields.
The component will implement <tt><a href="http://bits.netbeans.org/dev/javadoc/org-openide-util-lookup/org/openide/util/LookupListener.html">LookupListener</a></tt>
to listen for changes to the selection.</p>
<div class="indent">
<h3><a name="03a"></a>Creating the Module</h3>
<p>In this exercise you will create the MyViewer NetBeans module in the <tt>MavenSelectionSuite</tt> directory.</p>
<ol>
<li><p>Choose File &gt; New Project from the main menu (Ctrl-Shift-N).
Select NetBeans Module from the Maven category:</p>
<p><img src="../../images/tutorials/maven-suite/71/pic6.png" alt="screenshot" title="screenshot" class="margin-around b-all" /></p>
<p>Click Next.</p></li>
<li><p>Type <strong>MyViewer</strong> as the Project Name.
Make sure to set the Project Location to the <tt>MavenSelectionSuite</tt> directory:</p>
<p><img src="../../images/tutorials/maven-suite/71/pic7.png" alt="screenshot" title="screenshot" class="margin-around b-all" /></p>
<p>Click Next. Select the NetBeans API version you'd like to use.
Click Finish.</p></li>
<li><p>The module is going to use the API module, therefore
we need to set a dependency in the viewer module
on the API module. Right-click the Dependencies node in the
MyViewer project and choose Add Dependency:</p>
<p><img src="../../images/tutorials/maven-suite/71/pic8.png" alt="screenshot" title="screenshot" /></p>
</li>
<li><p>Select the MyAPI module in the
Open Projects tab:</p>
<p><img src="../../images/tutorials/maven-suite/71/pic9.png" alt="screenshot" title="screenshot" /></p>
</li>
</ol>
<p>When you click Add above, the IDE adds the
API module to the list of dependencies of the MyViewer module.
You can see the new dependency in the POM of the
MyViewer module and displayed as a new node
under the Dependencies node of the MyViewer module.</p>
<p>You've now learned how to create a new module
in your application and you've also learned
how to set dependencies between your modules. Once
a dependency has been set, a module can use the publicly
exposed classes of the modules it depends on.</p>
<a name="03b"></a>
<h3>Creating the Viewer Window</h3>
<p>In this exercise you will create a GUI component in your
viewer module. The data displayed in the GUI component
will be retrieved from the Lookup. That means you will learn
how to listen to the selection and how to update your GUI
component whenever an object of interest is published
into the Lookup.</p>
<ol>
<li>Right-click the MyViewer project and choose New &gt; Window.</li>
<li><p>Select <strong>explorer</strong>
and select Open on Application Start:</p>
<p><img src="../../images/tutorials/maven-suite/71/pic01.png" alt="screenshot" title="screenshot" /></p>
<p>Click Next.</p></li>
<li><p>Type <strong>MyViewer</strong> as the Class Name Prefix:</p>
<p><img src="../../images/tutorials/maven-suite/71/pic02.png" alt="screenshot" title="screenshot" /></p>
<p>Click Finish. You should see you have a new NetBeans Platform window
component ready to be designed within the Matisse GUI Builder:</p>
<p><img style="border:1px solid black" src="../../images/tutorials/maven-suite/71/pic03.png" alt="screenshot" title="screenshot" /></p>
</li>
<li>Drag two Labels from the Palette
into the window component:<br/>
<p><img style="border:1px solid black" src="../../images/tutorials/maven-suite/71/pic04.png" alt="screenshot" title="screenshot" /></p>
</li>
<li>Click the Source tab and modify the class signature to implement <tt>LookupListener</tt>.
<pre class="examplecode">public class MyViewerTopComponent extends TopComponent <strong>implements LookupListener</strong> {</pre></li>
<li>Add the following <tt>private</tt> field <tt>result</tt> and set the initial value to null.
<pre class="examplecode">private Lookup.Result&lt;Event&gt; result = null;</pre></li>
<li><p>Implement the LookupListener's "resultChanged" method as follows:</p>
<pre class="examplecode">@Override
public void resultChanged(LookupEvent le) {
if (!result.allInstances().isEmpty()) {
for (Event event : result.allInstances()) {
jLabel1.setText(Integer.toString(event.getIndex()));
jLabel2.setText(event.getDate().toString());
}
} else {
jLabel1.setText("[no selection]");
jLabel2.setText("");
}
}</pre>
</li>
<li>Make the following additions to the <tt>componentOpened()</tt> and
<tt>componentClosed()</tt> methods that have already been created
in the class by the New Window wizard:
<pre class="examplecode">@Override
public void componentOpened() {
<strong>result = Utilities.actionsGlobalContext().lookupResult(Event.class);
result.addLookupListener(this);</strong>
}
@Override
public void componentClosed() {
<strong>result.removeLookupListener (this);</strong>
}</pre>
<p class="notes">By using <tt><a href="http://bits.netbeans.org/dev/javadoc/org-openide-util/org/openide/util/Utilities.html#actionsGlobalContext%28%29">Utilities.actionsGlobalContext()</a></tt>,
each time a component is opened, the window
listens globally for Event objects. Whenever a new
Event is published into the Lookup, the "resultChanged"
method is automatically triggered to handle the event.</p>
</li>
</ol>
</div>
<!-- =================================================================== -->
<!-- ++++++++++++++++++ Creating the MyEditor Module +++++++++++++++++++ -->
<h2><a name="04"></a>Publishing a Selected Object</h2>
<p>In this section you will create a new module called MyEditor.
The module will contain a
<tt><a href="http://bits.netbeans.org/dev/javadoc/org-openide-windows/org/openide/windows/TopComponent.html">TopComponent</a></tt>
that will publish instances of the <tt>Event</tt> object into the
Lookup of the TopComponent. When the user selects the TopComponent,
the objects in its Lookup will be available globally. Once published
globally, the viewer window created in the previous section, which
is listening for Events in the global Lookup, will
automatically update itself.
</p>
<div class="indent">
<a name="04a"></a>
<h3>Creating the Module</h3>
<p>In this exercise you will create a NetBeans module in the <tt>MavenSelectionSuite</tt> directory and add a dependency
on the MyAPI module.</p>
<ol>
<li><p>Choose File &gt; New Project from the main menu.
Select NetBeans Module from the Maven category:</p>
<p><img src="../../images/tutorials/maven-suite/71/pic6.png" alt="screenshot" title="screenshot" class="margin-around b-all" /></p>
<p>Click Next.</p></li>
<li><p>Type <strong>MyEditor</strong> as the Project Name.
Make sure to set the Project Location to the <tt>MavenSelectionSuite</tt> directory:</p>
<p><img src="../../images/tutorials/maven-suite/71/pic05.png" alt="screenshot" title="screenshot" class="margin-around b-all" /></p>
<p>Click Next. Select the NetBeans API version you'd like to use.
Click Finish.</p></li>
<li><p>Just like you did in the previous section, you need
to set a dependency on the API module, so that you
can use its public packages. Right-click the Dependencies node
in the MyEditor project and choose Add Dependency:</p>
<p><img src="../../images/tutorials/maven-suite/71/pic06.png" alt="screenshot" title="screenshot" /></p>
</li>
<li><p>Select the MyAPI module in the Open Projects tab:</p>
<p><img src="../../images/tutorials/maven-suite/71/pic07.png" alt="screenshot" title="screenshot" /></p>
</li>
</ol>
<p>You've now created a second module. In this module, you'll now create
a TopComponent where new Events will be published into the Lookup.</p>
<a name="04c"></a>
<h3>Creating the Editor Window</h3>
<p>In this exercise you will create the window that opens in
the editor area when invoked from a menu item in the
main menubar of the application.</p>
<p class="notes">Rather than using the New Window wizard, as done in the previous section,
you will create a normal Java class here, then extend <tt>TopComponent</tt>,
and use the same annotations you previously used to register the
window in the NetBeans Platform application. You'll learn that you do
not need to use the wizards in the IDE to create the artifacts needed
to integrate with the NetBeans Platform.</p>
<ol>
<li><p>Create a new Java class named "MyEditorTopComponent".</p></li>
<li>Modify the constructor to create a new instance of <tt>Event</tt>
each time the class is invoked, together with a text field
to display the currently published object.
<pre class="examplecode">package com.mycompany.myeditor;
import com.mycompany.mavenselectionsuite.Event;
import java.awt.BorderLayout;
import javax.swing.JTextField;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.util.NbBundle.Messages;
import org.openide.util.lookup.Lookups;
import org.openide.windows.TopComponent;
@TopComponent.Description(preferredID = "MyEditorTopComponent",
persistenceType = TopComponent.PERSISTENCE_NEVER)
@TopComponent.Registration(mode = "editor", openAtStartup = false)
@ActionID(category = "Window", id = "com.mycompany.myeditor.MyEditorTopComponent")
@ActionReference(path = "Menu/Window")
@TopComponent.OpenActionRegistration(displayName = "#CTL_MyEditorAction")
@Messages({
"CTL_MyEditorAction=MyEditor",
"CTL_MyEditorTopComponent=MyEditor Window",
"HINT_MyEditorTopComponent=This is a MyEditor window"
})
public final class MyEditorTopComponent extends TopComponent {
public MyEditorTopComponent() {
setName(Bundle.CTL_MyEditorTopComponent());
setToolTipText(Bundle.HINT_MyEditorTopComponent());
Event obj = new Event();
associateLookup(Lookups.singleton(obj));
setLayout(new BorderLayout());
JTextField displayField = new JTextField();
add(displayField, BorderLayout.NORTH);
displayField.setText("Event #" + obj.getIndex() + " created at " + obj.getDate());
setDisplayName("MyEditor " + obj.getIndex());
}
}
</pre>
<p class="tips">The <tt>associateLookup(Lookups.singleton(obj));</tt> line in the constructor will create a
Lookup that contains the new instance of <tt>Event</tt>.</p></li>
</ol>
<p>The text field in the component only displays the index value and date
from the <tt>Event</tt>.
This will enable you to see that each MyEditor component is
unique and that MyViewer is displaying the
details of the MyEditor component that has the focus.</p>
</div>
<!-- =================================================================== -->
<!-- ++++++++++++++++ Building and Running the Module +++++++++++++++++ -->
<h2><a name="05"></a>Building and Running the Application</h2>
<p>Now that the target installation of
the IDE is specified, you can use the Run command
on the <b>MavenSelectionSuite-app</b> project. Before you do so,
you need to add the viewer module and editor module as
dependencies of the <b>MavenSelectionSuite-app</b> project.</p>
<ol>
<li><p>Right-click the Dependencies node of the <b>MavenSelectionSuite-app</b> project
and choose Add Dependency:</p>
<p><img src="../../images/tutorials/maven-suite/71/pic11.png" alt="screenshot" title="screenshot" /></p>
</li>
<li><p>In the Open Projects tab, choose "MyViewer" and click Add:</p>
<p><img src="../../images/tutorials/maven-suite/71/pic12.png" alt="screenshot" title="screenshot" /></p>
</li>
<li><p>Repeat the steps above to add the "MyEditor" module
as a dependency of the <b>MavenSelectionSuite-app</b> project.</p>
</li>
<li><p>Right-click <b>MavenSelectionSuite-app</b> project and choose Run.
The application starts up, consisting of the NetBeans Platform,
together with the custom modules you created.</p>
<p><img src="../../images/tutorials/maven-suite/71/pic13.png" alt="screenshot" title="screenshot" /></p>
<p>The MyViewer window opens when the application starts and
displays the two labels.
You can now choose MyEditor from the Window menu, multiple times, to
open multiple MyEditor components in the editor area.
The MyViewer window will display the details
of the currently selected MyEditor component.</p>
</li>
</ol>
<!-- =================================================================== -->
<!-- ++++++++++++++++ Modifying the Lookup with Button +++++++++++++++++ -->
<h2><a name="06"></a>Changing Selected Objects Dynamically</h2>
<p>Currently, a new <tt>Event</tt> is created each time you open a new MyEditor component.
In this section you will add a button to the MyEditor component.
Whenever the button is clicked, the MyEditor component's current <tt>Event</tt>
object will be replaced with a new one.</p>
<p>In short, you will modify the code to use
<tt><a href="http://bits.netbeans.org/dev/javadoc/org-openide-util-lookup/org/openide/util/lookup/InstanceContent.html">InstanceContent</a></tt>
to dynamically handle changes to the content of the Lookup.</p>
<ol>
<li><p>Change the MyEditorTopComponent to the following:</p>
<pre class="examplecode">package com.mycompany.myeditor;
import com.mycompany.mavenselectionsuite.Event;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JTextField;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.util.NbBundle.Messages;
import org.openide.util.lookup.AbstractLookup;
import org.openide.util.lookup.InstanceContent;
import org.openide.windows.TopComponent;
@TopComponent.Description(preferredID = "MyEditorTopComponent",
persistenceType = TopComponent.PERSISTENCE_NEVER)
@TopComponent.Registration(mode = "editor", openAtStartup = false)
@ActionID(category = "Window", id = "com.mycompany.myeditor.MyEditorTopComponent")
@ActionReference(path = "Menu/Window")
@TopComponent.OpenActionRegistration(displayName = "#CTL_MyEditorAction")
@Messages({
"CTL_MyEditorAction=MyEditor",
"CTL_MyEditorTopComponent=MyEditor Window",
"HINT_MyEditorTopComponent=This is a MyEditor window"
})
public final class MyEditorTopComponent extends TopComponent {
private final InstanceContent content = new InstanceContent();
public MyEditorTopComponent() {
setName(Bundle.CTL_MyEditorTopComponent());
setToolTipText(Bundle.HINT_MyEditorTopComponent());
associateLookup(new AbstractLookup(content));
setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
final JTextField displayField = new JTextField();
add(displayField);
JButton replaceButton = new JButton("Replace");
add(replaceButton);
replaceButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Event obj = new Event();
displayField.setText("Event #" + obj.getIndex() + " created at " + obj.getDate());
setDisplayName("MyEditor " + obj.getIndex());
content.set(Collections.singleton(obj), null);
}
});
}
}</pre>
<p class="notes"><b>Note:</b> To be able to publish the
<tt>InstanceContent</tt>, you are now using
<tt><a href="http://bits.netbeans.org/dev/javadoc/org-openide-util-lookup/org/openide/util/lookup/AbstractLookup.html#AbstractLookup%28org.openide.util.lookup.AbstractLookup.Content%29">AbstractLookup</a></tt>
instead of <tt>Lookup</tt> in the constructor.</p>
<p class="notes"><b>Note:</b> You have moved several statements into
the new button's action event handler.</p>
</li>
<li><p>When you run the application again, you will see the new button in each MyEditor
component:</p>
<p><img src="../../images/tutorials/maven-suite/71/pic14.png" alt="screenshot" title="screenshot" /></p>
</li>
<li><p>When you click the button, the index number in the text field will increase.
The label in the MyViewer window will also update to correspond to the new value.</p></li></ol>
<p>This tutorial demonstrated how to create and run a NetBeans Platform application
that you create from a Maven archetype.
You saw how applications are structured and
how you configure a modules POM to specify public packages. Most important of all,
you learned how to publish objects into the selection and how to listen
for them and update other parts of the application accordingly.
For more examples on how to build NetBeans Platform applications,
see the tutorials listed in the <a href="https://netbeans.org/kb/trails/platform.html">NetBeans Platform Learning Trail</a>.</p>
<!-- ======================================================================================== -->
<div class="feedback-box"><a name="feedback"></a>
<a href="https://netbeans.org/about/contact_form.html?to=3&amp;subject=Feedback:%20NetBeans%20Platform%20Selection%20Tutorial%20Using%20Maven%207.1">
Send Us Your Feedback</a></div>
<p>&nbsp;</p>
<!-- ======================================================================================== -->
<h2><a name="nextsteps"></a>See Also</h2>
<p>For more information about creating and developing on the NetBeans Platform, see the following resources.</p>
<ul>
<li><a href="https://netbeans.org/kb/trails/platform.html">NetBeans Platform Learning Trail</a></li>
<li><a href="http://wiki.netbeans.org/NetBeansDeveloperFAQ">NetBeans Developer FAQ</a></li>
<li><a href="http://bits.netbeans.org/dev/javadoc/">NetBeans API Javadoc</a></li>
</ul>
<p>If you have any questions about the NetBeans Platform, feel free
to write to the mailing list, dev@platform.netbeans.org, or view the
<a href="https://netbeans.org/projects/platform/lists/dev/archive">NetBeans Platform mailing list archive</a>.</p>
<!-- ======================================================================================== -->
</body>
</html>