KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > templates > DocumentTemplateContext


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.jface.text.templates;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.jface.text.BadLocationException;
16 import org.eclipse.jface.text.IDocument;
17 import org.eclipse.jface.text.Position;
18
19 /**
20  * Instances of this class describe the context of a template as a region of
21  * a document. That region may be either specified by its offset and length, or
22  * by a <code>Position</code> which may or may not be registered with the
23  * document.
24  * <p>
25  * Clients may instantiate and extend this class.
26  * </p>
27  *
28  * @since 3.0
29  */

30 public class DocumentTemplateContext extends TemplateContext {
31
32     /** The text of the document. */
33     private final IDocument fDocument;
34     /**
35      * The region of the document described by this context. We store a
36      * position since clients may specify the document region as (updateable)
37      * Positions.
38      */

39     private final Position fPosition;
40     /**
41      * The original offset of this context. Will only be updated by the setter
42      * method.
43      */

44     private int fOriginalOffset;
45     /**
46      * The original length of this context. Will only be updated by the setter
47      * method.
48      */

49     private int fOriginalLength;
50
51     /**
52      * Creates a document template context.
53      *
54      * @param type the context type
55      * @param document the document this context applies to
56      * @param offset the offset of the document region
57      * @param length the length of the document region
58      */

59     public DocumentTemplateContext(TemplateContextType type, IDocument document, int offset, int length) {
60         this(type, document, new Position(offset, length));
61     }
62
63     /**
64      * Creates a document template context. The supplied <code>Position</code>
65      * will be queried to compute the <code>getStart</code> and
66      * <code>getEnd</code> methods, which will therefore answer updated
67      * position data if it is registered with the document.
68      *
69      * @param type the context type
70      * @param document the document this context applies to
71      * @param position the position describing the area of the document which
72      * forms the template context
73      * @since 3.1
74      */

75     public DocumentTemplateContext(TemplateContextType type, IDocument document, Position position) {
76         super(type);
77
78         Assert.isNotNull(document);
79         Assert.isNotNull(position);
80         Assert.isTrue(position.getOffset() <= document.getLength());
81
82         fDocument= document;
83         fPosition= position;
84         fOriginalOffset= fPosition.getOffset();
85         fOriginalLength= fPosition.getLength();
86     }
87
88     /**
89      * Returns the document.
90      *
91      * @return the document
92      */

93     public IDocument getDocument() {
94         return fDocument;
95     }
96
97     /**
98      * Returns the completion offset within the string of the context.
99      *
100      * @return the completion offset within the string of the context
101      */

102     public int getCompletionOffset() {
103         return fOriginalOffset;
104     }
105
106     /**
107      * Sets the completion offset.
108      *
109      * @param newOffset the new completion offset
110      */

111     protected void setCompletionOffset(int newOffset) {
112         fOriginalOffset= newOffset;
113         fPosition.setOffset(newOffset);
114     }
115
116     /**
117      * Returns the completion length within the string of the context.
118      *
119      * @return the completion length within the string of the context
120      */

121     public int getCompletionLength() {
122         return fOriginalLength;
123     }
124
125     /**
126      * Sets the completion length.
127      *
128      * @param newLength the new completion length
129      */

130     protected void setCompletionLength(int newLength) {
131         fOriginalLength= newLength;
132         fPosition.setLength(newLength);
133     }
134
135     /**
136      * Returns the keyword which triggered template insertion.
137      *
138      * @return the keyword which triggered template insertion
139      */

140     public String JavaDoc getKey() {
141         int offset= getStart();
142         int length= getEnd() - offset;
143         try {
144             return fDocument.get(offset, length);
145         } catch (BadLocationException e) {
146             return ""; //$NON-NLS-1$
147
}
148     }
149
150     /**
151      * Returns the beginning offset of the keyword.
152      *
153      * @return the beginning offset of the keyword
154      */

155     public int getStart() {
156         return fPosition.getOffset();
157     }
158
159     /**
160      * Returns the end offset of the keyword.
161      *
162      * @return the end offset of the keyword
163      */

164     public int getEnd() {
165         return fPosition.getOffset() + fPosition.getLength();
166     }
167
168     /*
169      * @see org.eclipse.jface.text.templates.TemplateContext#canEvaluate(org.eclipse.jface.text.templates.Template)
170      */

171     public boolean canEvaluate(Template template) {
172         return true;
173     }
174
175     /*
176      * @see org.eclipse.jface.text.templates.TemplateContext#evaluate(org.eclipse.jface.text.templates.Template)
177      */

178     public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
179         if (!canEvaluate(template))
180             return null;
181
182         TemplateTranslator translator= new TemplateTranslator();
183         TemplateBuffer buffer= translator.translate(template);
184
185         getContextType().resolve(buffer, this);
186
187         return buffer;
188     }
189 }
190
Popular Tags