KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > registry > KeywordRegistry


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 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.ui.internal.registry;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
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 /**
26  * Contains extensions defined on the <code>keywords</code> extension point.
27  *
28  * @since 3.1
29  */

30 public final class KeywordRegistry implements IExtensionChangeHandler {
31
32     private static final String JavaDoc ATT_ID = "id"; //$NON-NLS-1$
33

34     private static final String JavaDoc ATT_LABEL = "label"; //$NON-NLS-1$
35

36     private static KeywordRegistry instance;
37
38     private static final String JavaDoc TAG_KEYWORD = "keyword"; //$NON-NLS-1$
39

40     /**
41      * Return the singleton instance of the <code>KeywordRegistry</code>.
42      *
43      * @return the singleton registry
44      */

45     public static KeywordRegistry getInstance() {
46         if (instance == null) {
47             instance = new KeywordRegistry();
48         }
49
50         return instance;
51     }
52
53     /**
54      * Map of id->labels.
55      */

56     private Map JavaDoc internalKeywordMap = new HashMap JavaDoc();
57     
58     /**
59      * Private constructor.
60      */

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     /* (non-Javadoc)
72      * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#addExtension(org.eclipse.core.runtime.dynamicHelpers.IExtensionTracker, org.eclipse.core.runtime.IExtension)
73      */

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 JavaDoc name = elements[i].getAttribute(ATT_LABEL);
79                 String JavaDoc 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     /**
93      * Return the label associated with the given keyword.
94      *
95      * @param id the keyword id
96      * @return the label or <code>null</code>
97      */

98     public String JavaDoc getKeywordLabel(String JavaDoc id) {
99         return (String JavaDoc) internalKeywordMap.get(id);
100     }
101
102     /* (non-Javadoc)
103      * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#removeExtension(org.eclipse.core.runtime.IExtension, java.lang.Object[])
104      */

105     public void removeExtension(IExtension extension, Object JavaDoc[] objects) {
106         for (int i = 0; i < objects.length; i++) {
107             if (objects[i] instanceof String JavaDoc) {
108                 internalKeywordMap.remove(objects[i]);
109             }
110         }
111     }
112 }
113
Popular Tags