KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > editors > text > templates > ContributionContextTypeRegistry


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.editors.text.templates;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IConfigurationElement;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Platform;
21 import org.eclipse.core.runtime.Status;
22
23 import org.eclipse.jface.text.templates.ContextTypeRegistry;
24 import org.eclipse.jface.text.templates.TemplateContextType;
25 import org.eclipse.jface.text.templates.TemplateVariableResolver;
26
27 import org.eclipse.ui.editors.text.EditorsUI;
28
29 import org.eclipse.ui.internal.editors.text.EditorsPlugin;
30
31
32 /**
33  * A registry for context types. Editor implementors will usually instantiate a
34  * registry and configure the context types available in their editor.
35  * <code>ContextType</code>s can be added either directly using
36  * {@link #addContextType(TemplateContextType)} or by instantiating and adding a
37  * contributed context type using {@link #addContextType(String)}.
38  *
39  * @since 3.0
40  */

41 public class ContributionContextTypeRegistry extends ContextTypeRegistry {
42
43     /* extension point string literals */
44     private static final String JavaDoc TEMPLATES_EXTENSION_POINT= "org.eclipse.ui.editors.templates"; //$NON-NLS-1$
45

46     private static final String JavaDoc CONTEXT_TYPE= "contextType"; //$NON-NLS-1$
47
private static final String JavaDoc ID= "id"; //$NON-NLS-1$
48
private static final String JavaDoc NAME= "name"; //$NON-NLS-1$
49
private static final String JavaDoc CLASS= "class"; //$NON-NLS-1$
50

51     private static final String JavaDoc RESOLVER= "resolver"; //$NON-NLS-1$
52
private static final String JavaDoc CONTEXT_TYPE_ID= "contextTypeId"; //$NON-NLS-1$
53
private static final String JavaDoc DESCRIPTION= "description"; //$NON-NLS-1$
54
private static final String JavaDoc TYPE= "type"; //$NON-NLS-1$
55

56     /**
57      * Tries to create a context type given an id. If there is already a context
58      * type registered under the given id, nothing happens. Otherwise,
59      * contributions to the <code>org.eclipse.ui.editors.templates</code>
60      * extension point are searched for the given identifier and the specified
61      * context type instantiated if it is found.
62      *
63      * @param id the id for the context type as specified in XML
64      */

65     public void addContextType(String JavaDoc id) {
66         Assert.isNotNull(id);
67         if (getContextType(id) != null)
68             return;
69
70         TemplateContextType type= createContextType(id);
71         if (type != null)
72             addContextType(type);
73
74     }
75
76     /**
77      * Tries to create a context type given an id. Contributions to the
78      * <code>org.eclipse.ui.editors.templates</code> extension point are
79      * searched for the given identifier and the specified context type
80      * instantiated if it is found. Any contributed
81      * {@link org.eclipse.jface.text.templates.TemplateVariableResolver}s
82      * are also instantiated and added to the context type.
83      *
84      * @param id the id for the context type as specified in XML
85      * @return the instantiated and configured context type, or
86      * <code>null</code> if it is not found or cannot be instantiated
87      */

88     public static TemplateContextType createContextType(String JavaDoc id) {
89         Assert.isNotNull(id);
90
91         IConfigurationElement[] extensions= getTemplateExtensions();
92         TemplateContextType type;
93         try {
94             type= createContextType(extensions, id);
95             if (type != null) {
96                 TemplateVariableResolver[] resolvers= createResolvers(extensions, id);
97                 for (int i= 0; i < resolvers.length; i++)
98                     type.addResolver(resolvers[i]);
99             }
100         } catch (CoreException e) {
101             EditorsPlugin.log(e);
102             type= null;
103         }
104
105         return type;
106     }
107
108     private static TemplateContextType createContextType(IConfigurationElement[] extensions, String JavaDoc contextTypeId) throws CoreException {
109         for (int i= 0; i < extensions.length; i++) {
110             // TODO create half-order over contributions
111
if (extensions[i].getName().equals(CONTEXT_TYPE)) {
112                 String JavaDoc id= extensions[i].getAttribute(ID);
113                 if (contextTypeId.equals(id))
114                     return createContextType(extensions[i]);
115             }
116         }
117
118         return null;
119     }
120
121     /**
122      * Instantiates the resolvers contributed to the context type with id
123      * <code>contextTypeId</code>. If instantiation of one resolver fails,
124      * the exception are logged and operation continues.
125      *
126      * @param extensions the configuration elements to parse
127      * @param contextTypeId the id of the context type for which resolvers are
128      * instantiated
129      * @return the instantiated resolvers
130      */

131     private static TemplateVariableResolver[] createResolvers(IConfigurationElement[] extensions, String JavaDoc contextTypeId) {
132         List JavaDoc resolvers= new ArrayList JavaDoc();
133         for (int i= 0; i < extensions.length; i++) {
134             if (extensions[i].getName().equals(RESOLVER)) {
135                 String JavaDoc declaredId= extensions[i].getAttribute(CONTEXT_TYPE_ID);
136                 if (contextTypeId.equals(declaredId)) {
137                     try {
138                         TemplateVariableResolver resolver= createResolver(extensions[i]);
139                         if (resolver != null)
140                             resolvers.add(resolver);
141                     } catch (CoreException e) {
142                         EditorsPlugin.log(e);
143                     }
144                 }
145             }
146         }
147
148         return (TemplateVariableResolver[]) resolvers.toArray(new TemplateVariableResolver[resolvers.size()]);
149
150     }
151
152     private static IConfigurationElement[] getTemplateExtensions() {
153         return Platform.getExtensionRegistry().getConfigurationElementsFor(TEMPLATES_EXTENSION_POINT);
154     }
155
156     private static TemplateContextType createContextType(IConfigurationElement element) throws CoreException {
157         String JavaDoc id= element.getAttribute(ID);
158         try {
159             TemplateContextType contextType= (TemplateContextType) element.createExecutableExtension(CLASS);
160             String JavaDoc name= element.getAttribute(NAME);
161             if (name == null)
162                 name= id;
163
164             if (contextType.getId() == null)
165                 contextType.setId(id);
166             if (contextType.getName() == null)
167                 contextType.setName(name);
168
169             return contextType;
170         } catch (ClassCastException JavaDoc e) {
171             throw new CoreException(new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, IStatus.OK, "extension does not implement " + TemplateContextType.class.getName(), e)); //$NON-NLS-1$
172
}
173     }
174
175     private static TemplateVariableResolver createResolver(IConfigurationElement element) throws CoreException {
176         try {
177             String JavaDoc type= element.getAttribute(TYPE);
178             if (type != null) {
179
180                 TemplateVariableResolver resolver= (TemplateVariableResolver) element.createExecutableExtension(CLASS);
181                 resolver.setType(type);
182
183                 String JavaDoc desc= element.getAttribute(DESCRIPTION);
184                 resolver.setDescription(desc == null ? "" : desc); //$NON-NLS-1$
185

186                 return resolver;
187             }
188         } catch (ClassCastException JavaDoc e) {
189             throw new CoreException(new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, IStatus.OK, "extension does not implement " + TemplateVariableResolver.class.getName(), e)); //$NON-NLS-1$
190
}
191
192         return null;
193     }
194 }
195
196
Popular Tags