Syntax highlighting is changeable.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / corext / template / php / CompilationUnitContext.java
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 net.sourceforge.phpdt.internal.ui.text.template.contentassist.MultiVariableGuess;
23
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         /** A flag to force evaluation in head-less mode. */
33         protected boolean fForceEvaluation;
34         /** A global state for proposals that change if a master proposal changes. */
35         protected MultiVariableGuess fMultiVariableGuess;
36
37         /**
38          * Creates a compilation unit context.
39          * 
40          * @param type   the context type
41          * @param document the document
42          * @param completionOffset the completion position within the document
43          * @param completionLength the completion length within the document
44          * @param compilationUnit the compilation unit (may be <code>null</code>)
45          */
46         protected CompilationUnitContext(TemplateContextType type, IDocument document, int completionOffset,
47                 int completionLength, ICompilationUnit compilationUnit)
48         {
49                 super(type, document, completionOffset, completionLength);
50                 fCompilationUnit= compilationUnit;
51         }
52         
53         /**
54          * Returns the compilation unit if one is associated with this context, <code>null</code> otherwise.
55          */
56         public final ICompilationUnit getCompilationUnit() {
57                 return fCompilationUnit;
58         }
59
60         /**
61          * Returns the enclosing element of a particular element type, <code>null</code>
62          * if no enclosing element of that type exists.
63          */
64         public IJavaElement findEnclosingElement(int elementType) {
65                 if (fCompilationUnit == null)
66                         return null;
67
68                 try {
69                         IJavaElement element= fCompilationUnit.getElementAt(getStart());
70                         if (element == null) {
71                                 element= fCompilationUnit;
72                         }
73                         
74                         return element.getAncestor(elementType);
75
76                 } catch (JavaModelException e) {
77                         return null;
78                 }       
79         }
80
81         /**
82          * Forces evaluation.
83          */
84         public void setForceEvaluation(boolean evaluate) {
85                 fForceEvaluation= evaluate;     
86         }
87         
88         /**
89          * Returns the multivariable guess - state
90          * @return
91          */
92         public MultiVariableGuess getMultiVariableGuess() {
93                 return fMultiVariableGuess;
94         } 
95
96         /**
97          * @param multiVariableGuess The multiVariableGuess to set.
98          */
99         public void setMultiVariableGuess(MultiVariableGuess multiVariableGuess) {
100                 fMultiVariableGuess= multiVariableGuess;
101         }
102 }