Hello Patrick,

you should create your own Lookup where to put what you want. I called this 
“MutableLookup” (I’ve found it on the web). You can use the put method to add 
the instances or your objects to lookup.

You could keep an instance of MutableLookup inside a TopComponent and call 
TopComponent.associateLookup(myMutableLookup) to expose your objects in the 
Lookup subsystem (via Utilities.actionsGlobalContext.lookup(...)) and retrieve 
them when the TopComponent is the active one.

Since the “mutable” nature of this Lookup, you can add and remove your objects 
at runtime.

Here follow the code of MutableLookup class:

public class MutableLookup extends AbstractLookup {
    private static final Logger LOGGER = 
Logger.getLogger(MutableLookup.class.getName());
    private final Object lock = new Object();
    private final InstanceContent content;
    
    static {
        LOGGER.setLevel(Level.SEVERE);
    }
    
    /**
     * Create a new DynamicLookup
     */
    public MutableLookup() {
        this(new InstanceContent());
    }
    
    /**
     * Create a new DynamicLookup with the specific content
     * @param content 
     */
    public MutableLookup(InstanceContent content) {
        super(content);
        this.content = content;
    }
    
    /**
     * Add a new object to this lookup.
     * 
     * @param instance 
     */
    public void add(Object instance) {
        synchronized (lock) {
            content.add(instance);
        }
    }
    
    /**
     * Put the instance in this lookup. This method ensures that there is only
     * one instance of that type class.
     * If there are other instances of the that class they will be removed.
     * If instance parameter is null than all instances of that type will be
     * removed from the lookup.
     * 
     * @param <T>
     * @param type
     * @param instance 
     */
    public <T> void put(Class<T> type, T instance) {
        synchronized (lock) {
            Collection<? extends T> all = lookupAll(type);
            if (instance == null) {
                remove(all);
            } else if (!all.contains(instance)) {
                remove(all);
                content.add(instance);
                LOGGER.log(Level.FINER, "added [{0}] to the lookup", instance);
            } else {
                List<T> instances = new LinkedList<T>(all);
                instances.remove(instance);
                remove(instances);
            }
        }
    }
    
    /**
     * Put collection in this lookup.
     * If there are other instances of the that class they will be replaced.
     * 
     * @param <T>
     * @param type
     * @param items 
     */
    public <T> void putAll(Class<T> type, Collection<? extends T> items) {
        synchronized (lock) {
            Collection<? extends T> all = lookupAll(type);
            remove(all);
            for (T item : items) {
                content.add(item);
            }
        }
    }
    
    /**
     * Remove instance from the lookup.
     * 
     * @param instance 
     */
    public void remove(Object instance) {
        synchronized (lock) {
            content.remove(instance);
        }
    }
    
    private <T> void remove(Collection<? extends T> items) {
        for (T item : items) {
            content.remove(item);
            LOGGER.log(Level.FINER, "removed [{0}] from the lookup.", item);
        }
    }
}

Marco Rossi

> Il giorno 13 ott 2019, alle ore 18:13, Patrik Karlström <pat...@trixon.se> ha 
> scritto:
> 
> I feel somewhat stupid asking this one, but I just can't grip how to 
> "publish" a "dynamically created" object so it will show up in lookupAll().
> 
> This is a really small library I'm working on and the only NetBeans 
> dependency I have is org-openide-util-lookup, if that matters at all.
> 
> The following declared class: 
> @ServiceProvider(service = Window.class)
> public class Left1bWindow extends Window {}
> 
> shows up as expected when I do:
> var windows = new ArrayList<>(Lookup.getDefault().lookupAll(Window.class));
> 
> but a simple object creation will not.
> Window someOtherWindow=new Window();
> 
> What can I do to get the desired result?


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@netbeans.apache.org
For additional commands, e-mail: users-h...@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to