StickyLookup for NetBeans
Posted: December 23, 2010 Filed under: Uncategorized | Tags: java, netbeans 2 CommentsThe 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); } } }
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 🙂 ).
Hi! Yes, that one is indeed useful here 🙂