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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.corext.template.php;
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;
18 import org.eclipse.jface.text.IDocument;
19 import org.eclipse.jface.text.templates.DocumentTemplateContext;
20 import org.eclipse.jface.text.templates.TemplateContextType;
22 //import net.sourceforge.phpdt.internal.ui.text.template.contentassist.MultiVariableGuess;
26 * A compilation unit context.
28 public abstract class CompilationUnitContext extends DocumentTemplateContext {
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;
38 * Creates a compilation unit context.
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>)
46 protected CompilationUnitContext(TemplateContextType type, IDocument document, int completionOffset,
47 int completionLength, ICompilationUnit compilationUnit)
49 super(type, document, completionOffset, completionLength);
50 fCompilationUnit= compilationUnit;
54 * Returns the compilation unit if one is associated with this context, <code>null</code> otherwise.
56 public final ICompilationUnit getCompilationUnit() {
57 return fCompilationUnit;
61 * Returns the enclosing element of a particular element type, <code>null</code>
62 * if no enclosing element of that type exists.
64 public IJavaElement findEnclosingElement(int elementType) {
65 if (fCompilationUnit == null)
69 IJavaElement element= fCompilationUnit.getElementAt(getStart());
70 if (element == null) {
71 element= fCompilationUnit;
74 return element.getAncestor(elementType);
76 } catch (JavaModelException e) {
84 public void setForceEvaluation(boolean evaluate) {
85 fForceEvaluation= evaluate;
89 * Returns the multivariable guess - state
92 public MultiVariableGuess getMultiVariableGuess() {
93 return fMultiVariableGuess;
97 * @param multiVariableGuess The multiVariableGuess to set.
99 public void setMultiVariableGuess(MultiVariableGuess multiVariableGuess) {
100 fMultiVariableGuess= multiVariableGuess;