comparison dwtx/jface/text/presentation/PresentationReconciler.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, 2008 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
14 module dwtx.jface.text.presentation.PresentationReconciler;
15
16 import dwt.dwthelper.utils;
17
18 import java.util.HashMap;
19 import java.util.Iterator;
20 import java.util.Map;
21
22 import dwt.custom.StyleRange;
23 import dwtx.core.runtime.Assert;
24 import dwtx.jface.text.BadLocationException;
25 import dwtx.jface.text.BadPositionCategoryException;
26 import dwtx.jface.text.DefaultPositionUpdater;
27 import dwtx.jface.text.DocumentEvent;
28 import dwtx.jface.text.DocumentPartitioningChangedEvent;
29 import dwtx.jface.text.IDocument;
30 import dwtx.jface.text.IDocumentExtension3;
31 import dwtx.jface.text.IDocumentListener;
32 import dwtx.jface.text.IDocumentPartitioningListener;
33 import dwtx.jface.text.IDocumentPartitioningListenerExtension;
34 import dwtx.jface.text.IDocumentPartitioningListenerExtension2;
35 import dwtx.jface.text.IPositionUpdater;
36 import dwtx.jface.text.IRegion;
37 import dwtx.jface.text.ITextInputListener;
38 import dwtx.jface.text.ITextListener;
39 import dwtx.jface.text.ITextViewer;
40 import dwtx.jface.text.ITextViewerExtension5;
41 import dwtx.jface.text.ITypedRegion;
42 import dwtx.jface.text.Region;
43 import dwtx.jface.text.TextEvent;
44 import dwtx.jface.text.TextPresentation;
45 import dwtx.jface.text.TextUtilities;
46 import dwtx.jface.text.TypedPosition;
47
48
49
50 /**
51 * Standard implementation of <code>IPresentationReconciler</code>. This
52 * implementation assumes that the tasks performed by its presentation damagers
53 * and repairers are lightweight and of low cost. This presentation reconciler
54 * runs in the UI thread and always repairs the complete damage caused by a
55 * document change rather than just the portion overlapping with the viewer's
56 * viewport.
57 * <p>
58 * Usually, clients instantiate this class and configure it before using it.
59 * </p>
60 */
61 public class PresentationReconciler : IPresentationReconciler, IPresentationReconcilerExtension {
62
63 /** Prefix of the name of the position category for tracking damage regions. */
64 protected final static String TRACKED_PARTITION= "__reconciler_tracked_partition"; //$NON-NLS-1$
65
66
67 /**
68 * Internal listener class.
69 */
70 class InternalListener :
71 ITextInputListener, IDocumentListener, ITextListener,
72 IDocumentPartitioningListener, IDocumentPartitioningListenerExtension, IDocumentPartitioningListenerExtension2 {
73
74 /** Set to <code>true</code> if between a document about to be changed and a changed event. */
75 private bool fDocumentChanging= false;
76 /**
77 * The cached redraw state of the text viewer.
78 * @since 3.0
79 */
80 private bool fCachedRedrawState= true;
81
82 /*
83 * @see ITextInputListener#inputDocumentAboutToBeChanged(IDocument, IDocument)
84 */
85 public void inputDocumentAboutToBeChanged(IDocument oldDocument, IDocument newDocument) {
86 if (oldDocument !is null) {
87 try {
88
89 fViewer.removeTextListener(this);
90 oldDocument.removeDocumentListener(this);
91 oldDocument.removeDocumentPartitioningListener(this);
92
93 oldDocument.removePositionUpdater(fPositionUpdater);
94 oldDocument.removePositionCategory(fPositionCategory);
95
96 } catch (BadPositionCategoryException x) {
97 // should not happened for former input documents;
98 }
99 }
100 }
101
102 /*
103 * @see ITextInputListener#inputDocumenChanged(IDocument, IDocument)
104 */
105 public void inputDocumentChanged(IDocument oldDocument, IDocument newDocument) {
106
107 fDocumentChanging= false;
108 fCachedRedrawState= true;
109
110 if (newDocument !is null) {
111
112 newDocument.addPositionCategory(fPositionCategory);
113 newDocument.addPositionUpdater(fPositionUpdater);
114
115 newDocument.addDocumentPartitioningListener(this);
116 newDocument.addDocumentListener(this);
117 fViewer.addTextListener(this);
118
119 setDocumentToDamagers(newDocument);
120 setDocumentToRepairers(newDocument);
121 processDamage(new Region(0, newDocument.getLength()), newDocument);
122 }
123 }
124
125 /*
126 * @see IDocumentPartitioningListener#documentPartitioningChanged(IDocument)
127 */
128 public void documentPartitioningChanged(IDocument document) {
129 if (!fDocumentChanging && fCachedRedrawState)
130 processDamage(new Region(0, document.getLength()), document);
131 else
132 fDocumentPartitioningChanged= true;
133 }
134
135 /*
136 * @see IDocumentPartitioningListenerExtension#documentPartitioningChanged(IDocument, IRegion)
137 * @since 2.0
138 */
139 public void documentPartitioningChanged(IDocument document, IRegion changedRegion) {
140 if (!fDocumentChanging && fCachedRedrawState) {
141 processDamage(new Region(changedRegion.getOffset(), changedRegion.getLength()), document);
142 } else {
143 fDocumentPartitioningChanged= true;
144 fChangedDocumentPartitions= changedRegion;
145 }
146 }
147
148 /*
149 * @see dwtx.jface.text.IDocumentPartitioningListenerExtension2#documentPartitioningChanged(dwtx.jface.text.DocumentPartitioningChangedEvent)
150 * @since 3.0
151 */
152 public void documentPartitioningChanged(DocumentPartitioningChangedEvent event) {
153 IRegion changedRegion= event.getChangedRegion(getDocumentPartitioning());
154 if (changedRegion !is null)
155 documentPartitioningChanged(event.getDocument(), changedRegion);
156 }
157
158 /*
159 * @see IDocumentListener#documentAboutToBeChanged(DocumentEvent)
160 */
161 public void documentAboutToBeChanged(DocumentEvent e) {
162
163 fDocumentChanging= true;
164 if (fCachedRedrawState) {
165 try {
166 int offset= e.getOffset() + e.getLength();
167 ITypedRegion region= getPartition(e.getDocument(), offset);
168 fRememberedPosition= new TypedPosition(region);
169 e.getDocument().addPosition(fPositionCategory, fRememberedPosition);
170 } catch (BadLocationException x) {
171 // can not happen
172 } catch (BadPositionCategoryException x) {
173 // should not happen on input elements
174 }
175 }
176 }
177
178 /*
179 * @see IDocumentListener#documentChanged(DocumentEvent)
180 */
181 public void documentChanged(DocumentEvent e) {
182 if (fCachedRedrawState) {
183 try {
184 e.getDocument().removePosition(fPositionCategory, fRememberedPosition);
185 } catch (BadPositionCategoryException x) {
186 // can not happen on input documents
187 }
188 }
189 fDocumentChanging= false;
190 }
191
192 /*
193 * @see ITextListener#textChanged(TextEvent)
194 */
195 public void textChanged(TextEvent e) {
196
197 fCachedRedrawState= e.getViewerRedrawState();
198 if (!fCachedRedrawState)
199 return;
200
201 IRegion damage= null;
202 IDocument document= null;
203
204 if (e.getDocumentEvent() is null) {
205 document= fViewer.getDocument();
206 if (document !is null) {
207 if (e.getOffset() is 0 && e.getLength() is 0 && e.getText() is null) {
208 // redraw state change, damage the whole document
209 damage= new Region(0, document.getLength());
210 } else {
211 IRegion region= widgetRegion2ModelRegion(e);
212 try {
213 String text= document.get(region.getOffset(), region.getLength());
214 DocumentEvent de= new DocumentEvent(document, region.getOffset(), region.getLength(), text);
215 damage= getDamage(de, false);
216 } catch (BadLocationException x) {
217 }
218 }
219 }
220 } else {
221 DocumentEvent de= e.getDocumentEvent();
222 document= de.getDocument();
223 damage= getDamage(de, true);
224 }
225
226 if (damage !is null && document !is null)
227 processDamage(damage, document);
228
229 fDocumentPartitioningChanged= false;
230 fChangedDocumentPartitions= null;
231 }
232
233 /**
234 * Translates the given text event into the corresponding range of the viewer's document.
235 *
236 * @param e the text event
237 * @return the widget region corresponding the region of the given event
238 * @since 2.1
239 */
240 protected IRegion widgetRegion2ModelRegion(TextEvent e) {
241
242 String text= e.getText();
243 int length= text is null ? 0 : text.length();
244
245 if (fViewer instanceof ITextViewerExtension5) {
246 ITextViewerExtension5 extension= (ITextViewerExtension5) fViewer;
247 return extension.widgetRange2ModelRange(new Region(e.getOffset(), length));
248 }
249
250 IRegion visible= fViewer.getVisibleRegion();
251 IRegion region= new Region(e.getOffset() + visible.getOffset(), length);
252 return region;
253 }
254 }
255
256 /** The map of presentation damagers. */
257 private Map fDamagers;
258 /** The map of presentation repairers. */
259 private Map fRepairers;
260 /** The target viewer. */
261 private ITextViewer fViewer;
262 /** The internal listener. */
263 private InternalListener fInternalListener= new InternalListener();
264 /** The name of the position category to track damage regions. */
265 private String fPositionCategory;
266 /** The position updated for the damage regions' position category. */
267 private IPositionUpdater fPositionUpdater;
268 /** The positions representing the damage regions. */
269 private TypedPosition fRememberedPosition;
270 /** Flag indicating the receipt of a partitioning changed notification. */
271 private bool fDocumentPartitioningChanged= false;
272 /** The range covering the changed partitioning. */
273 private IRegion fChangedDocumentPartitions= null;
274 /**
275 * The partitioning used by this presentation reconciler.
276 * @since 3.0
277 */
278 private String fPartitioning;
279
280 /**
281 * Creates a new presentation reconciler. There are no damagers or repairers
282 * registered with this reconciler by default. The default partitioning
283 * <code>IDocumentExtension3.DEFAULT_PARTITIONING</code> is used.
284 */
285 public PresentationReconciler() {
286 super();
287 fPartitioning= IDocumentExtension3.DEFAULT_PARTITIONING;
288 fPositionCategory= TRACKED_PARTITION + hashCode();
289 fPositionUpdater= new DefaultPositionUpdater(fPositionCategory);
290 }
291
292 /**
293 * Sets the document partitioning for this presentation reconciler.
294 *
295 * @param partitioning the document partitioning for this presentation reconciler.
296 * @since 3.0
297 */
298 public void setDocumentPartitioning(String partitioning) {
299 Assert.isNotNull(partitioning);
300 fPartitioning= partitioning;
301 }
302
303 /*
304 * @see dwtx.jface.text.presentation.IPresentationReconcilerExtension#geDocumenttPartitioning()
305 * @since 3.0
306 */
307 public String getDocumentPartitioning() {
308 return fPartitioning;
309 }
310
311 /**
312 * Registers the given presentation damager for a particular content type.
313 * If there is already a damager registered for this type, the old damager
314 * is removed first.
315 *
316 * @param damager the presentation damager to register, or <code>null</code> to remove an existing one
317 * @param contentType the content type under which to register
318 */
319 public void setDamager(IPresentationDamager damager, String contentType) {
320
321 Assert.isNotNull(contentType);
322
323 if (fDamagers is null)
324 fDamagers= new HashMap();
325
326 if (damager is null)
327 fDamagers.remove(contentType);
328 else
329 fDamagers.put(contentType, damager);
330 }
331
332 /**
333 * Registers the given presentation repairer for a particular content type.
334 * If there is already a repairer registered for this type, the old repairer
335 * is removed first.
336 *
337 * @param repairer the presentation repairer to register, or <code>null</code> to remove an existing one
338 * @param contentType the content type under which to register
339 */
340 public void setRepairer(IPresentationRepairer repairer, String contentType) {
341
342 Assert.isNotNull(contentType);
343
344 if (fRepairers is null)
345 fRepairers= new HashMap();
346
347 if (repairer is null)
348 fRepairers.remove(contentType);
349 else
350 fRepairers.put(contentType, repairer);
351 }
352
353 /*
354 * @see IPresentationReconciler#install(ITextViewer)
355 */
356 public void install(ITextViewer viewer) {
357 Assert.isNotNull(viewer);
358
359 fViewer= viewer;
360 fViewer.addTextInputListener(fInternalListener);
361
362 IDocument document= viewer.getDocument();
363 if (document !is null)
364 fInternalListener.inputDocumentChanged(null, document);
365 }
366
367 /*
368 * @see IPresentationReconciler#uninstall()
369 */
370 public void uninstall() {
371 fViewer.removeTextInputListener(fInternalListener);
372
373 // Ensure we uninstall all listeners
374 fInternalListener.inputDocumentAboutToBeChanged(fViewer.getDocument(), null);
375 }
376
377 /*
378 * @see IPresentationReconciler#getDamager(String)
379 */
380 public IPresentationDamager getDamager(String contentType) {
381
382 if (fDamagers is null)
383 return null;
384
385 return (IPresentationDamager) fDamagers.get(contentType);
386 }
387
388 /*
389 * @see IPresentationReconciler#getRepairer(String)
390 */
391 public IPresentationRepairer getRepairer(String contentType) {
392
393 if (fRepairers is null)
394 return null;
395
396 return (IPresentationRepairer) fRepairers.get(contentType);
397 }
398
399 /**
400 * Informs all registered damagers about the document on which they will work.
401 *
402 * @param document the document on which to work
403 */
404 protected void setDocumentToDamagers(IDocument document) {
405 if (fDamagers !is null) {
406 Iterator e= fDamagers.values().iterator();
407 while (e.hasNext()) {
408 IPresentationDamager damager= (IPresentationDamager) e.next();
409 damager.setDocument(document);
410 }
411 }
412 }
413
414 /**
415 * Informs all registered repairers about the document on which they will work.
416 *
417 * @param document the document on which to work
418 */
419 protected void setDocumentToRepairers(IDocument document) {
420 if (fRepairers !is null) {
421 Iterator e= fRepairers.values().iterator();
422 while (e.hasNext()) {
423 IPresentationRepairer repairer= (IPresentationRepairer) e.next();
424 repairer.setDocument(document);
425 }
426 }
427 }
428
429 /**
430 * Constructs a "repair description" for the given damage and returns this
431 * description as a text presentation. For this, it queries the partitioning
432 * of the damage region and asks the appropriate presentation repairer for
433 * each partition to construct the "repair description" for this partition.
434 *
435 * @param damage the damage to be repaired
436 * @param document the document whose presentation must be repaired
437 * @return the presentation repair description as text presentation or
438 * <code>null</code> if the partitioning could not be computed
439 */
440 protected TextPresentation createPresentation(IRegion damage, IDocument document) {
441 try {
442 if (fRepairers is null || fRepairers.isEmpty()) {
443 TextPresentation presentation= new TextPresentation(damage, 100);
444 presentation.setDefaultStyleRange(new StyleRange(damage.getOffset(), damage.getLength(), null, null));
445 return presentation;
446 }
447
448 TextPresentation presentation= new TextPresentation(damage, 1000);
449
450 ITypedRegion[] partitioning= TextUtilities.computePartitioning(document, getDocumentPartitioning(), damage.getOffset(), damage.getLength(), false);
451 for (int i= 0; i < partitioning.length; i++) {
452 ITypedRegion r= partitioning[i];
453 IPresentationRepairer repairer= getRepairer(r.getType());
454 if (repairer !is null)
455 repairer.createPresentation(presentation, r);
456 }
457
458 return presentation;
459
460 } catch (BadLocationException x) {
461 return null;
462 }
463 }
464
465
466 /**
467 * Checks for the first and the last affected partition affected by a
468 * document event and calls their damagers. Invalidates everything from the
469 * start of the damage for the first partition until the end of the damage
470 * for the last partition.
471 *
472 * @param e the event describing the document change
473 * @param optimize <code>true</code> if partition changes should be
474 * considered for optimization
475 * @return the damaged caused by the change or <code>null</code> if
476 * computing the partitioning failed
477 * @since 3.0
478 */
479 private IRegion getDamage(DocumentEvent e, bool optimize) {
480 int length= e.getText() is null ? 0 : e.getText().length();
481
482 if (fDamagers is null || fDamagers.isEmpty()) {
483 length= Math.max(e.getLength(), length);
484 length= Math.min(e.getDocument().getLength() - e.getOffset(), length);
485 return new Region(e.getOffset(), length);
486 }
487
488 bool isDeletion= length is 0;
489 IRegion damage= null;
490 try {
491 int offset= e.getOffset();
492 if (isDeletion)
493 offset= Math.max(0, offset - 1);
494 ITypedRegion partition= getPartition(e.getDocument(), offset);
495 IPresentationDamager damager= getDamager(partition.getType());
496 if (damager is null)
497 return null;
498
499 IRegion r= damager.getDamageRegion(partition, e, fDocumentPartitioningChanged);
500
501 if (!fDocumentPartitioningChanged && optimize && !isDeletion) {
502 damage= r;
503 } else {
504
505 int damageEnd= getDamageEndOffset(e);
506
507 int parititionDamageEnd= -1;
508 if (fChangedDocumentPartitions !is null)
509 parititionDamageEnd= fChangedDocumentPartitions.getOffset() + fChangedDocumentPartitions.getLength();
510
511 int end= Math.max(damageEnd, parititionDamageEnd);
512
513 damage= end is -1 ? r : new Region(r.getOffset(), end - r.getOffset());
514 }
515
516 } catch (BadLocationException x) {
517 }
518
519 return damage;
520 }
521
522 /**
523 * Returns the end offset of the damage. If a partition has been split by
524 * the given document event also the second half of the original
525 * partition must be considered. This is achieved by using the remembered
526 * partition range.
527 *
528 * @param e the event describing the change
529 * @return the damage end offset (excluding)
530 * @exception BadLocationException if method accesses invalid offset
531 */
532 private int getDamageEndOffset(DocumentEvent e) throws BadLocationException {
533
534 IDocument d= e.getDocument();
535
536 int length= 0;
537 if (e.getText() !is null) {
538 length= e.getText().length();
539 if (length > 0)
540 -- length;
541 }
542
543 ITypedRegion partition= getPartition(d, e.getOffset() + length);
544 int endOffset= partition.getOffset() + partition.getLength();
545 if (endOffset is e.getOffset())
546 return -1;
547
548 int end= fRememberedPosition is null ? -1 : fRememberedPosition.getOffset() + fRememberedPosition.getLength();
549 if (endOffset < end && end < d.getLength())
550 partition= getPartition(d, end);
551
552 IPresentationDamager damager= getDamager(partition.getType());
553 if (damager is null)
554 return -1;
555
556 IRegion r= damager.getDamageRegion(partition, e, fDocumentPartitioningChanged);
557
558 return r.getOffset() + r.getLength();
559 }
560
561 /**
562 * Processes the given damage.
563 * @param damage the damage to be repaired
564 * @param document the document whose presentation must be repaired
565 */
566 private void processDamage(IRegion damage, IDocument document) {
567 if (damage !is null && damage.getLength() > 0) {
568 TextPresentation p= createPresentation(damage, document);
569 if (p !is null)
570 applyTextRegionCollection(p);
571 }
572 }
573
574 /**
575 * Applies the given text presentation to the text viewer the presentation
576 * reconciler is installed on.
577 *
578 * @param presentation the text presentation to be applied to the text viewer
579 */
580 private void applyTextRegionCollection(TextPresentation presentation) {
581 fViewer.changeTextPresentation(presentation, false);
582 }
583
584 /**
585 * Returns the partition for the given offset in the given document.
586 *
587 * @param document the document
588 * @param offset the offset
589 * @return the partition
590 * @throws BadLocationException if offset is invalid in the given document
591 * @since 3.0
592 */
593 private ITypedRegion getPartition(IDocument document, int offset) throws BadLocationException {
594 return TextUtilities.getPartition(document, getDocumentPartitioning(), offset, false);
595 }
596 }