Mercurial > projects > dwt-addons
comparison dwtx/jface/text/AbstractLineTracker.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.AbstractLineTracker; | |
14 | |
15 import dwt.dwthelper.utils; | |
16 | |
17 import java.util.ArrayList; | |
18 import java.util.Iterator; | |
19 import java.util.List; | |
20 | |
21 /** | |
22 * Abstract implementation of <code>ILineTracker</code>. It lets the definition of line | |
23 * delimiters to subclasses. Assuming that '\n' is the only line delimiter, this abstract | |
24 * implementation defines the following line scheme: | |
25 * <ul> | |
26 * <li> "" -> [0,0] | |
27 * <li> "a" -> [0,1] | |
28 * <li> "\n" -> [0,1], [1,0] | |
29 * <li> "a\n" -> [0,2], [2,0] | |
30 * <li> "a\nb" -> [0,2], [2,1] | |
31 * <li> "a\nbc\n" -> [0,2], [2,3], [5,0] | |
32 * </ul> | |
33 * <p> | |
34 * This class must be subclassed. | |
35 * </p> | |
36 */ | |
37 public abstract class AbstractLineTracker : ILineTracker, ILineTrackerExtension { | |
38 | |
39 /** | |
40 * Tells whether this class is in debug mode. | |
41 * | |
42 * @since 3.1 | |
43 */ | |
44 private static final bool DEBUG= false; | |
45 | |
46 /** | |
47 * Combines the information of the occurrence of a line delimiter. <code>delimiterIndex</code> | |
48 * is the index where a line delimiter starts, whereas <code>delimiterLength</code>, | |
49 * indicates the length of the delimiter. | |
50 */ | |
51 protected static class DelimiterInfo { | |
52 public int delimiterIndex; | |
53 public int delimiterLength; | |
54 public String delimiter; | |
55 } | |
56 | |
57 /** | |
58 * Representation of replace and set requests. | |
59 * | |
60 * @since 3.1 | |
61 */ | |
62 protected static class Request { | |
63 public final int offset; | |
64 public final int length; | |
65 public final String text; | |
66 | |
67 public Request(int offset, int length, String text) { | |
68 this.offset= offset; | |
69 this.length= length; | |
70 this.text= text; | |
71 } | |
72 | |
73 public Request(String text) { | |
74 this.offset= -1; | |
75 this.length= -1; | |
76 this.text= text; | |
77 } | |
78 | |
79 public bool isReplaceRequest() { | |
80 return this.offset > -1 && this.length > -1; | |
81 } | |
82 } | |
83 | |
84 /** | |
85 * The active rewrite session. | |
86 * | |
87 * @since 3.1 | |
88 */ | |
89 private DocumentRewriteSession fActiveRewriteSession; | |
90 /** | |
91 * The list of pending requests. | |
92 * | |
93 * @since 3.1 | |
94 */ | |
95 private List fPendingRequests; | |
96 /** | |
97 * The implementation that this tracker delegates to. | |
98 * | |
99 * @since 3.2 | |
100 */ | |
101 private ILineTracker fDelegate= new ListLineTracker() { | |
102 public String[] getLegalLineDelimiters() { | |
103 return AbstractLineTracker.this.getLegalLineDelimiters(); | |
104 } | |
105 | |
106 protected DelimiterInfo nextDelimiterInfo(String text, int offset) { | |
107 return AbstractLineTracker.this.nextDelimiterInfo(text, offset); | |
108 } | |
109 }; | |
110 /** | |
111 * Whether the delegate needs conversion when the line structure is modified. | |
112 */ | |
113 private bool fNeedsConversion= true; | |
114 | |
115 /** | |
116 * Creates a new line tracker. | |
117 */ | |
118 protected AbstractLineTracker() { | |
119 } | |
120 | |
121 /* | |
122 * @see dwtx.jface.text.ILineTracker#computeNumberOfLines(java.lang.String) | |
123 */ | |
124 public int computeNumberOfLines(String text) { | |
125 return fDelegate.computeNumberOfLines(text); | |
126 } | |
127 | |
128 /* | |
129 * @see dwtx.jface.text.ILineTracker#getLineDelimiter(int) | |
130 */ | |
131 public String getLineDelimiter(int line) throws BadLocationException { | |
132 checkRewriteSession(); | |
133 return fDelegate.getLineDelimiter(line); | |
134 } | |
135 | |
136 /* | |
137 * @see dwtx.jface.text.ILineTracker#getLineInformation(int) | |
138 */ | |
139 public IRegion getLineInformation(int line) throws BadLocationException { | |
140 checkRewriteSession(); | |
141 return fDelegate.getLineInformation(line); | |
142 } | |
143 | |
144 /* | |
145 * @see dwtx.jface.text.ILineTracker#getLineInformationOfOffset(int) | |
146 */ | |
147 public IRegion getLineInformationOfOffset(int offset) throws BadLocationException { | |
148 checkRewriteSession(); | |
149 return fDelegate.getLineInformationOfOffset(offset); | |
150 } | |
151 | |
152 /* | |
153 * @see dwtx.jface.text.ILineTracker#getLineLength(int) | |
154 */ | |
155 public int getLineLength(int line) throws BadLocationException { | |
156 checkRewriteSession(); | |
157 return fDelegate.getLineLength(line); | |
158 } | |
159 | |
160 /* | |
161 * @see dwtx.jface.text.ILineTracker#getLineNumberOfOffset(int) | |
162 */ | |
163 public int getLineNumberOfOffset(int offset) throws BadLocationException { | |
164 checkRewriteSession(); | |
165 return fDelegate.getLineNumberOfOffset(offset); | |
166 } | |
167 | |
168 /* | |
169 * @see dwtx.jface.text.ILineTracker#getLineOffset(int) | |
170 */ | |
171 public int getLineOffset(int line) throws BadLocationException { | |
172 checkRewriteSession(); | |
173 return fDelegate.getLineOffset(line); | |
174 } | |
175 | |
176 /* | |
177 * @see dwtx.jface.text.ILineTracker#getNumberOfLines() | |
178 */ | |
179 public int getNumberOfLines() { | |
180 try { | |
181 checkRewriteSession(); | |
182 } catch (BadLocationException x) { | |
183 // TODO there is currently no way to communicate that exception back to the document | |
184 } | |
185 return fDelegate.getNumberOfLines(); | |
186 } | |
187 | |
188 /* | |
189 * @see dwtx.jface.text.ILineTracker#getNumberOfLines(int, int) | |
190 */ | |
191 public int getNumberOfLines(int offset, int length) throws BadLocationException { | |
192 checkRewriteSession(); | |
193 return fDelegate.getNumberOfLines(offset, length); | |
194 } | |
195 | |
196 /* | |
197 * @see dwtx.jface.text.ILineTracker#set(java.lang.String) | |
198 */ | |
199 public void set(String text) { | |
200 if (hasActiveRewriteSession()) { | |
201 fPendingRequests.clear(); | |
202 fPendingRequests.add(new Request(text)); | |
203 return; | |
204 } | |
205 | |
206 fDelegate.set(text); | |
207 } | |
208 | |
209 /* | |
210 * @see dwtx.jface.text.ILineTracker#replace(int, int, java.lang.String) | |
211 */ | |
212 public void replace(int offset, int length, String text) throws BadLocationException { | |
213 if (hasActiveRewriteSession()) { | |
214 fPendingRequests.add(new Request(offset, length, text)); | |
215 return; | |
216 } | |
217 | |
218 checkImplementation(); | |
219 | |
220 fDelegate.replace(offset, length, text); | |
221 } | |
222 | |
223 /** | |
224 * Converts the implementation to be a {@link TreeLineTracker} if it isn't yet. | |
225 * | |
226 * @since 3.2 | |
227 */ | |
228 private void checkImplementation() { | |
229 if (fNeedsConversion) { | |
230 fNeedsConversion= false; | |
231 fDelegate= new TreeLineTracker((ListLineTracker) fDelegate) { | |
232 protected DelimiterInfo nextDelimiterInfo(String text, int offset) { | |
233 return AbstractLineTracker.this.nextDelimiterInfo(text, offset); | |
234 } | |
235 | |
236 public String[] getLegalLineDelimiters() { | |
237 return AbstractLineTracker.this.getLegalLineDelimiters(); | |
238 } | |
239 }; | |
240 } | |
241 } | |
242 | |
243 /** | |
244 * Returns the information about the first delimiter found in the given text starting at the | |
245 * given offset. | |
246 * | |
247 * @param text the text to be searched | |
248 * @param offset the offset in the given text | |
249 * @return the information of the first found delimiter or <code>null</code> | |
250 */ | |
251 protected abstract DelimiterInfo nextDelimiterInfo(String text, int offset); | |
252 | |
253 /* | |
254 * @see dwtx.jface.text.ILineTrackerExtension#startRewriteSession(dwtx.jface.text.DocumentRewriteSession) | |
255 * @since 3.1 | |
256 */ | |
257 public final void startRewriteSession(DocumentRewriteSession session) { | |
258 if (fActiveRewriteSession !is null) | |
259 throw new IllegalStateException(); | |
260 fActiveRewriteSession= session; | |
261 fPendingRequests= new ArrayList(20); | |
262 } | |
263 | |
264 /* | |
265 * @see dwtx.jface.text.ILineTrackerExtension#stopRewriteSession(dwtx.jface.text.DocumentRewriteSession, java.lang.String) | |
266 * @since 3.1 | |
267 */ | |
268 public final void stopRewriteSession(DocumentRewriteSession session, String text) { | |
269 if (fActiveRewriteSession is session) { | |
270 fActiveRewriteSession= null; | |
271 fPendingRequests= null; | |
272 set(text); | |
273 } | |
274 } | |
275 | |
276 /** | |
277 * Tells whether there's an active rewrite session. | |
278 * | |
279 * @return <code>true</code> if there is an active rewrite session, <code>false</code> | |
280 * otherwise | |
281 * @since 3.1 | |
282 */ | |
283 protected final bool hasActiveRewriteSession() { | |
284 return fActiveRewriteSession !is null; | |
285 } | |
286 | |
287 /** | |
288 * Flushes the active rewrite session. | |
289 * | |
290 * @throws BadLocationException in case the recorded requests cannot be processed correctly | |
291 * @since 3.1 | |
292 */ | |
293 protected final void flushRewriteSession() throws BadLocationException { | |
294 if (DEBUG) | |
295 System.out.println("AbstractLineTracker: Flushing rewrite session: " + fActiveRewriteSession); //$NON-NLS-1$ | |
296 | |
297 Iterator e= fPendingRequests.iterator(); | |
298 | |
299 fPendingRequests= null; | |
300 fActiveRewriteSession= null; | |
301 | |
302 while (e.hasNext()) { | |
303 Request request= (Request) e.next(); | |
304 if (request.isReplaceRequest()) | |
305 replace(request.offset, request.length, request.text); | |
306 else | |
307 set(request.text); | |
308 } | |
309 } | |
310 | |
311 /** | |
312 * Checks the presence of a rewrite session and flushes it. | |
313 * | |
314 * @throws BadLocationException in case flushing does not succeed | |
315 * @since 3.1 | |
316 */ | |
317 protected final void checkRewriteSession() throws BadLocationException { | |
318 if (hasActiveRewriteSession()) | |
319 flushRewriteSession(); | |
320 } | |
321 } |