Platinum Partner
netbeans

Student Project: Automatic Import Statement Organizer in NetBeans IDE

I work in the Research Group Gear Design and Manufacturing Simulation at RWTH Aachen University in Germany. I was in Göttingen last year at the NetBeans Platform Certified Training during the Source Talk Tage. It was a great help to understand the basic concepts of the NetBeans Platform. We tried to learn some things by ourselves but fell into many traps. After the training we had to rewrite a bunch of code in our application (and are still doing so).

And today I published my first NetBeans plugin in the NetBeans Plugin Portal:

http://plugins.netbeans.org/PluginPortal/faces/PluginDetailPage.jsp?pluginid=27296

Install it in NetBeans IDE 6.7.1 or 6.8 and you can reorganize import statements. Let's say that the import statements in my class look like this:
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import org.openide.cookies.EditorCookie;
import java.util.Collections;
import java.util.List;
import javax.swing.Action;
import javax.swing.JEditorPane;
import javax.swing.text.EditorKit;
import javax.swing.text.JTextComponent;
import org.netbeans.api.editor.EditorRegistry;
import org.openide.windows.IOProvider;
import org.openide.windows.InputOutput;
import org.openide.windows.OutputWriter;

When I click Ctrl-Alt-i, the list above now is automatically as follows:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.Action;
import javax.swing.JEditorPane;
import javax.swing.text.EditorKit;
import javax.swing.text.JTextComponent;

import org.netbeans.api.editor.EditorRegistry;
import org.openide.cookies.EditorCookie;
import org.openide.windows.IOProvider;
import org.openide.windows.InputOutput;
import org.openide.windows.OutputWriter;

The plugin provides an Options window tab for configuring the import statement organizer:

Why did I make this? It's easy: I missed this feature. My first "real" Java IDE was Eclipse which has this feature "from beginning" as part of the "fix imports" feature. A great way to keep an overview of big import lists in big Java files. If you order the imports by yourself and later add some new import statements via the "fix imports" feature, you have to resort the imports every time. It's time consuming and you stop doing this every time you fix your imports.

When starting with NetBeans IDE, I missed this feature from the beginning. With every new release I wished someone else had done this because I had no time to do this at work or in my spare time. Working on another "big" Java file with many import statements, I no longer wanted to wait. So I started to search for a NetBeans module to manipulate my source files. It took about a month or so to get it working beside the actual work I needed to do.

Most work was done by searching the NetBeans site (I created a browser search plugin, which was a great advantage), reading the JavaDocs, and source code. The time I found out how to deal with the CodeGenerator classes, the rest was not that difficult. The only thing I did not find was an API to access some kind of code formatter to add the empty lines between the "import groups" so I has to "edit" the BaseDocument of the Java editor directly.

Hope the plugin will be useful!

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}