简体   繁体   中英

How do I make a global lookup context?

I am making an Openide application that uses several windows to view the same document, and I want to make it so the save button is enabled in every window if it is enabled. How do I do that?

It's rather easy, you make your own implementation of ContextGlobalProvider . These two sources can help you do that.

Using those sources, I was able to create two different versions of CentralLookup . Here is the first one for when your "context" does not change:

@ServiceProvider(service = ContextGlobalProvider.class,
    //this next arg is nessesary if you want yours to be the default
    supersedes = "org.netbeans.modules.openide.windows.GlobalActionContextImpl")
public class CentralLookup implements ContextGlobalProvider{
    private final InstanceContent content = new InstanceContent();
    private final Lookup lookup = new AbstractLookup(content);
    public CentralLookup() {}

    public void add(Object instance){
        content.add(instance);
    }

    public void remove(Object instance){
        content.remove(instance);
    }

    public static CentralLookup getInstance() {
        return CentralLookupHolder.INSTANCE;
    }

    // this is apperently only called once...
    @Override
    public Lookup createGlobalContext() {
        return lookup;
    }
    private static class CentralLookupHolder {
        //private static final CentralLookup INSTANCE = new CentralLookup();
        private static final CentralLookup INSTANCE = Lookup.getDefault().lookup(CentralLookup.class);
    }
}

If you want one that changes with your current context or "document", then use this:

    @ServiceProvider(service = ContextGlobalProvider.class,
    //this next arg is nessesary if you want yours to be the default
    supersedes = "org.netbeans.modules.openide.windows.GlobalActionContextImpl")
public class CentralLookup implements ContextGlobalProvider, Lookup.Provider{
    public CentralLookup() {}

    public void add(Object instance){
        getCurrentDocument().content.add(instance);
    }

    public void remove(Object instance){
        getCurrentDocument().content.remove(instance);
    }

    public static CentralLookup getInstance() {
        return CentralLookupHolder.INSTANCE;
    }

    // this is apperently only called once...
    @Override
    public Lookup createGlobalContext() {
        return Lookups.proxy(this);
    }

    @Override
    public Lookup getLookup(){
        return getCurrentDocument().lookup;
    }
    /**
     * Refresh which lookup is current.  Call this after changing the current document
     */
    public void updateLookupCurrent(){
        Utilities.actionsGlobalContext().lookup(ActionMap.class);
    }
    private static class CentralLookupHolder {
        //private static final CentralLookup INSTANCE = new CentralLookup();
        private static final CentralLookup INSTANCE = Lookup.getDefault().lookup(CentralLookup.class);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM