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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.config.ConfigManager;
  24. import com.dmdirc.interfaces.ConfigChangeListener;
  25. import com.dmdirc.util.ListenerList;
  26. import com.dmdirc.util.RollingList;
  27. import java.awt.Font;
  28. import java.io.Serializable;
  29. import java.text.AttributedCharacterIterator;
  30. import java.text.AttributedString;
  31. import java.util.ArrayList;
  32. import java.util.List;
  33. import javax.swing.UIManager;
  34. /**
  35. * Data contained in a TextPane.
  36. */
  37. public class IRCDocument implements Serializable, ConfigChangeListener {
  38. /**
  39. * A version number for this class. It should be changed whenever the class
  40. * structure is changed (or anything else that would prevent serialized
  41. * objects being unserialized with the new class).
  42. */
  43. private static final long serialVersionUID = 4;
  44. /** List of lines of text. */
  45. private final List<Line> lines;
  46. /** Listener list. */
  47. private final ListenerList listeners;
  48. /** Cached lines. */
  49. private final RollingList<Line> cachedLines;
  50. /** Cached attributed strings. */
  51. private final RollingList<AttributedString> cachedStrings;
  52. /** Config Manager for getting settings. */
  53. private final ConfigManager configManager;
  54. /** This document's styliser. */
  55. private final Styliser styliser;
  56. /** Font size. */
  57. private int fontSize;
  58. /** Font name. */
  59. private String fontName;
  60. /** Frame buffer size. */
  61. private Integer frameBufferSize;
  62. /**
  63. * Creates a new instance of IRCDocument.
  64. *
  65. * @param configManager Config Manager for required settings.
  66. * @param styliser Styliser to style text
  67. * @since 0.6.3
  68. */
  69. public IRCDocument(final ConfigManager configManager,
  70. final Styliser styliser) {
  71. this.configManager = configManager;
  72. this.styliser = styliser;
  73. lines = new ArrayList<Line>();
  74. listeners = new ListenerList();
  75. cachedLines = new RollingList<Line>(50);
  76. cachedStrings = new RollingList<AttributedString>(50);
  77. frameBufferSize = configManager.getOptionInt("ui", "frameBufferSize");
  78. configManager.addChangeListener("ui", "textPaneFontSize", this);
  79. configManager.addChangeListener("ui", "textPaneFontName", this);
  80. configManager.addChangeListener("ui", "frameBufferSize", this);
  81. setCachedSettings();
  82. }
  83. /**
  84. * Returns the number of lines in this document.
  85. *
  86. * @return Number of lines
  87. */
  88. public int getNumLines() {
  89. synchronized (lines) {
  90. return lines.size();
  91. }
  92. }
  93. /**
  94. * Returns the Line at the specified number.
  95. *
  96. * @param lineNumber Line number to retrieve
  97. *
  98. * @return Line at the specified number or null
  99. */
  100. public Line getLine(final int lineNumber) {
  101. synchronized (lines) {
  102. return lines.get(lineNumber);
  103. }
  104. }
  105. /**
  106. * Adds the stylised string to the canvas.
  107. *
  108. * @param text stylised string to add to the text
  109. */
  110. public void addText(final String[] text) {
  111. final int index;
  112. synchronized (lines) {
  113. final Line line = new Line(styliser, text, fontSize, fontName);
  114. lines.add(line);
  115. index = lines.indexOf(line);
  116. }
  117. fireLineAdded(index);
  118. }
  119. /**
  120. * Adds the stylised string to the canvas.
  121. *
  122. * @param text stylised string to add to the text
  123. * @param lineHeight Line height for the new line of text
  124. */
  125. public void addText(final String[] text, final int lineHeight) {
  126. final int index;
  127. synchronized (lines) {
  128. final Line line = new Line(styliser, text, lineHeight, fontName);
  129. lines.add(line);
  130. index = lines.indexOf(line);
  131. }
  132. fireLineAdded(index);
  133. }
  134. /**
  135. * Adds the stylised string to the canvas.
  136. *
  137. * @param text stylised string to add to the text
  138. */
  139. public void addText(final List<String[]> text) {
  140. final int start;
  141. synchronized (lines) {
  142. start = lines.size();
  143. for (String[] string : text) {
  144. lines.add(new Line(styliser, string, fontSize, fontName));
  145. }
  146. }
  147. fireLinesAdded(start, text.size());
  148. }
  149. /**
  150. * Adds the stylised string to the canvas.
  151. *
  152. * @param text stylised string to add to the text
  153. * @param lineHeights line heights for the new lines
  154. */
  155. public void addText(final List<String[]> text,
  156. final List<Integer> lineHeights) {
  157. final int start;
  158. synchronized (lines) {
  159. start = lines.size();
  160. for (int i = 0; i < text.size(); i++) {
  161. final String[] string = text.get(i);
  162. final int lineHeight = lineHeights.get(i);
  163. lines.add(new Line(styliser, string, lineHeight, fontName));
  164. }
  165. }
  166. fireLinesAdded(start, text.size());
  167. }
  168. /**
  169. * Trims the document to the specified number of lines.
  170. *
  171. * @param numLines Number of lines to trim the document to
  172. */
  173. public void trim(final int numLines) {
  174. synchronized (lines) {
  175. if (frameBufferSize != null && frameBufferSize > 0) {
  176. int i = 0;
  177. while (lines.size() > numLines) {
  178. i++;
  179. lines.remove(0);
  180. }
  181. if (i > 0) {
  182. fireTrimmed(numLines, i);
  183. }
  184. }
  185. }
  186. }
  187. /** Clears all lines from the document. */
  188. public void clear() {
  189. synchronized (lines) {
  190. lines.clear();
  191. }
  192. fireCleared();
  193. }
  194. /**
  195. * Adds a IRCDocumentListener to the listener list.
  196. *
  197. * @param listener Listener to add
  198. */
  199. public void addIRCDocumentListener(final IRCDocumentListener listener) {
  200. if (listener == null) {
  201. return;
  202. }
  203. listeners.add(IRCDocumentListener.class, listener);
  204. }
  205. /**
  206. * Removes a IRCDocumentListener from the listener list.
  207. *
  208. * @param listener Listener to remove
  209. */
  210. public void removeIRCDocumentListener(final IRCDocumentListener listener) {
  211. listeners.remove(IRCDocumentListener.class, listener);
  212. }
  213. /**
  214. * Fires the line added method on all listeners.
  215. *
  216. * @param index Index of the added line
  217. */
  218. protected void fireLineAdded(final int index) {
  219. for (IRCDocumentListener listener
  220. : listeners.get(IRCDocumentListener.class)) {
  221. listener.lineAdded(index, lines.size());
  222. }
  223. trim(frameBufferSize);
  224. }
  225. /**
  226. * Fires the lines added method on all listeners.
  227. *
  228. * @param index Index of the added line
  229. * @param size Number of lines added
  230. */
  231. protected void fireLinesAdded(final int index, final int size) {
  232. for (IRCDocumentListener listener
  233. : listeners.get(IRCDocumentListener.class)) {
  234. listener.linesAdded(index, size, lines.size());
  235. }
  236. trim(frameBufferSize);
  237. }
  238. /**
  239. * Fires the trimmed method on all listeners.
  240. *
  241. * @param newSize New document size
  242. * @param trimedLines Number of trimmed lines
  243. */
  244. protected void fireTrimmed(final int newSize, final int trimedLines) {
  245. for (IRCDocumentListener listener
  246. : listeners.get(IRCDocumentListener.class)) {
  247. listener.trimmed(newSize, trimedLines);
  248. }
  249. }
  250. /**
  251. * fires the cleared method on all listeners.
  252. */
  253. protected void fireCleared() {
  254. for (IRCDocumentListener listener
  255. : listeners.get(IRCDocumentListener.class)) {
  256. listener.cleared();
  257. }
  258. }
  259. /**
  260. * fires the need repaint method on all listeners.
  261. */
  262. protected void fireRepaintNeeded() {
  263. for (IRCDocumentListener listener
  264. : listeners.get(IRCDocumentListener.class)) {
  265. listener.repaintNeeded();
  266. }
  267. }
  268. /**
  269. * Returns an attributed character iterator for a particular line,
  270. * utilising the document cache where possible.
  271. *
  272. * @param line Line to be styled
  273. *
  274. * @return Styled line
  275. */
  276. protected AttributedCharacterIterator getStyledLine(final Line line) {
  277. synchronized (lines) {
  278. AttributedString styledLine = null;
  279. if (cachedLines.contains(line)) {
  280. final int index = cachedLines.getList().indexOf(line);
  281. styledLine = cachedStrings.get(index);
  282. }
  283. if (styledLine == null) {
  284. styledLine = line.getStyled();
  285. cachedLines.add(line);
  286. cachedStrings.add(styledLine);
  287. }
  288. return styledLine.getIterator();
  289. }
  290. }
  291. /**
  292. * Returns an attributed string for a particular line, utilising the
  293. * document cache where possible.
  294. *
  295. * @param line Line number to be styled
  296. *
  297. * @return Styled line
  298. */
  299. public AttributedCharacterIterator getStyledLine(final int line) {
  300. return getStyledLine(getLine(line));
  301. }
  302. /**
  303. * Returns the line height of the specified line.
  304. *
  305. * @param line Line
  306. *
  307. * @return Line height
  308. */
  309. protected int getLineHeight(final Line line) {
  310. return line.getFontSize();
  311. }
  312. /**
  313. * Returns the line height of the specified.
  314. *
  315. * @param line Line
  316. *
  317. * @return Line height
  318. */
  319. public int getLineHeight(final int line) {
  320. return getLineHeight(getLine(line));
  321. }
  322. /**
  323. * Sets all the cached settings in this document.
  324. */
  325. private void setCachedSettings() {
  326. final Font defaultFont = UIManager.getFont("TextPane.font");
  327. if (configManager.hasOptionString("ui", "textPaneFontName")) {
  328. fontName = configManager.getOption("ui", "textPaneFontName");
  329. } else {
  330. fontName = defaultFont.getName();
  331. }
  332. if (configManager.hasOptionString("ui", "textPaneFontSize")) {
  333. fontSize = configManager.getOptionInt("ui", "textPaneFontSize");
  334. } else {
  335. fontSize = defaultFont.getSize();
  336. }
  337. frameBufferSize = configManager.getOptionInt("ui", "frameBufferSize");
  338. trim(frameBufferSize);
  339. }
  340. /** {@inheritDoc} */
  341. @Override
  342. public void configChanged(final String domain, final String key) {
  343. setCachedSettings();
  344. cachedLines.clear();
  345. cachedStrings.clear();
  346. synchronized (lines) {
  347. for (Line line : lines) {
  348. line.setFontName(fontName);
  349. line.setFontSize(fontSize);
  350. }
  351. }
  352. fireRepaintNeeded();
  353. }
  354. }