01: /*
02: * ProxyContextProvider.java
03: *
04: * Created on January 25, 2004, 9:28 PM
05: */
06:
07: package org.netbeans.actions.spi;
08:
09: import java.util.Arrays;
10: import java.util.Collections;
11: import java.util.HashMap;
12: import java.util.Map;
13: import org.netbeans.actions.api.ContextProvider;
14:
15: /** Convenience implementation of ContextProvider which can proxy an
16: * array of ContextProviders.
17: *
18: * @author Tim Boudreau
19: */
20: public final class ProxyContextProvider implements ContextProvider {
21: private ContextProvider[] providers = null;
22:
23: public ProxyContextProvider() {
24: }
25:
26: /** Creates a new instance of ProxyContextProvider */
27: public ProxyContextProvider(ContextProvider[] providers) {
28: setProviders(providers);
29: assert !Arrays.asList(providers).contains(this ) : "ProxyContextProvider cannot recursively proxy itself"; //NOI18N
30: }
31:
32: /** Set the providers from which this provider will compose its
33: * context */
34: public void setProviders(ContextProvider[] providers) {
35: this .providers = providers;
36: }
37:
38: public Map getContext() {
39: if (providers == null || providers.length == 0) {
40: return Collections.EMPTY_MAP;
41: }
42: Map[] m = new Map[providers.length];
43: for (int i = 0; i < m.length; i++) {
44: if (providers[i] == this ) {
45: throw new IllegalStateException(
46: "ProxyContextProvider cannot recursively proxy itself"); //NOI18N
47: }
48: m[i] = providers[i].getContext();
49: }
50: return new ContextProviderSupport.ProxyMap(m);
51: }
52:
53: }
|