Sunday, May 11, 2008

NetBeans6.1 Review

Each time a new NetBeans Release is out, it exceeds your expectations. NetBeans6.1 has only reaffirmed that. The NetBeans Java Editor is the most loved feature. The ease with which the NetBeans editor allows you to program not only increases your productivity but helps you know more about the Java programming language. The hints that appear aside the line auto generates the code for importing classes, defining fields and variables and also auto generates the for and while loops for you and I can go and on.

The NetBeans6.1 editor enhancements too have been significant. The Bean Information for a class now gets generated at a click of the button.


It not only gives the auto generated code but also gives the designer view to edit the beans info.


But one cool feature I would like to talk is about the screen shot of the Top-Component(Windows seen inside NetBeans are called Top-Components) on Drag. Whenever a Top-Component is dragged a screen shot gets attached to it automatically.

This is cool because NetBeans is able to generate a compressed image of the Top-Component,. This can be leveraged to take a screen shot of the editor content while writing a tutorial.

Not to forget the Documentation support that NetBeans provides. The documentation has aided better understanding of existing modules as well as helped in writing new modules for NetBeans.

In accordance with this I present below a short tutorial on how add custom action to the NetBeans Java Editor.

  • Introduction: It happens many a times that you get stuck in your small code and you try debugging using the System.out.println() statements. Though NetBeans provides an excellent debugging support and a profiler, you would be discouraged to use that if the magnitude of your code is not that large. As students, we tend to use System.out.println() as our first choice. This tutorial will help you create a custom action for the Java Editor which will insert the System.out.println() statement for the selected variable automatically.
  • Lets Start
    • Pre-Requirements:- Familiarity with NetBeans IDE
    • Creating a New Module
      1. Create a module called "InsertPrintln" and keep the code name base as "org.modules.sack.insertprinltn". A detailed explaination on how to create a new module can be found on my previous posts.
    • Create the Action
      1. Right click the package node and choose New and select Action from the pop-up menu.
      2. Select Conditionally Enabled Action. Select EditorCookie from the drop down menu. Click Next.
      3. Select the category as Other. Check only Editor Context Menu Item box and select "text/x-java". Select the position after the "generate-code-HERE-" option. Click Next.
      4. Enter "InsertPrintln" as the class name and Insert "System.out.println" as the Display name. Keep other settings as default and click finish. The "InsertPrinln" class gets created.
    • Code Content
      1. All we need to do now is to add the logic for our action in the performAction(Node[] activatedNodes) function of the "InsertPrintln" class.
      2. First of all add the following module dependencies to your module.
        1. Editor Library
        2. Editor Library 2
        3. Editor Indentation
      3. To add the dependencies, right click the Libraries node of the project. Select "add module dependency" and add the above dependencies. Once you have added all verify it. To verify roght click the project node, select properties and then select libraries. You should find the list similar to the list shown below.
      4. Now add the following code to the performAction(Node[] activatedNodes) function in "InsertPrintln" class. Your performAction(Node[] activatedNodes) should look as follows:protected void performAction(Node[] activatedNodes) { EditorCookie editorCookie = activatedNodes[0].getLookup().lookup(EditorCookie.class);
        editor = EditorRegistry.lastFocusedComponent();

        selectedText = editor.getSelectedText();



        document = (StyledDocument)editor.getDocument();

        int currentLine;
        int nextLineOffset;
        if(selectedText!=null){

        try {

        stringToBeInserted = "System.out.println(\""+selectedText+" =\"+" +selectedText + ");\n";
        currentLine = NbDocument.findLineNumber(document, editor.getCaretPosition());
        nextLineOffset = NbDocument.findLineOffset(document, currentLine + 1);
        document.insertString(nextLineOffset, stringToBeInserted, null);
        BaseDocument bd = (BaseDocument) editor.getDocument();

        Reformat.get(bd).lock();
        bd.getFormatter().reformat(bd, 0, bd.getLength()-1);

        Reformat.get(bd).unlock();

        } catch (BadLocationException ex) {
        Exceptions.printStackTrace(ex);
        }
        }
        }
      5. Resolve the import references and make the following fields: private JTextComponent editor;
        private StyledDocument document;
        private String selectedText;
        private String stringToBeInserted;
      6. Your module is ready to run.
      7. To run, right click the project node and select "Run".
      8. The target platform opens up. Open any Java file in the Editor.
      9. Select a variable you wish to print using System.out.println. Right click and choose "Insert System.out.println".
      10. The statement automatically gets inserted on the next line.
    • Code Explaination
      1. The code is straight forward. At first, using the Editor Registry we derive the last focused component(which returns the editor component we want to work with).
      2. After obtaining the document from the editor and the selected text, we frame the string to be inserted. NbDocument helps us to find the appropriate line number and also helps us to derive the offset for the next line. Using the document.insertString() function we insert our line of code in the document.
      3. Finally we Reformat the document to maintain the indentation.
    • Use this as a plugin?
      1. You can use this as a plugin. Just right click on the project node and select "Create NBM". A .nbm file get created and the link is displayed in the output window.
      2. To install this plugin, go to Tools-> Plugins
      3. Go to downloaded plugins tab and browse for your .nbm file.
      4. Click install and complete the installation procedure.
      5. Your plugin is now ready to use.
Any suggestions/corrections/clarifications/comments are welcome.
Happy NetBeaning!

2 comments:

Martin FM Smith - Bolnisi (Georgia) said...

When I open 6.1. I only get the option to create a Free- FormProject, not the usual choice including an Application. Have you any idea what is going wrong? It allows only something very difficult - I am a beginner, looking at 5.5.1 and tried 6.1 out of curiosity.
Do you also know how it supports Bluej?
Many thanks - Martin - Belfast UK

Kunal Modi said...

Hey Martin,
Sorry for the long delay.
When I select the New Project option, I see the following options under the Java tab:
1.) Java Application
2.) Java Desktop Application
3.) Java Class Library
4.) Java Project with existing sources
5.) Java Free-form Project

I hope you downloaded NetBeans6.1 properly from http://www.netbeans.org/downloads/index.html

And as far as BlueJ+ NetBeans is concerned there is a plugin for it.
You may check
http://www.bluej.org/netbeans/

Let me know if this helps.
Again sorry for the delay.