当前页面: 开发资料首页 → Netbeans 专题 → Recognizing a File Type Tutorial
摘要: This tutorial shows you how to write a module that lets the IDE (or any other application built on the NetBeans Platform) recognize a new file type. File types that are recognized in the IDE have their own icons, menu items, and behavior.
This tutorial shows you how to write a module that lets the IDE (or any other application built on the NetBeans Platform) recognize a new file type. File types that are recognized in the IDE have their own icons, menu items, and behavior. The "files" being shown are FileObjects—wrappers around java.io.File or, in the case of configuration files, typically wrappers around data stored in some other way, such as inside XML files in modules. What you actually see are Nodes, which provide functionality like actions and localized names to objects like files. In between Nodes and FileObjects are DataObjects. A DataObject is like a FileObject, except that it knows what kind of file is being shown, and there are usually different types of DataObject for files with different extensions and XML files with different namespaces. Each DataObject is provided by a different module, each implementing support for one or more file types—for example, the Image module makes it possible to recognize and open .gif and .png files.
A module that recognizes a file type installs a DataLoader—a factory for a file-type-specific DataObject. When a folder is expanded, the IDE asks each known DataLoader, "Do you know what this is?" The first one that says "Yes" creates the DataObject for the file. In order to actually display something for each file, the system calls DataObject.getNodeDelegate() for each DataObject and the Nodes are what you actually see in the IDE.
Below, the diagram on the left shows what each item mentioned above makes available; the diagram on the right shows the relationship between them:
In this tutorial, you create a module that installs a DataLoader for JAR manifest files (.mf extension). By default, a manifest file is treated as any other file that the IDE does not recognize—it is treated as a text file and, as a result, the IDE provides the same functionality for manifest files as it does for text files. Once you have created the module, you will be shown how to enhance it with functionality that will be available to manifest files only. When you complete the development cycle, you can easily let others make use of your module—the IDE lets you create a binary that you can send to others, who can then install it through the Update Center.
The following topics are covered below:
Once the software is installed, this tutorial can be completed in 30 minutes.
For more information on working with NetBeans modules, see the NetBeans Development Project home on the NetBeans website. If you have questions, visit the NetBeans Developer FAQ or use the feedback link at the top of this page.
Before you begin, you need to install the following software on your computer:
Take the following steps to install the sample:
Click Next.
The IDE creates the Manifest File Type project. The project contains all of your 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). For example, the Projects window should now look as follows:
For basic information on each of the files above, see the Introduction to NetBeans Module Development.
The File Recognition panel should now look as follows:
Note the following about the fields in the File Recognition panel:
Note that manifests in JAR files are "MANIFEST.MF" and can be case-sensitive (at least on Unix). For this reason, you specify two MIME types in this tutorial—.mf and .MF.
Click Next.
Note that several 16x16 pixel image files are found within your NetBeans installation directory, for example, in this location:
enterprise2\jakarta-tomcat-5.5.7\server\webapps\admin\images.
For purposes of this tutorial, the Datasource.gif image in the above directory is used.
This is what it looks like:
The Projects window should now look as follows:
Each of the newly generated files is briefly introduced:
Click Next.
Next, Unselect Global Menu Item and then select File Type Contect Menu Item. In the Content Type drop-down list, select the MIME type you specified above in the New File Type wizard, as shown below:
Notice that you can set the position of the menu item and that you can separate the menu item from the item before it and after it. Click Next.
protected void performAction(Node[] activatedNodes) { ManifestDataObject d = (ManifestDataObject) activatedNodes[0].getCookie(ManifestDataObject.class); FileObject f = d.getPrimaryFile(); String displayName = FileUtil.getFileDisplayName(f); String msg = "I am " + displayName + "!"; NotifyDescriptor nd = new NotifyDescriptor.Message(msg); DialogDisplayer.getDefault().notify(nd); }
Press Alt-Shift-F. The IDE automatically adds import statements to the top of the class. Some code is still underlined in red, to indicate that not all of the required packages are on the classpath. Right-click the project node, choose Properties, and click Libraries in the Project Properties dialog box. Click add at the top of the Libraries pane and add the Dialogs API.
In the MyAction.java class, press Alt-Shift-F again. The red underlining disappears because the IDE finds the required packages in the Dialogs API.
As you can see from the last two steps, the System Filesystem Browser can be used to quickly reorganize the sequence of the items that are registered in the System Filesystem.
The IDE uses an Ant build script to build and install your module. The build script is created for you when you create the project.
The module is built and installed in the target IDE. The target IDE opens so that you can try out your new module. The default target IDE is the installation used by the current instance of the IDE.
Notice that the Module Manifest file has the icon you assigned to it in its module and that the list of actions defined in its layer.xml file is available from the right-click contextual menu:
If you want to provide default code via the dummy template, add the code to the ManifestTemplate.mf file that the New File Type wizard created for you.
The NBM file is created and you can view it in the Files window (Ctrl-2):
Some functionality you might want to add:
For more information about creating and developing NetBeans modules, see the following resources:
Version
|
Date
|
Changes
|
1 | 25 August 2005 |
|
2 | 23 September 2005 |
|
3 | 28 September 2005 |
|
4 | 4 October 2005 |
|
4 | 4 November 2005 |
|
5 | 29 November 2005 |
|
6 | 21 April 2006 |
|