Download NetBeans!

20061108 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?

  1. 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.

  2. Right-click the JSPTags node, choose New > File/Folder. In the NetBeans Module Development category, choose Action. Click Next.

  3. 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.

  4. Type "AddBoldAction" in Class Name and "Surround with Bold" in Display Name. Click Finish.

  5. 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>");

  6. Right-click the module and choose Install/Reload in Development IDE.

  7. 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

Trackback URL: http://blogs.sun.com/geertjan/entry/module_development_for_web_developers
Comments:

Hi Geertjan, it seems to me there was something like this: http://blogs.sun.com/geertjan/entry/east_extensions_to_your_development

Posted by Ivan on November 08, 2006 at 08:16 AM PST #

Repetition is the foundation of knowledge, Ivan!

Posted by Geertjan on November 08, 2006 at 09:20 AM PST #

This code is misleading. I found out the hard way that you need to use BaseDocument.runAtomic(new Runnable()) instead to do your changes. Otherwise your Undo/Redo buttons do not get enabled, since they are unaware of any changes to the edited file.

Posted by Jacek on November 08, 2006 at 12:05 PM PST #

Not true for me, Jacek. My undo/redo buttons work fine.

Posted by Geertjan on November 08, 2006 at 12:10 PM PST #

Hi Geertjan, Wow.... Excellent. Using the ideas given by you, i was able to create a module myself. I created a small module which will comment out a block using java multiline comments. (/* */) I was badly missing this feature in NB and now it feels really great to introduce this feature myself. That's the power of open source. Thank you so much.... Please continue to blog about NB module development.

Posted by James Selvakumar on November 08, 2006 at 10:24 PM PST #

Geertjan, I need one clarification though. After i tried out the module as mentioned by you, i pressed the undo button and the entire text vanish. But when i press undo again, the original text reappears. How to get along with this?

Posted by James Selvakumar on November 08, 2006 at 10:32 PM PST #

Hi James, that might be the problem Jacek talks about above. Currently, I'm just doing 'Undo' twice. That's not a big problem. After all, the code works like that: the selected text is replaced by the new text, which is the same as the selected text plus the new tags before and after. So, if you do 'Undo', the whole text, together with the tags before and after, disappears. If you don't like that behvaior, you'll have to code the module differently, maybe in the way that Jacek describes, I don't know for sure.

Posted by Geertjan on November 08, 2006 at 11:28 PM PST #

Hi Geertjan, This is not related to this particular section. still I beleive I can get an answer. I want to make the selectable part(where the name of the file opened will be displayed) of editor tabs bigger. It appears so small.How can I do that ?

Posted by Deepak on November 09, 2006 at 01:50 AM PST #

Hi Deepak, not sure exactly, but I think you need to play around with the code in this blog entry: http://blogs.sun.com/geertjan/entry/farewell_to_space_consuming_weird

Posted by Geertjan on November 09, 2006 at 01:57 AM PST #

Hi Geertjan. For my Link Inserter (functionality described here), I stole some code from Sandip. :-)

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 #

Hi Gregg, thanks for these insights. I'd like to try it out (even though I have no undo/redo problems, but want to see if there's a difference). Can you post the relevant snippet here (or somewhere)?

Posted by Gregg on November 10, 2006 at 01:01 AM PST #

Sorry, the comment above was not from Gregg, but by me...

Posted by Geertjan on November 10, 2006 at 01:02 AM PST #

The code you wanted to see has been posted: http://weblogs.java.net/blog/gsporar/archive/2006/11/geertjan_wants.html

Posted by Gregg Sporar on November 16, 2006 at 03:08 PM PST #

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: NOT allowed