You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CachingDocument.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.ui.messages;
  23. import com.dmdirc.events.DisplayProperty;
  24. import com.dmdirc.util.collections.RollingList;
  25. /**
  26. * Wraps an {@link IRCDocument} and caches recent lines.
  27. */
  28. public class CachingDocument<T> {
  29. /** The document to wrap and cache data from. */
  30. private final Document document;
  31. /** The maker to use to produce styled lines. */
  32. private final StyledMessageMaker<T> maker;
  33. /** Cached lines. */
  34. private final RollingList<Line> cachedLines;
  35. /** Cached attributed strings. */
  36. private final RollingList<T> cachedStrings;
  37. public CachingDocument(final Document document, final StyledMessageMaker<T> maker) {
  38. this.document = document;
  39. this.maker = maker;
  40. cachedLines = new RollingList<>(50);
  41. cachedStrings = new RollingList<>(50);
  42. }
  43. /**
  44. * Returns an attributed character iterator for a particular line, utilising the document cache
  45. * where possible.
  46. *
  47. * @param line Line to be styled
  48. *
  49. * @return Styled line
  50. */
  51. protected T getStyledLine(final Line line) {
  52. T styledLine = null;
  53. if (cachedLines.contains(line)) {
  54. final int index = cachedLines.getList().indexOf(line);
  55. styledLine = cachedStrings.get(index);
  56. }
  57. if (styledLine == null) {
  58. line.getDisplayableProperty(DisplayProperty.FOREGROUND_COLOUR)
  59. .ifPresent(maker::setDefaultForeground);
  60. line.getDisplayableProperty(DisplayProperty.BACKGROUND_COLOUR)
  61. .ifPresent(maker::setDefaultBackground);
  62. styledLine = line.getStyled(maker);
  63. cachedLines.add(line);
  64. cachedStrings.add(styledLine);
  65. }
  66. return styledLine;
  67. }
  68. /**
  69. * Returns an attributed string for a particular line, utilising the document cache where
  70. * possible.
  71. *
  72. * @param line Line number to be styled
  73. *
  74. * @return Styled line
  75. */
  76. public T getStyledLine(final int line) {
  77. return getStyledLine(document.getLine(line));
  78. }
  79. public int getNumLines() {
  80. return document.getNumLines();
  81. }
  82. public Line getLine(final int line) {
  83. return document.getLine(line);
  84. }
  85. }