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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Copyright (c) 2006-2014 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.DMDircMBassador;
  24. import com.dmdirc.events.AppErrorEvent;
  25. import com.dmdirc.events.DisplayPropertyMap;
  26. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  27. import com.dmdirc.interfaces.config.ConfigChangeListener;
  28. import com.dmdirc.logger.ErrorLevel;
  29. import com.dmdirc.util.collections.ListenerList;
  30. import com.dmdirc.util.collections.RollingList;
  31. import java.awt.Font;
  32. import java.io.Serializable;
  33. import java.text.AttributedCharacterIterator;
  34. import java.text.AttributedString;
  35. import java.util.ArrayList;
  36. import java.util.Date;
  37. import java.util.List;
  38. import javax.swing.UIManager;
  39. import javax.swing.text.BadLocationException;
  40. /**
  41. * Data contained in a TextPane.
  42. */
  43. public class IRCDocument implements Serializable, ConfigChangeListener {
  44. /** A version number for this class. */
  45. private static final long serialVersionUID = 4;
  46. /** List of lines of text. */
  47. private final List<Line> lines;
  48. /** Listener list. */
  49. private final ListenerList listeners;
  50. /** Cached lines. */
  51. private final RollingList<Line> cachedLines;
  52. /** Cached attributed strings. */
  53. private final RollingList<AttributedString> cachedStrings;
  54. /** Config Manager for getting settings. */
  55. private final AggregateConfigProvider configManager;
  56. /** This document's styliser. */
  57. private final Styliser styliser;
  58. /** The event bus to post errors to. */
  59. private final DMDircMBassador eventBus;
  60. /** Font size. */
  61. private int fontSize;
  62. /** Font name. */
  63. private String fontName;
  64. /** Frame buffer size. */
  65. private Integer frameBufferSize;
  66. public IRCDocument(final AggregateConfigProvider configManager,
  67. final Styliser styliser, final DMDircMBassador eventBus) {
  68. this.configManager = configManager;
  69. this.styliser = styliser;
  70. this.eventBus = eventBus;
  71. lines = new ArrayList<>();
  72. listeners = new ListenerList();
  73. cachedLines = new RollingList<>(50);
  74. cachedStrings = new RollingList<>(50);
  75. frameBufferSize = configManager.getOptionInt("ui", "frameBufferSize", false);
  76. configManager.addChangeListener("ui", "textPaneFontSize", this);
  77. configManager.addChangeListener("ui", "textPaneFontName", this);
  78. configManager.addChangeListener("ui", "frameBufferSize", this);
  79. setCachedSettings();
  80. }
  81. /**
  82. * Returns the number of lines in this document.
  83. *
  84. * @return Number of lines
  85. */
  86. public int getNumLines() {
  87. synchronized (lines) {
  88. return lines.size();
  89. }
  90. }
  91. /**
  92. * Returns the Line at the specified number.
  93. *
  94. * @param lineNumber Line number to retrieve
  95. *
  96. * @return Line at the specified number or null
  97. */
  98. public Line getLine(final int lineNumber) {
  99. synchronized (lines) {
  100. return lines.get(lineNumber);
  101. }
  102. }
  103. /**
  104. * Adds the stylised string to the canvas.
  105. *
  106. * @param timestamp The timestamp to show along with the text.
  107. * @param displayPropertyMap The display properties to use
  108. * @param text stylised string to add to the document
  109. */
  110. public void addText(final long timestamp, final DisplayPropertyMap displayPropertyMap,
  111. final String text) {
  112. final int start;
  113. synchronized (lines) {
  114. start = lines.size();
  115. lines.add(new Line(styliser, formatTimestamp(timestamp), text, displayPropertyMap,
  116. fontSize, fontName));
  117. }
  118. fireLinesAdded(start, 1);
  119. }
  120. private String formatTimestamp(final long timestamp) {
  121. return Formatter.formatMessage(configManager, "timestamp", new Date(timestamp));
  122. }
  123. /**
  124. * Trims the document to the specified number of lines.
  125. *
  126. * @param numLines Number of lines to trim the document to
  127. */
  128. public void trim(final int numLines) {
  129. synchronized (lines) {
  130. if (frameBufferSize != null && frameBufferSize > 0) {
  131. final int i = lines.size() - numLines;
  132. if (i > 0) {
  133. lines.subList(0, i).clear();
  134. fireTrimmed(numLines, i);
  135. }
  136. }
  137. }
  138. }
  139. /** Clears all lines from the document. */
  140. public void clear() {
  141. synchronized (lines) {
  142. lines.clear();
  143. }
  144. fireCleared();
  145. }
  146. /**
  147. * Adds a IRCDocumentListener to the listener list.
  148. *
  149. * @param listener Listener to add
  150. */
  151. public void addIRCDocumentListener(final IRCDocumentListener listener) {
  152. if (listener == null) {
  153. return;
  154. }
  155. listeners.add(IRCDocumentListener.class, listener);
  156. }
  157. /**
  158. * Removes a IRCDocumentListener from the listener list.
  159. *
  160. * @param listener Listener to remove
  161. */
  162. public void removeIRCDocumentListener(final IRCDocumentListener listener) {
  163. listeners.remove(IRCDocumentListener.class, listener);
  164. }
  165. /**
  166. * Fires the lines added method on all listeners.
  167. *
  168. * @param index Index of the added line
  169. * @param size Number of lines added
  170. */
  171. protected void fireLinesAdded(final int index, final int size) {
  172. for (IRCDocumentListener listener
  173. : listeners.get(IRCDocumentListener.class)) {
  174. listener.linesAdded(index, size, lines.size());
  175. }
  176. trim(frameBufferSize);
  177. }
  178. /**
  179. * Fires the trimmed method on all listeners.
  180. *
  181. * @param newSize New document size
  182. * @param trimmedLines Number of trimmed lines
  183. */
  184. protected void fireTrimmed(final int newSize, final int trimmedLines) {
  185. for (IRCDocumentListener listener
  186. : listeners.get(IRCDocumentListener.class)) {
  187. listener.trimmed(newSize, trimmedLines);
  188. }
  189. }
  190. /**
  191. * fires the cleared method on all listeners.
  192. */
  193. protected void fireCleared() {
  194. listeners.get(IRCDocumentListener.class).forEach(IRCDocumentListener::cleared);
  195. }
  196. /**
  197. * fires the need repaint method on all listeners.
  198. */
  199. protected void fireRepaintNeeded() {
  200. listeners.get(IRCDocumentListener.class).forEach(IRCDocumentListener::repaintNeeded);
  201. }
  202. /**
  203. * Returns an attributed character iterator for a particular line, utilising the document cache
  204. * where possible.
  205. *
  206. * @param line Line to be styled
  207. *
  208. * @return Styled line
  209. */
  210. protected AttributedCharacterIterator getStyledLine(final Line line) {
  211. synchronized (lines) {
  212. AttributedString styledLine = null;
  213. if (cachedLines.contains(line)) {
  214. final int index = cachedLines.getList().indexOf(line);
  215. styledLine = cachedStrings.get(index);
  216. }
  217. if (styledLine == null) {
  218. try {
  219. styledLine = line.getStyled();
  220. } catch (BadLocationException ex) {
  221. eventBus.publish(new AppErrorEvent(ErrorLevel.MEDIUM, ex,
  222. "Unable to add line: " + ex.getMessage(), ""));
  223. }
  224. cachedLines.add(line);
  225. cachedStrings.add(styledLine);
  226. }
  227. // TODO: Consider what to do if this is null from the BLE above
  228. return styledLine.getIterator();
  229. }
  230. }
  231. /**
  232. * Returns an attributed string for a particular line, utilising the document cache where
  233. * possible.
  234. *
  235. * @param line Line number to be styled
  236. *
  237. * @return Styled line
  238. */
  239. public AttributedCharacterIterator getStyledLine(final int line) {
  240. return getStyledLine(getLine(line));
  241. }
  242. /**
  243. * Returns the line height of the specified line.
  244. *
  245. * @param line Line
  246. *
  247. * @return Line height
  248. */
  249. protected int getLineHeight(final Line line) {
  250. return line.getFontSize();
  251. }
  252. /**
  253. * Returns the line height of the specified.
  254. *
  255. * @param line Line
  256. *
  257. * @return Line height
  258. */
  259. public int getLineHeight(final int line) {
  260. return getLineHeight(getLine(line));
  261. }
  262. /**
  263. * Sets all the cached settings in this document.
  264. */
  265. private void setCachedSettings() {
  266. final Font defaultFont = UIManager.getFont("TextPane.font");
  267. if (configManager.hasOptionString("ui", "textPaneFontName")) {
  268. fontName = configManager.getOption("ui", "textPaneFontName");
  269. } else {
  270. fontName = defaultFont.getName();
  271. }
  272. if (configManager.hasOptionString("ui", "textPaneFontSize")) {
  273. fontSize = configManager.getOptionInt("ui", "textPaneFontSize");
  274. } else {
  275. fontSize = defaultFont.getSize();
  276. }
  277. frameBufferSize = configManager.getOptionInt("ui", "frameBufferSize", true);
  278. trim(frameBufferSize);
  279. }
  280. @Override
  281. public void configChanged(final String domain, final String key) {
  282. setCachedSettings();
  283. cachedLines.clear();
  284. cachedStrings.clear();
  285. synchronized (lines) {
  286. for (Line line : lines) {
  287. line.setFontName(fontName);
  288. line.setFontSize(fontSize);
  289. }
  290. }
  291. fireRepaintNeeded();
  292. }
  293. }