1 12 package org.eclipse.jdt.internal.corext.template.java; 13 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.List ; 17 18 import org.eclipse.jface.text.templates.TemplateContext; 19 import org.eclipse.jface.text.templates.TemplateVariable; 20 import org.eclipse.jface.text.templates.TemplateVariableResolver; 21 22 import org.eclipse.jdt.internal.ui.text.template.contentassist.MultiVariable; 23 24 30 public class NameResolver extends TemplateVariableResolver { 31 32 private final String fDefaultType; 33 34 37 public NameResolver() { 38 this("java.lang.Object"); } 40 41 NameResolver(String defaultType) { 42 fDefaultType= defaultType; 43 } 44 45 48 public void resolve(TemplateVariable variable, TemplateContext context) { 49 List params= variable.getVariableType().getParams(); 50 String param; 51 if (params.size() == 0) 52 param= fDefaultType; 53 else 54 param= (String ) params.get(0); 55 JavaContext jc= (JavaContext) context; 56 TemplateVariable ref= jc.getTemplateVariable(param); 57 MultiVariable mv= (MultiVariable) variable; 58 if (ref instanceof MultiVariable) { 59 MultiVariable refVar= (MultiVariable) ref; 61 jc.addDependency(refVar, mv); 62 63 refVar.getAllChoices(); 64 Object [] types= flatten(refVar.getAllChoices()); 65 for (int i= 0; i < types.length; i++) { 66 String [] names= jc.suggestVariableNames(mv.toString(types[i])); 67 mv.setChoices(types[i], names); 68 } 69 70 mv.setKey(refVar.getCurrentChoice()); 71 jc.markAsUsed(mv.getDefaultValue()); 72 } else { 73 jc.addImport(param); 75 String [] names= jc.suggestVariableNames(param); 76 mv.setChoices(names); 77 jc.markAsUsed(names[0]); 78 } 79 } 80 81 private Object [] flatten(Object [][] allValues) { 82 List flattened= new ArrayList (allValues.length); 83 for (int i= 0; i < allValues.length; i++) { 84 flattened.addAll(Arrays.asList(allValues[i])); 85 } 86 return flattened.toArray(new Object [flattened.size()]); 87 } 88 } 89 | Popular Tags |