如何创建全局查找上下文?

时间:2013-04-01 23:10:12

标签: java openide

我正在创建一个使用多个窗口来查看同一文档的Openide应用程序,我想这样做,以便在每个窗口中启用保存按钮(如果已启用)。我该怎么做?

1 个答案:

答案 0 :(得分:1)

这很简单,你可以自己实现ContextGlobalProvider。这些two sources可以帮助您实现这一目标。

使用这些来源,我能够创建两个不同版本的CentralLookup。这是你的“背景”没有改变的第一个:

@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);
    }
}

如果您想要根据当前上下文或“文档”进行更改,请使用以下命令:

    @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);
    }
}