By using the SPI you can create lookups that can be used by the users of the Lookup API. While the Lookup API consists of a couple of classes in the package org.openide.util.*, the SPI has its own package org.openide.util.lookup.*.
singleton(...)
and
fixed(...)
do
not support dynamic changes and attaching listeners. Their content is
fixed from the time you call the creating method.
You simply create a new lookup like this:
import org.openide.util.Lookup; import org.openide.util.lookup.*; Lookup lookup1 = ...; Lookup lookup2 = Lookups.singleton(MyService.class); return new ProxyLookup(new Lookup[] { lookup, lookup2 });
The most powerful way to provide a lookup is to directly define what instances and items it should provide, by subclassing. For this, {@link org.openide.util.lookup.AbstractLookup AbstractLookup } is recommended as it is easiest to use.
The simplest way to use AbstractLookup
is to use its
public constructor (in which case you need not subclass it). Here you
provide an
{@link org.openide.util.lookup.AbstractLookup.Content AbstractLookup.Content }
object which you have created and hold on to privately, and which
keeps track of instances and permits them to be registered and
deregistered. Often
{@link org.openide.util.lookup.InstanceContent InstanceContent }
is used as the content implementation. To add something to the lookup,
simply use
{@link org.openide.util.lookup.InstanceContent#add(java.lang.Object) add(Object) }
(and remove(Object)
for the reverse). These may be called
at any time and will update the set of registered instances (firing
result changes as needed).
import org.openide.util.lookup.*; InstanceContent ic = new InstanceContent (); ic.add(firstObject); return new AbstractLookup (ic);
In case it is expensive to actually compute the object in the lookup, but there is some cheap "key" which can easily generate it, you may instead register the key by passing in an {@link org.openide.util.lookup.InstanceContent.Convertor InstanceContent.Convertor }. This convertor translates the key to the real instance that the lookup client sees, if and when needed. For example, if you have a long list of class names and wish to register default instances of each class, you might actually register the class name as the key, and supply a convertor which really loads the class and instantiates it. This makes it easy to set up the lookup, but nothing is really loaded until someone asks for it.