1 11 package org.eclipse.jface.text.templates; 12 13 import java.util.ArrayList ; 14 import java.util.Collections ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Map ; 19 20 import org.eclipse.core.runtime.Assert; 21 22 import org.eclipse.text.edits.MalformedTreeException; 23 import org.eclipse.text.edits.MultiTextEdit; 24 import org.eclipse.text.edits.RangeMarker; 25 import org.eclipse.text.edits.ReplaceEdit; 26 import org.eclipse.text.edits.TextEdit; 27 28 import org.eclipse.jface.text.BadLocationException; 29 import org.eclipse.jface.text.Document; 30 import org.eclipse.jface.text.IDocument; 31 32 33 45 public class TemplateContextType { 46 47 48 private String fId= null; 49 50 51 private final Map fResolvers= new HashMap (); 52 53 54 private String fName= null; 55 56 62 public TemplateContextType(String id) { 63 this(id, id); 64 } 65 66 72 public TemplateContextType(String id, String name) { 73 Assert.isNotNull(id); 74 Assert.isNotNull(name); 75 fId= id; 76 fName= name; 77 } 78 79 84 public String getId() { 85 return fId; 86 } 87 88 89 94 public String getName() { 95 return fName; 96 } 97 98 107 public TemplateContextType() { 108 } 109 110 122 public final void setId(String id) throws RuntimeException { 123 Assert.isNotNull(id); 124 Assert.isTrue(fId == null); fId= id; 126 } 127 128 139 public final void setName(String name) { 140 Assert.isTrue(fName == null); fName= name; 142 } 143 144 150 public void addResolver(TemplateVariableResolver resolver) { 151 Assert.isNotNull(resolver); 152 fResolvers.put(resolver.getType(), resolver); 153 } 154 155 160 public void removeResolver(TemplateVariableResolver resolver) { 161 Assert.isNotNull(resolver); 162 fResolvers.remove(resolver.getType()); 163 } 164 165 168 public void removeAllResolvers() { 169 fResolvers.clear(); 170 } 171 172 177 public Iterator resolvers() { 178 return Collections.unmodifiableMap(fResolvers).values().iterator(); 179 } 180 181 187 protected TemplateVariableResolver getResolver(String type) { 188 return (TemplateVariableResolver) fResolvers.get(type); 189 } 190 191 198 public void validate(String pattern) throws TemplateException { 199 TemplateTranslator translator= new TemplateTranslator(); 200 TemplateBuffer buffer= translator.translate(pattern); 201 validateVariables(buffer.getVariables()); 202 } 203 204 216 protected void validateVariables(TemplateVariable[] variables) throws TemplateException { 217 } 218 219 228 public void resolve(TemplateBuffer buffer, TemplateContext context) throws MalformedTreeException, BadLocationException { 229 Assert.isNotNull(context); 230 TemplateVariable[] variables= buffer.getVariables(); 231 232 List positions= variablesToPositions(variables); 233 List edits= new ArrayList (5); 234 235 for (int i= 0; i != variables.length; i++) { 237 TemplateVariable variable= variables[i]; 238 239 if (!variable.isResolved()) 240 resolve(variable, context); 241 242 String value= variable.getDefaultValue(); 243 int[] offsets= variable.getOffsets(); 244 for (int k= 0; k != offsets.length; k++) 246 edits.add(new ReplaceEdit(offsets[k], variable.getInitialLength(), value)); 247 248 } 249 250 IDocument document= new Document(buffer.getString()); 251 MultiTextEdit edit= new MultiTextEdit(0, document.getLength()); 252 edit.addChildren((TextEdit[]) positions.toArray(new TextEdit[positions.size()])); 253 edit.addChildren((TextEdit[]) edits.toArray(new TextEdit[edits.size()])); 254 edit.apply(document, TextEdit.UPDATE_REGIONS); 255 256 positionsToVariables(positions, variables); 257 258 buffer.setContent(document.get(), variables); 259 } 260 261 268 public void resolve(TemplateVariable variable, TemplateContext context) { 269 String type= variable.getType(); 270 TemplateVariableResolver resolver= (TemplateVariableResolver) fResolvers.get(type); 271 if (resolver == null) 272 resolver= new TemplateVariableResolver(type, ""); resolver.resolve(variable, context); 274 } 275 276 private static List variablesToPositions(TemplateVariable[] variables) { 277 List positions= new ArrayList (5); 278 for (int i= 0; i != variables.length; i++) { 279 int[] offsets= variables[i].getOffsets(); 280 for (int j= 0; j != offsets.length; j++) 281 positions.add(new RangeMarker(offsets[j], 0)); 282 } 283 284 return positions; 285 } 286 287 private static void positionsToVariables(List positions, TemplateVariable[] variables) { 288 Iterator iterator= positions.iterator(); 289 290 for (int i= 0; i != variables.length; i++) { 291 TemplateVariable variable= variables[i]; 292 293 int[] offsets= new int[variable.getOffsets().length]; 294 for (int j= 0; j != offsets.length; j++) 295 offsets[j]= ((TextEdit) iterator.next()).getOffset(); 296 297 variable.setOffsets(offsets); 298 } 299 } 300 } 301 | Popular Tags |