OnSave Actions in NetBeans 6.9

Is there a way to tell NetBeans to take a specific action when saving a file? For example, removing unused imports while saving the original file?

+4
source share
5 answers

This was an interesting question ... since I believe that you will have to write a custom NetBeans plugin to do what you want (functionality is not available out of the box), and I was looking for an excuse to study the development of NetBeans plugins.

However, after spending a couple of hours reading textbooks and slipping through javadocs ... it becomes clear that this subject is quite a big bite to chew on and probably much more active than you want.


I think the BEST clause forgets about deleting unused imports at save time and instead performs this step at build time . NetBeans offers excellent integration with Ant and / or Maven (for build purposes it is basically just a GUI wrapper around these tools), and there are several Ant tasks that can do what you want. Cm:

http://ant.apache.org/external.html
(find the tasks "CleanImports" and "Importscrubber")

If your NetBeans project is based on Maven, you can always connect one of these Ant tasks there using the AntRun plugin for Maven .

If you’re not used to accessing Ant or Maven directly in NetBeans, simply go to the Files tab and look at the project’s root directory. If his project is Maven, the build script will be called pom.xml . Otherwise, your project will usually be Ant, and the build script will be called build.xml . The documentation for these points above should clearly explain how to move on from there.


I noticed that these two Ant tasks were not updated after a while, so if you run into problems, you can check out the very popular and updated PMD system , which has its own documentation for integrating with NetBeans . However, the problem is PMD primarily for reporting ... I don’t know if it can be used to actually take action and modify the source files.

0
source

Not quite the answer to your question, but note that NB 7.1 allows you to immediately fix the import of the entire project: http://wiki.netbeans.org/NewAndNoteworthyNB71#Organize_Imports_Hint

0
source

This is not a good practice, and NetBeans does not support it.

0
source

I will resurrect this topic.

Ok, this code code has been tested with Netbeans 7.4. here I override the default save action in the actionPerformed method. If you decide to do this yourself, create a new action using the wizard, and then call the save action inside the actionPerformed method.

 package yourpackage; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JOptionPane; import org.openide.awt.ActionID; import org.openide.awt.ActionReference; import org.openide.awt.ActionReferences; import org.openide.awt.ActionRegistration; import org.openide.util.NbBundle.Messages; @ActionID( category = "File", id = "BZ.SaveAction" ) @ActionRegistration( iconBase = "BZ/Save.png", displayName = "#CTL_SaveAction" ) @ActionReferences({ @ActionReference(path = "Menu/File", position = 750), @ActionReference(path = "Toolbars/File", position = 0), @ActionReference(path = "Shortcuts", name = "DS") }) @Messages("CTL_SaveAction=Save") public final class SaveAction implements ActionListener { org.openide.actions.SaveAction sa = org.openide.util.actions.CallbackSystemAction.get(org.openide.actions.SaveAction.class); @Override public void actionPerformed(ActionEvent e) { // custom code JOptionPane.showMessageDialog(null, "custum message "); sa.performAction(); } } 
0
source

Goto Tools-> Options select Editor , then select On Save Tab , then select Java from the drop-down menu. So now select the Organize Imports option. Hope this helps you.

0
source

All Articles