StickyLookup for NetBeans

The following is a slightly more compact version of the elegant StickyLookup, first described by Ernest Lötter: Thanks, Ernest!

public class StickyLookup extends ProxyLookup implements LookupListener {
    private final Lookup.Result result;
    private final InstanceContent ic;

    public StickyLookup(final Lookup lookup, final Class<?> clazz) {
        this(lookup, clazz, new InstanceContent());

    private StickyLookup(final Lookup lookup, final Class<?> clazz, InstanceContent ic) {
        super(Lookups.exclude(lookup, clazz), new AbstractLookup(ic));
        this.ic = ic;

        this.result = lookup.lookupResult(clazz);
        this.ic.set(this.result.allInstances(), null);

    public void resultChanged(LookupEvent ev) {
        if (this.result.allInstances().isEmpty()) {
            // Wrapped lookup is empty. We pretend like nothing happened and keep 
            // exposing the same instances as before.
        } else {
            // Just copy whatever the wrapped instance has
            ic.set(result.allInstances(), null);

2 Comments on “StickyLookup for NetBeans”

  1. Ernest Lötter says:

    Hi Hugo, great to see it is of some use and that you’ve cleaned it up so nicely (I’ve apparently missed the useful set() method on InstanceContent 🙂 ).

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s