Wednesday Mar 19, 2014

One Click Open Sesame in NetBeans 8

And my first plugin for NetBeans IDE 8 is done and published!

@andypotter on Twitter, and on the NetBeans users mailing list, wants to be able to open files by selecting them, rather than double-clicking. And the topic of double-clicking is something he is quite passionate about: "As nice as NetBeans is I will not use it if I must double-click every time I want to open a file into the editor."

Well, then, here's a plugin I created today that fixes that:

The code is as simple as this:

import org.netbeans.api.actions.Openable;
import org.openide.modules.OnStart;
import org.openide.util.Lookup;
import org.openide.util.LookupEvent;
import org.openide.util.LookupListener;
import org.openide.util.Utilities;
import org.openide.util.WeakListeners;

public class OneClickOpenSesame implements Runnable, LookupListener {
    Lookup.Result openableResult = null;
    public void run() {
        openableResult = Utilities.actionsGlobalContext().lookupResult(Openable.class);
                WeakListeners.create(LookupListener.class, this, openableResult));
    public void resultChanged(LookupEvent le) {
        for(Openable o : openableResult.allInstances()){

Someone reading the above code should now be thinking: "But wait a minute, doesn't this mean that someone who has selected a file, and thereby opened it, is never going to be able to close that file, unless selection changes to a different file?"

Indeed, yes, well done. The fatal flaw of this awesome plugin. But not a big deal. If you want to be able to close the file you opened, just select a folder (i.e., a folder doesn't have an Openable in its Lookup), after which you can close the file as usual.


Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.


« March 2014 »