Wednesday November 08, 2006
Module development for web developers
Adding small, powerful extensions to the IDE can be part of any normal development process. For example, you're a web developer. You're creating JSP pages. You need to add some bold tags around bits of text and are getting tired of typing them in manually. Why not create a module that will insert those tags around a selected piece of text for you?
- Create a new module project. (File > New Project, then choose Module Project from the NetBeans Plug-in Modules category.) Name the project "JSPTags" and click Next and Finish.
- Right-click the JSPTags node, choose New > File/Folder. In the NetBeans Module Development category, choose Action. Click Next.
- Choose "Conditionally Enabled". In the Cookie Classes drop-down list, choose "EditorCookie". (Do not choose "EditCookie"; choose "EditorCookie"!) Click Next. Uncheck "Global Menu Item". Check "Editor Context Menu Item". Choose "text/x-jsp" from the drop-down list. Click Next.
- Type "AddBoldAction" in Class Name and "Surround with Bold" in Display Name. Click Finish.
- Declare a dependency on "Editor Library", in the module's Project Properties dialog box (use the Libraries panel). Now remove the line generated in the performAction method and replace it with the following three:
JTextComponent editor = Registry.getMostActiveComponent(); String selection = editor.getSelectedText(); editor.replaceSelection("<b>" + selection + "</b>"); - Right-click the module and choose Install/Reload in Development IDE.
- Now, in your JSP page, select some text with your mouse, right-click and choose "Surround with Bold". And... now you magically have bold tags around the selected text!
This procedure is so trivial, it is so easy to do, that it (as well as many small modules like it) can be created while you're in the middle of some development cycle, as a small diversion that is useful at the sme time. Hurray for NetBeans module development.
Nov 08 2006, 12:01:09 AM PST Permalink
Posted by Ivan on November 08, 2006 at 08:16 AM PST #
Posted by Geertjan on November 08, 2006 at 09:20 AM PST #
Posted by Geertjan on November 08, 2006 at 12:10 PM PST #
Posted by James Selvakumar on November 08, 2006 at 10:24 PM PST #
Posted by James Selvakumar on November 08, 2006 at 10:32 PM PST #
Posted by Geertjan on November 08, 2006 at 11:28 PM PST #
Posted by Deepak on November 09, 2006 at 01:50 AM PST #
Posted by Geertjan on November 09, 2006 at 01:57 AM PST #
Instead of using replaceSelection(), I called getDocument() to get the javax.swing.text.Document. Then I used insertString(). I don't have any problems with Undo/Redo, they seem to work fine and I'm wondering if modifying the text via the Document is the reason why.
I also check to see if the Document object is a org.netbeans.editor.BaseDocument and if it is I do an atomicLock() before and atomicUnlock() after the change to the string.
Posted by Gregg Sporar on November 09, 2006 at 01:11 PM PST #
Posted by Gregg on November 10, 2006 at 01:01 AM PST #
Posted by Geertjan on November 10, 2006 at 01:02 AM PST #
Posted by Gregg Sporar on November 16, 2006 at 03:08 PM PST #


