1 11 package org.eclipse.ui.internal.registry; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 15 16 import org.eclipse.core.runtime.IConfigurationElement; 17 import org.eclipse.core.runtime.IExtension; 18 import org.eclipse.core.runtime.IExtensionPoint; 19 import org.eclipse.core.runtime.Platform; 20 import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker; 21 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler; 22 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker; 23 import org.eclipse.ui.PlatformUI; 24 25 30 public final class KeywordRegistry implements IExtensionChangeHandler { 31 32 private static final String ATT_ID = "id"; 34 private static final String ATT_LABEL = "label"; 36 private static KeywordRegistry instance; 37 38 private static final String TAG_KEYWORD = "keyword"; 40 45 public static KeywordRegistry getInstance() { 46 if (instance == null) { 47 instance = new KeywordRegistry(); 48 } 49 50 return instance; 51 } 52 53 56 private Map internalKeywordMap = new HashMap (); 57 58 61 private KeywordRegistry() { 62 IExtensionTracker tracker = PlatformUI.getWorkbench().getExtensionTracker(); 63 tracker.registerHandler(this, ExtensionTracker.createExtensionPointFilter(getExtensionPointFilter())); 64 IExtension[] extensions = getExtensionPointFilter().getExtensions(); 65 for (int i = 0; i < extensions.length; i++) { 66 addExtension(PlatformUI.getWorkbench().getExtensionTracker(), 67 extensions[i]); 68 } 69 } 70 71 74 public void addExtension(IExtensionTracker tracker, IExtension extension) { 75 IConfigurationElement[] elements = extension.getConfigurationElements(); 76 for (int i = 0; i < elements.length; i++) { 77 if (elements[i].getName().equals(TAG_KEYWORD)) { 78 String name = elements[i].getAttribute(ATT_LABEL); 79 String id = elements[i].getAttribute(ATT_ID); 80 internalKeywordMap.put(id, name); 81 PlatformUI.getWorkbench().getExtensionTracker().registerObject( 82 extension, id, IExtensionTracker.REF_WEAK); 83 } 84 } 85 } 86 87 private IExtensionPoint getExtensionPointFilter() { 88 return Platform.getExtensionRegistry().getExtensionPoint( 89 PlatformUI.PLUGIN_ID, IWorkbenchRegistryConstants.PL_KEYWORDS); 90 } 91 92 98 public String getKeywordLabel(String id) { 99 return (String ) internalKeywordMap.get(id); 100 } 101 102 105 public void removeExtension(IExtension extension, Object [] objects) { 106 for (int i = 0; i < objects.length; i++) { 107 if (objects[i] instanceof String ) { 108 internalKeywordMap.remove(objects[i]); 109 } 110 } 111 } 112 } 113 | Popular Tags |