129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2007 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.DefaultDocumentAdapter;
|
|
14
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17
|
|
18 import java.util.ArrayList;
|
|
19 import java.util.Iterator;
|
|
20 import java.util.List;
|
|
21
|
|
22 import dwt.DWT;
|
|
23 import dwt.custom.TextChangeListener;
|
|
24 import dwt.custom.TextChangedEvent;
|
|
25 import dwt.custom.TextChangingEvent;
|
|
26 import dwtx.core.runtime.Assert;
|
|
27
|
|
28
|
|
29 /**
|
|
30 * Default implementation of {@link dwtx.jface.text.IDocumentAdapter}.
|
|
31 * <p>
|
|
32 * <strong>Note:</strong> This adapter does not work if the widget auto-wraps the text.
|
|
33 * </p>
|
|
34 */
|
|
35 class DefaultDocumentAdapter : IDocumentAdapter, IDocumentListener, IDocumentAdapterExtension {
|
|
36
|
|
37 /** The adapted document. */
|
|
38 private IDocument fDocument;
|
|
39 /** The document clone for the non-forwarding case. */
|
|
40 private IDocument fDocumentClone;
|
|
41 /** The original content */
|
|
42 private String fOriginalContent;
|
|
43 /** The original line delimiters */
|
|
44 private String[] fOriginalLineDelimiters;
|
|
45 /** The registered text change listeners */
|
|
46 private List fTextChangeListeners= new ArrayList(1);
|
|
47 /**
|
|
48 * The remembered document event
|
|
49 * @since 2.0
|
|
50 */
|
|
51 private DocumentEvent fEvent;
|
|
52 /** The line delimiter */
|
|
53 private String fLineDelimiter= null;
|
|
54 /**
|
|
55 * Indicates whether this adapter is forwarding document changes
|
|
56 * @since 2.0
|
|
57 */
|
|
58 private bool fIsForwarding= true;
|
|
59 /**
|
|
60 * Length of document at receipt of <code>documentAboutToBeChanged</code>
|
|
61 * @since 2.1
|
|
62 */
|
|
63 private int fRememberedLengthOfDocument;
|
|
64 /**
|
|
65 * Length of first document line at receipt of <code>documentAboutToBeChanged</code>
|
|
66 * @since 2.1
|
|
67 */
|
|
68 private int fRememberedLengthOfFirstLine;
|
|
69 /**
|
|
70 * The data of the event at receipt of <code>documentAboutToBeChanged</code>
|
|
71 * @since 2.1
|
|
72 */
|
|
73 private DocumentEvent fOriginalEvent= new DocumentEvent();
|
|
74
|
|
75
|
|
76 /**
|
|
77 * Creates a new document adapter which is initially not connected to
|
|
78 * any document.
|
|
79 */
|
|
80 public DefaultDocumentAdapter() {
|
|
81 }
|
|
82
|
|
83 /**
|
|
84 * Sets the given document as the document to be adapted.
|
|
85 *
|
|
86 * @param document the document to be adapted or <code>null</code> if there is no document
|
|
87 */
|
|
88 public void setDocument(IDocument document) {
|
|
89
|
|
90 if (fDocument !is null)
|
|
91 fDocument.removePrenotifiedDocumentListener(this);
|
|
92
|
|
93 fDocument= document;
|
|
94 fLineDelimiter= null;
|
|
95
|
|
96 if (!fIsForwarding) {
|
|
97 fDocumentClone= null;
|
|
98 if (fDocument !is null) {
|
|
99 fOriginalContent= fDocument.get();
|
|
100 fOriginalLineDelimiters= fDocument.getLegalLineDelimiters();
|
|
101 } else {
|
|
102 fOriginalContent= null;
|
|
103 fOriginalLineDelimiters= null;
|
|
104 }
|
|
105 }
|
|
106
|
|
107 if (fDocument !is null)
|
|
108 fDocument.addPrenotifiedDocumentListener(this);
|
|
109 }
|
|
110
|
|
111 /*
|
|
112 * @see StyledTextContent#addTextChangeListener(TextChangeListener)
|
|
113 */
|
|
114 public void addTextChangeListener(TextChangeListener listener) {
|
|
115 Assert.isNotNull(listener);
|
|
116 if (!fTextChangeListeners.contains(listener))
|
|
117 fTextChangeListeners.add(listener);
|
|
118 }
|
|
119
|
|
120 /*
|
|
121 * @see StyledTextContent#removeTextChangeListener(TextChangeListener)
|
|
122 */
|
|
123 public void removeTextChangeListener(TextChangeListener listener) {
|
|
124 Assert.isNotNull(listener);
|
|
125 fTextChangeListeners.remove(listener);
|
|
126 }
|
|
127
|
|
128 /**
|
|
129 * Tries to repair the line information.
|
|
130 *
|
|
131 * @param document the document
|
|
132 * @see IRepairableDocument#repairLineInformation()
|
|
133 * @since 3.0
|
|
134 */
|
|
135 private void repairLineInformation(IDocument document) {
|
|
136 if (document instanceof IRepairableDocument) {
|
|
137 IRepairableDocument repairable= (IRepairableDocument) document;
|
|
138 repairable.repairLineInformation();
|
|
139 }
|
|
140 }
|
|
141
|
|
142 /**
|
|
143 * Returns the line for the given line number.
|
|
144 *
|
|
145 * @param document the document
|
|
146 * @param line the line number
|
|
147 * @return the content of the line of the given number in the given document
|
|
148 * @throws BadLocationException if the line number is invalid for the adapted document
|
|
149 * @since 3.0
|
|
150 */
|
|
151 private String doGetLine(IDocument document, int line) throws BadLocationException {
|
|
152 IRegion r= document.getLineInformation(line);
|
|
153 return document.get(r.getOffset(), r.getLength());
|
|
154 }
|
|
155
|
|
156 private IDocument getDocumentForRead() {
|
|
157 if (!fIsForwarding) {
|
|
158 if (fDocumentClone is null) {
|
|
159 String content= fOriginalContent is null ? "" : fOriginalContent; //$NON-NLS-1$
|
|
160 String[] delims= fOriginalLineDelimiters is null ? DefaultLineTracker.DELIMITERS : fOriginalLineDelimiters;
|
|
161 fDocumentClone= new DocumentClone(content, delims);
|
|
162 }
|
|
163 return fDocumentClone;
|
|
164 }
|
|
165
|
|
166 return fDocument;
|
|
167 }
|
|
168
|
|
169 /*
|
|
170 * @see StyledTextContent#getLine(int)
|
|
171 */
|
|
172 public String getLine(int line) {
|
|
173
|
|
174 IDocument document= getDocumentForRead();
|
|
175 try {
|
|
176 return doGetLine(document, line);
|
|
177 } catch (BadLocationException x) {
|
|
178 repairLineInformation(document);
|
|
179 try {
|
|
180 return doGetLine(document, line);
|
|
181 } catch (BadLocationException x2) {
|
|
182 }
|
|
183 }
|
|
184
|
|
185 DWT.error(DWT.ERROR_INVALID_ARGUMENT);
|
|
186 return null;
|
|
187 }
|
|
188
|
|
189 /*
|
|
190 * @see StyledTextContent#getLineAtOffset(int)
|
|
191 */
|
|
192 public int getLineAtOffset(int offset) {
|
|
193 IDocument document= getDocumentForRead();
|
|
194 try {
|
|
195 return document.getLineOfOffset(offset);
|
|
196 } catch (BadLocationException x) {
|
|
197 repairLineInformation(document);
|
|
198 try {
|
|
199 return document.getLineOfOffset(offset);
|
|
200 } catch (BadLocationException x2) {
|
|
201 }
|
|
202 }
|
|
203
|
|
204 DWT.error(DWT.ERROR_INVALID_ARGUMENT);
|
|
205 return -1;
|
|
206 }
|
|
207
|
|
208 /*
|
|
209 * @see StyledTextContent#getLineCount()
|
|
210 */
|
|
211 public int getLineCount() {
|
|
212 return getDocumentForRead().getNumberOfLines();
|
|
213 }
|
|
214
|
|
215 /*
|
|
216 * @see StyledTextContent#getOffsetAtLine(int)
|
|
217 */
|
|
218 public int getOffsetAtLine(int line) {
|
|
219 IDocument document= getDocumentForRead();
|
|
220 try {
|
|
221 return document.getLineOffset(line);
|
|
222 } catch (BadLocationException x) {
|
|
223 repairLineInformation(document);
|
|
224 try {
|
|
225 return document.getLineOffset(line);
|
|
226 } catch (BadLocationException x2) {
|
|
227 }
|
|
228 }
|
|
229
|
|
230 DWT.error(DWT.ERROR_INVALID_ARGUMENT);
|
|
231 return -1;
|
|
232 }
|
|
233
|
|
234 /*
|
|
235 * @see StyledTextContent#getTextRange(int, int)
|
|
236 */
|
|
237 public String getTextRange(int offset, int length) {
|
|
238 try {
|
|
239 return getDocumentForRead().get(offset, length);
|
|
240 } catch (BadLocationException x) {
|
|
241 DWT.error(DWT.ERROR_INVALID_ARGUMENT);
|
|
242 return null;
|
|
243 }
|
|
244 }
|
|
245
|
|
246 /*
|
|
247 * @see StyledTextContent#replaceTextRange(int, int, String)
|
|
248 */
|
|
249 public void replaceTextRange(int pos, int length, String text) {
|
|
250 try {
|
|
251 fDocument.replace(pos, length, text);
|
|
252 } catch (BadLocationException x) {
|
|
253 DWT.error(DWT.ERROR_INVALID_ARGUMENT);
|
|
254 }
|
|
255 }
|
|
256
|
|
257 /*
|
|
258 * @see StyledTextContent#setText(String)
|
|
259 */
|
|
260 public void setText(String text) {
|
|
261 fDocument.set(text);
|
|
262 }
|
|
263
|
|
264 /*
|
|
265 * @see StyledTextContent#getCharCount()
|
|
266 */
|
|
267 public int getCharCount() {
|
|
268 return getDocumentForRead().getLength();
|
|
269 }
|
|
270
|
|
271 /*
|
|
272 * @see StyledTextContent#getLineDelimiter()
|
|
273 */
|
|
274 public String getLineDelimiter() {
|
|
275 if (fLineDelimiter is null)
|
|
276 fLineDelimiter= TextUtilities.getDefaultLineDelimiter(fDocument);
|
|
277 return fLineDelimiter;
|
|
278 }
|
|
279
|
|
280 /*
|
|
281 * @see IDocumentListener#documentChanged(DocumentEvent)
|
|
282 */
|
|
283 public void documentChanged(DocumentEvent event) {
|
|
284 // check whether the given event is the one which was remembered
|
|
285 if (fEvent is null || event !is fEvent)
|
|
286 return;
|
|
287
|
|
288 if (isPatchedEvent(event) || (event.getOffset() is 0 && event.getLength() is fRememberedLengthOfDocument)) {
|
|
289 fLineDelimiter= null;
|
|
290 fireTextSet();
|
|
291 } else {
|
|
292 if (event.getOffset() < fRememberedLengthOfFirstLine)
|
|
293 fLineDelimiter= null;
|
|
294 fireTextChanged();
|
|
295 }
|
|
296 }
|
|
297
|
|
298 /*
|
|
299 * @see IDocumentListener#documentAboutToBeChanged(DocumentEvent)
|
|
300 */
|
|
301 public void documentAboutToBeChanged(DocumentEvent event) {
|
|
302
|
|
303 fRememberedLengthOfDocument= fDocument.getLength();
|
|
304 try {
|
|
305 fRememberedLengthOfFirstLine= fDocument.getLineLength(0);
|
|
306 } catch (BadLocationException e) {
|
|
307 fRememberedLengthOfFirstLine= -1;
|
|
308 }
|
|
309
|
|
310 fEvent= event;
|
|
311 rememberEventData(fEvent);
|
|
312 fireTextChanging();
|
|
313 }
|
|
314
|
|
315 /**
|
|
316 * Checks whether this event has been changed between <code>documentAboutToBeChanged</code> and
|
|
317 * <code>documentChanged</code>.
|
|
318 *
|
|
319 * @param event the event to be checked
|
|
320 * @return <code>true</code> if the event has been changed, <code>false</code> otherwise
|
|
321 */
|
|
322 private bool isPatchedEvent(DocumentEvent event) {
|
|
323 return fOriginalEvent.fOffset !is event.fOffset || fOriginalEvent.fLength !is event.fLength || fOriginalEvent.fText !is event.fText;
|
|
324 }
|
|
325
|
|
326 /**
|
|
327 * Makes a copy of the given event and remembers it.
|
|
328 *
|
|
329 * @param event the event to be copied
|
|
330 */
|
|
331 private void rememberEventData(DocumentEvent event) {
|
|
332 fOriginalEvent.fOffset= event.fOffset;
|
|
333 fOriginalEvent.fLength= event.fLength;
|
|
334 fOriginalEvent.fText= event.fText;
|
|
335 }
|
|
336
|
|
337 /**
|
|
338 * Sends a text changed event to all registered listeners.
|
|
339 */
|
|
340 private void fireTextChanged() {
|
|
341
|
|
342 if (!fIsForwarding)
|
|
343 return;
|
|
344
|
|
345 TextChangedEvent event= new TextChangedEvent(this);
|
|
346
|
|
347 if (fTextChangeListeners !is null && fTextChangeListeners.size() > 0) {
|
|
348 Iterator e= new ArrayList(fTextChangeListeners).iterator();
|
|
349 while (e.hasNext())
|
|
350 ((TextChangeListener) e.next()).textChanged(event);
|
|
351 }
|
|
352 }
|
|
353
|
|
354 /**
|
|
355 * Sends a text set event to all registered listeners.
|
|
356 */
|
|
357 private void fireTextSet() {
|
|
358
|
|
359 if (!fIsForwarding)
|
|
360 return;
|
|
361
|
|
362 TextChangedEvent event = new TextChangedEvent(this);
|
|
363
|
|
364 if (fTextChangeListeners !is null && fTextChangeListeners.size() > 0) {
|
|
365 Iterator e= new ArrayList(fTextChangeListeners).iterator();
|
|
366 while (e.hasNext())
|
|
367 ((TextChangeListener) e.next()).textSet(event);
|
|
368 }
|
|
369 }
|
|
370
|
|
371 /**
|
|
372 * Sends the text changing event to all registered listeners.
|
|
373 */
|
|
374 private void fireTextChanging() {
|
|
375
|
|
376 if (!fIsForwarding)
|
|
377 return;
|
|
378
|
|
379 try {
|
|
380 IDocument document= fEvent.getDocument();
|
|
381 if (document is null)
|
|
382 return;
|
|
383
|
|
384 TextChangingEvent event= new TextChangingEvent(this);
|
|
385 event.start= fEvent.fOffset;
|
|
386 event.replaceCharCount= fEvent.fLength;
|
|
387 event.replaceLineCount= document.getNumberOfLines(fEvent.fOffset, fEvent.fLength) - 1;
|
|
388 event.newText= fEvent.fText;
|
|
389 event.newCharCount= (fEvent.fText is null ? 0 : fEvent.fText.length());
|
|
390 event.newLineCount= (fEvent.fText is null ? 0 : document.computeNumberOfLines(fEvent.fText));
|
|
391
|
|
392 if (fTextChangeListeners !is null && fTextChangeListeners.size() > 0) {
|
|
393 Iterator e= new ArrayList(fTextChangeListeners).iterator();
|
|
394 while (e.hasNext())
|
|
395 ((TextChangeListener) e.next()).textChanging(event);
|
|
396 }
|
|
397
|
|
398 } catch (BadLocationException e) {
|
|
399 }
|
|
400 }
|
|
401
|
|
402 /*
|
|
403 * @see IDocumentAdapterExtension#resumeForwardingDocumentChanges()
|
|
404 * @since 2.0
|
|
405 */
|
|
406 public void resumeForwardingDocumentChanges() {
|
|
407 fIsForwarding= true;
|
|
408 fDocumentClone= null;
|
|
409 fOriginalContent= null;
|
|
410 fOriginalLineDelimiters= null;
|
|
411 fireTextSet();
|
|
412 }
|
|
413
|
|
414 /*
|
|
415 * @see IDocumentAdapterExtension#stopForwardingDocumentChanges()
|
|
416 * @since 2.0
|
|
417 */
|
|
418 public void stopForwardingDocumentChanges() {
|
|
419 fDocumentClone= null;
|
|
420 fOriginalContent= fDocument.get();
|
|
421 fOriginalLineDelimiters= fDocument.getLegalLineDelimiters();
|
|
422 fIsForwarding= false;
|
|
423 }
|
|
424 }
|