1 11 package org.eclipse.help.internal.context; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Map ; 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 30 public class ContextManager { 31 32 private static final String EXTENSION_POINT_ID_CONTEXT = HelpPlugin.PLUGIN_ID + ".contexts"; private static final String ELEMENT_NAME_CONTEXT_PROVIDER = "contextProvider"; private static final String ATTRIBUTE_NAME_CLASS = "class"; 36 private Map providersByPluginId; 37 private List globalProviders; 38 39 private Map contextsById = new HashMap (); 40 private Map idsByContext = new HashMap (); 41 private int idCounter = 0; 42 43 47 public String addContext(IContext context) { 48 String plugin = HelpPlugin.PLUGIN_ID; 49 String id = (String )idsByContext.get(context); 50 if (id != null) { 51 } else { 53 id = "ID" + idCounter++; idsByContext.put(context, id); 56 contextsById.put(id, context); 57 } 58 return plugin + "." + id; } 60 61 64 public IContext getContext(String contextId, String locale) { 65 Context dynamicContext = (Context)contextsById.get(contextId); 67 if (dynamicContext != null) { 68 return dynamicContext; 69 } 70 71 int index = contextId.lastIndexOf('.'); 73 if (index != -1) { 74 String pluginId = contextId.substring(0, index); 75 Iterator 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 t) { 85 String msg = "Error querying context provider (" + provider.getClass().getName() + ") with context Id: " + contextId; HelpPlugin.logError(msg, t); 88 } 89 } 90 } 91 return null; 92 } 93 94 98 private List getContextProviders(String pluginId) { 99 if (providersByPluginId == null) { 100 loadContextProviders(); 101 } 102 List list = new ArrayList (); 103 List forPlugin = (List )providersByPluginId.get(pluginId); 104 if (forPlugin != null) { 105 list.addAll(forPlugin); 106 } 107 list.addAll(globalProviders); 108 return list; 109 } 110 111 114 private void loadContextProviders() { 115 providersByPluginId = new HashMap (); 116 globalProviders = new ArrayList (); 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 [] plugins = provider.getPlugins(); 126 if (plugins != null) { 127 for (int j=0;j<plugins.length;++j) { 128 List list = (List )providersByPluginId.get(plugins[j]); 129 if (list == null) { 130 list = new ArrayList (); 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 String msg = "Error instantiating context-sensitive help provider class \"" + elem.getAttribute(ATTRIBUTE_NAME_CLASS) + '"'; HelpPlugin.logError(msg, e); 144 } 145 } 146 } 147 } 148 } 149 | Popular Tags |