Mercurial > projects > dwt-addons
comparison dwtx/jface/viewers/StyledString.d @ 104:04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
These new wrappers now use the tango.util.containers instead of the tango.util.collections.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 07 Aug 2008 15:01:33 +0200 |
parents | 5df4896124c7 |
children |
comparison
equal
deleted
inserted
replaced
103:2d6540440fe6 | 104:04b47443bb01 |
---|---|
17 import dwtx.jface.preference.JFacePreferences; | 17 import dwtx.jface.preference.JFacePreferences; |
18 import dwtx.jface.resource.ColorRegistry; | 18 import dwtx.jface.resource.ColorRegistry; |
19 import dwtx.jface.resource.JFaceResources; | 19 import dwtx.jface.resource.JFaceResources; |
20 | 20 |
21 import dwt.dwthelper.utils; | 21 import dwt.dwthelper.utils; |
22 import dwtx.dwtxhelper.Collection; | |
22 import tango.text.convert.Format; | 23 import tango.text.convert.Format; |
23 import tango.util.collection.ArraySeq; | |
24 import tango.core.Exception; | 24 import tango.core.Exception; |
25 | 25 |
26 | 26 |
27 /** | 27 /** |
28 * A mutable string with styled ranges. All ranges mark substrings of the string | 28 * A mutable string with styled ranges. All ranges mark substrings of the string |
222 } | 222 } |
223 | 223 |
224 int offset = fBuffer.length(); | 224 int offset = fBuffer.length(); |
225 fBuffer.append(string.toString()); | 225 fBuffer.append(string.toString()); |
226 | 226 |
227 auto otherRuns = string.fStyleRuns; | 227 List otherRuns = string.fStyleRuns; |
228 if (otherRuns !is null && !otherRuns.drained()) { | 228 if (otherRuns !is null && !otherRuns.isEmpty()) { |
229 for (int i = 0; i < otherRuns.size(); i++) { | 229 for (int i = 0; i < otherRuns.size(); i++) { |
230 StyleRun curr = cast(StyleRun) otherRuns.get(i); | 230 StyleRun curr = cast(StyleRun) otherRuns.get(i); |
231 if (i is 0 && curr.offset !is 0) { | 231 if (i is 0 && curr.offset !is 0) { |
232 appendStyleRun(null, offset); // appended string will | 232 appendStyleRun(null, offset); // appended string will |
233 // start with the default | 233 // start with the default |
343 endRun = -(endRun + 1); | 343 endRun = -(endRun + 1); |
344 if (offset + length < fBuffer.length()) { | 344 if (offset + length < fBuffer.length()) { |
345 Styler prevStyle = endRun > 0 ? fStyleRuns.getRun(endRun - 1).style | 345 Styler prevStyle = endRun > 0 ? fStyleRuns.getRun(endRun - 1).style |
346 : null; | 346 : null; |
347 fStyleRuns | 347 fStyleRuns |
348 .addAt(endRun, new StyleRun(offset + length, prevStyle)); | 348 .add(endRun, new StyleRun(offset + length, prevStyle)); |
349 } | 349 } |
350 } | 350 } |
351 | 351 |
352 int startRun = findRun(offset); | 352 int startRun = findRun(offset); |
353 if (startRun >= 0) { | 353 if (startRun >= 0) { |
359 | 359 |
360 Styler prevStyle = startRun > 0 ? fStyleRuns.getRun(startRun - 1).style | 360 Styler prevStyle = startRun > 0 ? fStyleRuns.getRun(startRun - 1).style |
361 : null; | 361 : null; |
362 if (isDifferentStyle(prevStyle, styler) | 362 if (isDifferentStyle(prevStyle, styler) |
363 || (startRun is 0 && styler !is null)) { | 363 || (startRun is 0 && styler !is null)) { |
364 fStyleRuns.addAt(startRun, new StyleRun(offset, styler)); | 364 fStyleRuns.add(startRun, new StyleRun(offset, styler)); |
365 endRun++; // endrun is moved one back | 365 endRun++; // endrun is moved one back |
366 } else { | 366 } else { |
367 startRun--; // we use the previous | 367 startRun--; // we use the previous |
368 } | 368 } |
369 } | 369 } |
379 * @return an array of all {@link StyleRange} resulting from applying the | 379 * @return an array of all {@link StyleRange} resulting from applying the |
380 * stored stylers to this string. | 380 * stored stylers to this string. |
381 */ | 381 */ |
382 public StyleRange[] getStyleRanges() { | 382 public StyleRange[] getStyleRanges() { |
383 if (hasRuns()) { | 383 if (hasRuns()) { |
384 StyleRange[] res; | 384 ArrayList res = new ArrayList(); |
385 | 385 |
386 auto styleRuns = getStyleRuns(); | 386 List styleRuns = getStyleRuns(); |
387 int offset = 0; | 387 int offset = 0; |
388 Styler style = null; | 388 Styler style = null; |
389 for (int i = 0; i < styleRuns.size(); i++) { | 389 for (int i = 0; i < styleRuns.size(); i++) { |
390 StyleRun curr = cast(StyleRun) styleRuns.get(i); | 390 StyleRun curr = cast(StyleRun) styleRuns.get(i); |
391 if (isDifferentStyle(curr.style, style)) { | 391 if (isDifferentStyle(curr.style, style)) { |
392 if (curr.offset > offset && style !is null) { | 392 if (curr.offset > offset && style !is null) { |
393 res ~= createStyleRange(offset, curr.offset, style); | 393 res.add(createStyleRange(offset, curr.offset, style)); |
394 } | 394 } |
395 offset = curr.offset; | 395 offset = curr.offset; |
396 style = curr.style; | 396 style = curr.style; |
397 } | 397 } |
398 } | 398 } |
399 if (fBuffer.length() > offset && style !is null) { | 399 if (fBuffer.length() > offset && style !is null) { |
400 res ~= createStyleRange(offset, fBuffer.length(), style); | 400 res.add(createStyleRange(offset, fBuffer.length(), style)); |
401 } | 401 } |
402 return res; | 402 return arraycast!(StyleRange)(res.toArray()); |
403 } | 403 } |
404 return EMPTY; | 404 return EMPTY; |
405 } | 405 } |
406 | 406 |
407 private int findRun(int offset) { | 407 private int findRun(int offset) { |
429 style.applyStyles(styleRange); | 429 style.applyStyles(styleRange); |
430 return styleRange; | 430 return styleRange; |
431 } | 431 } |
432 | 432 |
433 private bool hasRuns() { | 433 private bool hasRuns() { |
434 return fStyleRuns !is null && !fStyleRuns.drained(); | 434 return fStyleRuns !is null && !fStyleRuns.isEmpty(); |
435 } | 435 } |
436 | 436 |
437 private void appendStyleRun(Styler style, int offset) { | 437 private void appendStyleRun(Styler style, int offset) { |
438 StyleRun lastRun = getLastRun(); | 438 StyleRun lastRun = getLastRun(); |
439 if (lastRun !is null && lastRun.offset is offset) { | 439 if (lastRun !is null && lastRun.offset is offset) { |
441 return; | 441 return; |
442 } | 442 } |
443 | 443 |
444 if (lastRun is null && style !is null || lastRun !is null | 444 if (lastRun is null && style !is null || lastRun !is null |
445 && isDifferentStyle(style, lastRun.style)) { | 445 && isDifferentStyle(style, lastRun.style)) { |
446 getStyleRuns().append(new StyleRun(offset, style)); | 446 getStyleRuns().add(new StyleRun(offset, style)); |
447 } | 447 } |
448 } | 448 } |
449 | 449 |
450 private bool isDifferentStyle(Styler style1, Styler style2) { | 450 private bool isDifferentStyle(Styler style1, Styler style2) { |
451 if (style1 is null) { | 451 if (style1 is null) { |
453 } | 453 } |
454 return !style1.opEquals(style2); | 454 return !style1.opEquals(style2); |
455 } | 455 } |
456 | 456 |
457 private StyleRun getLastRun() { | 457 private StyleRun getLastRun() { |
458 if (fStyleRuns is null || fStyleRuns.drained()) { | 458 if (fStyleRuns is null || fStyleRuns.isEmpty()) { |
459 return null; | 459 return null; |
460 } | 460 } |
461 return fStyleRuns.getRun(fStyleRuns.size() - 1); | 461 return fStyleRuns.getRun(fStyleRuns.size() - 1); |
462 } | 462 } |
463 | 463 |
464 private StyleRunList getStyleRuns() { | 464 private List getStyleRuns() { |
465 if (fStyleRuns is null) | 465 if (fStyleRuns is null) |
466 fStyleRuns = new StyleRunList(); | 466 fStyleRuns = new StyleRunList(); |
467 return fStyleRuns; | 467 return fStyleRuns; |
468 } | 468 } |
469 | 469 |
479 public override String toString() { | 479 public override String toString() { |
480 return Format("Offset {}, style: {}", offset, style ); //$NON-NLS-1$//$NON-NLS-2$ | 480 return Format("Offset {}, style: {}", offset, style ); //$NON-NLS-1$//$NON-NLS-2$ |
481 } | 481 } |
482 } | 482 } |
483 | 483 |
484 private static class StyleRunList : ArraySeq!(Object) { | 484 private static class StyleRunList : ArrayList { |
485 private static final long serialVersionUID = 123L; | 485 private static final long serialVersionUID = 123L; |
486 | 486 |
487 public this() { | 487 public this() { |
488 super(); | 488 super(3); |
489 capacity(3); | |
490 } | 489 } |
491 | 490 |
492 public StyleRun getRun(int index) { | 491 public StyleRun getRun(int index) { |
493 return cast(StyleRun) get(index); | 492 return cast(StyleRun) get(index); |
494 } | 493 } |
495 | 494 |
496 //public void removeRange(int fromIndex, int toIndex) { | 495 public void removeRange(int fromIndex, int toIndex) { |
497 // super.removeRange(fromIndex, toIndex); | 496 super.removeRange(fromIndex, toIndex); |
498 //} | 497 } |
499 } | 498 } |
500 | 499 |
501 private static class DefaultStyler : Styler { | 500 private static class DefaultStyler : Styler { |
502 private final String fForegroundColorName; | 501 private final String fForegroundColorName; |
503 private final String fBackgroundColorName; | 502 private final String fBackgroundColorName; |