]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/core extension/org/eclipse/jdt/internal/corext/template/java/AbstractJavaContextType.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / core extension / org / eclipse / jdt / internal / corext / template / java / AbstractJavaContextType.java
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  *     Sebastian Davids: sdavids@gmx.de - see bug 25376
11  *******************************************************************************/
12 package org.eclipse.jdt.internal.corext.template.java;
13
14 import org.eclipse.jface.text.IDocument;
15 import org.eclipse.jface.text.Position;
16 import org.eclipse.jface.text.templates.GlobalTemplateVariables;
17 import org.eclipse.jface.text.templates.TemplateContext;
18 import org.eclipse.jface.text.templates.TemplateVariable;
19 import org.eclipse.jface.text.templates.TemplateVariableResolver;
20
21 import org.eclipse.jdt.core.ICompilationUnit;
22 import org.eclipse.jdt.core.IJavaProject;
23
24 import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility;
25 import org.eclipse.jdt.internal.corext.template.java.CompilationUnitCompletion.Variable;
26
27 import org.eclipse.jdt.internal.ui.text.template.contentassist.MultiVariable;
28
29
30 /**
31  * An abstract context type for templates inside Java code.
32  *
33  * @since 3.4
34  */
35 public abstract class AbstractJavaContextType extends CompilationUnitContextType {
36
37
38         protected static abstract class AbstractIterable extends TemplateVariableResolver {
39                 public AbstractIterable(String type, String description) {
40                         super(type, description);
41                 }
42
43                 @Override
44                 protected String[] resolveAll(TemplateContext context) {
45                 JavaContext jc= (JavaContext) context;
46                 Variable[] iterables= getLocalVariables(jc);
47                 String[] names= new String[iterables.length];
48                 for (int i= 0; i < iterables.length; i++)
49                                 names[i]= iterables[i].getName();
50                 if (names.length > 0)
51                         jc.markAsUsed(names[0]);
52                         return names;
53             }
54
55                 abstract protected Variable[] getLocalVariables(JavaContext jc);
56
57                 /*
58                  * @see org.eclipse.jface.text.templates.TemplateVariableResolver#resolve(org.eclipse.jface.text.templates.TemplateVariable, org.eclipse.jface.text.templates.TemplateContext)
59                  */
60                 @Override
61                 public void resolve(TemplateVariable variable, TemplateContext context) {
62                         if (variable instanceof MultiVariable) {
63                                 JavaContext jc= (JavaContext) context;
64                                 JavaVariable jv= (JavaVariable) variable;
65                         Variable[] iterables= getLocalVariables(jc);
66                                 if (iterables.length > 0) {
67                                         jv.setChoices(iterables);
68                                         jc.markAsUsed(iterables[0].getName());
69
70                                         if (iterables.length > 1)
71                                                 variable.setUnambiguous(false);
72                                         else
73                                                 variable.setUnambiguous(isUnambiguous(context));
74
75                                         return;
76                                 }
77                         }
78
79                         super.resolve(variable, context);
80                 }
81         }
82
83         protected static class Array extends AbstractIterable {
84                 public Array() {
85                         super("array", JavaTemplateMessages.JavaContextType_variable_description_array);  //$NON-NLS-1$
86                 }
87
88                 @Override
89                 protected Variable[] getLocalVariables(JavaContext jc) {
90                         return jc.getArrays();
91                 }
92         }
93
94         protected static class Iterable extends AbstractIterable {
95                 public Iterable() {
96                     super("iterable", JavaTemplateMessages.JavaContextType_variable_description_iterable);  //$NON-NLS-1$
97                 }
98
99                 @Override
100                 protected Variable[] getLocalVariables(JavaContext jc) {
101                         return jc.getIterables();
102                 }
103         }
104
105         protected static abstract class AbstractIterableType extends TemplateVariableResolver {
106                 private String fMasterName;
107
108             public AbstractIterableType(String type, String desc, String master) {
109                 super(type, desc);
110                 fMasterName= master;
111             }
112             @Override
113                 protected String[] resolveAll(TemplateContext context) {
114                 JavaContext jc= (JavaContext) context;
115                 Variable[] iterables= getLocalVariables(jc);
116                 String[] types= new String[iterables.length];
117                 for (int i= 0; i < iterables.length; i++)
118                         types[i]= iterables[i].getMemberTypeNames()[0];
119                         return types;
120             }
121
122                 abstract protected Variable[] getLocalVariables(JavaContext jc);
123
124                 /*
125                  * @see org.eclipse.jface.text.templates.TemplateVariableResolver#resolve(org.eclipse.jface.text.templates.TemplateVariable, org.eclipse.jface.text.templates.TemplateContext)
126                  */
127                 @Override
128                 public void resolve(TemplateVariable variable, TemplateContext context) {
129                         if (variable instanceof MultiVariable) {
130                                 JavaContext jc= (JavaContext) context;
131                                 MultiVariable mv= (MultiVariable) variable;
132
133                                 Variable[] iterables= getLocalVariables(jc);
134                                 if (iterables.length > 0) {
135
136                                         for (int i= 0; i < iterables.length; i++)
137                                                 mv.setChoices(iterables[i], iterables[i].getMemberTypeNames());
138
139                                         TemplateVariable master= jc.getTemplateVariable(fMasterName);
140                                         if (master instanceof MultiVariable) {
141                                                 final MultiVariable masterMv= (MultiVariable) master;
142                                                 jc.addDependency(masterMv, mv);
143                                                 mv.setKey(masterMv.getCurrentChoice());
144                                         }
145
146                                         if (iterables.length > 1 || iterables.length == 1 && mv.getChoices().length > 1)
147                                                 variable.setUnambiguous(false);
148                                         else
149                                                 variable.setUnambiguous(isUnambiguous(context));
150
151                                         return;
152                                 }
153
154                         }
155
156                         super.resolve(variable, context);
157                 }
158         }
159
160         protected static class ArrayType extends AbstractIterableType {
161                 public ArrayType() {
162                         super("array_type", JavaTemplateMessages.JavaContextType_variable_description_array_type, "array");  //$NON-NLS-1$ //$NON-NLS-2$
163                 }
164                 @Override
165                 protected Variable[] getLocalVariables(JavaContext jc) {
166                         return jc.getArrays();
167                 }
168         }
169
170         protected static class IterableType extends AbstractIterableType {
171                 public IterableType() {
172                 super("iterable_type", JavaTemplateMessages.JavaContextType_variable_description_iterable_type, "iterable");  //$NON-NLS-1$ //$NON-NLS-2$
173                 }
174                 @Override
175                 protected Variable[] getLocalVariables(JavaContext jc) {
176                         return jc.getIterables();
177                 }
178         }
179
180         protected static abstract class AbstractIterableElement extends TemplateVariableResolver {
181                 private String fMasterName;
182
183             public AbstractIterableElement(String type, String desc, String master) {
184                 super(type, desc);
185                 fMasterName= master;
186             }
187
188             @Override
189                 protected String[] resolveAll(TemplateContext context) {
190                 JavaContext jc= (JavaContext) context;
191                 Variable[] iterables= getLocalVariables(jc);
192                 String[] elements= new String[iterables.length];
193                 for (int i= 0; i < iterables.length; i++) {
194                         elements[i]= jc.suggestVariableNames(iterables[i].getMemberTypeNames()[0])[0];
195                         if (i == 0)
196                                 jc.markAsUsed(elements[0]);
197                 }
198
199                 return elements;
200             }
201
202                 abstract protected Variable[] getLocalVariables(JavaContext jc);
203
204                 /*
205                  * @see org.eclipse.jface.text.templates.TemplateVariableResolver#resolve(org.eclipse.jface.text.templates.TemplateVariable, org.eclipse.jface.text.templates.TemplateContext)
206                  */
207                 @Override
208                 public void resolve(TemplateVariable variable, TemplateContext context) {
209                         if (variable instanceof MultiVariable) {
210                                 JavaContext jc= (JavaContext) context;
211                                 MultiVariable mv= (MultiVariable) variable;
212
213                         Variable[] iterables= getLocalVariables(jc);
214                                 if (iterables.length > 0) {
215                                         for (int i= 0; i < iterables.length; i++) {
216                                                 String[] elements= jc.suggestVariableNames(iterables[i].getMemberTypeNames()[0]);
217                                                 mv.setChoices(iterables[i], elements);
218                                         }
219
220
221                                         TemplateVariable master= jc.getTemplateVariable(fMasterName);
222                                         if (master instanceof MultiVariable) {
223                                                 final MultiVariable masterMv= (MultiVariable) master;
224                                                 jc.addDependency(masterMv, mv);
225                                                 mv.setKey(masterMv.getCurrentChoice());
226                                         }
227                                         jc.markAsUsed(mv.getDefaultValue());
228
229                                         if (iterables.length > 1 || iterables.length == 1 && mv.getChoices().length > 1)
230                                                 variable.setUnambiguous(false);
231                                         else
232                                                 variable.setUnambiguous(isUnambiguous(context));
233
234                                         return;
235                                 }
236
237                         }
238                         super.resolve(variable, context);
239                 }
240         }
241
242         protected static class ArrayElement extends AbstractIterableElement {
243                 public ArrayElement() {
244                         super("array_element", JavaTemplateMessages.JavaContextType_variable_description_array_element, "array");        //$NON-NLS-1$ //$NON-NLS-2$
245                 }
246                 @Override
247                 protected Variable[] getLocalVariables(JavaContext jc) {
248                         return jc.getArrays();
249                 }
250         }
251
252         protected static class IterableElement extends AbstractIterableElement {
253                 public IterableElement() {
254                 super("iterable_element", JavaTemplateMessages.JavaContextType_variable_description_iterable_element, "iterable");       //$NON-NLS-1$ //$NON-NLS-2$
255                 }
256                 @Override
257                 protected Variable[] getLocalVariables(JavaContext jc) {
258                         return jc.getIterables();
259                 }
260         }
261
262         protected static class Index extends NameResolver {
263             public Index() {
264                 super("int"); //$NON-NLS-1$
265                 setType("index"); //$NON-NLS-1$
266                 setDescription(JavaTemplateMessages.JavaContextType_variable_description_index);
267             }
268         }
269
270         protected static class Collection extends LocalVarResolver {
271             public Collection() {
272                 super("java.util.Collection"); //$NON-NLS-1$
273                 setType("collection"); //$NON-NLS-1$
274                 setDescription(JavaTemplateMessages.JavaContextType_variable_description_collection);
275                 }
276         }
277
278         protected static class Iterator extends NameResolver {
279             public Iterator() {
280                 super("java.util.Iterator"); //$NON-NLS-1$
281                 setType("iterator"); //$NON-NLS-1$
282                 setDescription(JavaTemplateMessages.JavaContextType_variable_description_iterator);
283                 }
284         }
285
286         protected static class Todo extends TemplateVariableResolver {
287
288                 public Todo() {
289                         super("todo", JavaTemplateMessages.JavaContextType_variable_description_todo);  //$NON-NLS-1$
290                 }
291                 @Override
292                 protected String resolve(TemplateContext context) {
293                         JavaContext javaContext= (JavaContext) context;
294                         ICompilationUnit compilationUnit= javaContext.getCompilationUnit();
295                         if (compilationUnit == null)
296                                 return "XXX"; //$NON-NLS-1$
297
298                         IJavaProject javaProject= compilationUnit.getJavaProject();
299                         String todoTaskTag= StubUtility.getTodoTaskTag(javaProject);
300                         if (todoTaskTag == null)
301                                 return "XXX"; //$NON-NLS-1$
302
303                         return todoTaskTag;
304                 }
305         }
306 /*
307         protected static class Arguments extends SimpleVariableResolver {
308             public Arguments() {
309                 super("arguments", TemplateMessages.getString("Javavariable.description.arguments"), "");
310             }
311         }
312 */
313
314         /**
315          * Initializes the context type resolvers.
316          * <p>
317          * <strong>Note:</strong> Only call this method if this
318          * context type doesn't inherit from another context type
319          * which already has these resolvers.</p>
320          *
321          * @since 3.4
322          */
323         public void initializeContextTypeResolvers() {
324
325                 // global
326                 addResolver(new GlobalTemplateVariables.Cursor());
327                 addResolver(new GlobalTemplateVariables.WordSelection());
328                 addResolver(new SurroundWithLineSelection());
329                 addResolver(new GlobalTemplateVariables.Dollar());
330                 addResolver(new GlobalTemplateVariables.Date());
331                 addResolver(new GlobalTemplateVariables.Year());
332                 addResolver(new GlobalTemplateVariables.Time());
333                 addResolver(new GlobalTemplateVariables.User());
334
335                 // compilation unit
336                 addResolver(new File());
337                 addResolver(new PrimaryTypeName());
338                 addResolver(new ReturnType());
339                 addResolver(new Method());
340                 addResolver(new Type());
341                 addResolver(new Package());
342                 addResolver(new Project());
343                 addResolver(new Arguments());
344
345                 // java
346                 addResolver(new Array());
347                 addResolver(new ArrayType());
348                 addResolver(new ArrayElement());
349                 addResolver(new Index());
350                 addResolver(new Iterator());
351                 addResolver(new Collection());
352                 addResolver(new Iterable());
353                 addResolver(new IterableType());
354                 addResolver(new IterableElement());
355                 addResolver(new Todo());
356         }
357
358
359         /*
360          * @see org.eclipse.jdt.internal.corext.template.java.CompilationUnitContextType#createContext(org.eclipse.jface.text.IDocument, int, int, org.eclipse.jdt.core.ICompilationUnit)
361          */
362         @Override
363         public CompilationUnitContext createContext(IDocument document, int offset, int length, ICompilationUnit compilationUnit) {
364                 JavaContext javaContext= new JavaContext(this, document, offset, length, compilationUnit);
365                 initializeContext(javaContext);
366                 return javaContext;
367         }
368
369         /*
370          * @see org.eclipse.jdt.internal.corext.template.java.CompilationUnitContextType#createContext(org.eclipse.jface.text.IDocument, org.eclipse.jface.text.Position, org.eclipse.jdt.core.ICompilationUnit)
371          */
372         @Override
373         public CompilationUnitContext createContext(IDocument document, Position completionPosition, ICompilationUnit compilationUnit) {
374                 JavaContext javaContext= new JavaContext(this, document, completionPosition, compilationUnit);
375                 initializeContext(javaContext);
376                 return javaContext;
377         }
378
379         /**
380          * Hook to initialize the context
381          *
382          * @param context the context
383          */
384         protected void initializeContext(JavaContext context) {
385         }
386
387 }