我正在制作一个 Openide 应用程序,它使用多个窗口来查看同一个文档,并且我想制作它,以便在每个窗口中启用保存按钮(如果已启用)。我怎么做?
问问题
653 次
1 回答
1
这很容易,您可以自己实现ContextGlobalProvider
. 这两个 来源可以帮助您做到这一点。
使用这些资源,我能够创建两个不同版本的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);
}
}
于 2013-04-01T23:10:11.773 回答