Benad's Web Site

As a habit from the NetBeans releases before 3.6, I tend to use it only when it is file, not Beta or even Release Candidate. But right now I'm still playing a lot with NetBeans 5.0 Beta. And what a release 5.0 is going to be...

Again, an example. Let's say you want to do something like this Perl code:

while () {
    #some code here
}

In Java, it is less obvious than this. While the preferences window of 5.0 Beta is a bit buggy, I created a new abbreviation in Editor -> Code Templates, quit NetBeans, and fired a text editor on the file ~/.netbeans/5.0beta/config/Editors/text/x-java/abbreviations.xml to change it like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE abbrevs PUBLIC "-//NetBeans//DTD Editor Abbreviations settings 1.0//EN"
                         "http://www.netbeans.org/dtds/EditorAbbreviations-1_0.dtd">
<abbrevs>
    <abbrev key="readline" xml:space="preserve">String line;
java.io.BufferedReader reader = new java.io.BufferedReader(
  new java.io.InputStreamReader(${is instanceof="java.io.InputStream"}));
while ((line = reader.readLine()) != null) {
    ${cursor}
}</abbrev>
</abbrevs>

Started up NetBeans, and when you type "readline" followed by a space, you'll have the above code. The cursor will be placed on the "${is ..." part, already filled by the most recently referenced InputStream. If you place another "${is}" above, then it will be automatically replaced as you type to replace the text. Finally, when you press enter or TAB, the cursor is placed where "${cursor}" is. Try the abbreviations "fori" or "forc".

"But then", you might say, "what about the IOException that can be thrown by readLine?". When you'll see the red line below the line "while ... reader.readLine() ...", click on it. You'll see a small light bulb with suggestions to fix you code. Yeah, like Eclipse, but in such a user-friendly way.

Published on November 3, 2005 at 17:18 EST

Older post: A free garbage collector and leak detector for C and C++

Newer post: Docs for Abbreviations in NetBeans 5.0