]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
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 | |
7 | * | |
8 | * Contributors: | |
9 | * IBM Corporation - initial API and implementation | |
10 | *******************************************************************************/ | |
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.HashSet; | |
16 | import java.util.Map; | |
17 | import java.util.Set; | |
18 | ||
19 | import org.eclipse.core.runtime.Assert; | |
20 | ||
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; | |
25 | ||
26 | import org.eclipse.jdt.core.Signature; | |
27 | ||
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; | |
31 | ||
32 | ||
33 | /** | |
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 | |
39 | * the slave variable. | |
40 | */ | |
41 | public class MultiVariable extends TemplateVariable { | |
42 | private static final Object DEFAULT_KEY= new Object(); | |
43 | ||
44 | private final Map<Object, Object[]> fValueMap= new HashMap<Object, Object[]>(); | |
45 | /** The master key defining the active set. */ | |
46 | private Object fKey; | |
47 | /** The currently active object. */ | |
48 | private Object fCurrentChoice; | |
49 | ||
50 | public MultiVariable(TemplateVariableType type, String name, int[] offsets) { | |
51 | super(type, name, name, offsets); | |
52 | fKey= DEFAULT_KEY; | |
53 | fValueMap.put(fKey, new String[] { name }); | |
54 | fCurrentChoice= getChoices()[0]; | |
55 | } | |
56 | ||
57 | /** | |
58 | * Sets the values of this variable under a specific key. | |
59 | * | |
60 | * @param key the key for which the values are valid | |
61 | * @param values the possible values of this variable | |
62 | */ | |
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); | |
69 | if (key.equals(fKey)) | |
70 | fCurrentChoice= getChoices()[0]; | |
71 | setResolved(true); | |
72 | } | |
73 | } | |
74 | ||
75 | public void setKey(Object defaultKey) { | |
76 | Assert.isTrue(fValueMap.containsKey(defaultKey)); | |
77 | if (!fKey.equals(defaultKey)) { | |
78 | fKey= defaultKey; | |
79 | fCurrentChoice= getChoices()[0]; | |
80 | } | |
81 | } | |
82 | ||
83 | public Object getCurrentChoice() { | |
84 | return fCurrentChoice; | |
85 | } | |
86 | ||
87 | public void setCurrentChoice(Object currentChoice) { | |
88 | Assert.isTrue(Arrays.asList(getChoices()).contains(currentChoice)); | |
89 | fCurrentChoice= currentChoice; | |
90 | } | |
91 | ||
92 | /* | |
93 | * @see org.eclipse.jface.text.templates.TemplateVariable#setValues(java.lang.String[]) | |
94 | */ | |
95 | @Override | |
96 | public void setValues(String[] values) { | |
97 | setChoices(values); | |
98 | } | |
99 | ||
100 | public void setChoices(Object[] values) { | |
101 | setChoices(DEFAULT_KEY, values); | |
102 | } | |
103 | ||
104 | /* | |
105 | * @see org.eclipse.jface.text.templates.TemplateVariable#getDefaultValue() | |
106 | * @since 3.3 | |
107 | */ | |
108 | @Override | |
109 | public String getDefaultValue() { | |
110 | return toString(fCurrentChoice); | |
111 | } | |
112 | ||
113 | public String toString(Object object) { | |
114 | return object.toString(); | |
115 | } | |
116 | ||
117 | /* | |
118 | * @see org.eclipse.jface.text.templates.TemplateVariable#getValues() | |
119 | */ | |
120 | @Override | |
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]); | |
126 | return result; | |
127 | } | |
128 | ||
129 | public Object[] getChoices() { | |
130 | return getChoices(fKey); | |
131 | } | |
132 | ||
133 | /** | |
134 | * Returns the choices for the set identified by <code>key</code>. | |
135 | * | |
136 | * @param key the key | |
137 | * @return the choices for this variable and the given set, or | |
138 | * <code>null</code> if the set is not defined. | |
139 | */ | |
140 | public Object[] getChoices(Object key) { | |
141 | return fValueMap.get(key); | |
142 | } | |
143 | ||
144 | public Object[][] getAllChoices() { | |
145 | return fValueMap.values().toArray(new Object[fValueMap.size()][]); | |
146 | } | |
147 | ||
148 | public Object[] generated_106998113967763239(MultiVariableGuess multivariableguess) { | |
149 | MultiVariable master= multivariableguess.fBackwardDeps.get(this); | |
150 | Object[] choices; | |
151 | if (master == null) | |
152 | choices= getChoices(); | |
153 | else | |
154 | choices= getChoices(master.getCurrentChoice()); | |
155 | return choices; | |
156 | } | |
157 | ||
158 | public void generated_768778533744750197(IDocument document, Object choice, MultiVariableGuess multivariableguess) { | |
159 | VariablePosition pos= multivariableguess.fPositions.get(this); | |
160 | ||
161 | Object slavesOldChoice= getCurrentChoice(); | |
162 | setKey(choice); // resets the current choice | |
163 | try { | |
164 | document.replace(pos.getOffset(), pos.getLength(), getDefaultValue()); | |
165 | } catch (BadLocationException x) { | |
166 | // ignore and continue | |
167 | } | |
168 | // handle slaves recursively | |
169 | if (multivariableguess.fDependencies.containsKey(this)) | |
170 | multivariableguess.updateSlaves(this, document, slavesOldChoice); | |
171 | } | |
172 | ||
173 | public void generated_8652397690278625374(MultiVariableGuess multivariableguess, MultiVariable slave) { | |
174 | Object parent= this; | |
175 | while (parent != null) { | |
176 | parent= multivariableguess.fBackwardDeps.get(parent); | |
177 | if (parent == slave) | |
178 | throw new IllegalArgumentException("cycle detected"); //$NON-NLS-1$ | |
179 | } | |
180 | ||
181 | Set<MultiVariable> slaves= multivariableguess.fDependencies.get(this); | |
182 | if (slaves == null) { | |
183 | slaves= new HashSet<MultiVariable>(); | |
184 | multivariableguess.fDependencies.put(this, slaves); | |
185 | } | |
186 | multivariableguess.fBackwardDeps.put(slave, this); | |
187 | slaves.add(slave); | |
188 | } | |
189 | ||
190 | public void generated_5819339108212610659(MultiVariableGuess guess, VariablePosition variableposition) { | |
191 | Assert.isNotNull(this); | |
192 | variableposition.fVariable= this; | |
193 | variableposition.fGuess= guess; | |
194 | } | |
195 | ||
196 | public void generated_4175174713144518848(JavaVariable master, JavaContext context) { | |
197 | context.addDependency(master, this); | |
198 | setKey(master.getCurrentChoice()); | |
199 | } | |
200 | ||
201 | public void generated_8767132048821270976(JavaContext jc, MultiVariable mv) { | |
202 | jc.addDependency(this, mv); | |
203 | ||
204 | getAllChoices(); | |
205 | } | |
206 | ||
207 | public void generated_7711907377430792203(String reference) { | |
208 | setValue(reference); | |
209 | setUnambiguous(true); | |
210 | } | |
211 | ||
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); | |
218 | } | |
219 | setKey(master.getCurrentChoice()); | |
220 | } | |
221 | ||
222 | public void generated_6477520692459289244(JavaContext jc, MultiVariable mv) { | |
223 | jc.addDependency(this, mv); | |
224 | mv.setKey(getCurrentChoice()); | |
225 | } | |
226 | ||
227 | public void generated_3993856917599233246(JavaContext jc, MultiVariable mv) { | |
228 | jc.addDependency(this, mv); | |
229 | mv.setKey(getCurrentChoice()); | |
230 | } | |
231 | } |