a7defad94bf2712493f48e2327aba0e40e63f971
[phpeclipse.git] /
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation 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  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.corext.template.php;
12
13 import net.sourceforge.phpdt.core.ICompilationUnit;
14 import net.sourceforge.phpdt.core.IJavaElement;
15 import net.sourceforge.phpdt.core.JavaModelException;
16 import net.sourceforge.phpdt.internal.ui.text.template.contentassist.MultiVariableGuess;
17
18 import org.eclipse.jface.text.IDocument;
19 import org.eclipse.jface.text.templates.DocumentTemplateContext;
20 import org.eclipse.jface.text.templates.TemplateContextType;
21
22 // import
23 // net.sourceforge.phpdt.internal.ui.text.template.contentassist.MultiVariableGuess;
24
25 /**
26  * A compilation unit context.
27  */
28 public abstract class CompilationUnitContext extends DocumentTemplateContext {
29
30         /** The compilation unit, may be <code>null</code>. */
31         private final ICompilationUnit fCompilationUnit;
32
33         /** A flag to force evaluation in head-less mode. */
34         protected boolean fForceEvaluation;
35
36         /** A global state for proposals that change if a master proposal changes. */
37         protected MultiVariableGuess fMultiVariableGuess;
38
39         /**
40          * Creates a compilation unit context.
41          * 
42          * @param type
43          *            the context type
44          * @param document
45          *            the document
46          * @param completionOffset
47          *            the completion position within the document
48          * @param completionLength
49          *            the completion length within the document
50          * @param compilationUnit
51          *            the compilation unit (may be <code>null</code>)
52          */
53         protected CompilationUnitContext(TemplateContextType type,
54                         IDocument document, int completionOffset, int completionLength,
55                         ICompilationUnit compilationUnit) {
56                 super(type, document, completionOffset, completionLength);
57                 fCompilationUnit = compilationUnit;
58         }
59
60         /**
61          * Returns the compilation unit if one is associated with this context,
62          * <code>null</code> otherwise.
63          */
64         public final ICompilationUnit getCompilationUnit() {
65                 return fCompilationUnit;
66         }
67
68         /**
69          * Returns the enclosing element of a particular element type,
70          * <code>null</code> if no enclosing element of that type exists.
71          */
72         public IJavaElement findEnclosingElement(int elementType) {
73                 if (fCompilationUnit == null)
74                         return null;
75
76                 try {
77                         IJavaElement element = fCompilationUnit.getElementAt(getStart());
78                         if (element == null) {
79                                 element = fCompilationUnit;
80                         }
81
82                         return element.getAncestor(elementType);
83
84                 } catch (JavaModelException e) {
85                         return null;
86                 }
87         }
88
89         /**
90          * Forces evaluation.
91          */
92         public void setForceEvaluation(boolean evaluate) {
93                 fForceEvaluation = evaluate;
94         }
95
96         /**
97          * Returns the multivariable guess - state
98          * 
99          * @return
100          */
101         public MultiVariableGuess getMultiVariableGuess() {
102                 return fMultiVariableGuess;
103         }
104
105         /**
106          * @param multiVariableGuess
107          *            The multiVariableGuess to set.
108          */
109         public void setMultiVariableGuess(MultiVariableGuess multiVariableGuess) {
110                 fMultiVariableGuess = multiVariableGuess;
111         }
112 }