This tutorial demonstrates how to create a module that extends the functionality offered by one of the IDE's editors. The IDE has several editors—for example, the XML editor, the Java editor, the JSP editor, and the SQL editor. Normally all the IDE's editors are referred to collectively as the Source Editor. However, each of the editors is distinct—its functionality is targeted at the file type for which it exists. In this tutorial, you add an action to the XML editor. After you create and install the module, and you open an XML file, the editor's contextual menu will include a menu item that displays the XML file's tags in the Output Window.
For more information on working with 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:
Now that you know what the end result looks like, you will create the module from scratch and learn about each part while creating it.
Before you start writing the module, you have to make sure you that your project is set up correctly. The IDE provides a wizard that sets up all the basic files needed for a module.
The IDE creates the Show XML Structure 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.
You will need to subclass several classes that belong to NetBeans APIs. Each has to be declared as a module dependency. Use the Project Properties dialog box for this purpose.
Click OK to exit the Project Properties dialog box.
Click Next.
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.
public void performAction(Node[] activatedNodes) { EditorCookie editorCookie = activatedNodes[0].getCookie(EditorCookie.class); String tabName = NbBundle.getMessage(ShowXMLStructureAction.class,"LBL_tabName"); // "XML Tree" tab is created in output window for writing the list of tags InputOutput io = IOProvider.getDefault().getIO(tabName,false); io.select(); //XML Tree tab is selected OutputWriter writer = io.getOut(); try { writer.reset(); //clean the output window java.io.InputStream is = ((org.openide.text.CloneableEditorSupport)editorCookie).getInputStream(); parse(new InputSource(is)); is.close(); for (int i=0;i<tags.length;i++) { writer.println(tags[i]); //write tag to output window } } catch (IOException ex) { } catch (SAXException ex){ writer.println("Parse Error: "+ex.getMessage()); } writer.flush(); writer.close(); }
/** Parses XML document and creates the list of tags */ private void parse(InputSource is) throws IOException, SAXException { XMLReader xmlReader = XMLUtil.createXMLReader(); TagHandler handler = new TagHandler(); xmlReader.setContentHandler(handler); xmlReader.parse(is); tags = handler.getTags(); }
private String[] tags;
ACT_name=Show XML Structure LBL_tabName=XML Structure
package org.netbeans.modules.showxmlstructure; import org.xml.sax.Attributes; import org.xml.sax.SAXException; /** Specific XML handler used for creating list of starting and ending tags, e.g. : * <AAA> * <BBB> * <CCC/> * </BBB> * </AAA> */ public class TagHandler extends org.xml.sax.helpers.DefaultHandler { private final int indentLength=2; private java.util.List tagList; private String indent; private String space; private String lastElement; TagHandler() { tagList = new java.util.ArrayList(); StringBuffer indentBuf = new StringBuffer(); for (int i=0;i<indentLength;i++) indentBuf.append(' '); space=indentBuf.toString(); indent=""; } public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { tagList.add(indent+"<"+qName+">"); indent+=space; lastElement=qName; } public void endElement(String uri, String localName, String qName) throws SAXException { indent=indent.substring(indentLength); if (qName.equals(lastElement)) { int lastIndex = tagList.size()-1; String lastInList = (String)tagList.get(lastIndex); String replacedString = lastInList.replace(">","/>"); tagList.set(lastIndex,replacedString); } else { tagList.add(indent+"</"+qName+">"); } lastElement=null; } String[] getTags() { String[] tags = new String[tagList.size()]; tagList.toArray(tags); return tags; } }
The IDE uses an Ant build script to build and install your module. The build script is created for you when you create the module project.
In the Projects window, right-click the Show XML Structure project and choose Install/Reload in Target Platform.
The module is built and installed in the target IDE or Platform. The target IDE or Platform opens so that you can try out your new module. The default target IDE or Platform is the installation used by the current instance of the development IDE. Note that when you run your module, you will be using a temporary test user directory, not the development IDE's user directory.
The NBM file is created and you can view it in the Files window (Ctrl-2):
For more information about creating and developing NetBeans modules, see the following resources:
Version
|
Date
|
Changes
|
1 | 11 July 2005 | Initial version |
2 | 27 September 2005 |
|
3 | 28 September 2005 |
|
4 | 11 June 2007 | Worked through whole tutorial, and cleaned up, for 6.0, also changed screenshots. |
5 | 17 November 2007 | Fixed spacing between steps. Tried out the attached sample, and it works as described. |
Issue Number | Description | Status |
1 | Code and tutorial itself need to be reviewed. | To be fixed. |
2 | Tutorial needs to be updated once Phase III and IV are complete. | Done. |
3 | Some APIs used in this tutorial have deprecated methods. This will produce errors in the Output window, but should not impact functioning of module. | To be fixed. |
4 | Clear explanations -- and links to Javadoc -- to be added for all APIs, classes, and methods. Also Javadoc links for each of the dependencies and why they are needed in this tutorial. | To be fixed. |
5 | Maybe other identifiers for JSP editor, HTML editor, etc. should be mentioned. For example, instead of "xml" (in layer.xml), use "html", "x-properties", "base" etc. | To be fixed. |
6 | Explain what a cookie is. Explain what a cookie action is. | To be fixed. |
7 | Need to change downloadable, because currently the tag handler and the show XML action are separate files while in the downloadable code, they're in the same file. For the same reason, must change screenshots where one file instead of two are shown. | To be fixed. |