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:

http://plugins.netbeans.org/plugin/53723

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;

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

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.

Comments:

I didn't quite understand why wouldn't you be able to close a file? the o.open() should only called when selection changes, right?

Posted by Dmitry Avtonomov on March 25, 2014 at 07:32 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

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.

Search

Archives
« July 2015
SunMonTueWedThuFriSat
   
4
7
11
12
20
30
31
 
       
Today