1 11 package org.eclipse.ui.editors.text.templates; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 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 41 public class ContributionContextTypeRegistry extends ContextTypeRegistry { 42 43 44 private static final String TEMPLATES_EXTENSION_POINT= "org.eclipse.ui.editors.templates"; 46 private static final String CONTEXT_TYPE= "contextType"; private static final String ID= "id"; private static final String NAME= "name"; private static final String CLASS= "class"; 51 private static final String RESOLVER= "resolver"; private static final String CONTEXT_TYPE_ID= "contextTypeId"; private static final String DESCRIPTION= "description"; private static final String TYPE= "type"; 56 65 public void addContextType(String 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 88 public static TemplateContextType createContextType(String 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 contextTypeId) throws CoreException { 109 for (int i= 0; i < extensions.length; i++) { 110 if (extensions[i].getName().equals(CONTEXT_TYPE)) { 112 String id= extensions[i].getAttribute(ID); 113 if (contextTypeId.equals(id)) 114 return createContextType(extensions[i]); 115 } 116 } 117 118 return null; 119 } 120 121 131 private static TemplateVariableResolver[] createResolvers(IConfigurationElement[] extensions, String contextTypeId) { 132 List resolvers= new ArrayList (); 133 for (int i= 0; i < extensions.length; i++) { 134 if (extensions[i].getName().equals(RESOLVER)) { 135 String 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 id= element.getAttribute(ID); 158 try { 159 TemplateContextType contextType= (TemplateContextType) element.createExecutableExtension(CLASS); 160 String 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 e) { 171 throw new CoreException(new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, IStatus.OK, "extension does not implement " + TemplateContextType.class.getName(), e)); } 173 } 174 175 private static TemplateVariableResolver createResolver(IConfigurationElement element) throws CoreException { 176 try { 177 String type= element.getAttribute(TYPE); 178 if (type != null) { 179 180 TemplateVariableResolver resolver= (TemplateVariableResolver) element.createExecutableExtension(CLASS); 181 resolver.setType(type); 182 183 String desc= element.getAttribute(DESCRIPTION); 184 resolver.setDescription(desc == null ? "" : desc); 186 return resolver; 187 } 188 } catch (ClassCastException e) { 189 throw new CoreException(new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, IStatus.OK, "extension does not implement " + TemplateVariableResolver.class.getName(), e)); } 191 192 return null; 193 } 194 } 195 196 | Popular Tags |