Mercurial > projects > dwt-addons
comparison dwtx/jface/text/templates/DocumentTemplateContext.d @ 129:eb30df5ca28b
Added JFace Text sources
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 23 Aug 2008 19:10:48 +0200 |
parents | |
children | c4fb132a086c |
comparison
equal
deleted
inserted
replaced
128:8df1d4193877 | 129:eb30df5ca28b |
---|---|
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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.jface.text.templates.DocumentTemplateContext; | |
14 | |
15 import dwt.dwthelper.utils; | |
16 | |
17 | |
18 import dwtx.core.runtime.Assert; | |
19 import dwtx.jface.text.BadLocationException; | |
20 import dwtx.jface.text.IDocument; | |
21 import dwtx.jface.text.Position; | |
22 | |
23 /** | |
24 * Instances of this class describe the context of a template as a region of | |
25 * a document. That region may be either specified by its offset and length, or | |
26 * by a <code>Position</code> which may or may not be registered with the | |
27 * document. | |
28 * <p> | |
29 * Clients may instantiate and extend this class. | |
30 * </p> | |
31 * | |
32 * @since 3.0 | |
33 */ | |
34 public class DocumentTemplateContext : TemplateContext { | |
35 | |
36 /** The text of the document. */ | |
37 private final IDocument fDocument; | |
38 /** | |
39 * The region of the document described by this context. We store a | |
40 * position since clients may specify the document region as (updateable) | |
41 * Positions. | |
42 */ | |
43 private final Position fPosition; | |
44 /** | |
45 * The original offset of this context. Will only be updated by the setter | |
46 * method. | |
47 */ | |
48 private int fOriginalOffset; | |
49 /** | |
50 * The original length of this context. Will only be updated by the setter | |
51 * method. | |
52 */ | |
53 private int fOriginalLength; | |
54 | |
55 /** | |
56 * Creates a document template context. | |
57 * | |
58 * @param type the context type | |
59 * @param document the document this context applies to | |
60 * @param offset the offset of the document region | |
61 * @param length the length of the document region | |
62 */ | |
63 public DocumentTemplateContext(TemplateContextType type, IDocument document, int offset, int length) { | |
64 this(type, document, new Position(offset, length)); | |
65 } | |
66 | |
67 /** | |
68 * Creates a document template context. The supplied <code>Position</code> | |
69 * will be queried to compute the <code>getStart</code> and | |
70 * <code>getEnd</code> methods, which will therefore answer updated | |
71 * position data if it is registered with the document. | |
72 * | |
73 * @param type the context type | |
74 * @param document the document this context applies to | |
75 * @param position the position describing the area of the document which | |
76 * forms the template context | |
77 * @since 3.1 | |
78 */ | |
79 public DocumentTemplateContext(TemplateContextType type, IDocument document, Position position) { | |
80 super(type); | |
81 | |
82 Assert.isNotNull(document); | |
83 Assert.isNotNull(position); | |
84 Assert.isTrue(position.getOffset() <= document.getLength()); | |
85 | |
86 fDocument= document; | |
87 fPosition= position; | |
88 fOriginalOffset= fPosition.getOffset(); | |
89 fOriginalLength= fPosition.getLength(); | |
90 } | |
91 | |
92 /** | |
93 * Returns the document. | |
94 * | |
95 * @return the document | |
96 */ | |
97 public IDocument getDocument() { | |
98 return fDocument; | |
99 } | |
100 | |
101 /** | |
102 * Returns the completion offset within the string of the context. | |
103 * | |
104 * @return the completion offset within the string of the context | |
105 */ | |
106 public int getCompletionOffset() { | |
107 return fOriginalOffset; | |
108 } | |
109 | |
110 /** | |
111 * Sets the completion offset. | |
112 * | |
113 * @param newOffset the new completion offset | |
114 */ | |
115 protected void setCompletionOffset(int newOffset) { | |
116 fOriginalOffset= newOffset; | |
117 fPosition.setOffset(newOffset); | |
118 } | |
119 | |
120 /** | |
121 * Returns the completion length within the string of the context. | |
122 * | |
123 * @return the completion length within the string of the context | |
124 */ | |
125 public int getCompletionLength() { | |
126 return fOriginalLength; | |
127 } | |
128 | |
129 /** | |
130 * Sets the completion length. | |
131 * | |
132 * @param newLength the new completion length | |
133 */ | |
134 protected void setCompletionLength(int newLength) { | |
135 fOriginalLength= newLength; | |
136 fPosition.setLength(newLength); | |
137 } | |
138 | |
139 /** | |
140 * Returns the keyword which triggered template insertion. | |
141 * | |
142 * @return the keyword which triggered template insertion | |
143 */ | |
144 public String getKey() { | |
145 int offset= getStart(); | |
146 int length= getEnd() - offset; | |
147 try { | |
148 return fDocument.get(offset, length); | |
149 } catch (BadLocationException e) { | |
150 return ""; //$NON-NLS-1$ | |
151 } | |
152 } | |
153 | |
154 /** | |
155 * Returns the beginning offset of the keyword. | |
156 * | |
157 * @return the beginning offset of the keyword | |
158 */ | |
159 public int getStart() { | |
160 return fPosition.getOffset(); | |
161 } | |
162 | |
163 /** | |
164 * Returns the end offset of the keyword. | |
165 * | |
166 * @return the end offset of the keyword | |
167 */ | |
168 public int getEnd() { | |
169 return fPosition.getOffset() + fPosition.getLength(); | |
170 } | |
171 | |
172 /* | |
173 * @see dwtx.jface.text.templates.TemplateContext#canEvaluate(dwtx.jface.text.templates.Template) | |
174 */ | |
175 public bool canEvaluate(Template template) { | |
176 return true; | |
177 } | |
178 | |
179 /* | |
180 * @see dwtx.jface.text.templates.TemplateContext#evaluate(dwtx.jface.text.templates.Template) | |
181 */ | |
182 public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException { | |
183 if (!canEvaluate(template)) | |
184 return null; | |
185 | |
186 TemplateTranslator translator= new TemplateTranslator(); | |
187 TemplateBuffer buffer= translator.translate(template); | |
188 | |
189 getContextType().resolve(buffer, this); | |
190 | |
191 return buffer; | |
192 } | |
193 } |