Geertjan's Blog

  • November 8, 2006

Module development for web developers

Geertjan Wielenga
Product Manager
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.

Join the discussion

Comments ( 13 )
  • Ivan Wednesday, November 8, 2006
    Hi Geertjan, it seems to me there was something like this: http://blogs.sun.com/geertjan/entry/east_extensions_to_your_development
  • Geertjan Wednesday, November 8, 2006
    Repetition is the foundation of knowledge, Ivan!
  • Jacek Wednesday, November 8, 2006
    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.
  • Geertjan Wednesday, November 8, 2006
    Not true for me, Jacek. My undo/redo buttons work fine.
  • James Selvakumar Wednesday, November 8, 2006
    Hi Geertjan,
    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.
  • James Selvakumar Wednesday, November 8, 2006
    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?
  • Geertjan Wednesday, November 8, 2006
    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.
  • Deepak Thursday, November 9, 2006
    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 ?
  • Geertjan Thursday, November 9, 2006
    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
  • Gregg Sporar Thursday, November 9, 2006
    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.
  • Gregg Friday, November 10, 2006
    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)?
  • Geertjan Friday, November 10, 2006
    Sorry, the comment above was not from Gregg, but by me...
  • Gregg Sporar Thursday, November 16, 2006
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.