KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > template > java > CompilationUnitContext


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.template.java;
12
13 import org.eclipse.jface.text.IDocument;
14 import org.eclipse.jface.text.Position;
15 import org.eclipse.jface.text.templates.DocumentTemplateContext;
16 import org.eclipse.jface.text.templates.TemplateContextType;
17
18 import org.eclipse.jdt.core.ICompilationUnit;
19 import org.eclipse.jdt.core.IJavaElement;
20 import org.eclipse.jdt.core.IJavaProject;
21 import org.eclipse.jdt.core.JavaModelException;
22
23 import org.eclipse.jdt.internal.ui.text.template.contentassist.MultiVariableGuess;
24
25
26 /**
27  * A compilation unit context.
28  */

29 public abstract class CompilationUnitContext extends DocumentTemplateContext {
30
31     /** The compilation unit, may be <code>null</code>. */
32     private final ICompilationUnit fCompilationUnit;
33     /** A flag to force evaluation in head-less mode. */
34     protected boolean fForceEvaluation;
35     /** A global state for proposals that change if a master proposal changes. */
36     protected MultiVariableGuess fMultiVariableGuess;
37     /** <code>true</code> if the context has a managed (i.e. added to the document) position, <code>false</code> otherwise. */
38     protected final boolean fIsManaged;
39
40     /**
41      * Creates a compilation unit context.
42      *
43      * @param type the context type
44      * @param document the document
45      * @param completionOffset the completion position within the document
46      * @param completionLength the completion length within the document
47      * @param compilationUnit the compilation unit (may be <code>null</code>)
48      */

49     protected CompilationUnitContext(TemplateContextType type, IDocument document, int completionOffset, int completionLength, ICompilationUnit compilationUnit) {
50         super(type, document, completionOffset, completionLength);
51         fCompilationUnit= compilationUnit;
52         fIsManaged= false;
53     }
54     
55     /**
56      * Creates a compilation unit context.
57      *
58      * @param type the context type
59      * @param document the document
60      * @param completionPosition the position defining the completion offset and length
61      * @param compilationUnit the compilation unit (may be <code>null</code>)
62      * @since 3.2
63      */

64     protected CompilationUnitContext(TemplateContextType type, IDocument document, Position completionPosition, ICompilationUnit compilationUnit) {
65         super(type, document, completionPosition);
66         fCompilationUnit= compilationUnit;
67         fIsManaged= true;
68     }
69     
70     /**
71      * Returns the compilation unit if one is associated with this context,
72      * <code>null</code> otherwise.
73      *
74      * @return the compilation unit of this context or <code>null</code>
75      */

76     public final ICompilationUnit getCompilationUnit() {
77         return fCompilationUnit;
78     }
79
80     /**
81      * Returns the enclosing element of a particular element type,
82      * <code>null</code> if no enclosing element of that type exists.
83      *
84      * @param elementType the element type
85      * @return the enclosing element of the given type or <code>null</code>
86      */

87     public IJavaElement findEnclosingElement(int elementType) {
88         if (fCompilationUnit == null)
89             return null;
90
91         try {
92             IJavaElement element= fCompilationUnit.getElementAt(getStart());
93             if (element == null) {
94                 element= fCompilationUnit;
95             }
96             
97             return element.getAncestor(elementType);
98
99         } catch (JavaModelException e) {
100             return null;
101         }
102     }
103
104     /**
105      * Sets whether evaluation is forced or not.
106      *
107      * @param evaluate <code>true</code> in order to force evaluation,
108      * <code>false</code> otherwise
109      */

110     public void setForceEvaluation(boolean evaluate) {
111         fForceEvaluation= evaluate;
112     }
113     
114     /**
115      * Returns the multi-variable guess.
116      *
117      * @return the multi-variable guess
118      */

119     public MultiVariableGuess getMultiVariableGuess() {
120         return fMultiVariableGuess;
121     }
122
123     /**
124      * @param multiVariableGuess The multiVariableGuess to set.
125      */

126     void setMultiVariableGuess(MultiVariableGuess multiVariableGuess) {
127         fMultiVariableGuess= multiVariableGuess;
128     }
129     
130     protected IJavaProject getJavaProject() {
131         ICompilationUnit compilationUnit= getCompilationUnit();
132         IJavaProject project= compilationUnit == null ? null : compilationUnit.getJavaProject();
133         return project;
134     }
135 }
136
Popular Tags