<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<!-- -*- xhtml -*- --> | |
<title>NetBeans Editor Component Palette Module Tutorial for NetBeans Platform 6.0</title> | |
<link rel="stylesheet" type="text/css" href="https://netbeans.org/netbeans.css"> | |
<meta name="AUDIENCE" content="NBUSER"> | |
<meta name="TYPE" content="ARTICLE"> | |
<meta name="EXPIRES" content="N"> | |
<meta name="developer" content="gwielenga@netbeans.org"> | |
<meta name="indexed" content="y"> | |
<meta name="description" | |
content="A short guide to using the Nodes API."> | |
<!-- Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. --> | |
<!-- Use is subject to license terms.--> | |
</head> | |
<body> | |
<h1>NetBeans Editor Component Palette Module Tutorial</h1> | |
<p>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 a different file type—one | |
that is not recognized by the IDE by default. | |
<p><strong class="notes">Note: </strong>This is not the latest version of this | |
document. It applies to NetBeans IDE 6.0/6.1 only. | |
<a href="../nbm-palette-api2.html">Click here</a> to | |
see the most up to date version. | |
<p><b>Contents</b></p> | |
<img src="../../images/articles/60/netbeans-stamp60-61.gif" class="stamp" width="114" height="114" alt="Content on this page applies to NetBeans IDE 6.1" title="Content on this page applies to NetBeans IDE 6.1"> </p> | |
<ul class="toc"> | |
<li><a href="#intro">Introduction to Component Palettes</a> | |
<ul> | |
<li><a href="#installing-sample">Getting to Know the Sample</a></li> | |
</ul> | |
</li> | |
<li><a href="#creatingthemoduleproject">Setting Up the Module Project</a> | |
<li><a href="#recognizing-filetype">Recognizing the New File Type</a> | |
<li><a href="#coding-module">Creating the Component Palette and Code Snippets</a> | |
<ul> | |
<li><a href="#creating-palettes">Creating the Component Palette</a> | |
<li><a href="#creating-snippets">Creating the Code Snippets</a> | |
</ul> | |
<li><a href="#registering-module">Registering the Module</a> | |
<ul> | |
<li><a href="#declaring-resources">Declaring the Resources</a> | |
<li><a href="#registering-resoucres">Registering the Resources</a> | |
</ul> | |
</li> | |
<li><a href="#reusing">Reusing Code Snippets from Other Palettes</a> | |
<li><a href="#building">Building and Installing the Module</a> | |
<ul> | |
<li><a href="#install-plugin">Installing and Using the Module</a> | |
<li><a href="#share-plugin">Creating a Shareable Module Binary</a> | |
</ul> | |
</ul> | |
<p><b>To follow this tutorial, you need the software and resources listed in the following | |
table.</b></p> | |
<table> | |
<tbody> | |
<tr> | |
<th class="tblheader" scope="col">Software or Resource</th> | |
<th class="tblheader" scope="col">Version Required</th> | |
</tr> | |
<tr> | |
<td class="tbltd1">NetBeans IDE</td> | |
<td class="tbltd1">version | |
<a href="http://download.netbeans.org/netbeans/6.1/final/">version 6.1</a> or<br> | |
version 6.0</td> | |
</tr> | |
<tr> | |
<td class="tbltd1">Java Developer Kit (JDK)</td> | |
<td class="tbltd1"><a href="http://java.sun.com/javase/downloads/index.jsp">version 6</a> or<br> | |
version 5</td> | |
</tr> | |
</tbody> | |
</table> | |
<h2 class="tutorial"><a name="intro"></a>Introduction to Component Palettes</h2> | |
<p>In this tutorial, you first use the New File Type wizard to create the | |
classes that enable the IDE to recognize | |
a new file type. Then, you implement several classes provided by the | |
<a href="https://netbeans.org/download/dev/javadoc/org-netbeans-spi-palette/overview-summary.html">NetBeans Palette API</a>. | |
Finally, you register the new component palette in the <tt>layer.xml</tt> file. | |
<p>The component palette that you create in this tutorial will be used by the JBoss deployment descriptor file (<tt>jboss-web.xml</tt>). | |
The <tt>jboss-web.xml</tt> file is distinguished from all other XML files by its namespace: | |
<p><pre class=examplecode><!DOCTYPE jboss-web | |
PUBLIC "-//JBoss//DTD Web Application 2.3//EN" | |
"http://www.jboss.org/j2ee/dtd/jboss-web_3_0.dtd"></pre> | |
<p>The component palette that you create in this tutorial will only be visible when a file with the namespace above | |
is open in the IDE's Source Editor. For these files only, the component palette will include code snippets for three typical elements in | |
a <tt>jboss-web.xml</tt> file: "Context Root", "Security Domain", | |
and "Resource Reference". As shown below, the tooltip will display the tags that will be inserted at the cursor in the Source Editor | |
when the code snippet is dragged-and-dropped: | |
<p><img border="1" src="../../images/tutorials/componentpalette/completed-module-60.png" alt="Completed module."> | |
<p>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 <a href="nbm-palette-api1.html">NetBeans Code Snippet Module Tutorial</a>. | |
<div class="indent"> | |
<h3 class="tutorial"><a name="installing-sample"></a>Getting to Know the Sample</h3> | |
<p>Take the following steps to install the sample: | |
<ol><li>Unzip the <a href="https://netbeans.org/files/documents/4/651/Jbosswebxml.zip">attached file</a>. | |
<p><li>In the IDE, choose File > Open Project and browse to the folder that contains the unzipped file. | |
Open the module project. It should look as follows:</p> | |
<p><img src="../../images/tutorials/componentpalette/completed-projects-window-60.png" border="1" alt="Completed projects window."> | |
<p><li>Right-click the project node and choose Install/Reload in Target Platform. The target | |
platform opens and the module is installed. | |
<p><li>Verify that the module is correctly installed by opening the Files window (Ctrl-2) and | |
double-clicking the <tt>test-jboss-web.xml</tt> file that you find there:</p> | |
<p><img src="../../images/tutorials/componentpalette/test-jboss-60.png" border="1" alt="Test file."> | |
<p>The file opens in the Source Editor and the component palette opens, and displays three code snippets, as | |
in the screenshot shown in the introduction of this tutorial. | |
If the component palette does not open automatically, press Ctrl-Shift-8 to open it manually. | |
<p><li>Choose Tools > Palette > JBoss Code Clips from the main menu bar, as shown below:</p> | |
<p><img src="../../images/tutorials/componentpalette/palette-menu-item-60.png" border="1" alt="New menu item."> | |
<p>The Palette Manager appears, containing your new code snippets, as shown below:</p> | |
<p><img src="../../images/tutorials/componentpalette/palette-manager-60.png" border="1" alt="New palette manager."> | |
</ol> | |
<p>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. | |
</div> | |
<br /> | |
<!-- ===================================================================================== --> | |
<h2 class="tutorial"><a name="creatingthemoduleproject"></a>Setting up the Module Project</h2> | |
<p>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.</p> | |
<ol> | |
<li>Choose File > New Project (Ctrl-Shift-N). Under Categories, select NetBeans Modules. Under projects, | |
select Module Project and click Next.</li> | |
<p><li>In the Name and Location panel, type <tt>Jbosswebxml</tt> in Project Name. | |
Change the | |
Project Location to any directory on your computer, such as <tt>c:\mymodules</tt>. Leave the Standalone Module radiobutton | |
and the Set as Main Project checkbox selected. | |
Click Next. | |
<p><li>In the Basic Module Configuration panel, replace <tt>yourorghere</tt> in Code Name Base with <tt>org.netbeans.modules</tt>, | |
so that the whole code name base is <tt>org.netbeans.modules.jbosswebxml</tt>. | |
Leave the location of the localizing bundle and XML layer, so that they will be stored in a | |
package with the name <tt>org/netbeans/modules/jbosswebxml</tt>. Click Finish.</ol> | |
<p> The IDE creates the <tt>Jbosswebxml</tt> | |
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).</ol> | |
<br /> | |
<!-- ===================================================================================== --> | |
<h2><a name="recognizing-filetype"></a>Recognizing the New File Type</h2> | |
<p>In order to treat JBoss deployment descriptor files | |
differently to all other types of files, we need to create | |
a NetBeans data object specifically for this | |
type of file, as well as a loader for loading our | |
new data object. | |
<ol> | |
<li>Right-click the project node and | |
choose New > Other and then, in the NetBeans Module Development | |
category, choose File Type. Click Next.</li> | |
<p><li>In the File Recognition panel, do the following: | |
<p><ul><li>Type <tt>text/x-jboss+xml</tt> in the MIME Type edit box. | |
<li>Type <tt>public-id="-//JBoss//DTD Web Application 2.3//EN"</tt> in the by XML Root Element edit box.</ul> | |
<p>The File Recognition panel should now look as follows:</p> | |
<p align="left"><img border="1" src="../../images/tutorials/componentpalette/filewizard1-60.png" alt="Step 1 of New File wizard."> | |
<p>Note the following about the fields in the File Recognition panel: | |
<ul> | |
<li><b>MIME Type.</b> Specifies the data object's unique MIME type. | |
<li><b>by | |
<ul><li> Filename Extension.</b> Specifies one or more file extensions that the IDE will recognize | |
as belonging to the specified MIME type. The file extension can optionally be preceded | |
by a dot. Separators are commas, spaces, or both. Therefore, all of the following are valid: | |
<p><ul><li><tt>.abc,.def</tt> | |
<li><tt>.abc .def</tt> | |
<li><tt>abc def</tt> | |
<li><tt>abc,.def ghi, .wow</ul></tt> | |
<p>Note that manifests in JAR files are "MANIFEST.MF" and can be case-sensitive (at least on Unix). For | |
this reason, you specify <i>two</i> MIME types in this tutorial—<tt>.mf</tt> and <tt>.MF</tt>. | |
<li><b>XML Root Element.</b> Specifies a unique namespace that distinguishes the XML file | |
type from all other XML file types. Since many XML files have the same extension (<tt>xml</tt>), | |
the IDE distinguishes between | |
XML files via their XML root elements. More specifically, the IDE can distinguish between namespaces and | |
the first XML element in XML files. In this tutorial, the distinction will be based on the XML file's | |
<tt>doctype</tt> element. | |
</ul></ul> | |
<p>Click Next. | |
<p><li>In the Name and Location panel, type <tt>Jbossdd</tt> as the Class Name Prefix | |
and browse to any 16x16 pixel image file as the new file type's icon, as shown below. | |
<p align="left"><p align="left"><img border="1" src="../../images/tutorials/componentpalette/filewizard2_60.png" alt="Step 2 of New File wizard."> | |
<p><b>Note:</b> If you do not have a suitable | |
16x16 pixel image file, you can right-click | |
on this image, download it, and | |
access it in the wizard above: <img src="../../images/tutorials/filetype/Datasource.gif" alt="Datasource.gif"> | |
<p><li>Click Finish.</p> | |
<p>The Projects window should now look as follows: | |
<p align="left"><img src="../../images/tutorials/componentpalette/projectswindow2_60.png" border="1" alt="Final Projects window."></ol> | |
<p>Each of the newly generated files is briefly introduced: | |
<ul> | |
<p><li><b>JbossddDataLoader.java.</b> Recognizes the <tt>text/x-jboss+xml</tt> MIME type. Functions as a factory for | |
the <tt>DataObject</tt>. For more information, see <a href="http://wiki.netbeans.org/wiki/view/DevFaqDataLoader">What is a DataLoader?</a>. | |
<p><li><b>JbossddResolver.xml.</b> Maps the MIME type to an <tt><xml-rule></tt> that restricts the <tt>DataLoader</tt> | |
to only recongize XML files that match the <tt><xml-rule></tt>. Change the rule so that a <tt><doc-type></tt> defines it, | |
as shown below (the line to be changed is highlighted below): | |
<p><pre class=examplecode><?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE MIME-resolver PUBLIC "-//NetBeans//DTD MIME Resolver 1.0//EN" | |
"https://netbeans.org/dtds/mime-resolver-1_0.dtd"> | |
<MIME-resolver> | |
<file> | |
<ext name="xml"/> | |
<resolver mime="text/x-jboss+xml"> | |
<xml-rule> | |
<b><doctype public-id="-//JBoss//DTD Web Application 2.3//EN"/></b> | |
</xml-rule> | |
</resolver> | |
</file> | |
</MIME-resolver></pre> | |
<p><li><b>JbossddDataObject.java.</b> Wraps a <tt>FileObject</tt>. <tt>DataObjects</tt> are produced by <tt>DataLoaders</tt>. | |
For more information, see <a href="wiki.netbeans.org/wiki/view/DevFaqDataObject">What is a DataObject?</a>. | |
<p><li><b>JbossddDataNode.java.</b> Provides what you <i>see</i> in the IDE—functionality like actions, icons, and localized names. | |
<p><li><b>JbossddDataLoaderBeanInfo.java.</b> Controls the appearance of the <tt>DataLoader</tt> in the Object Types section | |
of the Options window. | |
</ul> | |
</p> | |
<br /> | |
<!-- ===================================================================================== --> | |
<h2><a name="coding-module"></a>Creating the Component Palette and Code Snippets</h2> | |
<br /> | |
<div class="indent"> | |
<h3 class="tutorial"><a name="specifying"></a>Specifying the Module's Dependencies</h3> | |
<p>You will need to subclass several classes that belong to <a href="https://netbeans.org/download/dev/javadoc/">NetBeans APIs</a>. | |
Each has to be declared as a Module dependency. Use the Project Properties dialog box for this purpose. | |
<ol> | |
<li>In the Projects window, right-click the <tt>Jbosswebxml</tt> project node and choose Properties. | |
In the Project Properties dialog box, click Libraries.</li> | |
<p><li>For each of the following APIs, click "Add...", | |
select the name from the Module list, and then click OK to confirm it:</p> | |
<p><img src="../../images/tutorials/componentpalette/projprops_60.png" border="1" alt="All source files."> | |
<p><li>Click OK to exit the Project Properties dialog box. | |
<p><li>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.</li> | |
</ol> | |
<h3 class="tutorial"><a name="creating-palettes"></a>Creating the Component Palette</h3> | |
<p>Component Palettes are defined | |
by the <a href="https://netbeans.org/download/dev/javadoc/org-netbeans-spi-palette/overview-summary.html">NetBeans Palette API</a>. | |
Implementing the NetBeans Palette API for this tutorial means implementing the following NetBeans Palette API classes: | |
<p><table width="76%" border="1"> | |
<tbody><tr> | |
<td> | |
<div align="left"><b>File</b></div> | |
</td> | |
<td> | |
<div align="left"><b>Description</b></div> | |
</td> | |
</tr> | |
<tr> | |
<td align="left" valign="top"><tt><a href="https://netbeans.org/download/dev/javadoc/org-netbeans-spi-palette/org/netbeans/spi/palette/PaletteFactory.html">PaletteFactory</a></tt></td> | |
<td>Creates a new instance of the Component Palette. To do so, it invokes | |
the <tt>createPalette</tt> method which creates a new palette from the <tt>JBOSSPalette</tt> folder, which is defined | |
in the <tt>layer.xml</tt> file.</td> | |
</tr> | |
<tr> | |
<td align="left" valign="top"><tt><a href="https://netbeans.org/download/dev/javadoc/org-netbeans-spi-palette/org/netbeans/spi/palette/PaletteController.html">PaletteController</a></tt></td> | |
<td>Provides access to data in the Component Palette.</td> | |
</tr> | |
<tr> | |
<td align="left" valign="top"><tt><a href="https://netbeans.org/download/dev/javadoc/org-netbeans-spi-palette/org/netbeans/spi/palette/PaletteActions.html">PaletteActions</a></tt></td> | |
<td>(to be done) | |
</td> | |
</tr> | |
</tbody> | |
</table> | |
<p>In addition, you will also create the following supporting classes: | |
<p><table width="76%" border="1"> | |
<tbody><tr> | |
<td> | |
<div align="left"><b>File</b></div> | |
</td> | |
<td> | |
<div align="left"><b>Description</b></div> | |
</td> | |
</tr> | |
<tr> | |
<td align="left" valign="top"><tt>JbossddPaletteUtilities</tt></td> | |
<td>Handles insertion of the dropped code snippet.</td> | |
</tr> | |
<tr> | |
<td align="left" valign="top"><tt>JbossddPaletteCustomizerAction </tt></td> | |
<td>Provides the menu item that opens the Palette Manager. | |
</td> | |
</tr> | |
<tr> | |
<td align="left" valign="top"><tt>JbossddPaletteDropDefault</tt></td> | |
<td>(to be done) | |
</td> | |
</tr> | |
</tbody> | |
</table> | |
<p>To implement the NetBeans Palette API, | |
take the following steps: | |
<ol> | |
<li>Right-click the <tt>org.netbeans.modules.jbosswebxml</tt> project node and | |
choose New > Java Class. Create the first of the following files, type | |
<tt>org.netbeans.modules.jbosswebxml.palette</tt> in the package drop-down list, | |
click Finish, and then repeat the process to create the others: | |
<p><ul> | |
<li><tt>JbossddPaletteActions</tt> | |
<li><tt>JbossddPaletteCustomizerAction</tt> | |
<li><tt>JbossddPaletteDropDefault</tt> | |
<li><tt>JbossddPaletteFactory</tt> | |
<li><tt>JbossddPaletteUtilities</tt></ul> | |
<p>You should now have the following files (highlighted in the screenshot below): | |
<p align="left"><img border="1" src="../../images/tutorials/componentpalette/palette1.png" alt="Main palette files."> | |
<p><li>Replace the default content of the <tt>JbossddPaletteFactory.java</tt> file with the following: | |
<p><pre class="examplecode">package org.netbeans.modules.jbosswebxml.palette; | |
import java.io.IOException; | |
import <a href="https://netbeans.org/download/dev/javadoc/org-netbeans-spi-palette/org/netbeans/spi/palette/PaletteController.html">org.netbeans.spi.palette.PaletteController</a>; | |
import <a href="https://netbeans.org/download/dev/javadoc/org-netbeans-spi-palette/org/netbeans/spi/palette/PaletteFactory.html">org.netbeans.spi.palette.PaletteFactory</a>; | |
public class JbossddPaletteFactory { | |
public static final String JBOSS_PALETTE_FOLDER = "JBOSSPalette"; | |
private static PaletteController palette = null; | |
public static PaletteController getPalette() throws IOException { | |
if (palette == null) | |
palette = PaletteFactory.createPalette(JBOSS_PALETTE_FOLDER, new JbossddPaletteActions()); | |
return palette; | |
} | |
}</pre> | |
<p><li>Replace the default content of the <tt>JbossddPaletteActions.java</tt> file with the following: | |
<p><pre class="examplecode">package org.netbeans.modules.jbosswebxml.palette; | |
import java.awt.event.ActionEvent; | |
import java.io.IOException; | |
import javax.swing.AbstractAction; | |
import javax.swing.Action; | |
import javax.swing.text.JTextComponent; | |
import org.netbeans.editor.Utilities; | |
import org.netbeans.spi.palette.PaletteActions; | |
import org.netbeans.spi.palette.PaletteController; | |
import org.openide.DialogDisplayer; | |
import org.openide.NotifyDescriptor; | |
import org.openide.text.ActiveEditorDrop; | |
import org.openide.util.Lookup; | |
import org.openide.util.NbBundle; | |
public class JbossddPaletteActions extends <a href="https://netbeans.org/download/dev/javadoc/org-netbeans-spi-palette/org/netbeans/spi/palette/PaletteActions.html">PaletteActions</a> { | |
/** Creates a new instance of JbossddPaletteActions */ | |
public JbossddPaletteActions() { | |
} | |
public Action[] getImportActions() { | |
return new Action[0]; //TODO implement this | |
} | |
public Action[] getCustomCategoryActions(Lookup category) { | |
return new Action[0]; //TODO implement this | |
} | |
public Action[] getCustomItemActions(Lookup item) { | |
return new Action[0]; //TODO implement this | |
} | |
public Action[] getCustomPaletteActions() { | |
return new Action[0]; //TODO implement this | |
} | |
public Action getPreferredAction( Lookup item ) { | |
return new MFPaletteInsertAction(item); | |
} | |
private static class MFPaletteInsertAction extends AbstractAction { | |
private Lookup item; | |
MFPaletteInsertAction(Lookup item) { | |
this.item = item; | |
} | |
public void actionPerformed(ActionEvent e) { | |
ActiveEditorDrop drop = (ActiveEditorDrop) item.lookup(ActiveEditorDrop.class); | |
JTextComponent target = Utilities.getFocusedComponent(); | |
if (target == null) { | |
String msg = NbBundle.getMessage(JbossddPaletteActions.class, "MSG_ErrorNoFocusedDocument"); | |
DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(msg, NotifyDescriptor.ERROR_MESSAGE)); | |
return; | |
} | |
try { | |
drop.handleTransfer(target); | |
} | |
finally { | |
Utilities.requestFocus(target); | |
} | |
try { | |
PaletteController pc = JbossddPaletteFactory.getPalette(); | |
pc.clearSelection(); | |
} | |
catch (IOException ioe) { | |
} //should not occur | |
} | |
} | |
}</pre> | |
<p><li>Replace the default content of the <tt>JbossddPaletteUtilities.java</tt> file with the following: | |
<p><pre class="examplecode">package org.netbeans.modules.jbosswebxml.palette; | |
import java.awt.Component; | |
import java.awt.Container; | |
import java.util.StringTokenizer; | |
import javax.swing.JTree; | |
import javax.swing.text.BadLocationException; | |
import javax.swing.text.Caret; | |
import javax.swing.text.Document; | |
import javax.swing.text.JTextComponent; | |
import org.netbeans.api.project.FileOwnerQuery; | |
import org.netbeans.api.project.Project; | |
import org.netbeans.api.project.ProjectUtils; | |
import org.netbeans.api.project.SourceGroup; | |
import org.netbeans.api.project.Sources; | |
import org.netbeans.editor.BaseDocument; | |
import org.netbeans.editor.Formatter; | |
import org.openide.filesystems.FileObject; | |
public class JbossddPaletteUtilities { | |
/** Creates a new instance of JbossddPaletteUtilities */ | |
public JbossddPaletteUtilities() { | |
} | |
public static void insert(String s, JTextComponent target) | |
throws BadLocationException { | |
insert(s, target, true); | |
} | |
public static void insert(String s, JTextComponent target, boolean reformat) | |
throws BadLocationException { | |
if (s == null) | |
s = ""; | |
Document doc = target.getDocument(); | |
if (doc == null) | |
return; | |
if (doc instanceof BaseDocument) | |
((BaseDocument)doc).atomicLock(); | |
int start = insert(s, target, doc); | |
if (reformat && start >= 0 && doc instanceof BaseDocument) { // format the inserted text | |
int end = start + s.length(); | |
Formatter f = ((BaseDocument)doc).getFormatter(); | |
f.reformat((BaseDocument)doc, start, end); | |
} | |
if (doc instanceof BaseDocument) | |
((BaseDocument)doc).atomicUnlock(); | |
} | |
private static int insert(String s, JTextComponent target, Document doc) | |
throws BadLocationException { | |
int start = -1; | |
try { | |
//at first, find selected text range | |
Caret caret = target.getCaret(); | |
int p0 = Math.min(caret.getDot(), caret.getMark()); | |
int p1 = Math.max(caret.getDot(), caret.getMark()); | |
doc.remove(p0, p1 - p0); | |
//replace selected text by the inserted one | |
start = caret.getDot(); | |
doc.insertString(start, s, null); | |
} catch (BadLocationException ble) {} | |
return start; | |
} | |
}</pre> | |
<p><li>Replace the default content of the <tt>JbossddPaletteCustomizerAction.java</tt> file with the following: | |
<p><pre class="examplecode">package org.netbeans.modules.jbosswebxml.palette; | |
import java.io.IOException; | |
import org.openide.ErrorManager; | |
import org.openide.util.HelpCtx; | |
import org.openide.util.NbBundle; | |
import <a href="https://netbeans.org/download/dev/javadoc/org-openide-util/org/openide/util/actions/CallableSystemAction.html">org.openide.util.actions.CallableSystemAction</a>; | |
public class JbossddPaletteCustomizerAction extends <a href="https://netbeans.org/download/dev/javadoc/org-openide-util/org/openide/util/actions/CallableSystemAction.html">CallableSystemAction</a> { | |
private static String name; | |
public JbossddPaletteCustomizerAction () { | |
putValue("noIconInMenu", Boolean.TRUE); // NOI18N | |
} | |
protected boolean asynchronous() { | |
return false; | |
} | |
/** Human presentable name of the action. This should be | |
* presented as an item in a menu. | |
* @return the name of the action | |
*/ | |
public String getName() { | |
if (name == null) | |
name = NbBundle.getBundle(JbossddPaletteCustomizerAction.class).getString("ACT_OpenJbossddCustomizer"); // NOI18N | |
return name; | |
} | |
/** Help context where to find more about the action. | |
* @return the help context for this action | |
*/ | |
public HelpCtx getHelpCtx() { | |
return null; | |
} | |
/** This method is called by one of the "invokers" as a result of | |
* some user's action that should lead to actual "performing" of the action. | |
*/ | |
public void performAction() { | |
try { | |
JbossddPaletteFactory.getPalette().showCustomizer(); | |
} | |
catch (IOException ioe) { | |
ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, ioe); | |
} | |
} | |
}</pre> | |
<p><li>Replace the default content of the <tt>JbossddPaletteDropDefault.java</tt> file with the following: | |
<p><pre class="examplecode">package org.netbeans.modules.jbosswebxml.palette; | |
import javax.swing.text.BadLocationException; | |
import javax.swing.text.JTextComponent; | |
import org.openide.text.ActiveEditorDrop; | |
public class JbossddPaletteDropDefault implements ActiveEditorDrop { | |
String body; | |
public JbossddPaletteDropDefault(String body) { | |
this.body = body; | |
} | |
public boolean handleTransfer(JTextComponent targetComponent) { | |
if (targetComponent == null) | |
return false; | |
try { | |
JbossddPaletteUtilities.insert(body, (JTextComponent)targetComponent); | |
} | |
catch (BadLocationException ble) { | |
return false; | |
} | |
return true; | |
} | |
}</pre> | |
<p><li>Right-click the <tt>org.netbeans.modules.jbosswebxml.palette</tt> node, choose New > File/Folder, | |
and select Properties File from the Other category. Click Next. Name the file Bundle and then click Finish. | |
Add the following keys to the new <tt>Bundle.properties</tt> file: | |
<p><pre class="examplecode">MSG_ErrorNoFocusedDocument=No document selected. Please select a document to insert the item into. | |
ACT_OpenJbossddCustomizer=&JBoss Code Clips</pre> | |
</ol> | |
<h3 class="tutorial"><a name="creating-snippets"></a>Creating the Code Snippets</h3> | |
<p>Each code snippet requires the following files: | |
<ul> | |
<li>A Java class that defines the piece of code to be dragged into the Source Editor. This Java class | |
must refer to <tt>JbossddPaletteUtilities.java</tt>, which defines how the piece of code should be | |
inserted. For example, indentation and formatting are defined here. | |
<li>A properties file that defines the labels and tooltips. | |
<li>A 16x16 pixel image for the 'Small Icon' display. | |
<li>A 32x32 pixel image for the 'Big Icon' display.</ul> | |
<p>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 <tt>layer.xml</tt> file. | |
<p>For details on all of the items above, work through the <a href="nbm-palette-api1.html">NetBeans Code Snippet Module Tutorial</a>. | |
<p>As an example, | |
when you create three code snippets called <tt>ContextRoot.java</tt>, <tt>ResourceReference.java</tt>, and | |
<tt>SecurityDomain.java</tt>, the result might be as follows (the highlighted packages below contain the code snippets and | |
their supporting resources): | |
<p align="left"><img src="../../images/tutorials/componentpalette/palette4-60.png" border="1" alt="Main palette files."> | |
</div> | |
<br /> | |
<h2><a name="registering-module"></a>Registering the Module</h2> | |
<p>In this section, we register the menu item | |
and code snippets in the <tt>layer.xml</tt> file and in the | |
<tt>Bundle.properties</tt> file. | |
<ol> | |
<li>Add the following tags to the <tt>layer.xml</tt> file: | |
<p><pre class="examplecode"><folder name="Menu"> | |
<folder name="Tools"> | |
<folder name="PaletteManager"> | |
<file name="org-netbeans-modules-jbosswebxml-palette-JbossddPaletteCustomizerAction.instance"/> | |
</folder> | |
</folder> | |
</folder> | |
<folder name="JBOSSPalette"> | |
<folder name="JBoss"> | |
<attr name="SystemFileSystem.localizingBundle" | |
stringvalue="org.netbeans.modules.jbosswebxml.palette.Bundle"/> | |
<file name="ContextRoot.xml" | |
url="nbresloc:/org/netbeans/modules/jbosswebxml/palette/items/resources/ContextRoot.xml"/> | |
<file name="SecurityDomain.xml" | |
url="nbresloc:/org/netbeans/modules/jbosswebxml/palette/items/resources/SecurityDomain.xml"/> | |
<file name="ResourceReference.xml" | |
url="nbresloc:/org/netbeans/modules/jbosswebxml/palette/items/resources/ResourceReference.xml"/> | |
</folder> | |
</folder></pre> | |
<p><li>Add the following to the <tt>Bundle.properties</tt> file that is in the same package as | |
the <tt>layer.xml</tt> file: | |
<p><pre class="examplecode">JBOSSPalette/JBoss=JBoss Deployment Descriptor | |
JBOSSPalette/JBoss/ContextRoot.xml=Context Root | |
JBOSSPalette/JBoss/ResourceReference.xml=Resource Reference | |
JBOSSPalette/JBoss/SecurityDomain.xml=Security Domain</pre> | |
<p>The key-value pairs listed above localize the items registered in the <tt>layer.xml</tt> file. | |
</ol> | |
<h2><a name="reusing"></a>Reusing Code Snippets from Other Palettes</h2> | |
<p class="tips">Optionally, you can merge your collection of code snippets with the code snippets provided | |
by other modules. For example, if you want to merge your code snippets so that the <tt>jboss-web.xml</tt> file has | |
access to the code snippets provided by the JSP module and the HTML module, | |
take the steps below. | |
<ol><li>In <tt>org.netbeans.modules.jbosswebxml.palette</tt>, change the value for | |
the <tt>ACT_OpenJbossddCustomizer</tt> so that it matches the names of the same action | |
provided by the HTML module and the JSP module: | |
<pre class="examplecode">ACT_OpenJbossddCustomizer=&HTML/JSP Code Clips</pre> | |
<li>Hide the other two actions (the action in the | |
HTML module and the JSP module that displays the JSP/HTML Code Clips | |
Palette Manager) by adding the two lines in bold below to the <tt>layer.xml</tt> file: | |
<pre class="examplecode"><folder name="Menu"> | |
<folder name="Tools"> | |
<folder name="PaletteManager"> | |
<file name="org-netbeans-modules-jbosswebxml-palette-JbossddPaletteCustomizerAction.instance"/> | |
<b><file name="org-netbeans-modules-html-palette-HTMLPaletteCustomizerAction.instance_hidden"/> | |
<file name="org-netbeans-modules-web-core-palette-JSPPaletteCustomizerAction.instance_hidden"/></b> | |
</folder> | |
</folder> | |
</folder></pre> | |
<li>Finally, declare the resource declaration XML files of the HTML module and the JSP module as | |
shadow files:</i> | |
<pre class="examplecode"><folder name="JBOSSPalette"> | |
<folder name="JBoss"> | |
<attr name="SystemFileSystem.localizingBundle" | |
stringvalue="org.netbeans.modules.jbosswebxml.Bundle"/> | |
<file name="ContextRoot.xml" | |
url="ContextRoot.xml"/> | |
<file name="ResourceReference.xml" | |
url="ResourceReference.xml"/> | |
<file name="SecurityDomain.xml" | |
url="SecurityDomain.xml"/> | |
</folder> | |
<b><folder name="HTML.shadow"> | |
<attr name="SystemFileSystem.localizingBundle" | |
stringvalue="org.netbeans.modules.web.core.palette.Bundle"/> | |
<attr name="originalFile" | |
stringvalue="HTMLPalette/HTML"/> | |
</folder> | |
<folder name="HTML_Forms.shadow"> | |
<attr name="SystemFileSystem.localizingBundle" | |
stringvalue="org.netbeans.modules.web.core.palette.Bundle"/> | |
<attr name="originalFile" | |
stringvalue="HTMLPalette/HTML_Forms"/> | |
</folder> | |
<folder name="JSP.shadow"> | |
<attr name="SystemFileSystem.localizingBundle" | |
stringvalue="org.netbeans.modules.web.core.palette.Bundle"/> | |
<attr name="originalFile" | |
stringvalue="JSPPalette/JSP"/> | |
</folder> | |
<folder name="Database.shadow"> | |
<attr name="SystemFileSystem.localizingBundle" | |
stringvalue="org.netbeans.modules.web.core.palette.Bundle"/> | |
<attr name="originalFile" | |
stringvalue="JSPPalette/Database"/> | |
</folder></b> | |
</folder></pre></ul> | |
</ol> | |
<p>Now, when you build and install the module, as described in the next section, the Component Palette for | |
<tt>jboss-web.xml</tt> files will contain <i>all</i> the code snippets provided by the JSP module | |
as well as <i>all</i> the code snippets provided by the HTML module. | |
<br /> | |
<!-- ======================================================================================= --> | |
<h2><a name="building"></a>Building and Installing the Module</h2> | |
<p>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.</p> | |
<div class="indent"> | |
<h3 class="tutorial"><a name="install-plugin"></a>Installing and Using the Module</h3> | |
<ol> | |
<li>In the Projects window, right-click the <tt>Jbosswebxml</tt> project and choose Install/Reload | |
in Target Platform. | |
<p>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. | |
<p><li>Verify that the module is correctly installed by using it as | |
shown in <a href="#installing-sample">Installing the Sample</a>. | |
</ol> | |
<h3 class="tutorial"><a name="share-plugin"></a>Creating a Shareable Module Binary</h3> | |
<ol> | |
<li>In the Projects window, right-click the the project node and choose Create NBM.</p> | |
<p>The NBM file is created and you can view it in the Files window (Ctrl-2): | |
<p align="left"><img src="../../images/tutorials/componentpalette/create-nbm-60.png" border="1" alt="Shareable NBM."> | |
<li>Make the module available to others via, for example, e-mail. | |
<p><li>The recipient can install the module by using the Plugin Manager. Choose Tools > Plugins | |
from the main menu. | |
</ol> | |
</div> | |
<br /> | |
<br> | |
<div class="feedback-box"><a href="https://netbeans.org/about/contact_form.html?to=3&subject=Feedback:%20Component%20Palette%20Module%20Tutorial">Send Us Your Feedback</a></div> | |
<br style="clear:both;" /> | |
<!-- ======================================================================================== --> | |
<h2><a name="nextsteps"></a>Next Steps</h2> | |
<p>For more information about creating and developing NetBeans modules, see the following resources: | |
<ul> | |
<p><li><a href="https://netbeans.org/kb/trails/platform.html">Other Related Tutorials</a></li> | |
<p><li><a href="https://netbeans.org/download/dev/javadoc/">NetBeans API Javadoc</a></li> | |
</ul> | |
<hr> | |
<!-- ======================================================================================== --> | |
<h2><a name="version"></a>Versioning </h2> | |
<p> | |
<table width="76%" border="1"> | |
<tbody> | |
<tr> | |
<td> | |
<div align="left"><b>Version</b></div> | |
</td> | |
<td> | |
<div align="left"><b>Date</b></div> | |
</td> | |
<td> | |
<div align="left"><b>Changes</b></div> | |
</td> | |
<td> | |
<div align="left"><b>Open Issues</b></div> | |
</td> | |
</tr> | |
<tr> | |
<td> | |
1 | |
</td> | |
<td> | |
29 November 2005 | |
</td> | |
<td> | |
Initial version | |
</td> | |
<td> | |
<ul> | |
<li>Needs to be reviewed! Use at your own risk! | |
<li>Need to add explanation for adding own dialog box for predefining values. | |
<li>Explanatory text for the use of the NetBeans APIs to be added. | |
<li>Check for copy-paste errors. | |
<li>Need to add more Javadoc links, for NetBeans API classes in the code. | |
</ul> | |
</td> | |
</tr> | |
<tr> | |
<td> | |
2 | |
</td> | |
<td> | |
30 November 2005 | |
</td> | |
<td> | |
<li>Changed the ZIP file because of problems (see <a href="https://netbeans.org/bugzilla/show_bug.cgi?id=69571">Issue 69571</a>). | |
<li>Removed hyphens and changed screenshots. | |
</td> | |
<td> | |
All other issues from above must still be done. | |
</td> | |
</tr> | |
<tr> | |
<td> | |
3 | |
</td> | |
<td> | |
8 December 2005 | |
</td> | |
<td> | |
<li>Fixed reopened issue <a href="https://netbeans.org/bugzilla/show_bug.cgi?id=69571">Issue 69571</a> | |
<td> | |
All other issues from above must still be done. | |
</td> | |
</tr> | |
<tr> | |
<td> | |
4 | |
</td> | |
<td> | |
22 August 2006 | |
</td> | |
<td> | |
<li>Minor tweaks. | |
<td> | |
All other issues from above must still be done. | |
<br>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. | |
<br>Discovered that there are several gaps and tbds in this tutorial. | |
</td> | |
</tr> | |
<tr> | |
<td> | |
5 | |
</td> | |
<td> | |
30 May 2007 | |
</td> | |
<td> | |
Began updating this tutorial for 6.0. | |
</td> | |
</tr> | |
</tbody> | |
</table> | |
</body> | |
</html> |