Professional Documents
Culture Documents
NetBeans IDE 5.0 FeedReader Tutorial
NetBeans IDE 5.0 FeedReader Tutorial
NetBeans IDE 5.0 FeedReader Tutorial
0 FeedReader Tutorial
Welcome to the NetBeans IDE 5.0 FeedReader tutorial. The FeedReader application that you
build in this tutorial is a simple RSS/Atom feed browser, modeled after the Sage plug-in for
Mozilla Firefox. It presents a tree of feeds with subnodes representing individual feed entries
that you can open in a browser. As an example, here a feed entry from the PlanetNetBeans
RSS feed is opened in the IDE's internal browser:
Table of Contents
Introduction
■ About Frequently Used Terms
■ About the FeedReader Application
■ About this Tutorial
■ About the Resources
Introduction
Before beginning to code the FeedReader application, it’s a good idea to familiarize yourself
with some of the frequently used terms in the area of application development in NetBeans
IDE. In the process, you will build a general understanding of the application that you will
create, find out about what you are about to learn, and set up everything that you are going
to need.
2. Choose File > New Project (Ctrl-Shift-N), then expand Samples, then select NetBeans
Plug-in Modules, and choose FeedReader. Click Next.
3. Name the project whatever you like, choose a location to store your project, and click
Finish. The IDE opens the FeedReader sample.
4. Right-click the FeedReader Application project node and choose Run Project. The
application starts up. During installation, notice that a splash screen is displayed.
1. Right-click RSS/Atom Feeds node, choose Add, and enter a URL to an RSS/Atom
feed. For example, enter the NetBeans.org RSS feed (http://www.netbeans.org/rss-
091.xml) or the PlanetNetBeans RSS feed (http://www.planetnetbeans.org/rss20.xml).
2. Click OK. A node is added for the feed; its children are the feed entries. Normally,
feed entries are articles or topics that the feed makes available.
gives you a lot of functionality for free, like the ability to delete, listening for changes,
etc. So when you add a new RSS feed, all you do is create a new Feed object and
serialize it into that folder.
Supporting files:
■ build.xml
Provides Ant targets for common tasks, such as building and running the application.
■ Bundle.properties
Localization key-value pairs.
■ FeedTopComponentSettings.xml
Specifies all the interfaces of the FeedReader application.
■ FeedTopComponentWstcref.xml
Specifies a reference to the component.
■ layer.xml
Registers folders and files in the System Filesystem. You will be shown how to use the
System Filesystem Browser to work with this file.
■ project.xml
Declares project metadata, such as module dependencies. You will be shown how to
use the Project Properties dialog box to work with this file.
■ rss16.gif
Icon displayed by the application's menu item and in its Help > About dialog box.
■ splash.gif
Splash screen.
2. In the Name and Location panel, type feedreader-suite in Project Name. Change the
Project Location to any directory on your computer, such as c:\mymodules. Click
Finish.
The IDE creates the feedreader-suite project. The project will contain the module project
and library wrapper module projects that you will create in the following subsections.
The project opens in the IDE. You can view its logical structure in the Projects window
(Ctrl-1) and its file structure in the Files window (Ctrl-2).
1. Choose File > New Project (Ctrl-Shift-N). Under Categories, select NetBeans Plug-in
Modules. Under projects, select Library Wrapper Module Project. Click Next.
2. In the Select Library panel, browse to the folder where you downloaded JDom, and
then select jdom.jar and LICENSE.txt. Click Next.
3. In the Name and Location panel, accept all the defaults. Note that the library wrapper
module project will be housed within the module suite project. You could also house it
somewhere else, but for versioning purposes it is a good idea to put it within the
module suite project. Also note that the feedreader-suite module suite project is
selected in the Add to Module Suite drop-down. Click Next.
4. In the Basic Module Configuration panel, accept all the defaults. Click Finish. The
new library wrapper module project opens in the IDE and displays in the Projects
window.
5. Create a library wrapper module project for Rome. Accept all the defaults.
2. In the Name and Location panel, type FeedReader in Project Name. Accept all the
defaults. Click Next.
3. In the Basic Module Configuration panel, replace yourorghere in Code Name Base
with myorg, so that the whole code name base is org.myorg.feedreader. Leave the
location of the localizing bundle and XML layer, so that they will be stored in a
package with the name org/myorg/feedreader. Click Finish.
The IDE creates the FeedReader project. The project contains all of the module’s sources and
project metadata, such as the project’s Ant build script. The project opens in the IDE. You
can view its logical structure in the Projects window (Ctrl-1) and its file structure in the Files
window (Ctrl-2). The Projects window should now show the following:
Right-click the feedreader-suite project node, choose Properties, and click Sources in the
Project Properties dialog box. The panel shows the modules that were added to the module
suite while you were creating their projects. You should see FeedReader, jdom, rome, and rome-
fetcher listed in the Suite Modules list.
1. Right-click the FeedReader project node and choose New > File/Folder. Under
Categories, select NetBeans Module Development. Under File Types, select Window
Component. Click Next.
2. In the Basic Settings panel, select explorer in the drop-down list and click Open on
Application Start. Click Next.
3. In the Name and Location panel, type Feed as the Class Name Prefix and browse to the
location where you saved rss16.gif. The GIF file will be shown in the menu item that
invokes the action.
4. Click Finish.
The IDE creates the following new files:
■ FeedAction.java. Defines the action that appears in the Window menu with the label
Open Feed Window and the rss16.gif image. It opens the Feed Window.
■ FeedTopComponent.java. Defines the Feed Window.
■ FeedTopComponentSettings.xml. Specifies all the interfaces of the org.myorg.feedreader
rich-client application. Enables easy lookup of instances, without the need to instantiate
each. Avoids the need to load classes or create objects and therefore improves
performance. Registered in the Windows2/Components folder of the layer.xml file.
■ FeedTopComponentWstcref.xml. Specifies a reference to the component. Enables the
component to belong to more than one mode. Registered in the Windows2/Modes
folder of the layer.xml file.
The IDE modifies the following existing files:
■ project.xml. Two module dependencies have been added, Utilities API (click here
for Javadoc) and Window System API (click here for Javadoc).
■ Bundle.properties. Three key-value pairs have been added:
■ Finally, three registration entries have been added to the layer.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE filesystem PUBLIC "-//NetBeans//DTD Filesystem 1.1//EN"
"http://www.netbeans.org/dtds/filesystem-1_1.dtd">
<filesystem>
<folder name="Actions">
<folder name="Window">
<file name="org-myorg-feedreader-FeedAction.instance"/>
</folder>
</folder>
<folder name="Menu">
<folder name="Window">
<file name="FeedAction.shadow">
<attr name="originalFile" stringvalue=
"Actions/Window/org-myorg-feedreader-FeedAction.instance"/>
</file>
</folder>
</folder>
<folder name="Windows2">
<folder name="Components">
<file name="FeedTopComponent.settings" url=
"FeedTopComponentSettings.xml"/>
</folder>
<folder name="Modes">
<folder name="explorer">
commonpalette|
editor
properties
leftSlidingSide
rightSlidingSide\
bottomSlidingSide
navigator
debugger
output
The IDE uses an Ant build script to build and install your application. The build script was
created for you when you created the module project.
1. In the Projects window, right-click the feedreader-suite project and choose Run. The
application is built and is installed in another instance of your installation of NetBeans
IDE. The IDE opens and displays the new Feed Window.
2. In the IDE’s Window menu, you should see the new menu item.
3. Hover over the window’s label. You should see the hint displayed.
4. Click the small cross on the right side of the Feed Window’s label. The window closes.
6. From the main menu, choose File > Exit to exit the IDE.
1. In the Projects window, right-click the FeedReader project and choose Properties. In the
Project Properties dialog box, click Libraries. Notice that some APIs have already been
declared as Module Dependencies. This was done for you by the Window Component
wizard.
Actions API
Datasystems API
Dialogs API
Explorer and Property Sheet API
File System API
Nodes API
rome
rome-fetcher
5. Optionally, in the Projects window, expand the FeedReader module project in the IDE,
expand the Important Files node, double-click Project Metadata so that it opens in the
IDE. Notice that the APIs you selected have been declared as dependencies.
6. Next, you need to make Rome dependent on JDom. Right-click the Rome library
wrapper moduleproject in the Projects window and choose Properties. In the Project
Properties dialog box, click Libraries and then click Add next to the Module
Dependencies list. Add JDom. Click OK to exit the Project Properties dialog box.
7. Finally, since Rome Fetcher depends on both Rome and JDom, you need to make
Rome Fetcher dependent on Rome. Because Rome already depends on JDom, you do
not need to make Rome Fetcher dependent on JDom.
8. Optionally, in the Projects window, expand the Rome module in the IDE, expand the
Important Files node, double-click Project Metadata so that it opens in the IDE. Notice
that the Rome JAR files are on the classpath, that JDom is a module dependency, and
that their packages are publicly exposed. You should see the same in Rome Fetcher’s
Project Metadata file.
1. In the Projects window, expand the FeedReader project node, expand the Important
Files node, and then expand the XML Layer node. The System Filesystem Browser
opens. It shows you the following nodes:
2. Right-click the <this layer> node and choose New > Folder.
3. Type RssFeeds in the New Folder dialog box. Click OK. Double-click the node for the
layer.xml file so that it opens in the Source Editor. Notice that this entry has been
added:
<folder name="RssFeeds"/>
1. Right-click the FeedReader project node, choose New > File/Folder and then choose the
Java Class file type in the Java Class category. Click Next.
2. Name the class Feed and select org.myorg.feedreader in the Package drop-down. Click
Finish.
3. In the Source Editor, replace the default Feed class with the following:
public class Feed implements Serializable {
private static FeedFetcher s_feedFetcher = new HttpURLFeedFetcher(
HashMapFeedInfoCache.getInstance());
private transient SyndFeed m_syndFeed;
private URL m_url;
private String m_name;
protected Feed() {
}
2. Press Alt-Shift-F, select java.net.URL, click OK, and the IDE adds the following
import statements below the package statement:
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.fetcher.FeedFetcher;
import com.sun.syndication.fetcher.impl.HashMapFeedInfoCache;
import com.sun.syndication.fetcher.impl.HttpURLFeedFetcher;
import java.io.IOException;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
All the red underlining should now have disappeared. If not, do not continue with this
tutorial until you have solved the problem.
3. Press Alt-Enter in the line and click on the suggestion. The IDE adds an import
statement for the required package org.openide.explorer.ExplorerManager.
4. Press Alt-Enter again and click on the suggestion. The IDE implements the abstract
method getExplorerManager().
5. Type return manager; in the body of the new getExplorerManager() method. Press Alt-
Enter in the line and let the IDE create a field called manager for you. Replace the
default definition with this one:
6. Right below the field declaration in the previous step, declare this one:
setLayout(new BorderLayout());
add(view, BorderLayout.CENTER);
view.setRootVisible(true);
try {
manager.setRootContext(new RssNode.RootRssNode());
} catch (DataObjectNotFoundException ex) {
ErrorManager.getDefault().notify(ex);
}
ActionMap map = getActionMap();
map.put("delete", ExplorerUtils.actionDelete(manager, true));
associateLookup(ExplorerUtils.createLookup(manager, map));
Now a lot of code is underlined, because you have not declared their associated packages.
You do this in the next steps.
Take the following steps to reformat the file and declare its dependencies:
2. Press Alt-Shift-F, select org.openide.ErrorManager, click OK, and the IDE adds several
import statements below the package statement. The complete list of import statements
should now be as follows:
import java.awt.BorderLayout;
import java.io.Serializable;
import javax.swing.ActionMap;
import org.openide.ErrorManager;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.explorer.view.BeanTreeView;
import org.openide.loaders.DataObjectNotFoundException;
import org.openide.util.NbBundle;
import org.openide.util.RequestProcessor;
import org.openide.util.Utilities;
import org.openide.windows.TopComponent;
{
super(folderNode, new RssFolderChildren(folderNode));
}
/** Declaring the Add Feed action and Add Folder action */
public Action[] getActions(boolean popup) {
DataFolder df =
(DataFolder)getLookup().lookup(DataFolder.class);
return new Action[] { new AddRssAction(df),
new AddFolderAction(df) };
}
.getRoot().getFileObject("RssFeeds")).getNodeDelegate());
}
public String getDisplayName() {
return NbBundle.getMessage(RssNode.class, "FN_title");
}
}
ErrorManager.getDefault().notify(exc);
}
// Some other type of Node (gotta do something)
return new Node[] { new FilterNode(n) };
}
}
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
//Should never happen - no reason for it to fail abov
return new Node[] { new AbstractNode(Children.LEAF)
{
public String getHtmlDisplayName() {
return "<font color=’red’>" +
ex.getMessage() + "</font>";
}
}};
}
}
}
}
}
/** Specifying what should happen when the user invokes the Open
action */
private static class EntryOpenCookie implements OpenCookie {
private final SyndEntry entry;
EntryOpenCookie(SyndEntry entry) {
this.entry = entry;
}
NotifyDescriptor.InputLine nd = new
NotifyDescriptor.InputLine(
NbBundle.getMessage(RssNode.class,
"FN_askfolder_msg"), //NOI18N
NbBundle.getMessage(RssNode.class,
"FN_askfolder_title"), //NOI18N
NotifyDescriptor.OK_CANCEL_OPTION,
NotifyDescriptor.PLAIN_MESSAGE);
Object result = DialogDisplayer.getDefault().notify(nd);
if (result.equals(NotifyDescriptor.OK_OPTION)) {
final String folderString = nd.getInputText();
try {
DataFolder.create(folder, folderString);
} catch (IOException ex) {
ErrorManager.getDefault().notify(ex);
}
}
}
}
A lot of code is underlined, because you have not declared their packages. You do this in
the next step. Now take the following steps to reformat the file and declare its
dependencies:
org.openide.actions.OpenAction
javax.swing.Action
org.openide.nodes.Children
org.openide.filesystems.FileLock
java.awt.Image
org.openide.nodes.Node
org.openide.ErrorManager
org.openide.util.Utilities
java.net.URL
org.openide.filesystems.Repository
java.beans.IntrospectionException
3. Click OK, and the IDE adds a lot of import statements below the package statement.
All red underlining should now have disappeared. If not, do not continue with this
tutorial until you have solved the problem.
FN_title=RSS/Atom Feeds
FN_addbutton=Add
FN_askurl_title=New Feed
FN_askurl_msg=Enter the URL of an RSS/Atom Feed
FN_askurl_err=Invalid URL: {0}|
FN_addfolderbutton=Add Folder
FN_askfolder_msg=Enter the folder name
FN_askfolder_title=New Folder
Here is an explanation of the new key-value pairs, which localize strings defined in
RssNode.java:
■ FN_title. Localizes the label of the highest node in the Feed Window.
Localization of user interface for adding a feed:
■ FN_addbutton. Localizes the label of the Add menu item that appears in the highest
node’s pop-up.
■ FN_askurl_title. Localizes the title of the New Feed dialog box.
■ FN_askurl_msg. Localizes the message that appears in the New Feed dialog box.
■ FN_askurl_err. Localizes the error string that is displayed if the URL is invalid.
Localization of user interface for adding a folder:
■ FN_addfolderbutton. Localizes the label of the Add Folder menu item that appears
in the highest node’s pop-up.
■ FN_askfolder_msg. Localizes the message that appears in the Add Folder dialog
box.
■ FN_askfolder_title. Localizes the title of the Add Folder dialog box.
These questions relate to branding, the activity of personalizing an application built on top of
the NetBeans Platform. The IDE provides a panelin the Project Properties dialog box of
module suite projects to help you with branding.
1. Right-click the feedreader-suite project node (not the FeedReader project node) and
choose Properties. In the Project Properties dialog box, click Application.
2. In the Application panel, click Create Standalone Application. The IDE prompts you to
exclude the IDE-related modules. Click Exclude, because the FeedReader application
will not need IDE-related modules, such as those that provide the Source Editor in
NetBeans IDE.
4. Click Browse to browse to the rss16.gif icon. The icon will be displayed in the Help
> About dialog box.
5. In the Splash Screen panel, click Browse to browse to splash.gif. Optionally, change
the color and text size of the progress bar. Or, if you do not want a progress bar,
unselect Enabled.
6. Click OK.
The branding folder is created in the FeedReader Application project. It is visible in the
Files window (Ctrl-2).
7. In the Files window, expand the FeedReader Application project node. Then continue
expanding nodes until you find this one:
branding/modules/org-netbeans-core-window.jar/org/netbeans/core/windows
8. Right-click the node, choose New > File/Folder, and select Folder in the Other
category. Click Next and name the folder resources. Click Finish.
9. Right-click the new resources node, choose New > File/Folder, and select XML
Document in the XML category. Click Next. Name the file layer. Click Next and then
click Finish. Replace the content of the new layer.xml file with the following:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE filesystem PUBLIC "-//NetBeans//DTD Filesystem 1.1//EN"
"http://www.netbeans.org/dtds/filesystem-1_1.dtd">
<!--
This is a ‘branding’ layer. It gets merged with the layer file it’s branding.
In this case, it’s just hiding menu items and toolbars we don’t want.
-->
<filesystem>
<folder name="Menu">
<folder name="File">
<file name="org-openide-actions-
SaveAction.instance_hidden"/>
<file name="org-openide-actions-
SaveAllAction.instance_hidden"/>
<file name="org-netbeans-core-actions-
RefreshAllFilesystemsAction.instance_hidden"/>
<file name="org-openide-actions-
PageSetupAction.instance_hidden"/>
<file name="org-openide-actions-
PrintAction.instance_hidden"/>
</folder>
<folder name="Edit_hidden"/>
<folder name="Tools_hidden"/>
</folder>
</filesystem>
1. In the Projects window, right-click the FeedReader Application project node and choose
Build ZIP Distribution. The Output window shows you where the ZIP distribution is
created.
2. In your filesystem, find the feedreader.zip distribution in the dist folder in your
project directory. Unzip it. Launch the application, which you will find in the bin
folder. During start up, the splash screen is displayed. When the application has started
up, go to the Help > About dialog box and notice the icon and splash screen that you
specified in the Branding the Application section.
When it is up and running, the FeedReader application displays the RSS/Atom Feeds
window, containing a node called RSS/Atom Feeds. See Playing with the Application for
details.