2013-04-01 22 views

回答

1

这是相当容易的,你让你自己实现的ContextGlobalProvider。这些twosources可以帮助你做到这一点。

利用这些资源,我能创造两个不同版本的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); 
    } 
}