Skip navigation

Monthly Archives: November 2007

After my last adventure with XML code completion, I am ready for one more nifty add-on. This one is straight from Eclipse XML editor. When you request completion for an attribute in an XML tag, you might want to add the default value (by default). Such a thing is not provided by NetBeans. However, as with yesterday’s hack, this one is mighty easy to do.

If you have followed yesterday’s post, you know where the sources of the schema aware code completion module reside. When we type CTRL+SPACE for invoking code completion for attributes of a tag, we get a list of org.netbeans.modules.xml.schema.completion.AttributeResultItem objects, each one representing a possible attribute that can belong at the position where completion was invoked. I make the following modifications to the code.

First, I need to find if there is a default value for the particular attribute. So, I add the following code to the constructor of AttributeResultItem:

 

The getDefaultValue() method is as given below: 

 

 One final thing. The getReplacementText() method needs to be modified.

 

That’s it. Now, when you invoke code completion for attributes (after you have compiled your changes and re-run the development IDE), you can see the attributes with their default values.

 

 

More XML completion fun to come in the next post! 

If you are used to heavily editing XML files in NetBeans, you would have wished for the code completion assistance to be a bit smarter. As an example, let us take the case of editing a Spring Framework Beans Configuration file. I want to add an <alias> tag to the XML configuration file. This tag has two required attributes: name and alias. It all starts by invoking the code completion assistance by typing in ‘<‘. The following dialog shows up:

 

When I select the alias tag and hit Enter, the completion assistance just puts in the tag name:

To insert the attributes I have to again press CTRL + SPACE:

 

If the code completion were a bit smarter, it would insert the required attributes as well. I mean, why will anyone use a tag without using the required attributes? Anyway, NetBeans doesn’t offer this feature, so I decided to do some hacking.

I downloaded the NetBeans sources and opened up the module project for the Schema based code completion in NetBeans (NB_SRC_ROOT/xml/schema/completion)

 

 

The items displayed in the completion assistance popup are implemented in ElementResultItem.java and AttributeResultItem.java. Since I want additional text to be added when the user wants to add a new XML tag (element), I will modify the ElementResultItem.java. I modified the constructors of the class to add the additional text:

 

I needed to override a method to return the new text to the completion infrastructure.

 

That’s it. Now I built NetBeans, and run the same scenario for code completion, and here’s what I see:

 

Notice the required attributes appearing alongwith the end tag (another nifty thing which the current XML code completion misses). Now pressing Enter gives me the tag complete with it’s required attributes and end tag.

 

The hack still needs some work, but you can see the point. If there is something you want in NetBeans, just dig through the sources and modify it and contribute it back to the community. This in my mind is the single most advantage of using an Open Source tool.

 

Many a times, we have many projects open in the IDE and we want to close all of them, except one. The usual way in such a case is to select all the project nodes using CTRL+A and right click and select "Close XX Projects" action and then re-open the project that you wanted to keep open. Or else, you can select all projects using CTRL + A and then unselect the project you want to keep open and then invoke "Close Projects" action.

 

I am a programmer, I am supposed to be lazy. So here’s my little creation, an action which lets you close all other projects apart from the selected one. Select a project which you want to keep open, and click File -> Close other projects.

 

I have uploaded it to the Plugin Portal, and you can download the sources from here.

If you are like me, developing NetBeans modules, then you will like this module. I developed this one in 10 minutes!