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.

IRCDocument.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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.DisplayPropertyMap;
  24. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  25. import com.dmdirc.interfaces.config.ConfigChangeListener;
  26. import com.dmdirc.util.collections.ListenerList;
  27. import java.awt.Font;
  28. import java.io.Serializable;
  29. import java.time.LocalDateTime;
  30. import java.util.ArrayList;
  31. import java.util.List;
  32. import javax.swing.UIManager;
  33. /**
  34. * Data contained in a TextPane.
  35. */
  36. public class IRCDocument implements Serializable, ConfigChangeListener, Document {
  37. /** A version number for this class. */
  38. private static final long serialVersionUID = 4;
  39. /** List of lines of text. */
  40. private final List<Line> lines;
  41. /** Listener list. */
  42. private final ListenerList listeners;
  43. /** Config Manager for getting settings. */
  44. private final AggregateConfigProvider configManager;
  45. /** This document's styliser. */
  46. private final Styliser styliser;
  47. /** Font size. */
  48. private int fontSize;
  49. /** Font name. */
  50. private String fontName;
  51. /** Frame buffer size. */
  52. private Integer frameBufferSize;
  53. public IRCDocument(final AggregateConfigProvider configManager, final Styliser styliser) {
  54. this.configManager = configManager;
  55. this.styliser = styliser;
  56. lines = new ArrayList<>();
  57. listeners = new ListenerList();
  58. frameBufferSize = configManager.getOptionInt("ui", "frameBufferSize", false);
  59. configManager.addChangeListener("ui", "textPaneFontSize", this);
  60. configManager.addChangeListener("ui", "textPaneFontName", this);
  61. configManager.addChangeListener("ui", "frameBufferSize", this);
  62. setCachedSettings();
  63. }
  64. @Override
  65. public int getNumLines() {
  66. synchronized (lines) {
  67. return lines.size();
  68. }
  69. }
  70. @Override
  71. public Line getLine(final int lineNumber) {
  72. synchronized (lines) {
  73. return lines.get(lineNumber);
  74. }
  75. }
  76. @Override
  77. public void addText(final LocalDateTime timestamp, final DisplayPropertyMap displayPropertyMap,
  78. final String text) {
  79. final int start;
  80. synchronized (lines) {
  81. start = lines.size();
  82. lines.add(new IRCLine(styliser, formatTimestamp(timestamp), text, displayPropertyMap,
  83. fontSize, fontName));
  84. }
  85. fireLinesAdded(start, 1);
  86. }
  87. private String formatTimestamp(final LocalDateTime timestamp) {
  88. return Formatter.formatMessage(configManager, "timestamp", timestamp);
  89. }
  90. @Override
  91. public void trim(final int numLines) {
  92. synchronized (lines) {
  93. if (frameBufferSize != null && frameBufferSize > 0) {
  94. final int i = lines.size() - numLines;
  95. if (i > 0) {
  96. lines.subList(0, i).clear();
  97. fireTrimmed(numLines, i);
  98. }
  99. }
  100. }
  101. }
  102. @Override
  103. public void clear() {
  104. synchronized (lines) {
  105. lines.clear();
  106. }
  107. fireCleared();
  108. }
  109. @Override
  110. public void addIRCDocumentListener(final DocumentListener listener) {
  111. if (listener == null) {
  112. return;
  113. }
  114. listeners.add(DocumentListener.class, listener);
  115. }
  116. @Override
  117. public void removeIRCDocumentListener(final DocumentListener listener) {
  118. listeners.remove(DocumentListener.class, listener);
  119. }
  120. /**
  121. * Fires the lines added method on all listeners.
  122. *
  123. * @param index Index of the added line
  124. * @param size Number of lines added
  125. */
  126. protected void fireLinesAdded(final int index, final int size) {
  127. for (DocumentListener listener
  128. : listeners.get(DocumentListener.class)) {
  129. listener.linesAdded(index, size, lines.size());
  130. }
  131. trim(frameBufferSize);
  132. }
  133. /**
  134. * Fires the trimmed method on all listeners.
  135. *
  136. * @param newSize New document size
  137. * @param trimmedLines Number of trimmed lines
  138. */
  139. protected void fireTrimmed(final int newSize, final int trimmedLines) {
  140. for (DocumentListener listener
  141. : listeners.get(DocumentListener.class)) {
  142. listener.trimmed(newSize, trimmedLines);
  143. }
  144. }
  145. /**
  146. * fires the cleared method on all listeners.
  147. */
  148. protected void fireCleared() {
  149. listeners.get(DocumentListener.class).forEach(DocumentListener::cleared);
  150. }
  151. /**
  152. * fires the need repaint method on all listeners.
  153. */
  154. protected void fireRepaintNeeded() {
  155. listeners.get(DocumentListener.class).forEach(DocumentListener::repaintNeeded);
  156. }
  157. /**
  158. * Returns the line height of the specified line.
  159. *
  160. * @param line Line
  161. *
  162. * @return Line height
  163. */
  164. protected int getLineHeight(final Line line) {
  165. return line.getFontSize();
  166. }
  167. @Override
  168. public int getLineHeight(final int line) {
  169. return getLineHeight(getLine(line));
  170. }
  171. /**
  172. * Sets all the cached settings in this document.
  173. */
  174. private void setCachedSettings() {
  175. final Font defaultFont = UIManager.getFont("TextPane.font");
  176. if (configManager.hasOptionString("ui", "textPaneFontName")) {
  177. fontName = configManager.getOption("ui", "textPaneFontName");
  178. } else {
  179. fontName = defaultFont.getName();
  180. }
  181. if (configManager.hasOptionString("ui", "textPaneFontSize")) {
  182. fontSize = configManager.getOptionInt("ui", "textPaneFontSize");
  183. } else {
  184. fontSize = defaultFont.getSize();
  185. }
  186. frameBufferSize = configManager.getOptionInt("ui", "frameBufferSize", true);
  187. trim(frameBufferSize);
  188. }
  189. @Override
  190. public void configChanged(final String domain, final String key) {
  191. setCachedSettings();
  192. synchronized (lines) {
  193. for (Line line : lines) {
  194. line.setFontName(fontName);
  195. line.setFontSize(fontSize);
  196. }
  197. }
  198. fireRepaintNeeded();
  199. }
  200. }