001: /*
002: * ProxyContainerProvider.java
003: *
004: * Created on January 24, 2004, 3:42 PM
005: */
006:
007: package org.netbeans.actions.spi;
008:
009: import java.util.ArrayList;
010: import java.util.Arrays;
011: import java.util.HashMap;
012: import java.util.List;
013: import java.util.Map;
014: import org.netbeans.actions.spi.ContainerProvider;
015:
016: /** Container provider which can proxy other container providers.
017: *
018: * @author Tim Boudreau
019: */
020: public abstract class ProxyContainerProvider extends ContainerProvider {
021: private Map caches = new HashMap();
022: private String[] menuContainerContexts = null;
023: private String[] toolbarContainerContexts = null;
024:
025: protected ProxyContainerProvider() {
026: }
027:
028: protected abstract ContainerProvider[] getProviders();
029:
030: /** Get the cache for this particular container type, such as TYPE_TOOLBAR.
031: * This cache is used to store previous reverse lookups, mapping container
032: * contexts to the providers that provide them. Implementations that
033: * support container types not defined on ContainerProvider can use this
034: * to create caches for additional types, if they override findProviderOf().
035: */
036: protected final Map getCache(Object type) {
037: Map result = (Map) caches.get(type);
038: if (result == null) {
039: result = new HashMap();
040: caches.put(type, result);
041: }
042: return result;
043: }
044:
045: protected String[] sort(ContainerProvider[] providers,
046: Object[] containers, int count) {
047: String[] result = new String[count];
048: int pos = 0;
049: for (int i = 0; i < providers.length; i++) {
050: String[] curr = (String[]) containers[i];
051: System.arraycopy(curr, 0, result, pos, curr.length);
052: pos += curr.length;
053: }
054: return result;
055: }
056:
057: protected final void providersChanged() {
058: caches.clear();
059: menuContainerContexts = null;
060: toolbarContainerContexts = null;
061: }
062:
063: protected ContainerProvider findProviderOf(String containerCtx,
064: Object type) {
065: Map cache = getCache(type);
066: ContainerProvider result = (ContainerProvider) cache
067: .get(containerCtx);
068: if (result == null && type == TYPE_TOOLBAR || type == TYPE_MENU) {
069: ContainerProvider[] providers = getProviders();
070: for (int i = 0; i < providers.length; i++) {
071: String[] curr = type == TYPE_TOOLBAR ? providers[i]
072: .getToolbarContainerContexts() : providers[i]
073: .getMenuContainerContexts();
074:
075: if (Arrays.asList(curr).contains(containerCtx)) {
076: result = providers[i];
077: cache.put(containerCtx, providers[i]);
078: break;
079: }
080: }
081: }
082: return result;
083: }
084:
085: public final int getContainerState(Object type,
086: String containerCtx, java.util.Map context) {
087: ContainerProvider provider = findProviderOf(containerCtx, type);
088: return provider.getContainerState(type, containerCtx, context);
089: }
090:
091: public final String[] getMenuContainerContexts() {
092: if (menuContainerContexts != null) {
093: return menuContainerContexts;
094: }
095:
096: ContainerProvider[] providers = getProviders();
097: Object[] items = new Object[providers.length];
098: int total = 0;
099: for (int i = 0; i < providers.length; i++) {
100: String[] curr = providers[i].getMenuContainerContexts();
101: total += curr.length;
102: items[i] = curr;
103: }
104: String[] result = sort(providers, items, total);
105: if (result == null) {
106: throw new NullPointerException("Sort may not return null");
107: }
108: if (result.length != total) {
109: throw new IllegalStateException("Sort returned "
110: + result.length + " items but was passed " + total);
111: }
112: menuContainerContexts = result;
113: return result;
114: }
115:
116: public final String[] getToolbarContainerContexts() {
117: if (toolbarContainerContexts != null) {
118: return toolbarContainerContexts;
119: }
120:
121: ContainerProvider[] providers = getProviders();
122: Object[] items = new Object[providers.length];
123: int total = 0;
124: for (int i = 0; i < providers.length; i++) {
125: String[] curr = providers[i].getToolbarContainerContexts();
126: total += curr.length;
127: items[i] = curr;
128: }
129: String[] result = sort(providers, items, total);
130: if (result == null) {
131: throw new NullPointerException("Sort may not return null"); //NOI18N
132: }
133: if (result.length != total) {
134: throw new IllegalStateException("Sort returned "
135: + result.length + " items but was passed " + total); //NOI18N
136: }
137: toolbarContainerContexts = result;
138: return result;
139: }
140:
141: public String getDisplayName(Object type, String containerCtx) {
142: ContainerProvider provider = findProviderOf(containerCtx, type);
143: return provider.getDisplayName(type, containerCtx);
144: }
145:
146: }
|