InsertSystemOutPrintln

Tutorial | Simulating the 'soutv' code template for Java Editor

Doc Contributed by Kunal Modi

Contents

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 or rather the 'soutv' code template. The 'soutv' (Followed by the Tab Key) code template lets you insert a System.out.println() statement for the particular variable. For example,

    String example = "Hello World!"

Now if I want to print this variable using System.out.println(), all I need to do is to type 'soutv' followed by the TabKey. So the code will look as follows:

    String example = "Hello World!" // On the next line we will type 'soutv' followed by the Tab Key
    System.out.println("example = " + example); // This statement gets generated automatically, when you follow the above instruction
This tutorial will help you create a custom action for the Java Editor which will simulate the 'soutv' code template and thus insert the System.out.println() statement for the selected variable automatically.

Lets Start

Pre-Requirements

  • Familiarity with NetBeans IDE

Creating a New Module

  • Create a module called "InsertPrintln" and keep the code name base as "org.modules.sack.insertprinltn". A detailed explanation on how to create a new module can be found in my previous tutorial.

Create the Action

  • Right click the package node and choose New and select Action from the pop-up menu.
  • Select Conditionally Enabled Action. Select EditorCookie from the drop down menu. Click Next.

File:scr13_InsertSystemOutPrintln.JPG

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

File:scr14_InsertSystemOutPrintln.JPG

Code Content

  • All we need to do now is to add the logic for our action in the performAction(Node[[ | ]] activatedNodes) function of the "InsertPrintln" class.
  • First of all add the following module dependencies to your module.
  • Editor Library
  • Editor Library 2
  • Editor Indentation
  • 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 right click the project node, select properties and then select libraries. You should find the list similar to the list shown below.

File:scr15_InsertSystemOutPrintln.JPG

  • 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);
                  }
                  }
                  }
  • Resolve the import references and make the following fields:
                  private JTextComponent editor;
                  private StyledDocument document;
                  private String selectedText;
                  private String stringToBeInserted;
  • Your module is ready to run.
  • To run, right click the project node and select "Run".
  • The target platform opens up. Open any Java file in the Editor.
  • Select a variable you wish to print using System.out.println. Right click and choose "Insert System.out.println".

File:scr16_InsertSystemOutPrintln.JPG

  • The statement automatically gets inserted on the next line.

File:scr17_InsertSystemOutPrintln.JPG

Code Explaination

  • 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).
  • 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(int,String,Attribute) function we insert our line of code in the document.
  • Finally we Reformat the document to maintain the indentation.

Remarks

  • Any suggestions/corrections/clarifications/comments are welcome.
Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo