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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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.util.ArrayList;
  30. import java.util.Date;
  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 {
  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. /**
  65. * Returns the number of lines in this document.
  66. *
  67. * @return Number of lines
  68. */
  69. public int getNumLines() {
  70. synchronized (lines) {
  71. return lines.size();
  72. }
  73. }
  74. /**
  75. * Returns the Line at the specified number.
  76. *
  77. * @param lineNumber Line number to retrieve
  78. *
  79. * @return Line at the specified number or null
  80. */
  81. public Line getLine(final int lineNumber) {
  82. synchronized (lines) {
  83. return lines.get(lineNumber);
  84. }
  85. }
  86. /**
  87. * Adds the stylised string to the canvas.
  88. *
  89. * @param timestamp The timestamp to show along with the text.
  90. * @param displayPropertyMap The display properties to use
  91. * @param text stylised string to add to the document
  92. */
  93. public void addText(final long timestamp, final DisplayPropertyMap displayPropertyMap,
  94. final String text) {
  95. final int start;
  96. synchronized (lines) {
  97. start = lines.size();
  98. lines.add(new Line(styliser, formatTimestamp(timestamp), text, displayPropertyMap,
  99. fontSize, fontName));
  100. }
  101. fireLinesAdded(start, 1);
  102. }
  103. private String formatTimestamp(final long timestamp) {
  104. return Formatter.formatMessage(configManager, "timestamp", new Date(timestamp));
  105. }
  106. /**
  107. * Trims the document to the specified number of lines.
  108. *
  109. * @param numLines Number of lines to trim the document to
  110. */
  111. public void trim(final int numLines) {
  112. synchronized (lines) {
  113. if (frameBufferSize != null && frameBufferSize > 0) {
  114. final int i = lines.size() - numLines;
  115. if (i > 0) {
  116. lines.subList(0, i).clear();
  117. fireTrimmed(numLines, i);
  118. }
  119. }
  120. }
  121. }
  122. /** Clears all lines from the document. */
  123. public void clear() {
  124. synchronized (lines) {
  125. lines.clear();
  126. }
  127. fireCleared();
  128. }
  129. /**
  130. * Adds a IRCDocumentListener to the listener list.
  131. *
  132. * @param listener Listener to add
  133. */
  134. public void addIRCDocumentListener(final IRCDocumentListener listener) {
  135. if (listener == null) {
  136. return;
  137. }
  138. listeners.add(IRCDocumentListener.class, listener);
  139. }
  140. /**
  141. * Removes a IRCDocumentListener from the listener list.
  142. *
  143. * @param listener Listener to remove
  144. */
  145. public void removeIRCDocumentListener(final IRCDocumentListener listener) {
  146. listeners.remove(IRCDocumentListener.class, listener);
  147. }
  148. /**
  149. * Fires the lines added method on all listeners.
  150. *
  151. * @param index Index of the added line
  152. * @param size Number of lines added
  153. */
  154. protected void fireLinesAdded(final int index, final int size) {
  155. for (IRCDocumentListener listener
  156. : listeners.get(IRCDocumentListener.class)) {
  157. listener.linesAdded(index, size, lines.size());
  158. }
  159. trim(frameBufferSize);
  160. }
  161. /**
  162. * Fires the trimmed method on all listeners.
  163. *
  164. * @param newSize New document size
  165. * @param trimmedLines Number of trimmed lines
  166. */
  167. protected void fireTrimmed(final int newSize, final int trimmedLines) {
  168. for (IRCDocumentListener listener
  169. : listeners.get(IRCDocumentListener.class)) {
  170. listener.trimmed(newSize, trimmedLines);
  171. }
  172. }
  173. /**
  174. * fires the cleared method on all listeners.
  175. */
  176. protected void fireCleared() {
  177. listeners.get(IRCDocumentListener.class).forEach(IRCDocumentListener::cleared);
  178. }
  179. /**
  180. * fires the need repaint method on all listeners.
  181. */
  182. protected void fireRepaintNeeded() {
  183. listeners.get(IRCDocumentListener.class).forEach(IRCDocumentListener::repaintNeeded);
  184. }
  185. /**
  186. * Returns the line height of the specified line.
  187. *
  188. * @param line Line
  189. *
  190. * @return Line height
  191. */
  192. protected int getLineHeight(final Line line) {
  193. return line.getFontSize();
  194. }
  195. /**
  196. * Returns the line height of the specified.
  197. *
  198. * @param line Line
  199. *
  200. * @return Line height
  201. */
  202. public int getLineHeight(final int line) {
  203. return getLineHeight(getLine(line));
  204. }
  205. /**
  206. * Sets all the cached settings in this document.
  207. */
  208. private void setCachedSettings() {
  209. final Font defaultFont = UIManager.getFont("TextPane.font");
  210. if (configManager.hasOptionString("ui", "textPaneFontName")) {
  211. fontName = configManager.getOption("ui", "textPaneFontName");
  212. } else {
  213. fontName = defaultFont.getName();
  214. }
  215. if (configManager.hasOptionString("ui", "textPaneFontSize")) {
  216. fontSize = configManager.getOptionInt("ui", "textPaneFontSize");
  217. } else {
  218. fontSize = defaultFont.getSize();
  219. }
  220. frameBufferSize = configManager.getOptionInt("ui", "frameBufferSize", true);
  221. trim(frameBufferSize);
  222. }
  223. @Override
  224. public void configChanged(final String domain, final String key) {
  225. setCachedSettings();
  226. synchronized (lines) {
  227. for (Line line : lines) {
  228. line.setFontName(fontName);
  229. line.setFontSize(fontSize);
  230. }
  231. }
  232. fireRepaintNeeded();
  233. }
  234. }