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

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