KickJava   Java API By Example, From Geeks To Geeks.

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


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.preference.IPreferenceStore;
14
15 import org.eclipse.jface.text.BadLocationException;
16 import org.eclipse.jface.text.IDocument;
17 import org.eclipse.jface.text.IRegion;
18 import org.eclipse.jface.text.Position;
19 import org.eclipse.jface.text.TextUtilities;
20 import org.eclipse.jface.text.templates.TemplateContextType;
21 import org.eclipse.jface.text.templates.Template;
22 import org.eclipse.jface.text.templates.TemplateBuffer;
23 import org.eclipse.jface.text.templates.TemplateException;
24 import org.eclipse.jface.text.templates.TemplateTranslator;
25
26 import org.eclipse.jdt.core.ICompilationUnit;
27 import org.eclipse.jdt.core.IJavaProject;
28
29 import org.eclipse.jdt.internal.corext.util.Strings;
30
31 import org.eclipse.jdt.ui.PreferenceConstants;
32
33 import org.eclipse.jdt.internal.ui.JavaPlugin;
34
35
36 /**
37  * A context for javadoc.
38  */

39 public class JavaDocContext extends CompilationUnitContext {
40
41     // tags
42
private static final char HTML_TAG_BEGIN= '<';
43     private static final char HTML_TAG_END= '>';
44     private static final char JAVADOC_TAG_BEGIN= '@';
45
46     /**
47      * Creates a javadoc template context.
48      *
49      * @param type the context type.
50      * @param document the document.
51      * @param completionOffset the completion offset within the document.
52      * @param completionLength the completion length within the document.
53      * @param compilationUnit the compilation unit (may be <code>null</code>).
54      */

55     public JavaDocContext(TemplateContextType type, IDocument document, int completionOffset, int completionLength, ICompilationUnit compilationUnit) {
56         super(type, document, completionOffset, completionLength, compilationUnit);
57     }
58
59     /**
60      * Creates a javadoc template context.
61      *
62      * @param type the context type.
63      * @param document the document.
64      * @param completionPosition the position defining the completion offset and length
65      * @param compilationUnit the compilation unit (may be <code>null</code>).
66      * @since 3.2
67      */

68     public JavaDocContext(TemplateContextType type, IDocument document, Position completionPosition, ICompilationUnit compilationUnit) {
69         super(type, document, completionPosition, compilationUnit);
70     }
71     
72     /*
73      * @see TemplateContext#canEvaluate(Template templates)
74      */

75     public boolean canEvaluate(Template template) {
76         String JavaDoc key= getKey();
77         
78         if (fForceEvaluation)
79             return true;
80
81         return
82             template.matches(key, getContextType().getId()) &&
83             (key.length() != 0) && template.getName().toLowerCase().startsWith(key.toLowerCase());
84     }
85
86     /*
87      * @see DocumentTemplateContext#getStart()
88      */

89     public int getStart() {
90         if (fIsManaged && getCompletionLength() > 0)
91             return super.getStart();
92         
93         try {
94             IDocument document= getDocument();
95
96             if (getCompletionLength() == 0) {
97                 int start= getCompletionOffset();
98         
99                 if ((start != 0) && (document.getChar(start - 1) == HTML_TAG_END))
100                     start--;
101         
102                 while ((start != 0) && Character.isUnicodeIdentifierPart(document.getChar(start - 1)))
103                     start--;
104                 
105                 if ((start != 0) && Character.isUnicodeIdentifierStart(document.getChar(start - 1)))
106                     start--;
107         
108                 // include html and javadoc tags
109
if ((start != 0) && (
110                     (document.getChar(start - 1) == HTML_TAG_BEGIN) ||
111                     (document.getChar(start - 1) == JAVADOC_TAG_BEGIN)))
112                 {
113                     start--;
114                 }
115         
116                 return start;
117                 
118             }
119
120             int start= getCompletionOffset();
121             int end= getCompletionOffset() + getCompletionLength();
122             
123             while (start != 0 && Character.isUnicodeIdentifierPart(document.getChar(start - 1)))
124                 start--;
125             
126             while (start != end && Character.isWhitespace(document.getChar(start)))
127                 start++;
128             
129             if (start == end)
130                 start= getCompletionOffset();
131             
132             return start;
133             
134
135         } catch (BadLocationException e) {
136             return getCompletionOffset();
137         }
138     }
139
140     /*
141      * @see org.eclipse.jdt.internal.corext.template.DocumentTemplateContext#getEnd()
142      */

143     public int getEnd() {
144         
145         if (fIsManaged || getCompletionLength() == 0)
146             return super.getEnd();
147
148         try {
149             IDocument document= getDocument();
150
151             int start= getCompletionOffset();
152             int end= getCompletionOffset() + getCompletionLength();
153             
154             while (start != end && Character.isWhitespace(document.getChar(end - 1)))
155                 end--;
156             
157             return end;
158
159         } catch (BadLocationException e) {
160             return super.getEnd();
161         }
162     }
163
164     /*
165      * @see org.eclipse.jdt.internal.corext.template.DocumentTemplateContext#getKey()
166      */

167     public String JavaDoc getKey() {
168
169         if (getCompletionLength() == 0)
170             return super.getKey();
171
172         try {
173             IDocument document= getDocument();
174
175             int start= getStart();
176             int end= getCompletionOffset();
177             return start <= end
178                 ? document.get(start, end - start)
179                 : ""; //$NON-NLS-1$
180

181         } catch (BadLocationException e) {
182             return super.getKey();
183         }
184     }
185
186     /*
187      * @see TemplateContext#evaluate(Template)
188      */

189     public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
190         TemplateTranslator translator= new TemplateTranslator();
191         TemplateBuffer buffer= translator.translate(template);
192
193         getContextType().resolve(buffer, this);
194         
195         IPreferenceStore prefs= JavaPlugin.getDefault().getPreferenceStore();
196         boolean useCodeFormatter= prefs.getBoolean(PreferenceConstants.TEMPLATES_USE_CODEFORMATTER);
197
198         IJavaProject project= getJavaProject();
199         JavaFormatter formatter= new JavaFormatter(TextUtilities.getDefaultLineDelimiter(getDocument()), getIndentation(), useCodeFormatter, project);
200         formatter.format(buffer, this);
201             
202         return buffer;
203     }
204
205     /**
206      * Returns the indentation level at the position of code completion.
207      *
208      * @return the indentation level at the position of the code completion
209      */

210     private int getIndentation() {
211         int start= getStart();
212         IDocument document= getDocument();
213         try {
214             IRegion region= document.getLineInformationOfOffset(start);
215             String JavaDoc lineContent= document.get(region.getOffset(), region.getLength());
216             IJavaProject project= getJavaProject();
217             return Strings.computeIndentUnits(lineContent, project);
218         } catch (BadLocationException e) {
219             return 0;
220         }
221     }
222 }
223
224
Popular Tags