StickyLookup for NetBeans

The following is a slightly more compact version of the elegant StickyLookup, first described by Ernest Lötter: http://netbeans.dzone.com/articles/sticky-lookup. 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);
        this.result.addLookupListener(this);
    }

    @Override
    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.
            return;
        } 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 comment