001: /*******************************************************************************
002: * Copyright (c) 2004, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.registry;
011:
012: import java.util.HashMap;
013: import java.util.Map;
014:
015: import org.eclipse.core.runtime.IConfigurationElement;
016: import org.eclipse.core.runtime.IExtension;
017: import org.eclipse.core.runtime.IExtensionPoint;
018: import org.eclipse.core.runtime.Platform;
019: import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker;
020: import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;
021: import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
022: import org.eclipse.ui.PlatformUI;
023:
024: /**
025: * Contains extensions defined on the <code>keywords</code> extension point.
026: *
027: * @since 3.1
028: */
029: public final class KeywordRegistry implements IExtensionChangeHandler {
030:
031: private static final String ATT_ID = "id"; //$NON-NLS-1$
032:
033: private static final String ATT_LABEL = "label"; //$NON-NLS-1$
034:
035: private static KeywordRegistry instance;
036:
037: private static final String TAG_KEYWORD = "keyword"; //$NON-NLS-1$
038:
039: /**
040: * Return the singleton instance of the <code>KeywordRegistry</code>.
041: *
042: * @return the singleton registry
043: */
044: public static KeywordRegistry getInstance() {
045: if (instance == null) {
046: instance = new KeywordRegistry();
047: }
048:
049: return instance;
050: }
051:
052: /**
053: * Map of id->labels.
054: */
055: private Map internalKeywordMap = new HashMap();
056:
057: /**
058: * Private constructor.
059: */
060: private KeywordRegistry() {
061: IExtensionTracker tracker = PlatformUI.getWorkbench()
062: .getExtensionTracker();
063: tracker.registerHandler(this , ExtensionTracker
064: .createExtensionPointFilter(getExtensionPointFilter()));
065: IExtension[] extensions = getExtensionPointFilter()
066: .getExtensions();
067: for (int i = 0; i < extensions.length; i++) {
068: addExtension(PlatformUI.getWorkbench()
069: .getExtensionTracker(), extensions[i]);
070: }
071: }
072:
073: /* (non-Javadoc)
074: * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#addExtension(org.eclipse.core.runtime.dynamicHelpers.IExtensionTracker, org.eclipse.core.runtime.IExtension)
075: */
076: public void addExtension(IExtensionTracker tracker,
077: IExtension extension) {
078: IConfigurationElement[] elements = extension
079: .getConfigurationElements();
080: for (int i = 0; i < elements.length; i++) {
081: if (elements[i].getName().equals(TAG_KEYWORD)) {
082: String name = elements[i].getAttribute(ATT_LABEL);
083: String id = elements[i].getAttribute(ATT_ID);
084: internalKeywordMap.put(id, name);
085: PlatformUI.getWorkbench().getExtensionTracker()
086: .registerObject(extension, id,
087: IExtensionTracker.REF_WEAK);
088: }
089: }
090: }
091:
092: private IExtensionPoint getExtensionPointFilter() {
093: return Platform.getExtensionRegistry().getExtensionPoint(
094: PlatformUI.PLUGIN_ID,
095: IWorkbenchRegistryConstants.PL_KEYWORDS);
096: }
097:
098: /**
099: * Return the label associated with the given keyword.
100: *
101: * @param id the keyword id
102: * @return the label or <code>null</code>
103: */
104: public String getKeywordLabel(String id) {
105: return (String) internalKeywordMap.get(id);
106: }
107:
108: /* (non-Javadoc)
109: * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#removeExtension(org.eclipse.core.runtime.IExtension, java.lang.Object[])
110: */
111: public void removeExtension(IExtension extension, Object[] objects) {
112: for (int i = 0; i < objects.length; i++) {
113: if (objects[i] instanceof String) {
114: internalKeywordMap.remove(objects[i]);
115: }
116: }
117: }
118: }
|