1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.text.template.contentassist;
13 import java.util.Arrays;
14 import java.util.HashMap;
15 import java.util.HashSet;
19 import org.eclipse.core.runtime.Assert;
21 import org.eclipse.jface.text.BadLocationException;
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.jface.text.templates.TemplateVariable;
24 import org.eclipse.jface.text.templates.TemplateVariableType;
26 import org.eclipse.jdt.core.Signature;
28 import org.eclipse.jdt.internal.corext.template.java.CompilationUnitCompletion.Variable;
29 import org.eclipse.jdt.internal.corext.template.java.JavaContext;
30 import org.eclipse.jdt.internal.corext.template.java.JavaVariable;
34 * {@link MultiVariable}s can store multiple sets of data; the currently active set is determined
35 * by the active <em>key</em>. The key may be set via {@link #setKey(Object)}. Data sets are
36 * opaque {@link Object} arrays that are converted to the {@link String} values expected by
37 * {@link TemplateVariable} using {@link Object#toString() toString}. The
38 * {@link #getCurrentChoice() choice} of a master variable is the {@link #setKey(Object) key} for
41 public class MultiVariable extends TemplateVariable {
42 private static final Object DEFAULT_KEY= new Object();
44 private final Map<Object, Object[]> fValueMap= new HashMap<Object, Object[]>();
45 /** The master key defining the active set. */
47 /** The currently active object. */
48 private Object fCurrentChoice;
50 public MultiVariable(TemplateVariableType type, String name, int[] offsets) {
51 super(type, name, name, offsets);
53 fValueMap.put(fKey, new String[] { name });
54 fCurrentChoice= getChoices()[0];
58 * Sets the values of this variable under a specific key.
60 * @param key the key for which the values are valid
61 * @param values the possible values of this variable
63 public void setChoices(Object key, Object[] values) {
64 Assert.isNotNull(key);
65 Assert.isTrue(values.length > 0);
66 // no action when called from super ctor
67 if (fValueMap != null) {
68 fValueMap.put(key, values);
70 fCurrentChoice= getChoices()[0];
75 public void setKey(Object defaultKey) {
76 Assert.isTrue(fValueMap.containsKey(defaultKey));
77 if (!fKey.equals(defaultKey)) {
79 fCurrentChoice= getChoices()[0];
83 public Object getCurrentChoice() {
84 return fCurrentChoice;
87 public void setCurrentChoice(Object currentChoice) {
88 Assert.isTrue(Arrays.asList(getChoices()).contains(currentChoice));
89 fCurrentChoice= currentChoice;
93 * @see org.eclipse.jface.text.templates.TemplateVariable#setValues(java.lang.String[])
96 public void setValues(String[] values) {
100 public void setChoices(Object[] values) {
101 setChoices(DEFAULT_KEY, values);
105 * @see org.eclipse.jface.text.templates.TemplateVariable#getDefaultValue()
109 public String getDefaultValue() {
110 return toString(fCurrentChoice);
113 public String toString(Object object) {
114 return object.toString();
118 * @see org.eclipse.jface.text.templates.TemplateVariable#getValues()
121 public String[] getValues() {
122 Object[] values= getChoices();
123 String[] result= new String[values.length];
124 for (int i= 0; i < result.length; i++)
125 result[i]= toString(values[i]);
129 public Object[] getChoices() {
130 return getChoices(fKey);
134 * Returns the choices for the set identified by <code>key</code>.
137 * @return the choices for this variable and the given set, or
138 * <code>null</code> if the set is not defined.
140 public Object[] getChoices(Object key) {
141 return fValueMap.get(key);
144 public Object[][] getAllChoices() {
145 return fValueMap.values().toArray(new Object[fValueMap.size()][]);
148 public Object[] generated_106998113967763239(MultiVariableGuess multivariableguess) {
149 MultiVariable master= multivariableguess.fBackwardDeps.get(this);
152 choices= getChoices();
154 choices= getChoices(master.getCurrentChoice());
158 public void generated_768778533744750197(IDocument document, Object choice, MultiVariableGuess multivariableguess) {
159 VariablePosition pos= multivariableguess.fPositions.get(this);
161 Object slavesOldChoice= getCurrentChoice();
162 setKey(choice); // resets the current choice
164 document.replace(pos.getOffset(), pos.getLength(), getDefaultValue());
165 } catch (BadLocationException x) {
166 // ignore and continue
168 // handle slaves recursively
169 if (multivariableguess.fDependencies.containsKey(this))
170 multivariableguess.updateSlaves(this, document, slavesOldChoice);
173 public void generated_8652397690278625374(MultiVariableGuess multivariableguess, MultiVariable slave) {
175 while (parent != null) {
176 parent= multivariableguess.fBackwardDeps.get(parent);
178 throw new IllegalArgumentException("cycle detected"); //$NON-NLS-1$
181 Set<MultiVariable> slaves= multivariableguess.fDependencies.get(this);
182 if (slaves == null) {
183 slaves= new HashSet<MultiVariable>();
184 multivariableguess.fDependencies.put(this, slaves);
186 multivariableguess.fBackwardDeps.put(slave, this);
190 public void generated_5819339108212610659(MultiVariableGuess guess, VariablePosition variableposition) {
191 Assert.isNotNull(this);
192 variableposition.fVariable= this;
193 variableposition.fGuess= guess;
196 public void generated_4175174713144518848(JavaVariable master, JavaContext context) {
197 context.addDependency(master, this);
198 setKey(master.getCurrentChoice());
201 public void generated_8767132048821270976(JavaContext jc, MultiVariable mv) {
202 jc.addDependency(this, mv);
207 public void generated_7711907377430792203(String reference) {
209 setUnambiguous(true);
212 public void generated_8790225314270930017(JavaVariable master, int index, Object[] choices, Variable[] variables, String type) {
213 for (int i= 0; i < choices.length; i++) {
214 String[] bounds= variables[i].getTypeArgumentBoundSignatures(type, index);
215 for (int j= 0; j < bounds.length; j++)
216 bounds[j]= Signature.getSignatureSimpleName(bounds[j]);
217 setChoices(variables[i], bounds);
219 setKey(master.getCurrentChoice());
222 public void generated_6477520692459289244(JavaContext jc, MultiVariable mv) {
223 jc.addDependency(this, mv);
224 mv.setKey(getCurrentChoice());
227 public void generated_3993856917599233246(JavaContext jc, MultiVariable mv) {
228 jc.addDependency(this, mv);
229 mv.setKey(getCurrentChoice());