KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > context > ContextManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.context;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IConfigurationElement;
21 import org.eclipse.core.runtime.IExtensionRegistry;
22 import org.eclipse.core.runtime.Platform;
23 import org.eclipse.help.AbstractContextProvider;
24 import org.eclipse.help.IContext;
25 import org.eclipse.help.internal.HelpPlugin;
26
27 /*
28  * Manages all context-sensitive help data for the help system.
29  */

30 public class ContextManager {
31
32     private static final String JavaDoc EXTENSION_POINT_ID_CONTEXT = HelpPlugin.PLUGIN_ID + ".contexts"; //$NON-NLS-1$
33
private static final String JavaDoc ELEMENT_NAME_CONTEXT_PROVIDER = "contextProvider"; //$NON-NLS-1$
34
private static final String JavaDoc ATTRIBUTE_NAME_CLASS = "class"; //$NON-NLS-1$
35

36     private Map JavaDoc providersByPluginId;
37     private List JavaDoc globalProviders;
38
39     private Map JavaDoc contextsById = new HashMap JavaDoc();
40     private Map JavaDoc idsByContext = new HashMap JavaDoc();
41     private int idCounter = 0;
42
43     /*
44      * Adds the given dynamically generated IContext to the system, and
45      * generates a unique ID for it.
46      */

47     public String JavaDoc addContext(IContext context) {
48         String JavaDoc plugin = HelpPlugin.PLUGIN_ID;
49         String JavaDoc id = (String JavaDoc)idsByContext.get(context);
50         if (id != null) {
51             // context already registered
52
} else {
53             // generate ID and register the context
54
id = "ID" + idCounter++; //$NON-NLS-1$
55
idsByContext.put(context, id);
56             contextsById.put(id, context);
57         }
58         return plugin + "." + id; //$NON-NLS-1$
59
}
60     
61     /*
62      * Returns the Context for the given id and locale.
63      */

64     public IContext getContext(String JavaDoc contextId, String JavaDoc locale) {
65         // first check for dynamic context definitions
66
Context dynamicContext = (Context)contextsById.get(contextId);
67         if (dynamicContext != null) {
68             return dynamicContext;
69         }
70         
71         // ask the providers
72
int index = contextId.lastIndexOf('.');
73         if (index != -1) {
74             String JavaDoc pluginId = contextId.substring(0, index);
75             Iterator JavaDoc iter = getContextProviders(pluginId).iterator();
76             while (iter.hasNext()) {
77                 AbstractContextProvider provider = (AbstractContextProvider)iter.next();
78                 try {
79                     IContext context = provider.getContext(contextId, locale);
80                     if (context != null) {
81                         return new Context(context, contextId);
82                     }
83                 }
84                 catch (Throwable JavaDoc t) {
85                     // log and skip
86
String JavaDoc msg = "Error querying context provider (" + provider.getClass().getName() + ") with context Id: " + contextId; //$NON-NLS-1$ //$NON-NLS-2$
87
HelpPlugin.logError(msg, t);
88                 }
89             }
90         }
91         return null;
92     }
93     
94     /*
95      * Returns all registered context providers (potentially cached) for the
96      * given plug-in id.
97      */

98     private List JavaDoc getContextProviders(String JavaDoc pluginId) {
99         if (providersByPluginId == null) {
100             loadContextProviders();
101         }
102         List JavaDoc list = new ArrayList JavaDoc();
103         List JavaDoc forPlugin = (List JavaDoc)providersByPluginId.get(pluginId);
104         if (forPlugin != null) {
105             list.addAll(forPlugin);
106         }
107         list.addAll(globalProviders);
108         return list;
109     }
110     
111     /*
112      * Finds and instantiates all registered context-sensitive help providers.
113      */

114     private void loadContextProviders() {
115         providersByPluginId = new HashMap JavaDoc();
116         globalProviders = new ArrayList JavaDoc();
117         
118         IExtensionRegistry registry = Platform.getExtensionRegistry();
119         IConfigurationElement[] elements = registry.getConfigurationElementsFor(EXTENSION_POINT_ID_CONTEXT);
120         for (int i=0;i<elements.length;++i) {
121             IConfigurationElement elem = elements[i];
122             if (elem.getName().equals(ELEMENT_NAME_CONTEXT_PROVIDER)) {
123                 try {
124                     AbstractContextProvider provider = (AbstractContextProvider)elem.createExecutableExtension(ATTRIBUTE_NAME_CLASS);
125                     String JavaDoc[] plugins = provider.getPlugins();
126                     if (plugins != null) {
127                         for (int j=0;j<plugins.length;++j) {
128                             List JavaDoc list = (List JavaDoc)providersByPluginId.get(plugins[j]);
129                             if (list == null) {
130                                 list = new ArrayList JavaDoc();
131                                 providersByPluginId.put(plugins[j], list);
132                             }
133                             list.add(provider);
134                         }
135                     }
136                     else {
137                         globalProviders.add(provider);
138                     }
139                 }
140                 catch (CoreException e) {
141                     // log and skip
142
String JavaDoc msg = "Error instantiating context-sensitive help provider class \"" + elem.getAttribute(ATTRIBUTE_NAME_CLASS) + '"'; //$NON-NLS-1$
143
HelpPlugin.logError(msg, e);
144                 }
145             }
146         }
147     }
148 }
149
Popular Tags