16
16
import java .util .ArrayList ;
17
17
import java .util .Iterator ;
18
18
import java .util .NoSuchElementException ;
19
+ import java .util .function .BiConsumer ;
19
20
20
21
import org .eclipse .swt .SWT ;
21
22
import org .eclipse .swt .custom .StyleRange ;
@@ -233,7 +234,7 @@ public void addStyleRange(StyleRange range) {
233
234
* @since 3.0
234
235
*/
235
236
public void replaceStyleRange (StyleRange range ) {
236
- applyStyleRange (range , false );
237
+ applyStyleRange (range , this :: replaceStyle );
237
238
}
238
239
239
240
/**
@@ -244,18 +245,20 @@ public void replaceStyleRange(StyleRange range) {
244
245
* @since 3.0
245
246
*/
246
247
public void mergeStyleRange (StyleRange range ) {
247
- applyStyleRange (range , true );
248
+ applyStyleRange (range , this :: mergeStyle );
248
249
}
249
250
250
251
/**
251
252
* Applies the given range to this presentation. The range must be a
252
253
* subrange of the presentation's default range.
253
254
*
254
255
* @param range the range to be added
255
- * @param merge <code>true</code> if the style should be merged instead of replaced
256
+ * @param styleRangeApplier method accepting a StyleRange template to be applied and a
257
+ * StyleRange target, the style being applied to. The target may be modified by the
258
+ * method
256
259
* @since 3.0
257
260
*/
258
- private void applyStyleRange (StyleRange range , boolean merge ) {
261
+ public void applyStyleRange (StyleRange range , BiConsumer < StyleRange , StyleRange > styleRangeApplier ) {
259
262
if (range .length == 0 )
260
263
return ;
261
264
@@ -272,7 +275,7 @@ private void applyStyleRange(StyleRange range, boolean merge) {
272
275
273
276
defaultRange .start = start ;
274
277
defaultRange .length = length ;
275
- applyStyle (range , defaultRange , merge );
278
+ styleRangeApplier . accept (range , defaultRange );
276
279
fRanges .add (defaultRange );
277
280
} else {
278
281
IRegion rangeRegion = new Region (start , length );
@@ -284,7 +287,7 @@ private void applyStyleRange(StyleRange range, boolean merge) {
284
287
defaultRange = range ;
285
288
defaultRange .start = start ;
286
289
defaultRange .length = length ;
287
- applyStyle (range , defaultRange , merge );
290
+ styleRangeApplier . accept (range , defaultRange );
288
291
fRanges .add (defaultRange );
289
292
return ;
290
293
}
@@ -313,14 +316,14 @@ private void applyStyleRange(StyleRange range, boolean merge) {
313
316
314
317
defaultRange .start = start ;
315
318
defaultRange .length = currentStart - start ;
316
- applyStyle (range , defaultRange , merge );
319
+ styleRangeApplier . accept (range , defaultRange );
317
320
fRanges .add (i , defaultRange );
318
321
i ++; last ++;
319
322
320
323
321
324
// Apply style to first part of current range
322
325
current .length = Math .min (end , currentEnd ) - currentStart ;
323
- applyStyle (range , current , merge );
326
+ styleRangeApplier . accept (range , current );
324
327
}
325
328
326
329
if (start >= currentStart ) {
@@ -333,7 +336,7 @@ private void applyStyleRange(StyleRange range, boolean merge) {
333
336
i ++; last ++;
334
337
fRanges .add (i , current );
335
338
}
336
- applyStyle (range , current , merge );
339
+ styleRangeApplier . accept (range , current );
337
340
current .start = start ;
338
341
current .length = Math .min (end , currentEnd ) - start ;
339
342
}
@@ -359,7 +362,7 @@ private void applyStyleRange(StyleRange range, boolean merge) {
359
362
defaultRange = range ;
360
363
defaultRange .start = start ;
361
364
defaultRange .length = end - start ;
362
- applyStyle (range , defaultRange , merge );
365
+ styleRangeApplier . accept (range , defaultRange );
363
366
fRanges .add (last , defaultRange );
364
367
}
365
368
}
@@ -374,7 +377,7 @@ private void applyStyleRange(StyleRange range, boolean merge) {
374
377
* @since 3.0
375
378
*/
376
379
public void replaceStyleRanges (StyleRange [] ranges ) {
377
- applyStyleRanges (ranges , false );
380
+ applyStyleRanges (ranges , this :: replaceStyle );
378
381
}
379
382
380
383
/**
@@ -386,7 +389,7 @@ public void replaceStyleRanges(StyleRange[] ranges) {
386
389
* @since 3.0
387
390
*/
388
391
public void mergeStyleRanges (StyleRange [] ranges ) {
389
- applyStyleRanges (ranges , true );
392
+ applyStyleRanges (ranges , this :: mergeStyle );
390
393
}
391
394
392
395
/**
@@ -395,10 +398,12 @@ public void mergeStyleRanges(StyleRange[] ranges) {
395
398
* by increasing offset and must not overlap (but may be adjacent).
396
399
*
397
400
* @param ranges the ranges to be added
398
- * @param merge <code>true</code> if the style should be merged instead of replaced
401
+ * @param styleRangeApplier method accepting a StyleRange template to be applied and a
402
+ * StyleRange target, the style being applied to. The target may be modified by the
403
+ * method
399
404
* @since 3.0
400
405
*/
401
- private void applyStyleRanges (StyleRange [] ranges , boolean merge ) {
406
+ public void applyStyleRanges (StyleRange [] ranges , BiConsumer < StyleRange , StyleRange > styleRangeApplier ) {
402
407
int j = 0 ;
403
408
ArrayList <StyleRange > oldRanges = fRanges ;
404
409
ArrayList <StyleRange > newRanges = new ArrayList <>(2 *ranges .length + oldRanges .size ());
@@ -407,67 +412,72 @@ private void applyStyleRanges(StyleRange[] ranges, boolean merge) {
407
412
for (int m = getFirstIndexAfterWindow (new Region (range .start , range .length )); j < m ; j ++)
408
413
newRanges .add (oldRanges .get (j ));
409
414
fRanges = newRanges ; // for mergeStyleRange(...)
410
- applyStyleRange (range , merge );
415
+ applyStyleRange (range , styleRangeApplier );
411
416
}
412
417
for (int m = oldRanges .size (); j < m ; j ++)
413
418
newRanges .add (oldRanges .get (j ));
414
419
fRanges = newRanges ;
415
420
}
416
421
417
422
/**
418
- * Applies the template's style to the target.
423
+ * Merges the template's style to the target.
419
424
*
420
425
* @param template the style range to be used as template
421
426
* @param target the style range to which to apply the template
422
- * @param merge <code>true</code> if the style should be merged instead of replaced
423
427
* @since 3.0
424
428
*/
425
- private void applyStyle (StyleRange template , StyleRange target , boolean merge ) {
426
- if (merge ) {
427
- if (template .font != null )
428
- target .font = template .font ;
429
- target .fontStyle |= template .fontStyle ;
430
-
431
- if (template .metrics != null )
432
- target .metrics = template .metrics ;
429
+ private void mergeStyle (StyleRange template , StyleRange target ) {
430
+ if (template .font != null )
431
+ target .font = template .font ;
432
+ target .fontStyle |= template .fontStyle ;
433
433
434
- if (template .foreground != null || template .underlineStyle == SWT .UNDERLINE_LINK )
435
- target .foreground = template .foreground ;
436
- if (template .background != null )
437
- target .background = template .background ;
434
+ if (template .metrics != null )
435
+ target .metrics = template .metrics ;
438
436
439
- target .strikeout |= template .strikeout ;
440
- if (template .strikeoutColor != null )
441
- target .strikeoutColor = template .strikeoutColor ;
437
+ if (template .foreground != null || template .underlineStyle == SWT .UNDERLINE_LINK )
438
+ target .foreground = template .foreground ;
439
+ if (template .background != null )
440
+ target .background = template .background ;
442
441
443
- target .underline |= template .underline ;
444
- if (template .underlineStyle != SWT . NONE && target . underlineStyle != SWT . UNDERLINE_LINK )
445
- target .underlineStyle = template .underlineStyle ;
442
+ target .strikeout |= template .strikeout ;
443
+ if (template .strikeoutColor != null )
444
+ target .strikeoutColor = template .strikeoutColor ;
446
445
447
- if (template .underlineColor != null )
448
- target .underlineColor = template .underlineColor ;
446
+ target .underline |= template .underline ;
447
+ if (template .underlineStyle != SWT .NONE && target .underlineStyle != SWT .UNDERLINE_LINK )
448
+ target .underlineStyle = template .underlineStyle ;
449
449
450
- if (template .borderStyle != SWT .NONE )
451
- target .borderStyle = template .borderStyle ;
452
- if (template .borderColor != null )
453
- target .borderColor = template .borderColor ;
450
+ if (template .underlineColor != null )
451
+ target .underlineColor = template .underlineColor ;
454
452
455
- } else {
456
- target .font = template .font ;
457
- target .fontStyle = template .fontStyle ;
458
- target .metrics = template .metrics ;
459
- target .foreground = template .foreground ;
460
- target .background = template .background ;
461
- target .strikeout = template .strikeout ;
462
- target .strikeoutColor = template .strikeoutColor ;
463
- target .underline = template .underline ;
464
- target .underlineStyle = template .underlineStyle ;
465
- target .underlineColor = template .underlineColor ;
466
- target .borderStyle = template .borderStyle ;
467
- target .borderColor = template .borderColor ;
468
- }
453
+ if (template .borderStyle != SWT .NONE )
454
+ target .borderStyle = template .borderStyle ;
455
+ if (template .borderColor != null )
456
+ target .borderColor = template .borderColor ;
469
457
}
470
458
459
+ /**
460
+ * Replaces the target's style with the template.
461
+ *
462
+ * @param template the style range to be used as template
463
+ * @param target the style range to which to apply the template
464
+ * @since 3.0
465
+ */
466
+ private void replaceStyle (StyleRange template , StyleRange target ) {
467
+ target .font = template .font ;
468
+ target .fontStyle = template .fontStyle ;
469
+ target .metrics = template .metrics ;
470
+ target .foreground = template .foreground ;
471
+ target .background = template .background ;
472
+ target .strikeout = template .strikeout ;
473
+ target .strikeoutColor = template .strikeoutColor ;
474
+ target .underline = template .underline ;
475
+ target .underlineStyle = template .underlineStyle ;
476
+ target .underlineColor = template .underlineColor ;
477
+ target .borderStyle = template .borderStyle ;
478
+ target .borderColor = template .borderColor ;
479
+ }
480
+
471
481
/**
472
482
* Checks whether the given range is a subrange of the presentation's
473
483
* default style range.
0 commit comments