您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

IRCDocument.java 11KB

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