blob: 0912fbb7573e14a7ebc0f78de9010ae951d8f148 [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 Editor Component Palette 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 Editor Component Palette Module Tutorial - Apache NetBeans
:keywords: Apache NetBeans Platform, Platform Tutorials, NetBeans Editor Component Palette Module Tutorial
This tutorial demonstrates how to create a component palette that provides drag-and-drop code snippets for a new file type. Code snippets serve to speed up coding. The IDE provides a component palette for JSP files, HTML files, and Form files. In this tutorial, you learn how to create a component palette for Java source files.
NOTE: This document uses the NetBeans IDE 6.5 Release. If you are using an earlier version, see link:60/nbm-palette-api2.html[the 6.0/6.1 version of this document].
Optionally, for troubleshooting purposes, you can link:http://plugins.netbeans.org/PluginPortal/faces/PluginDetailPage.jsp?pluginid=2761[download the completed sample].
== Introduction to Component Palettes
In this tutorial, you implement several classes provided by the link:http://bits.netbeans.org/dev/javadoc/org-netbeans-spi-palette/overview-summary.html[NetBeans Palette API]. Then you register the new component palette in the ``layer.xml`` file, for the ``text/x-java`` MIME type. The palette that you create in this way will only be visible if a Java source file is open.
If you do not want to create a new component palette, but only want to add a code snippet to an existing component palette, see the link:nbm-palette-api1.html[NetBeans Code Snippet Module Tutorial].
=== Getting to Know the Sample
Take the following steps to install the sample:
[start=1]
1. link:http://plugins.netbeans.org/PluginPortal/faces/PluginDetailPage.jsp?pluginid=2761[Download the completed sample] from the Plugin Portal and install it in the IDE.
[start=2]
1. In the New Project wizard, you will find the sample here:
image::images/componentpalette_65-new-proj-dialog.png[]
[start=3]
1. Complete the wizard and your project source structure will be as follows:
image::images/componentpalette_65-new-proj-wiz.png[]
[start=4]
1. Right-click the project node and choose Run. A new instance of the IDE opens and the module is installed.
[start=5]
1. Verify that the module is correctly installed by opening a Java source file. You should now see your new Java source file palette, containing one item:
image::images/componentpalette_65-open-java-file.png[]
[start=6]
1. Drag the item into the Java source file and, when you drop the item, you will see the related customizer:
image::images/componentpalette_65-customizer-in-java-file.png[]
[start=7]
1. When you complete the customizer, you will have a new main method, together with the comment you typed in the customizer:
image::images/componentpalette_65-dropped-main-method.png[]
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.
== 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.
[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 ``JavaSourceFilePalette`` in the Project Name field. Change the Project Location to any directory on your computer. Leave the Standalone Module option and Set as Main Project checkbox selected. Click Next.
[start=3]
1. In the Basic Module Configuration panel, type ``org.netbeans.modules.javasourcefilepalette`` in Code Name Base.
[start=4]
1. Select "Generate XML Layer". Leave the locations of both the localizing bundle and the XML layer file so that they will be stored in a package with the name ``org/netbeans/modules/javasourcefilepalette`` . Click Finish.
The IDE creates the ``JavaSourceFilePalette`` 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).
== Creating the Component Palette and Code Snippets
=== Specifying the Module's Dependencies
You will need to subclass several classes that belong to link:http://bits.netbeans.org/dev/javadoc/index.html[NetBeans APIs]. Each has to be declared as a Module dependency. Use the Project Properties dialog box for this purpose.
[start=1]
1. In the Projects window, right-click the ``JavaSourceFilePalette`` project node and choose Properties. In the Project Properties dialog box, click Libraries.
[start=2]
1. For each of the following APIs, click "Add...", select the name from the Module list, and then click OK to confirm it:
image::images/componentpalette_65-proj-props-1.png[]
[start=3]
1. Click OK to exit the Project Properties dialog box.
[start=4]
1. In the Projects window, expand the Important Files node, double-click the Project Metadata node, and note the long list of APIs that you selected have been declared as module dependencies.
=== Creating the Component Palette
Component Palettes are defined by the link:http://bits.netbeans.org/dev/javadoc/org-netbeans-spi-palette/overview-summary.html[NetBeans Palette API]. Implementing the NetBeans Palette API for this tutorial means implementing the following NetBeans Palette API classes:
|===
|*File* |*Description*
| `` link:http://bits.netbeans.org/dev/javadoc/org-netbeans-spi-palette/org/netbeans/spi/palette/PaletteFactory.html[PaletteFactory]`` |Creates a new instance of the Component Palette. To do so, it invokes the ``createPalette`` method which creates a new palette from a specified folder registered in the ``layer.xml`` .
| `` link:http://bits.netbeans.org/dev/javadoc/org-netbeans-spi-palette/org/netbeans/spi/palette/PaletteController.html[PaletteController]`` |Provides access to data in the Component Palette.
| `` link:http://bits.netbeans.org/dev/javadoc/org-netbeans-spi-palette/org/netbeans/spi/palette/PaletteActions.html[PaletteActions]`` |Provides the actions on the palette, categories, and items in the palette.
|===
To use the Palette API to create the palette in this tutorial, take the following steps:
[start=1]
1. Right-click the ``JavaSourceFilePalette`` project node and choose New > Java Class. Create a Java file called ``JavaSourceFileLayerPaletteFactory`` .
[start=2]
1. Replace the default content of the ``JavaSourceFileLayerPaletteFactory.java`` file with the following:
[source,java]
----
public class JavaSourceFileLayerPaletteFactory {
*//Name of the folder in the layer.xml file that is the root of the palette:*
public static final String JAVA_PALETTE_FOLDER = "JavaPalette";
private static PaletteController palette = null;
public JavaSourceFileLayerPaletteFactory() {
}
public static PaletteController createPalette() {
try {
if (null == palette)
palette = PaletteFactory.createPalette(JAVA_PALETTE_FOLDER, new MyActions());
return palette;
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
return null;
}
private static class MyActions extends PaletteActions {
*//Add new buttons to the Palette Manager here:*
public Action[] getImportActions() {
return null;
}
*//Add new contextual menu items to the palette here:*
public Action[] getCustomPaletteActions() {
return null;
}
*//Add new contextual menu items to the categories here:*
public Action[] getCustomCategoryActions(Lookup arg0) {
return null;
}
*//Add new contextual menu items to the items here:*
public Action[] getCustomItemActions(Lookup arg0) {
return null;
}
*//Define the default action here:*
public Action getPreferredAction(Lookup arg0) {
return null;
}
}
}
----
=== Adding a DragAndDropHandler
In this section, we change the ``PaletteController`` , in the code above, and add a ``DragAndDropHandler`` . In doing so, we will let the user drag code snippets FROM the editor INTO the palette:
[source,java]
----
public static PaletteController createPalette() {
try {
if (null == palette) {
*//Add null for the PaletteFilter, which we are not using here,
//and then instantiate your implementation of the DragAndDropHandler*:
palette = PaletteFactory.createPalette(JAVA_PALETTE_FOLDER, new MyActions(), *null, new MyDragAndDropHandler()*);
}
return palette;
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
return null;
}
*//Definition of the DragAndDropHandler:
private static class MyDragAndDropHandler extends DragAndDropHandler {
MyDragAndDropHandler() {
super(true);
}
//Maybe you don't like the default 'add to palette' implementation,
//so you could create your own here:
@Override
public void customize(ExTransferable t, Lookup item) {
}
}*
----
The above default implementation is all you need. Now the user will be able to drag from the editor into the palette.
=== Creating the Code Snippets
Each code snippet requires the following files:
* A Java class that defines the piece of code to be dragged into the Source Editor. This Java class must refer to ``JavaSourceFilePaletteUtilities.java`` , which defines how the piece of code should be inserted. For example, indentation and formatting are defined here.
* Optionally, a customizer where the user can type something that will be added to the snippet, such as comments.
* A properties file that defines the labels and tooltips.
* A 16x16 pixel image for the 'Small Icon' display.
* A 32x32 pixel image for the 'Big Icon' display.
After you have created or added the above files to the NetBeans module, you declare them in a resource declaration XML file, which you register in the NetBeans System Filesystem by using the ``layer.xml`` file.
For details on all of the items above, work through the link:nbm-palette-api1.html[NetBeans Code Snippet Module Tutorial] and refer to the sample that you downloaded at the start of this tutorial.
== Registering the Module
In this section, we register the menu item and code snippets in the ``layer.xml`` file and in the ``Bundle.properties`` file.
[start=1]
1. Add the following tags to the ``layer.xml`` file, between the <filesystem> tags:
[source,xml]
----
<folder name="Editors">
<folder name="text">
<folder name="x-java">
<file name="PaletteFactory.instance">
<attr name="instanceOf" stringvalue="org.netbeans.spi.palette.PaletteController"/>
<attr name="instanceCreate" methodvalue="org.netbeans.modules.javasourcefilepalette.JavaSourceFileLayerPaletteFactory.createPalette"/>
</file>
</folder>
</folder>
</folder>
<folder name="JavaPalette">
<folder name="Items">
<attr name="SystemFileSystem.localizingBundle" stringvalue="org.netbeans.modules.javasourcefilepalette.Bundle"/>
<file name="Item.xml" url="items/resources/Item.xml">
<attr name="SystemFileSystem.localizingBundle" stringvalue="org.netbeans.modules.javasourcefilepalette.Bundle"/>
</file>
</folder>
</folder>
----
[start=2]
1. Add the following to the ``Bundle.properties`` file that is in the same package as the ``layer.xml`` file:
[source,java]
----
JavaPalette/Items=Items
JavaPalette/Items/Item.xml=Item
----
The key-value pairs listed above localize the items registered in the ``layer.xml`` file.
== Building and Installing the Module
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.
=== Installing and Using the Module
[start=1]
1. In the Projects window, right-click the ``JavaSourceFilePalette`` project and choose Run.
The module is built and installed in the target platform. The target platform opens so that you can try out your new module. The default target platform is the installation used by the current instance of the development IDE.
[start=2]
1. Verify that the module is correctly installed by using it as shown in <<installing-sample,Installing the Sample>>.
=== Creating a Shareable Module Binary
[start=1]
1. In the Projects window, right-click the the project node and choose Create NBM.
The NBM file is created and you can view it in the Files window (Ctrl-2).
[start=2]
1. Make the module available to others by uploading it to the link:http://plugins.netbeans.org[Plugin Portal].
[start=3]
1. The recipient can install the module by using the Plugin Manager. Choose Tools > Plugins from the main menu.
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:http://bits.netbeans.org/dev/javadoc/index.html[NetBeans API Javadoc]
== Versioning
|===
|*Version* |*Date* |*Changes* |*Open Issues*
|1 |29 November 2005 |Initial version |
* Needs to be reviewed! Use at your own risk!
* Need to add explanation for adding own dialog box for predefining values.
* Explanatory text for the use of the NetBeans APIs to be added.
* Check for copy-paste errors.
* Need to add more Javadoc links, for NetBeans API classes in the code.
|2 |30 November 2005 |
* Changed the ZIP file because of problems (see link:https://bz.apache.org/netbeans/show_bug.cgi?id=69571[Issue 69571]).
* Removed hyphens and changed screenshots.
|All other issues from above must still be done.
|3 |8 December 2005 |
* Fixed reopened issue link:https://bz.apache.org/netbeans/show_bug.cgi?id=69571[Issue 69571]
|All other issues from above must still be done.
|4 |22 August 2006 |
* Minor tweaks.
|All other issues from above must still be done.
Discovered that the editor support file is missing in the tutorial, but not in the module that you can download from the top of the file.
Discovered that there are several gaps and tbds in this tutorial.
|5 |30 May 2007 |Began updating this tutorial for 6.0. |...
|6 |5 November 2008 |Began &amp; completed updating this tutorial for 6.5.
Main changes:
* New sample download link to Plugin Portal.
* Rewritten code to use ``layer.xml`` registration.
* Instead of JBoss palette and snippets for JBoss XML file, created a palette for Java source files, which is now possible because editor and palette do not need to be in the same module. Therefore sections like 'Recognizing the New File Type' are not necessary (except if the palette is for a new file type, in which case the File Type Module tutorial should first be used).
* Omitted the code for creating the palette item, because it is already described in the Code Snippet Module Tutorial.
|...
|===