blob: f160fdbb513c724a3b41e7f1cdd048b954d10e6a [file] [log] [blame]
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
= NetBeans XML Editor Extension Module Tutorial
:jbake-type: platform_tutorial
:jbake-tags: tutorials
:jbake-status: published
:syntax: true
:source-highlighter: pygments
:toc: left
:toc-title:
:icons: font
:experimental:
:description: NetBeans XML Editor Extension Module Tutorial - Apache NetBeans
:keywords: Apache NetBeans Platform, Platform Tutorials, NetBeans XML Editor Extension Module Tutorial
This tutorial demonstrates how to create a module that extends the functionality offered by one of NetBeans 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 distinctits 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:
image::images/taghandler_72_result.png[]
NOTE: This document uses NetBeans Platform 8.0 and above with NetBeans IDE 8.0 and above. If you are using an earlier version of NetBeans IDE or the NetBeans Platform, see link:74/nbm-xmleditor.html[the previous version of this document].
For troubleshooting purposes, you are welcome to download the link:http://web.archive.org/web/20170409072842/http://java.net/projects/nb-api-samples/show/versions/8.0/tutorials/ShowXMLStructure[completed tutorial source code].
== Setting up the Module Project
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.
=== Creating the Module Project
[start=1]
1. Choose File > New Project (Ctrl+Shift+N). Under Categories, select NetBeans Modules. Under Projects, select Module. Click Next.
[start=2]
1. In the Name and Location panel, type ``ShowXMLStructure`` in the Project Name field. Change the Project Location to any directory on your computer. Click Next.
[start=3]
1. In the Basic Module Configuration panel, type ``org.netbeans.modules.showxmlstructure`` in Code Name Base. Click Finish.
The IDE creates the ``ShowXMLStructure`` 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).
=== Specifying the Module's Dependencies
You will need to subclass several classes that belong to the NetBeans APIs. Each is declared as a module dependency.
[start=1]
1. In the Projects window, right-click the Libraries node and choose Add Module Dependency:
image::images/taghandler_72_add-dep.png[]
[start=2]
1. Search for each of the following APIs in the Add Module Dependency dialog, select the API, and then click OK to confirm it:
* ``I/O APIs``
* ``Nodes API``
* ``Text API``
* ``Utilities API``
* ``Window System API``
In the Projects window, you should see the following list of dependencies:
image::images/taghandler_72_add-dep-1.png[]
== Coding the Module
In this section, you use the New Action wizard to create a new Java class, which is annotated such that the user will be able to invoke it after right-clicking within the XML Editor.
[start=1]
1. Right-click the project node and choose New > Other. Under Categories, select Module Development. Under Projects, select Action.
image::images/taghandler_72_new-action-1.png[]
Click Next.
[start=2]
1. In the Action Type panel, click Conditionally Enabled. Select `` link:http://bits.netbeans.org/dev/javadoc/org-openide-text/org/openide/cookies/EditorCookie.html[EditorCookie]`` . Because of this selection, the Action will be enabled when an EditorCookie is available in the Lookup. That will be the case whenever a document is open in the Source Editor. You should now see the following:
image::images/taghandler_72_new-action-2.png[]
Click Next.
[start=3]
1. In the GUI Registration panel, select the Edit category in the Category drop-down list. The Category drop-down list controls where an Action is shown in the Keyboard Shortcuts editor in the IDE.
Next, select Editor Context Menu Item and then select the ``text/xml`` MIME type, as shown below:
image::images/taghandler_72_new-action-3.png[]
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.
[start=4]
1. In the Name and Location panel, type ``ShowXMLStructureActionListener`` as the Class Name and type ``Show XML Structure`` as the Display Name. You should now see the following:
image::images/taghandler_72_new-action-4.png[]
Menu items provided by contextual menus do not display icons. Therefore, click Finish and ``ShowXMLStructureActionListener.java`` is added to the package. The content of the file is as follows:
[source,java]
----
package org.netbeans.modules.showxmlstructure;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionRegistration;
import org.openide.cookies.EditorCookie;
import org.openide.util.NbBundle.Messages;
link:http://bits.netbeans.org/dev/javadoc/org-openide-awt/org/openide/awt/ActionID.html[@ActionID](
category = "Edit",
id = "org.netbeans.modules.showxmlstructure.ShowXMLStructureActionListener")
link:http://bits.netbeans.org/dev/javadoc/org-openide-awt/org/openide/awt/ActionRegistration.html[@ActionRegistration](
displayName = "#CTL_ShowXMLStructureActionListener")
link:http://bits.netbeans.org/dev/javadoc/org-openide-awt/org/openide/awt/ActionReference.html[@ActionReference](path = "Editors/text/xml/Popup", position = 1100)
link:http://bits.netbeans.org/dev/javadoc/org-openide-util/org/openide/util/NbBundle.Messages.html[@Messages]("CTL_ShowXMLStructureActionListener=Show XML Structure")
public final class ShowXMLStructureActionListener implements ActionListener {
private final EditorCookie context;
public ShowXMLStructureActionListener(EditorCookie context) {
this.context = context;
}
@Override
public void actionPerformed(ActionEvent ev) {
// TODO use context
}
}
----
[start=5]
1. In the Source Editor, fill out the ``actionPerformed`` method as follows, after reading and understanding the comments in the code:
[source,java]
----
@Override
public void actionPerformed(ActionEvent ev) {
// "XML Structure" tab is created in Output Window for writing the list of tags:
link:http://bits.netbeans.org/dev/javadoc/org-openide-io/org/openide/windows/InputOutput.html[InputOutput] io = link:http://bits.netbeans.org/dev/javadoc/org-openide-io/org/openide/windows/IOProvider.html[IOProvider].getDefault().getIO(Bundle.CTL_ShowXMLStructureActionListener(), false);
io.select(); //"XML Structure" tab is selected
try {
//Get the InputStream from the EditorCookie:
InputStream is = ((org.openide.text.CloneableEditorSupport) context).getInputStream();
//Use the NetBeans org.openide.xml.XMLUtil class to create a org.w3c.dom.Document:
Document doc = link:http://bits.netbeans.org/dev/javadoc/org-openide-util/org/openide/xml/XMLUtil.html[XMLUtil.parse(new InputSource(is), true, true, null, null)];
//Create a list of nodes, for all the elements:
NodeList list = doc.getElementsByTagName("*");
//Iterate through the list:
for (int i = 0; i < list.getLength(); i++) {
//For each node in the list, create a org.w3c.dom.Node:
org.w3c.dom.Node mainNode = list.item(i);
//Create a map for all the attributes of the org.w3c.dom.Node:
NamedNodeMap map = mainNode.getAttributes();
//Get the name of the node:
String nodeName = mainNode.getNodeName();
//Create a StringBuilder for the Attributes of the Node:
StringBuilder attrBuilder = new StringBuilder();
//Iterate through the map of attributes:
for (int j = 0; j < map.getLength(); j++) {
//Each iteration, create a new Node:
org.w3c.dom.Node attrNode = map.item(j);
//Get the name of the current Attribute:
String attrName = attrNode.getNodeName();
//Add the current Attribute to the StringBuilder:
attrBuilder.append("*").append(attrName).append(" ");
}
//Print the element and its attributes to the Output window:
io.getOut().println("ELEMENT: " + nodeName
+ " --> ATTRIBUTES: " + attrBuilder.toString());
}
//Close the InputStream:
is.close();
} catch (SAXException ex) {
Exceptions.printStackTrace(ex);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
}
----
[start=6]
1. You will need these import statements:
[source,java]
----
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionRegistration;
import org.openide.cookies.EditorCookie;
import org.openide.util.Exceptions;
import org.openide.util.NbBundle.Messages;
import org.openide.windows.IOProvider;
import org.openide.windows.InputOutput;
import org.openide.xml.XMLUtil;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
----
== Building and Installing the Module
In the Projects window, right-click the ``ShowXMLStructure`` project and choose Run.
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.
[start=1]
1. Open an XML file and right-click anywhere in the Source Editor. Notice the new popup menu item called "Show XML Structure".
[start=2]
1.
Choose the menu item and notice that the tag handler prints all the elements and attributes to the Output window (Ctrl-4), which is at at the bottom of the IDE, as shown below:
image::images/taghandler_72_result.png[]
link:http://netbeans.apache.org/community/mailing-lists.html[Send Us Your Feedback]
== Next Steps
For more information about creating and developing NetBeans modules, see the following resources:
* link:https://netbeans.apache.org/kb/docs/platform.html[Other Related Tutorials]
* link:https://bits.netbeans.org/dev/javadoc/[NetBeans API Javadoc]