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;
23 // 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;
33 /** A flag to force evaluation in head-less mode. */
34 protected boolean fForceEvaluation;
36 /** A global state for proposals that change if a master proposal changes. */
37 protected MultiVariableGuess fMultiVariableGuess;
40 * Creates a compilation unit context.
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>)
53 protected CompilationUnitContext(TemplateContextType type,
54 IDocument document, int completionOffset, int completionLength,
55 ICompilationUnit compilationUnit) {
56 super(type, document, completionOffset, completionLength);
57 fCompilationUnit = compilationUnit;
61 * Returns the compilation unit if one is associated with this context,
62 * <code>null</code> otherwise.
64 public final ICompilationUnit getCompilationUnit() {
65 return fCompilationUnit;
69 * Returns the enclosing element of a particular element type,
70 * <code>null</code> if no enclosing element of that type exists.
72 public IJavaElement findEnclosingElement(int elementType) {
73 if (fCompilationUnit == null)
77 IJavaElement element = fCompilationUnit.getElementAt(getStart());
78 if (element == null) {
79 element = fCompilationUnit;
82 return element.getAncestor(elementType);
84 } catch (JavaModelException e) {
92 public void setForceEvaluation(boolean evaluate) {
93 fForceEvaluation = evaluate;
97 * Returns the multivariable guess - state
101 public MultiVariableGuess getMultiVariableGuess() {
102 return fMultiVariableGuess;
106 * @param multiVariableGuess
107 * The multiVariableGuess to set.
109 public void setMultiVariableGuess(MultiVariableGuess multiVariableGuess) {
110 fMultiVariableGuess = multiVariableGuess;