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!
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)




Comments
Toni Epple replied on Wed, 2010/04/07 - 12:22pm
Hallo Jens,
very cool, congratulations to your first release!
--Toni
Frederic Conrotte replied on Wed, 2010/04/07 - 1:14pm
I'm a Netbeans user but what amaze me is that such feature is not present by default in Netbeans. Eclipse has it for a long time already.
And what about automatic code formatting when saving a Java file ?
Geertjan Wielenga replied on Wed, 2010/04/07 - 1:32pm
in response to: effco
Fabrizio Giudici replied on Wed, 2010/04/07 - 1:39pm
Tomás Lázaro replied on Wed, 2010/04/07 - 6:38pm
So simple yet so great. How about moving it into Netbeans offical release? Perhaps not as a standalone plugin but at least the functionality.
Thanks for your work!
Jens Hofschröer replied on Thu, 2010/04/08 - 4:48am
http://kenai.com/projects/organizeimports
Jens
Geertjan Wielenga replied on Thu, 2010/04/08 - 6:58am
And here's an enhancement request to get it integrated into the NetBeans IDE sources:
http://netbeans.org/bugzilla/show_bug.cgi?id=183703
Aljoscha Rittner replied on Fri, 2010/04/09 - 3:16am
in response to: effco
In the past we had this feature, but it was lost. Software development is a floating process. Some times back, but most of the time ahead.
br, josh.
Lukas Kirchhart replied on Tue, 2010/04/13 - 7:13am
Gratz from me too. Nice idea, nice work.
Proud to be your student ;-)
Luke
hantsy bai replied on Fri, 2010/04/16 - 7:44am
in response to: geertjan
e4 has a GUI designer , what's about NetBeans?
What's in NetBeans fight for eclipse next generation GUI?
Michael Bedward replied on Wed, 2010/04/21 - 1:25am
Marcelo Ruiz replied on Mon, 2010/10/04 - 4:55pm
Matt Coleman replied on Thu, 2012/03/15 - 2:56am
this is great stuff Jens...it makes it easier for students now because of this.Cheers!
website design buffalo
Manish Chowdhary replied on Thu, 2012/03/22 - 7:49pm