1 11 package org.eclipse.jdt.internal.ui.text.template.contentassist; 12 13 import java.util.Arrays ; 14 import java.util.HashMap ; 15 import java.util.Map ; 16 17 import org.eclipse.core.runtime.Assert; 18 19 import org.eclipse.jface.text.templates.TemplateVariable; 20 import org.eclipse.jface.text.templates.TemplateVariableType; 21 22 23 31 public class MultiVariable extends TemplateVariable { 32 private static final Object DEFAULT_KEY= new Object (); 33 34 private final Map fValueMap= new HashMap (); 36 private Object fKey; 37 38 private Object fCurrentChoice; 39 40 public MultiVariable(TemplateVariableType type, String name, int[] offsets) { 41 super(type, name, name, offsets); 42 fKey= DEFAULT_KEY; 43 fValueMap.put(fKey, new String [] { name }); 44 fCurrentChoice= getChoices()[0]; 45 } 46 47 53 public void setChoices(Object key, Object [] values) { 54 Assert.isNotNull(key); 55 Assert.isTrue(values.length > 0); 56 if (fValueMap != null) { 58 fValueMap.put(key, values); 59 if (key.equals(fKey)) 60 fCurrentChoice= getChoices()[0]; 61 setResolved(true); 62 } 63 } 64 65 public void setKey(Object defaultKey) { 66 Assert.isTrue(fValueMap.containsKey(defaultKey)); 67 if (!fKey.equals(defaultKey)) { 68 fKey= defaultKey; 69 fCurrentChoice= getChoices()[0]; 70 } 71 } 72 73 public Object getCurrentChoice() { 74 return fCurrentChoice; 75 } 76 77 public void setCurrentChoice(Object currentChoice) { 78 Assert.isTrue(Arrays.asList(getChoices()).contains(currentChoice)); 79 fCurrentChoice= currentChoice; 80 } 81 82 85 public void setValues(String [] values) { 86 setChoices(values); 87 } 88 89 public void setChoices(Object [] values) { 90 setChoices(DEFAULT_KEY, values); 91 } 92 93 97 public String getDefaultValue() { 98 return toString(fCurrentChoice); 99 } 100 101 public String toString(Object object) { 102 return object.toString(); 103 } 104 105 108 public String [] getValues() { 109 Object [] values= getChoices(); 110 String [] result= new String [values.length]; 111 for (int i= 0; i < result.length; i++) 112 result[i]= toString(values[i]); 113 return result; 114 } 115 116 public Object [] getChoices() { 117 return getChoices(fKey); 118 } 119 120 127 public Object [] getChoices(Object key) { 128 return (Object []) fValueMap.get(key); 129 } 130 131 public Object [][] getAllChoices() { 132 return (Object [][]) fValueMap.values().toArray(new Object [fValueMap.size()][]); 133 } 134 } 135 | Popular Tags |