f121bdf9baa571bfdaf0a4064f7b32781d3224ce
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / corext / template / php / JavaContextType.java
1 /*******************************************************************************
2  * Copyright (c) 2000, orporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *     Sebastian Davids: sdavids@gmx.de - see bug 25376
11  *******************************************************************************/
12 package net.sourceforge.phpdt.internal.corext.template.php;
13
14 import net.sourceforge.phpdt.core.ICompilationUnit;
15 import net.sourceforge.phpdt.core.IJavaProject;
16 import net.sourceforge.phpdt.internal.corext.codemanipulation.StubUtility;
17
18 import org.eclipse.jface.text.IDocument;
19 import org.eclipse.jface.text.templates.GlobalTemplateVariables;
20 import org.eclipse.jface.text.templates.TemplateContext;
21 import org.eclipse.jface.text.templates.TemplateVariableResolver;
22
23 /**
24  * A context type for java code.
25  */
26 public class JavaContextType extends CompilationUnitContextType {
27
28         public static final String NAME = "php"; //$NON-NLS-1$
29
30         // protected static class Array extends TemplateVariableResolver {
31         // public Array() {
32         // super("array",
33         // TemplateMessages.getString("JavaContextType.variable.description.array"));
34         // //$NON-NLS-1$ //$NON-NLS-2$
35         // }
36         // protected String[] resolveAll(TemplateContext context) {
37         // return ((JavaContext) context).guessArrays();
38         // }
39         // /*
40         // * @see
41         // org.eclipse.jface.text.templates.TemplateVariableResolver#resolve(org.eclipse.jface.text.templates.TemplateVariable,
42         // org.eclipse.jface.text.templates.TemplateContext)
43         // */
44         // public void resolve(TemplateVariable variable, TemplateContext context) {
45         // if (variable instanceof MultiVariable) {
46         // JavaContext jc= (JavaContext) context;
47         // MultiVariable mv= (MultiVariable) variable;
48         // String[] bindings= resolveAll(context);
49         // if (bindings.length > 0) {
50         // mv.setValues(bindings);
51         // MultiVariableGuess guess= jc.getMultiVariableGuess();
52         // if (guess == null) {
53         // guess= new MultiVariableGuess(mv);
54         // jc.setMultiVariableGuess(guess);
55         // }
56         // }
57         // if (bindings.length > 1)
58         // variable.setUnambiguous(false);
59         // else
60         // variable.setUnambiguous(isUnambiguous(context));
61         // } else
62         // super.resolve(variable, context);
63         // }
64         // }
65         //
66         // protected static class ArrayType extends TemplateVariableResolver {
67         // public ArrayType() {
68         // super("array_type",
69         // TemplateMessages.getString("JavaContextType.variable.description.array.type"));
70         // //$NON-NLS-1$ //$NON-NLS-2$
71         // }
72         // protected String[] resolveAll(TemplateContext context) {
73         //              
74         // String[] arrayTypes= ((JavaContext) context).guessArrayTypes();
75         // if (arrayTypes != null)
76         // return arrayTypes;
77         // else
78         // return super.resolveAll(context);
79         // }
80         //          
81         // /*
82         // * @see
83         // org.eclipse.jface.text.templates.TemplateVariableResolver#resolve(org.eclipse.jface.text.templates.TemplateVariable,
84         // org.eclipse.jface.text.templates.TemplateContext)
85         // */
86         // public void resolve(TemplateVariable variable, TemplateContext context) {
87         // if (variable instanceof MultiVariable) {
88         // MultiVariable mv= (MultiVariable) variable;
89         // String[] arrays= ((JavaContext) context).guessArrays();
90         // String[][] types= ((JavaContext) context).guessGroupedArrayTypes();
91         //                              
92         // for (int i= 0; i < arrays.length; i++) {
93         // mv.setValues(arrays[i], types[i]);
94         // }
95         //
96         // if (arrays.length > 1 || types.length == 1 && types[0].length > 1)
97         // variable.setUnambiguous(false);
98         // else
99         // variable.setUnambiguous(isUnambiguous(context));
100         //                              
101         // } else
102         // super.resolve(variable, context);
103         // }
104         // }
105         //
106         // protected static class ArrayElement extends TemplateVariableResolver {
107         // public ArrayElement() {
108         // super("array_element",
109         // TemplateMessages.getString("JavaContextType.variable.description.array.element"));
110         // //$NON-NLS-1$ //$NON-NLS-2$
111         // }
112         // protected String[] resolveAll(TemplateContext context) {
113         // return ((JavaContext) context).guessArrayElements();
114         // }
115         //
116         // /*
117         // * @see
118         // org.eclipse.jface.text.templates.TemplateVariableResolver#resolve(org.eclipse.jface.text.templates.TemplateVariable,
119         // org.eclipse.jface.text.templates.TemplateContext)
120         // */
121         // public void resolve(TemplateVariable variable, TemplateContext context) {
122         // if (variable instanceof MultiVariable) {
123         // MultiVariable mv= (MultiVariable) variable;
124         // String[] arrays= ((JavaContext) context).guessArrays();
125         // String[][] elems= ((JavaContext) context).guessGroupedArrayElements();
126         //                              
127         // for (int i= 0; i < arrays.length; i++) {
128         // mv.setValues(arrays[i], elems[i]);
129         // }
130         //
131         // if (arrays.length > 1 || elems.length == 1 && elems[0].length > 1)
132         // variable.setUnambiguous(false);
133         // else
134         // variable.setUnambiguous(isUnambiguous(context));
135         //                              
136         // } else
137         // super.resolve(variable, context);
138         // }
139         // }
140         //
141         // protected static class Index extends TemplateVariableResolver {
142         // public Index() {
143         // super("index",
144         // TemplateMessages.getString("JavaContextType.variable.description.index"));
145         // //$NON-NLS-1$ //$NON-NLS-2$
146         // }
147         // protected String resolve(TemplateContext context) {
148         // return ((JavaContext) context).getIndex();
149         // }
150         // }
151         //
152         // protected static class Collection extends TemplateVariableResolver {
153         // public Collection() {
154         // super("collection",
155         // TemplateMessages.getString("JavaContextType.variable.description.collection"));
156         // //$NON-NLS-1$ //$NON-NLS-2$
157         // }
158         //          
159         // protected String[] resolveAll(TemplateContext context) {
160         // String[] collections= ((JavaContext) context).guessCollections();
161         // if (collections.length > 0)
162         // return collections;
163         // else
164         // return super.resolveAll(context);
165         // }
166         // }
167         //
168         // protected static class Iterator extends TemplateVariableResolver {
169         //
170         // public Iterator() {
171         // super("iterator",
172         // TemplateMessages.getString("JavaContextType.variable.description.iterator"));
173         // //$NON-NLS-1$ //$NON-NLS-2$
174         // }
175         // protected String resolve(TemplateContext context) {
176         // JavaContext javaContext= (JavaContext) context;
177         //
178         // if (!context.isReadOnly())
179         // javaContext.addIteratorImport();
180         //              
181         // return javaContext.getIterator();
182         // }
183         // }
184         //      
185         protected static class Todo extends TemplateVariableResolver {
186
187                 public Todo() {
188                         super(
189                                         "todo", JavaTemplateMessages.getString("JavaContextType.variable.description.todo")); //$NON-NLS-1$ //$NON-NLS-2$
190                 }
191
192                 protected String resolve(TemplateContext context) {
193                         JavaContext javaContext = (JavaContext) context;
194                         ICompilationUnit compilationUnit = javaContext.getCompilationUnit();
195                         if (compilationUnit == null)
196                                 return "XXX"; //$NON-NLS-1$
197
198                         IJavaProject javaProject = compilationUnit.getJavaProject();
199                         String todoTaskTag = StubUtility.getTodoTaskTag(javaProject);
200                         if (todoTaskTag == null)
201                                 return "XXX"; //$NON-NLS-1$
202
203                         return todoTaskTag;
204                 }
205         }
206
207         /*
208          * protected static class Arguments extends SimpleVariableResolver { public
209          * Arguments() { super("arguments",
210          * TemplateMessages.getString("JavaContextType.variable.description.arguments"),
211          * ""); } }
212          */
213
214         /**
215          * Creates a java context type.
216          */
217         public JavaContextType() {
218                 super(NAME);
219
220                 // global
221                 addResolver(new GlobalTemplateVariables.Cursor());
222                 addResolver(new GlobalTemplateVariables.WordSelection());
223                 addResolver(new GlobalTemplateVariables.LineSelection());
224                 addResolver(new GlobalTemplateVariables.Dollar());
225                 addResolver(new GlobalTemplateVariables.Date());
226                 addResolver(new GlobalTemplateVariables.Year());
227                 addResolver(new GlobalTemplateVariables.Time());
228                 addResolver(new GlobalTemplateVariables.User());
229
230                 // compilation unit
231                 addResolver(new File());
232                 addResolver(new PrimaryTypeName());
233                 addResolver(new ReturnType());
234                 addResolver(new Method());
235                 addResolver(new Type());
236                 addResolver(new Package());
237                 addResolver(new Project());
238                 addResolver(new Arguments());
239
240                 // java
241                 // addResolver(new Array());
242                 // addResolver(new ArrayType());
243                 // addResolver(new ArrayElement());
244                 // addResolver(new Index());
245                 // addResolver(new Iterator());
246                 // addResolver(new Collection());
247                 addResolver(new Todo());
248         }
249
250         /*
251          * (non-Javadoc)
252          * 
253          * @see net.sourceforge.phpdt.internal.corext.template.java.CompilationUnitContextType#createContext(org.eclipse.jface.text.IDocument,
254          *      int, int, net.sourceforge.phpdt.core.ICompilationUnit)
255          */
256         public CompilationUnitContext createContext(IDocument document, int offset,
257                         int length, ICompilationUnit compilationUnit) {
258                 return new JavaContext(this, document, offset, length, compilationUnit);
259         }
260
261 }