Mercurial > projects > dwt2
comparison org.eclipse.jface.text/src/org/eclipse/jface/text/TextPresentation.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 | 52184e4b815c |
comparison
equal
deleted
inserted
replaced
11:43904fec5dca | 12:bc29606a740c |
---|---|
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 org.eclipse.jface.text.TextPresentation; | |
14 | |
15 import org.eclipse.jface.text.IDocumentPartitioningListener; // packageimport | |
16 import org.eclipse.jface.text.DefaultTextHover; // packageimport | |
17 import org.eclipse.jface.text.AbstractInformationControl; // packageimport | |
18 import org.eclipse.jface.text.TextUtilities; // packageimport | |
19 import org.eclipse.jface.text.IInformationControlCreatorExtension; // packageimport | |
20 import org.eclipse.jface.text.AbstractInformationControlManager; // packageimport | |
21 import org.eclipse.jface.text.ITextViewerExtension2; // packageimport | |
22 import org.eclipse.jface.text.IDocumentPartitioner; // packageimport | |
23 import org.eclipse.jface.text.DefaultIndentLineAutoEditStrategy; // packageimport | |
24 import org.eclipse.jface.text.ITextSelection; // packageimport | |
25 import org.eclipse.jface.text.Document; // packageimport | |
26 import org.eclipse.jface.text.FindReplaceDocumentAdapterContentProposalProvider; // packageimport | |
27 import org.eclipse.jface.text.ITextListener; // packageimport | |
28 import org.eclipse.jface.text.BadPartitioningException; // packageimport | |
29 import org.eclipse.jface.text.ITextViewerExtension5; // packageimport | |
30 import org.eclipse.jface.text.IDocumentPartitionerExtension3; // packageimport | |
31 import org.eclipse.jface.text.IUndoManager; // packageimport | |
32 import org.eclipse.jface.text.ITextHoverExtension2; // packageimport | |
33 import org.eclipse.jface.text.IRepairableDocument; // packageimport | |
34 import org.eclipse.jface.text.IRewriteTarget; // packageimport | |
35 import org.eclipse.jface.text.DefaultPositionUpdater; // packageimport | |
36 import org.eclipse.jface.text.RewriteSessionEditProcessor; // packageimport | |
37 import org.eclipse.jface.text.TextViewerHoverManager; // packageimport | |
38 import org.eclipse.jface.text.DocumentRewriteSession; // packageimport | |
39 import org.eclipse.jface.text.TextViewer; // packageimport | |
40 import org.eclipse.jface.text.ITextViewerExtension8; // packageimport | |
41 import org.eclipse.jface.text.RegExMessages; // packageimport | |
42 import org.eclipse.jface.text.IDelayedInputChangeProvider; // packageimport | |
43 import org.eclipse.jface.text.ITextOperationTargetExtension; // packageimport | |
44 import org.eclipse.jface.text.IWidgetTokenOwner; // packageimport | |
45 import org.eclipse.jface.text.IViewportListener; // packageimport | |
46 import org.eclipse.jface.text.GapTextStore; // packageimport | |
47 import org.eclipse.jface.text.MarkSelection; // packageimport | |
48 import org.eclipse.jface.text.IDocumentPartitioningListenerExtension; // packageimport | |
49 import org.eclipse.jface.text.IDocumentAdapterExtension; // packageimport | |
50 import org.eclipse.jface.text.IInformationControlExtension; // packageimport | |
51 import org.eclipse.jface.text.IDocumentPartitioningListenerExtension2; // packageimport | |
52 import org.eclipse.jface.text.DefaultDocumentAdapter; // packageimport | |
53 import org.eclipse.jface.text.ITextViewerExtension3; // packageimport | |
54 import org.eclipse.jface.text.IInformationControlCreator; // packageimport | |
55 import org.eclipse.jface.text.TypedRegion; // packageimport | |
56 import org.eclipse.jface.text.ISynchronizable; // packageimport | |
57 import org.eclipse.jface.text.IMarkRegionTarget; // packageimport | |
58 import org.eclipse.jface.text.TextViewerUndoManager; // packageimport | |
59 import org.eclipse.jface.text.IRegion; // packageimport | |
60 import org.eclipse.jface.text.IInformationControlExtension2; // packageimport | |
61 import org.eclipse.jface.text.IDocumentExtension4; // packageimport | |
62 import org.eclipse.jface.text.IDocumentExtension2; // packageimport | |
63 import org.eclipse.jface.text.IDocumentPartitionerExtension2; // packageimport | |
64 import org.eclipse.jface.text.DefaultInformationControl; // packageimport | |
65 import org.eclipse.jface.text.IWidgetTokenOwnerExtension; // packageimport | |
66 import org.eclipse.jface.text.DocumentClone; // packageimport | |
67 import org.eclipse.jface.text.DefaultUndoManager; // packageimport | |
68 import org.eclipse.jface.text.IFindReplaceTarget; // packageimport | |
69 import org.eclipse.jface.text.IAutoEditStrategy; // packageimport | |
70 import org.eclipse.jface.text.ILineTrackerExtension; // packageimport | |
71 import org.eclipse.jface.text.IUndoManagerExtension; // packageimport | |
72 import org.eclipse.jface.text.TextSelection; // packageimport | |
73 import org.eclipse.jface.text.DefaultAutoIndentStrategy; // packageimport | |
74 import org.eclipse.jface.text.IAutoIndentStrategy; // packageimport | |
75 import org.eclipse.jface.text.IPainter; // packageimport | |
76 import org.eclipse.jface.text.IInformationControl; // packageimport | |
77 import org.eclipse.jface.text.IInformationControlExtension3; // packageimport | |
78 import org.eclipse.jface.text.ITextViewerExtension6; // packageimport | |
79 import org.eclipse.jface.text.IInformationControlExtension4; // packageimport | |
80 import org.eclipse.jface.text.DefaultLineTracker; // packageimport | |
81 import org.eclipse.jface.text.IDocumentInformationMappingExtension; // packageimport | |
82 import org.eclipse.jface.text.IRepairableDocumentExtension; // packageimport | |
83 import org.eclipse.jface.text.ITextHover; // packageimport | |
84 import org.eclipse.jface.text.FindReplaceDocumentAdapter; // packageimport | |
85 import org.eclipse.jface.text.ILineTracker; // packageimport | |
86 import org.eclipse.jface.text.Line; // packageimport | |
87 import org.eclipse.jface.text.ITextViewerExtension; // packageimport | |
88 import org.eclipse.jface.text.IDocumentAdapter; // packageimport | |
89 import org.eclipse.jface.text.TextEvent; // packageimport | |
90 import org.eclipse.jface.text.BadLocationException; // packageimport | |
91 import org.eclipse.jface.text.AbstractDocument; // packageimport | |
92 import org.eclipse.jface.text.AbstractLineTracker; // packageimport | |
93 import org.eclipse.jface.text.TreeLineTracker; // packageimport | |
94 import org.eclipse.jface.text.ITextPresentationListener; // packageimport | |
95 import org.eclipse.jface.text.Region; // packageimport | |
96 import org.eclipse.jface.text.ITextViewer; // packageimport | |
97 import org.eclipse.jface.text.IDocumentInformationMapping; // packageimport | |
98 import org.eclipse.jface.text.MarginPainter; // packageimport | |
99 import org.eclipse.jface.text.IPaintPositionManager; // packageimport | |
100 import org.eclipse.jface.text.IFindReplaceTargetExtension; // packageimport | |
101 import org.eclipse.jface.text.ISlaveDocumentManagerExtension; // packageimport | |
102 import org.eclipse.jface.text.ISelectionValidator; // packageimport | |
103 import org.eclipse.jface.text.IDocumentExtension; // packageimport | |
104 import org.eclipse.jface.text.PropagatingFontFieldEditor; // packageimport | |
105 import org.eclipse.jface.text.ConfigurableLineTracker; // packageimport | |
106 import org.eclipse.jface.text.SlaveDocumentEvent; // packageimport | |
107 import org.eclipse.jface.text.IDocumentListener; // packageimport | |
108 import org.eclipse.jface.text.PaintManager; // packageimport | |
109 import org.eclipse.jface.text.IFindReplaceTargetExtension3; // packageimport | |
110 import org.eclipse.jface.text.ITextDoubleClickStrategy; // packageimport | |
111 import org.eclipse.jface.text.IDocumentExtension3; // packageimport | |
112 import org.eclipse.jface.text.Position; // packageimport | |
113 import org.eclipse.jface.text.TextMessages; // packageimport | |
114 import org.eclipse.jface.text.CopyOnWriteTextStore; // packageimport | |
115 import org.eclipse.jface.text.WhitespaceCharacterPainter; // packageimport | |
116 import org.eclipse.jface.text.IPositionUpdater; // packageimport | |
117 import org.eclipse.jface.text.DefaultTextDoubleClickStrategy; // packageimport | |
118 import org.eclipse.jface.text.ListLineTracker; // packageimport | |
119 import org.eclipse.jface.text.ITextInputListener; // packageimport | |
120 import org.eclipse.jface.text.BadPositionCategoryException; // packageimport | |
121 import org.eclipse.jface.text.IWidgetTokenKeeperExtension; // packageimport | |
122 import org.eclipse.jface.text.IInputChangedListener; // packageimport | |
123 import org.eclipse.jface.text.ITextOperationTarget; // packageimport | |
124 import org.eclipse.jface.text.IDocumentInformationMappingExtension2; // packageimport | |
125 import org.eclipse.jface.text.ITextViewerExtension7; // packageimport | |
126 import org.eclipse.jface.text.IInformationControlExtension5; // packageimport | |
127 import org.eclipse.jface.text.IDocumentRewriteSessionListener; // packageimport | |
128 import org.eclipse.jface.text.JFaceTextUtil; // packageimport | |
129 import org.eclipse.jface.text.AbstractReusableInformationControlCreator; // packageimport | |
130 import org.eclipse.jface.text.TabsToSpacesConverter; // packageimport | |
131 import org.eclipse.jface.text.CursorLinePainter; // packageimport | |
132 import org.eclipse.jface.text.ITextHoverExtension; // packageimport | |
133 import org.eclipse.jface.text.IEventConsumer; // packageimport | |
134 import org.eclipse.jface.text.IDocument; // packageimport | |
135 import org.eclipse.jface.text.IWidgetTokenKeeper; // packageimport | |
136 import org.eclipse.jface.text.DocumentCommand; // packageimport | |
137 import org.eclipse.jface.text.TypedPosition; // packageimport | |
138 import org.eclipse.jface.text.IEditingSupportRegistry; // packageimport | |
139 import org.eclipse.jface.text.IDocumentPartitionerExtension; // packageimport | |
140 import org.eclipse.jface.text.AbstractHoverInformationControlManager; // packageimport | |
141 import org.eclipse.jface.text.IEditingSupport; // packageimport | |
142 import org.eclipse.jface.text.IMarkSelection; // packageimport | |
143 import org.eclipse.jface.text.ISlaveDocumentManager; // packageimport | |
144 import org.eclipse.jface.text.DocumentEvent; // packageimport | |
145 import org.eclipse.jface.text.DocumentPartitioningChangedEvent; // packageimport | |
146 import org.eclipse.jface.text.ITextStore; // packageimport | |
147 import org.eclipse.jface.text.JFaceTextMessages; // packageimport | |
148 import org.eclipse.jface.text.DocumentRewriteSessionEvent; // packageimport | |
149 import org.eclipse.jface.text.SequentialRewriteTextStore; // packageimport | |
150 import org.eclipse.jface.text.DocumentRewriteSessionType; // packageimport | |
151 import org.eclipse.jface.text.TextAttribute; // packageimport | |
152 import org.eclipse.jface.text.ITextViewerExtension4; // packageimport | |
153 import org.eclipse.jface.text.ITypedRegion; // packageimport | |
154 | |
155 import java.lang.all; | |
156 import java.util.ArrayList; | |
157 import java.util.Iterator; | |
158 import java.util.Set; | |
159 import tango.core.Exception; | |
160 | |
161 import org.eclipse.swt.SWT; | |
162 import org.eclipse.swt.custom.StyleRange; | |
163 import org.eclipse.swt.custom.StyledText; | |
164 import org.eclipse.core.runtime.Assert; | |
165 | |
166 | |
167 /** | |
168 * Describes the presentation styles for a section of an indexed text such as a | |
169 * document or string. A text presentation defines a default style for the whole | |
170 * section and in addition style differences for individual subsections. Text | |
171 * presentations can be narrowed down to a particular result window. All methods | |
172 * are result window aware, i.e. ranges outside the result window are always | |
173 * ignored. | |
174 * <p> | |
175 * All iterators provided by a text presentation assume that they enumerate non | |
176 * overlapping, consecutive ranges inside the default range. Thus, all these | |
177 * iterators do not include the default range. The default style range must be | |
178 * explicitly asked for using <code>getDefaultStyleRange</code>. | |
179 */ | |
180 public class TextPresentation { | |
181 | |
182 /** | |
183 * Applies the given presentation to the given text widget. Helper method. | |
184 * | |
185 * @param presentation the style information | |
186 * @param text the widget to which to apply the style information | |
187 * @since 2.0 | |
188 */ | |
189 public static void applyTextPresentation(TextPresentation presentation, StyledText text) { | |
190 | |
191 StyleRange[] ranges= new StyleRange[presentation.getDenumerableRanges()]; | |
192 | |
193 int i= 0; | |
194 Iterator e= presentation.getAllStyleRangeIterator(); | |
195 while (e.hasNext()) | |
196 ranges[i++]= cast(StyleRange) e.next(); | |
197 | |
198 text.setStyleRanges(ranges); | |
199 } | |
200 | |
201 | |
202 | |
203 | |
204 /** | |
205 * Enumerates all the <code>StyleRange</code>s included in the presentation. | |
206 */ | |
207 class FilterIterator : Iterator { | |
208 | |
209 /** The index of the next style range to be enumerated */ | |
210 protected int fIndex; | |
211 /** The upper bound of the indices of style ranges to be enumerated */ | |
212 protected int fLength; | |
213 /** Indicates whether ranges similar to the default range should be enumerated */ | |
214 protected bool fSkipDefaults; | |
215 /** The result window */ | |
216 protected IRegion fWindow; | |
217 | |
218 /** | |
219 * <code>skipDefaults</code> tells the enumeration to skip all those style ranges | |
220 * which define the same style as the presentation's default style range. | |
221 * | |
222 * @param skipDefaults <code>false</code> if ranges similar to the default range should be enumerated | |
223 */ | |
224 protected this(bool skipDefaults) { | |
225 | |
226 fSkipDefaults= skipDefaults; | |
227 | |
228 fWindow= fResultWindow; | |
229 fIndex= getFirstIndexInWindow(fWindow); | |
230 fLength= getFirstIndexAfterWindow(fWindow); | |
231 | |
232 if (fSkipDefaults) | |
233 computeIndex(); | |
234 } | |
235 | |
236 /* | |
237 * @see Iterator#next() | |
238 */ | |
239 public Object next() { | |
240 try { | |
241 StyleRange r= cast(StyleRange) fRanges.get(fIndex++); | |
242 return createWindowRelativeRange(fWindow, r); | |
243 } catch (ArrayIndexOutOfBoundsException x) { | |
244 throw new NoSuchElementException(null); | |
245 } finally { | |
246 if (fSkipDefaults) | |
247 computeIndex(); | |
248 } | |
249 } | |
250 | |
251 /* | |
252 * @see Iterator#hasNext() | |
253 */ | |
254 public bool hasNext() { | |
255 return fIndex < fLength; | |
256 } | |
257 | |
258 /* | |
259 * @see Iterator#remove() | |
260 */ | |
261 public void remove() { | |
262 throw new UnsupportedOperationException(); | |
263 } | |
264 | |
265 /** | |
266 * Returns whether the given object should be skipped. | |
267 * | |
268 * @param o the object to be checked | |
269 * @return <code>true</code> if the object should be skipped by the iterator | |
270 */ | |
271 protected bool skip(Object o) { | |
272 StyleRange r= cast(StyleRange) o; | |
273 return r.similarTo(fDefaultRange); | |
274 } | |
275 | |
276 /** | |
277 * Computes the index of the styled range that is the next to be enumerated. | |
278 */ | |
279 protected void computeIndex() { | |
280 while (fIndex < fLength && skip(fRanges.get(fIndex))) | |
281 ++ fIndex; | |
282 } | |
283 } | |
284 | |
285 /** The style information for the range covered by the whole presentation */ | |
286 private StyleRange fDefaultRange; | |
287 /** The member ranges of the presentation */ | |
288 private ArrayList fRanges; | |
289 /** A clipping region against which the presentation can be clipped when asked for results */ | |
290 private IRegion fResultWindow; | |
291 /** | |
292 * The optional extent for this presentation. | |
293 * @since 3.0 | |
294 */ | |
295 private IRegion fExtent; | |
296 | |
297 | |
298 /** | |
299 * Creates a new empty text presentation. | |
300 */ | |
301 public this() { | |
302 fRanges= new ArrayList(50); | |
303 } | |
304 | |
305 /** | |
306 * Creates a new empty text presentation. <code>sizeHint</code> tells the | |
307 * expected size of this presentation. | |
308 * | |
309 * @param sizeHint the expected size of this presentation | |
310 */ | |
311 public this(int sizeHint) { | |
312 Assert.isTrue(sizeHint > 0); | |
313 fRanges= new ArrayList(sizeHint); | |
314 } | |
315 | |
316 /** | |
317 * Creates a new empty text presentation with the given extent. | |
318 * <code>sizeHint</code> tells the expected size of this presentation. | |
319 * | |
320 * @param extent the extent of the created <code>TextPresentation</code> | |
321 * @param sizeHint the expected size of this presentation | |
322 * @since 3.0 | |
323 */ | |
324 public this(IRegion extent, int sizeHint) { | |
325 this(sizeHint); | |
326 Assert.isNotNull(cast(Object)extent); | |
327 fExtent= extent; | |
328 } | |
329 | |
330 /** | |
331 * Sets the result window for this presentation. When dealing with | |
332 * this presentation all ranges which are outside the result window | |
333 * are ignored. For example, the size of the presentation is 0 | |
334 * when there is no range inside the window even if there are ranges | |
335 * outside the window. All methods are aware of the result window. | |
336 * | |
337 * @param resultWindow the result window | |
338 */ | |
339 public void setResultWindow(IRegion resultWindow) { | |
340 fResultWindow= resultWindow; | |
341 } | |
342 | |
343 /** | |
344 * Set the default style range of this presentation. | |
345 * The default style range defines the overall area covered | |
346 * by this presentation and its style information. | |
347 * | |
348 * @param range the range describing the default region | |
349 */ | |
350 public void setDefaultStyleRange(StyleRange range) { | |
351 fDefaultRange= range; | |
352 } | |
353 | |
354 /** | |
355 * Returns this presentation's default style range. The returned <code>StyleRange</code> | |
356 * is relative to the start of the result window. | |
357 * | |
358 * @return this presentation's default style range | |
359 */ | |
360 public StyleRange getDefaultStyleRange() { | |
361 StyleRange range= createWindowRelativeRange(fResultWindow, fDefaultRange); | |
362 if (range is null) | |
363 return null; | |
364 return cast(StyleRange)range.clone(); | |
365 | |
366 } | |
367 | |
368 /** | |
369 * Add the given range to the presentation. The range must be a | |
370 * subrange of the presentation's default range. | |
371 * | |
372 * @param range the range to be added | |
373 */ | |
374 public void addStyleRange(StyleRange range) { | |
375 checkConsistency(range); | |
376 fRanges.add(range); | |
377 } | |
378 | |
379 /** | |
380 * Replaces the given range in this presentation. The range must be a | |
381 * subrange of the presentation's default range. | |
382 * | |
383 * @param range the range to be added | |
384 * @since 3.0 | |
385 */ | |
386 public void replaceStyleRange(StyleRange range) { | |
387 applyStyleRange(range, false); | |
388 } | |
389 | |
390 /** | |
391 * Merges the given range into this presentation. The range must be a | |
392 * subrange of the presentation's default range. | |
393 * | |
394 * @param range the range to be added | |
395 * @since 3.0 | |
396 */ | |
397 public void mergeStyleRange(StyleRange range) { | |
398 applyStyleRange(range, true); | |
399 } | |
400 | |
401 /** | |
402 * Applies the given range to this presentation. The range must be a | |
403 * subrange of the presentation's default range. | |
404 * | |
405 * @param range the range to be added | |
406 * @param merge <code>true</code> if the style should be merged instead of replaced | |
407 * @since 3.0 | |
408 */ | |
409 private void applyStyleRange(StyleRange range, bool merge) { | |
410 if (range.length is 0) | |
411 return; | |
412 | |
413 checkConsistency(range); | |
414 | |
415 int start= range.start; | |
416 int length= range.length; | |
417 int end= start + length; | |
418 | |
419 if (fRanges.size() is 0) { | |
420 StyleRange defaultRange= getDefaultStyleRange(); | |
421 if (defaultRange is null) | |
422 defaultRange= range; | |
423 | |
424 defaultRange.start= start; | |
425 defaultRange.length= length; | |
426 applyStyle(range, defaultRange, merge); | |
427 fRanges.add(defaultRange); | |
428 } else { | |
429 IRegion rangeRegion= new Region(start, length); | |
430 int first= getFirstIndexInWindow(rangeRegion); | |
431 | |
432 if (first is fRanges.size()) { | |
433 StyleRange defaultRange= getDefaultStyleRange(); | |
434 if (defaultRange is null) | |
435 defaultRange= range; | |
436 defaultRange.start= start; | |
437 defaultRange.length= length; | |
438 applyStyle(range, defaultRange, merge); | |
439 fRanges.add(defaultRange); | |
440 return; | |
441 } | |
442 | |
443 int last= getFirstIndexAfterWindow(rangeRegion); | |
444 for (int i= first; i < last && length > 0; i++) { | |
445 | |
446 StyleRange current= cast(StyleRange)fRanges.get(i); | |
447 int currentStart= current.start; | |
448 int currentEnd= currentStart + current.length; | |
449 | |
450 if (end <= currentStart) { | |
451 fRanges.add(i, range); | |
452 return; | |
453 } | |
454 | |
455 if (start >= currentEnd) | |
456 continue; | |
457 | |
458 StyleRange currentCopy= null; | |
459 if (end < currentEnd) | |
460 currentCopy= cast(StyleRange)current.clone(); | |
461 | |
462 if (start < currentStart) { | |
463 // Apply style to new default range and add it | |
464 StyleRange defaultRange= getDefaultStyleRange(); | |
465 if (defaultRange is null) | |
466 defaultRange= new StyleRange(); | |
467 | |
468 defaultRange.start= start; | |
469 defaultRange.length= currentStart - start; | |
470 applyStyle(range, defaultRange, merge); | |
471 fRanges.add(i, defaultRange); | |
472 i++; last++; | |
473 | |
474 | |
475 // Apply style to first part of current range | |
476 current.length= Math.min(end, currentEnd) - currentStart; | |
477 applyStyle(range, current, merge); | |
478 } | |
479 | |
480 if (start >= currentStart) { | |
481 // Shorten the current range | |
482 current.length= start - currentStart; | |
483 | |
484 // Apply the style to the rest of the current range and add it | |
485 if (current.length > 0) { | |
486 current= cast(StyleRange)current.clone(); | |
487 i++; last++; | |
488 fRanges.add(i, current); | |
489 } | |
490 applyStyle(range, current, merge); | |
491 current.start= start; | |
492 current.length= Math.min(end, currentEnd) - start; | |
493 } | |
494 | |
495 if (end < currentEnd) { | |
496 // Add rest of current range | |
497 currentCopy.start= end; | |
498 currentCopy.length= currentEnd - end; | |
499 i++; last++; | |
500 fRanges.add(i, currentCopy); | |
501 } | |
502 | |
503 // Update range | |
504 range.start= currentEnd; | |
505 range.length= Math.max(end - currentEnd, 0); | |
506 start= range.start; | |
507 length= range.length; | |
508 } | |
509 if (length > 0) { | |
510 // Apply style to new default range and add it | |
511 StyleRange defaultRange= getDefaultStyleRange(); | |
512 if (defaultRange is null) | |
513 defaultRange= range; | |
514 defaultRange.start= start; | |
515 defaultRange.length= end - start; | |
516 applyStyle(range, defaultRange, merge); | |
517 fRanges.add(last, defaultRange); | |
518 } | |
519 } | |
520 } | |
521 | |
522 /** | |
523 * Replaces the given ranges in this presentation. Each range must be a | |
524 * subrange of the presentation's default range. The ranges must be ordered | |
525 * by increasing offset and must not overlap (but may be adjacent). | |
526 * | |
527 * @param ranges the ranges to be added | |
528 * @since 3.0 | |
529 */ | |
530 public void replaceStyleRanges(StyleRange[] ranges) { | |
531 applyStyleRanges(ranges, false); | |
532 } | |
533 | |
534 /** | |
535 * Merges the given ranges into this presentation. Each range must be a | |
536 * subrange of the presentation's default range. The ranges must be ordered | |
537 * by increasing offset and must not overlap (but may be adjacent). | |
538 * | |
539 * @param ranges the ranges to be added | |
540 * @since 3.0 | |
541 */ | |
542 public void mergeStyleRanges(StyleRange[] ranges) { | |
543 applyStyleRanges(ranges, true); | |
544 } | |
545 | |
546 /** | |
547 * Applies the given ranges to this presentation. Each range must be a | |
548 * subrange of the presentation's default range. The ranges must be ordered | |
549 * by increasing offset and must not overlap (but may be adjacent). | |
550 * | |
551 * @param ranges the ranges to be added | |
552 * @param merge <code>true</code> if the style should be merged instead of replaced | |
553 * @since 3.0 | |
554 */ | |
555 private void applyStyleRanges(StyleRange[] ranges, bool merge) { | |
556 int j= 0; | |
557 ArrayList oldRanges= fRanges; | |
558 ArrayList newRanges= new ArrayList(2*ranges.length + oldRanges.size()); | |
559 for (int i= 0, n= ranges.length; i < n; i++) { | |
560 StyleRange range= ranges[i]; | |
561 fRanges= oldRanges; // for getFirstIndexAfterWindow(...) | |
562 for (int m= getFirstIndexAfterWindow(new Region(range.start, range.length)); j < m; j++) | |
563 newRanges.add(oldRanges.get(j)); | |
564 fRanges= newRanges; // for mergeStyleRange(...) | |
565 applyStyleRange(range, merge); | |
566 } | |
567 for (int m= oldRanges.size(); j < m; j++) | |
568 newRanges.add(oldRanges.get(j)); | |
569 fRanges= newRanges; | |
570 } | |
571 | |
572 /** | |
573 * Applies the template_'s style to the target. | |
574 * | |
575 * @param template_ the style range to be used as template_ | |
576 * @param target the style range to which to apply the template_ | |
577 * @param merge <code>true</code> if the style should be merged instead of replaced | |
578 * @since 3.0 | |
579 */ | |
580 private void applyStyle(StyleRange template_, StyleRange target, bool merge) { | |
581 if (merge) { | |
582 if (template_.font !is null) | |
583 target.font= template_.font; | |
584 target.fontStyle|= template_.fontStyle; | |
585 | |
586 if (template_.metrics !is null) | |
587 target.metrics= template_.metrics; | |
588 | |
589 if (template_.foreground !is null) | |
590 target.foreground= template_.foreground; | |
591 if (template_.background !is null) | |
592 target.background= template_.background; | |
593 | |
594 target.strikeout|= template_.strikeout; | |
595 if (template_.strikeoutColor !is null) | |
596 target.strikeoutColor= template_.strikeoutColor; | |
597 | |
598 target.underline|= template_.underline; | |
599 if (template_.underlineStyle !is SWT.NONE) | |
600 target.underlineStyle= template_.underlineStyle; | |
601 if (template_.underlineColor !is null) | |
602 target.underlineColor= template_.underlineColor; | |
603 | |
604 if (template_.borderStyle !is SWT.NONE) | |
605 target.borderStyle= template_.borderStyle; | |
606 if (template_.borderColor !is null) | |
607 target.borderColor= template_.borderColor; | |
608 | |
609 } else { | |
610 target.font= template_.font; | |
611 target.fontStyle= template_.fontStyle; | |
612 target.metrics= template_.metrics; | |
613 target.foreground= template_.foreground; | |
614 target.background= template_.background; | |
615 target.strikeout= template_.strikeout; | |
616 target.strikeoutColor= template_.strikeoutColor; | |
617 target.underline= template_.underline; | |
618 target.underlineStyle= template_.underlineStyle; | |
619 target.underlineColor= template_.underlineColor; | |
620 target.borderStyle= template_.borderStyle; | |
621 target.borderColor= template_.borderColor; | |
622 } | |
623 } | |
624 | |
625 /** | |
626 * Checks whether the given range is a subrange of the presentation's | |
627 * default style range. | |
628 * | |
629 * @param range the range to be checked | |
630 * @exception IllegalArgumentException if range is not a subrange of the presentation's default range | |
631 */ | |
632 private void checkConsistency(StyleRange range) { | |
633 | |
634 if (range is null) | |
635 throw new IllegalArgumentException(null); | |
636 | |
637 if (fDefaultRange !is null) { | |
638 | |
639 if (range.start < fDefaultRange.start) | |
640 range.start= fDefaultRange.start; | |
641 | |
642 int defaultEnd= fDefaultRange.start + fDefaultRange.length; | |
643 int end= range.start + range.length; | |
644 if (end > defaultEnd) | |
645 range.length -= (end - defaultEnd); | |
646 } | |
647 } | |
648 | |
649 /** | |
650 * Returns the index of the first range which overlaps with the | |
651 * specified window. | |
652 * | |
653 * @param window the window to be used for searching | |
654 * @return the index of the first range overlapping with the window | |
655 */ | |
656 private int getFirstIndexInWindow(IRegion window) { | |
657 if (window !is null) { | |
658 int start= window.getOffset(); | |
659 int i= -1, j= fRanges.size(); | |
660 while (j - i > 1) { | |
661 int k= (i + j) >> 1; | |
662 StyleRange r= cast(StyleRange) fRanges.get(k); | |
663 if (r.start + r.length > start) | |
664 j= k; | |
665 else | |
666 i= k; | |
667 } | |
668 return j; | |
669 } | |
670 return 0; | |
671 } | |
672 | |
673 /** | |
674 * Returns the index of the first range which comes after the specified window and does | |
675 * not overlap with this window. | |
676 * | |
677 * @param window the window to be used for searching | |
678 * @return the index of the first range behind the window and not overlapping with the window | |
679 */ | |
680 private int getFirstIndexAfterWindow(IRegion window) { | |
681 if (window !is null) { | |
682 int end= window.getOffset() + window.getLength(); | |
683 int i= -1, j= fRanges.size(); | |
684 while (j - i > 1) { | |
685 int k= (i + j) >> 1; | |
686 StyleRange r= cast(StyleRange) fRanges.get(k); | |
687 if (r.start < end) | |
688 i= k; | |
689 else | |
690 j= k; | |
691 } | |
692 return j; | |
693 } | |
694 return fRanges.size(); | |
695 } | |
696 | |
697 /** | |
698 * Returns a style range which is relative to the specified window and | |
699 * appropriately clipped if necessary. The original style range is not | |
700 * modified. | |
701 * | |
702 * @param window the reference window | |
703 * @param range the absolute range | |
704 * @return the window relative range based on the absolute range | |
705 */ | |
706 private StyleRange createWindowRelativeRange(IRegion window, StyleRange range) { | |
707 if (window is null || range is null) | |
708 return range; | |
709 | |
710 int start= range.start - window.getOffset(); | |
711 if (start < 0) | |
712 start= 0; | |
713 | |
714 int rangeEnd= range.start + range.length; | |
715 int windowEnd= window.getOffset() + window.getLength(); | |
716 int end= (rangeEnd > windowEnd ? windowEnd : rangeEnd); | |
717 end -= window.getOffset(); | |
718 | |
719 StyleRange newRange= cast(StyleRange) range.clone(); | |
720 newRange.start= start; | |
721 newRange.length= end - start; | |
722 return newRange; | |
723 } | |
724 | |
725 /** | |
726 * Returns the region which is relative to the specified window and | |
727 * appropriately clipped if necessary. | |
728 * | |
729 * @param coverage the absolute coverage | |
730 * @return the window relative region based on the absolute coverage | |
731 * @since 3.0 | |
732 */ | |
733 private IRegion createWindowRelativeRegion(IRegion coverage) { | |
734 if (fResultWindow is null || coverage is null) | |
735 return coverage; | |
736 | |
737 int start= coverage.getOffset() - fResultWindow.getOffset(); | |
738 if (start < 0) | |
739 start= 0; | |
740 | |
741 int rangeEnd= coverage.getOffset() + coverage.getLength(); | |
742 int windowEnd= fResultWindow.getOffset() + fResultWindow.getLength(); | |
743 int end= (rangeEnd > windowEnd ? windowEnd : rangeEnd); | |
744 end -= fResultWindow.getOffset(); | |
745 | |
746 return new Region(start, end - start); | |
747 } | |
748 | |
749 /** | |
750 * Returns an iterator which enumerates all style ranged which define a style | |
751 * different from the presentation's default style range. The default style range | |
752 * is not enumerated. | |
753 * | |
754 * @return a style range iterator | |
755 */ | |
756 public Iterator getNonDefaultStyleRangeIterator() { | |
757 return new FilterIterator(fDefaultRange !is null); | |
758 } | |
759 | |
760 /** | |
761 * Returns an iterator which enumerates all style ranges of this presentation | |
762 * except the default style range. The returned <code>StyleRange</code>s | |
763 * are relative to the start of the presentation's result window. | |
764 * | |
765 * @return a style range iterator | |
766 */ | |
767 public Iterator getAllStyleRangeIterator() { | |
768 return new FilterIterator(false); | |
769 } | |
770 | |
771 /** | |
772 * Returns whether this collection contains any style range including | |
773 * the default style range. | |
774 * | |
775 * @return <code>true</code> if there is no style range in this presentation | |
776 */ | |
777 public bool isEmpty() { | |
778 return (fDefaultRange is null && getDenumerableRanges() is 0); | |
779 } | |
780 | |
781 /** | |
782 * Returns the number of style ranges in the presentation not counting the default | |
783 * style range. | |
784 * | |
785 * @return the number of style ranges in the presentation excluding the default style range | |
786 */ | |
787 public int getDenumerableRanges() { | |
788 int size= getFirstIndexAfterWindow(fResultWindow) - getFirstIndexInWindow(fResultWindow); | |
789 return (size < 0 ? 0 : size); | |
790 } | |
791 | |
792 /** | |
793 * Returns the style range with the smallest offset ignoring the default style range or null | |
794 * if the presentation is empty. | |
795 * | |
796 * @return the style range with the smallest offset different from the default style range | |
797 */ | |
798 public StyleRange getFirstStyleRange() { | |
799 try { | |
800 | |
801 StyleRange range= cast(StyleRange) fRanges.get(getFirstIndexInWindow(fResultWindow)); | |
802 return createWindowRelativeRange(fResultWindow, range); | |
803 | |
804 } catch (NoSuchElementException x) { | |
805 } catch (IndexOutOfBoundsException x) { | |
806 } | |
807 | |
808 return null; | |
809 } | |
810 | |
811 /** | |
812 * Returns the style range with the highest offset ignoring the default style range. | |
813 * | |
814 * @return the style range with the highest offset different from the default style range | |
815 */ | |
816 public StyleRange getLastStyleRange() { | |
817 try { | |
818 | |
819 StyleRange range= cast(StyleRange) fRanges.get(getFirstIndexAfterWindow(fResultWindow) - 1); | |
820 return createWindowRelativeRange(fResultWindow, range); | |
821 | |
822 } catch (NoSuchElementException x) { | |
823 return null; | |
824 } catch (IndexOutOfBoundsException x) { | |
825 return null; | |
826 } | |
827 } | |
828 | |
829 /** | |
830 * Returns the coverage of this presentation as clipped by the presentation's | |
831 * result window. | |
832 * | |
833 * @return the coverage of this presentation | |
834 */ | |
835 public IRegion getCoverage() { | |
836 | |
837 if (fDefaultRange !is null) { | |
838 StyleRange range= getDefaultStyleRange(); | |
839 return new Region(range.start, range.length); | |
840 } | |
841 | |
842 StyleRange first= getFirstStyleRange(); | |
843 StyleRange last= getLastStyleRange(); | |
844 | |
845 if (first is null || last is null) | |
846 return null; | |
847 | |
848 return new Region(first.start, last.start - first. start + last.length); | |
849 } | |
850 | |
851 /** | |
852 * Returns the extent of this presentation clipped by the | |
853 * presentation's result window. | |
854 * | |
855 * @return the clipped extent | |
856 * @since 3.0 | |
857 */ | |
858 public IRegion getExtent() { | |
859 if (fExtent !is null) | |
860 return createWindowRelativeRegion(fExtent); | |
861 return getCoverage(); | |
862 } | |
863 | |
864 /** | |
865 * Clears this presentation by resetting all applied changes. | |
866 * @since 2.0 | |
867 */ | |
868 public void clear() { | |
869 fDefaultRange= null; | |
870 fResultWindow= null; | |
871 fRanges.clear(); | |
872 } | |
873 | |
874 | |
875 } |