2013-04-01 19 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); 
    } 
}