Jens has posted 2 posts at DZone. View Full User Profile

Student Project: Automatic Import Statement Organizer in NetBeans IDE

04.07.2010
| 12423 views |
  • submit to reddit

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!

Published at DZone with permission of its author, Jens Hofschröer.

(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: Frederic Conrotte

It amazes me that Eclipse doesn't have an excellent free Swing GUI Builder out of the box like NetBeans IDE has. It really should have one. :-)

Fabrizio Giudici replied on Wed, 2010/04/07 - 1:39pm

Thanks for sharing. I'm pretty picky about sorting my imports and I always did by hand. Sounds like starting from tonight I'll be able to do it automatically :-)

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

The source code is available now at Kenai:

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: Frederic Conrotte

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.

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 Wielenga

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

Excellent idea - many thanks.

Marcelo Ruiz replied on Mon, 2010/10/04 - 4:55pm

Hi Jens, Thanks for the plugin, it is really helpful! One suggestion you can consider for a future new version is to add an option for triggering a "fix imports" before organizing the import statements. I make this suggestion cause I find myself doing those two steps in that order very often. Thanks again for sharing your plugin!

Matt Coleman replied on Thu, 2012/08/16 - 12:58am

what a great project for the students.more acitivities like this please

website design buffalo

Mateo Gomez replied on Thu, 2012/08/16 - 1:13am

it's great for these students to have the opportunity

 mexican dessert recipes

Oskar Strączkowski replied on Sat, 2013/06/01 - 1:55pm

Just llike you did some time ago I've just switched from Eclipse to NetBeans and guess what was one of my first thoughts? ;) "Why this organize-imports feature is not working as it should?!". And then I've found your plugin :) Thx!

Cata Nic replied on Mon, 2013/09/02 - 7:29am

 Interesting solution. I think the model can be used at large scale too. The stability is one of its advantages.

Samad Khatri replied on Mon, 2014/03/31 - 10:29am in response to: Toni Epple

 very cool, congratulations to your first release! www.fibroidsmiracle.co.uk

Samad Khatri replied on Sun, 2014/04/20 - 5:14am in response to: Toni Epple

 amazes me that Eclipse doesn't have an excellent free Swing GUI Builder out of the box like NetBeans IDE has. It really https://rebelmouse.com/superheroabsreviews/ 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.