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