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.

TextPaneCanvas.java 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.ui_swing.textpane;
  18. import com.dmdirc.addons.ui_swing.UIUtilities;
  19. import com.dmdirc.addons.ui_swing.textpane.LineRenderer.RenderResult;
  20. import com.dmdirc.config.provider.AggregateConfigProvider;
  21. import com.dmdirc.config.provider.ConfigChangeListener;
  22. import com.dmdirc.ui.messages.CachingDocument;
  23. import com.dmdirc.ui.messages.IRCTextAttribute;
  24. import com.dmdirc.ui.messages.LinePosition;
  25. import com.dmdirc.util.StringUtils;
  26. import com.dmdirc.util.collections.ListenerList;
  27. import java.awt.Cursor;
  28. import java.awt.Graphics;
  29. import java.awt.Graphics2D;
  30. import java.awt.Point;
  31. import java.awt.Rectangle;
  32. import java.awt.Toolkit;
  33. import java.awt.event.AdjustmentEvent;
  34. import java.awt.event.AdjustmentListener;
  35. import java.awt.event.ComponentEvent;
  36. import java.awt.event.ComponentListener;
  37. import java.awt.event.InputEvent;
  38. import java.awt.event.MouseEvent;
  39. import java.awt.font.TextHitInfo;
  40. import java.awt.font.TextLayout;
  41. import java.awt.geom.Rectangle2D;
  42. import java.text.AttributedCharacterIterator;
  43. import java.text.AttributedString;
  44. import java.util.HashMap;
  45. import java.util.Map;
  46. import javax.swing.JPanel;
  47. import javax.swing.SwingUtilities;
  48. import javax.swing.ToolTipManager;
  49. import javax.swing.event.MouseInputListener;
  50. /** Canvas object to draw text. */
  51. class TextPaneCanvas extends JPanel implements MouseInputListener,
  52. ComponentListener, AdjustmentListener, ConfigChangeListener {
  53. /** A version number for this class. */
  54. private static final long serialVersionUID = 8;
  55. /** Hand cursor. */
  56. private static final Cursor HAND_CURSOR = new Cursor(Cursor.HAND_CURSOR);
  57. /** Single Side padding for textpane. */
  58. private static final int SINGLE_SIDE_PADDING = 3;
  59. /** Both Side padding for textpane. */
  60. private static final int DOUBLE_SIDE_PADDING = SINGLE_SIDE_PADDING * 2;
  61. /** IRCDocument. */
  62. private final CachingDocument<AttributedString> document;
  63. /** parent textpane. */
  64. private final TextPane textPane;
  65. /** Position -> LineInfo. */
  66. private final Map<LineInfo, Rectangle2D.Float> lineAreas;
  67. /** TextLayout -> Line numbers. */
  68. private final Map<LineInfo, TextLayout> lineLayouts;
  69. /** Start line. */
  70. private int startLine;
  71. /** Selection. */
  72. private LinePosition selection;
  73. /** First visible line (from the top). */
  74. private int firstVisibleLine;
  75. /** Last visible line (from the top). */
  76. private int lastVisibleLine;
  77. /** Config Manager. */
  78. private final AggregateConfigProvider manager;
  79. /** Quick copy? */
  80. private boolean quickCopy;
  81. /** Mouse click listeners. */
  82. private final ListenerList listeners = new ListenerList();
  83. /** Renderer to use for lines. */
  84. private final LineRenderer lineRenderer;
  85. /**
  86. * Creates a new text pane canvas.
  87. *
  88. * @param parent parent text pane for the canvas
  89. * @param document IRCDocument to be displayed
  90. */
  91. public TextPaneCanvas(final TextPane parent, final CachingDocument<AttributedString> document) {
  92. this.document = document;
  93. textPane = parent;
  94. this.manager = parent.getWindow().getConfigManager();
  95. this.lineRenderer = new BasicTextLineRenderer(textPane, this, document);
  96. startLine = 0;
  97. setDoubleBuffered(true);
  98. setOpaque(true);
  99. lineLayouts = new HashMap<>();
  100. lineAreas = new HashMap<>();
  101. selection = new LinePosition(-1, -1, -1, -1);
  102. addMouseListener(this);
  103. addMouseMotionListener(this);
  104. addComponentListener(this);
  105. manager.addChangeListener("ui", "quickCopy", this);
  106. updateCachedSettings();
  107. ToolTipManager.sharedInstance().registerComponent(this);
  108. }
  109. /**
  110. * Paints the text onto the canvas.
  111. *
  112. * @param graphics graphics object to draw onto
  113. */
  114. @Override
  115. public void paintComponent(final Graphics graphics) {
  116. final Graphics2D g = (Graphics2D) graphics;
  117. final Map<?, ?> desktopHints = (Map<?, ?>) Toolkit.getDefaultToolkit().
  118. getDesktopProperty("awt.font.desktophints");
  119. if (desktopHints != null) {
  120. g.addRenderingHints(desktopHints);
  121. }
  122. paintOntoGraphics(g);
  123. }
  124. /**
  125. * Re calculates positions of lines and repaints if required.
  126. */
  127. protected void recalc() {
  128. if (isVisible()) {
  129. repaint();
  130. }
  131. }
  132. /**
  133. * Updates cached config settings.
  134. */
  135. private void updateCachedSettings() {
  136. quickCopy = manager.getOptionBool("ui", "quickCopy");
  137. UIUtilities.invokeLater(this::recalc);
  138. }
  139. private void paintOntoGraphics(final Graphics2D g) {
  140. final float formatWidth = getWidth() - DOUBLE_SIDE_PADDING;
  141. final float formatHeight = getHeight();
  142. float drawPosY = formatHeight - DOUBLE_SIDE_PADDING;
  143. lineLayouts.clear();
  144. lineAreas.clear();
  145. //check theres something to draw and theres some space to draw in
  146. if (document.getNumLines() == 0 || formatWidth < 1) {
  147. setCursor(Cursor.getDefaultCursor());
  148. return;
  149. }
  150. // Check the start line is in range
  151. startLine = Math.max(0, Math.min(document.getNumLines() - 1, startLine));
  152. //sets the last visible line
  153. lastVisibleLine = startLine;
  154. firstVisibleLine = startLine;
  155. // Iterate through the lines
  156. for (int line = startLine; line >= 0; line--) {
  157. drawPosY = paintLineOntoGraphics(g, formatWidth, formatHeight, drawPosY, line);
  158. if (drawPosY <= 0) {
  159. break;
  160. }
  161. }
  162. checkForLink();
  163. }
  164. private float paintLineOntoGraphics(final Graphics2D g, final float formatWidth,
  165. final float formatHeight, final float drawPosY, final int line) {
  166. final RenderResult result = lineRenderer.render(g, formatWidth, formatHeight, drawPosY,
  167. line);
  168. lineAreas.putAll(result.drawnAreas);
  169. lineLayouts.putAll(result.textLayouts);
  170. firstVisibleLine = result.firstVisibleLine;
  171. return drawPosY - result.totalHeight;
  172. }
  173. @Override
  174. public void adjustmentValueChanged(final AdjustmentEvent e) {
  175. if (startLine != e.getValue()) {
  176. startLine = e.getValue();
  177. recalc();
  178. }
  179. }
  180. @Override
  181. public void mouseClicked(final MouseEvent e) {
  182. final LineInfo lineInfo = getClickPosition(getMousePosition(), true);
  183. fireMouseEvents(getClickType(lineInfo), MouseEventType.CLICK, e);
  184. if (lineInfo.getLine() != -1) {
  185. final String clickedText = document.getLine(lineInfo.getLine()).getText();
  186. final int start;
  187. final int end;
  188. if (lineInfo.getIndex() == -1) {
  189. start = -1;
  190. end = -1;
  191. } else {
  192. final int[] extent = StringUtils.indiciesOfWord(clickedText, lineInfo.getIndex());
  193. start = extent[0];
  194. end = extent[1];
  195. }
  196. if (e.getClickCount() == 2) {
  197. setSelection(lineInfo.getLine(), start, end, e.isShiftDown());
  198. } else if (e.getClickCount() == 3) {
  199. setSelection(lineInfo.getLine(), 0, clickedText.length(), e.isShiftDown());
  200. }
  201. }
  202. }
  203. /**
  204. * Sets the selection to a range of characters on the specified line. If quick copy is enabled,
  205. * the selection will be copied.
  206. *
  207. * @param line The line of the selection
  208. * @param start The start of the selection
  209. * @param end The end of the selection
  210. * @param copyControlCharacters Whether or not to copy control characters.
  211. */
  212. private void setSelection(final int line, final int start, final int end,
  213. final boolean copyControlCharacters) {
  214. selection.setStartLine(line);
  215. selection.setEndLine(line);
  216. selection.setStartPos(start);
  217. selection.setEndPos(end);
  218. if (quickCopy) {
  219. textPane.copy(copyControlCharacters);
  220. clearSelection();
  221. }
  222. }
  223. /**
  224. * Returns the type of text this click represents.
  225. *
  226. * @param lineInfo Line info of click.
  227. *
  228. * @return Click type for specified position
  229. */
  230. public ClickTypeValue getClickType(final LineInfo lineInfo) {
  231. if (lineInfo.getLine() != -1) {
  232. final AttributedCharacterIterator iterator = document.getStyledLine(
  233. lineInfo.getLine()).getIterator();
  234. final int index = lineInfo.getIndex();
  235. if (index >= iterator.getBeginIndex() && index <= iterator.getEndIndex()) {
  236. iterator.setIndex(lineInfo.getIndex());
  237. final Object linkAttribute =
  238. iterator.getAttributes().get(IRCTextAttribute.HYPERLINK);
  239. if (linkAttribute instanceof String) {
  240. return new ClickTypeValue(ClickType.HYPERLINK, (String) linkAttribute);
  241. }
  242. final Object channelAttribute =
  243. iterator.getAttributes().get(IRCTextAttribute.CHANNEL);
  244. if (channelAttribute instanceof String) {
  245. return new ClickTypeValue(ClickType.CHANNEL, (String) channelAttribute);
  246. }
  247. final Object nickAttribute =
  248. iterator.getAttributes().get(IRCTextAttribute.NICKNAME);
  249. if (nickAttribute instanceof String) {
  250. return new ClickTypeValue(ClickType.NICKNAME, (String) nickAttribute);
  251. }
  252. } else {
  253. return new ClickTypeValue(ClickType.NORMAL, "");
  254. }
  255. }
  256. return new ClickTypeValue(ClickType.NORMAL, "");
  257. }
  258. @Override
  259. public void mousePressed(final MouseEvent e) {
  260. fireMouseEvents(getClickType(getClickPosition(e.getPoint(), false)),
  261. MouseEventType.PRESSED, e);
  262. if (e.getButton() == MouseEvent.BUTTON1) {
  263. highlightEvent(MouseEventType.CLICK, e);
  264. }
  265. }
  266. @Override
  267. public void mouseReleased(final MouseEvent e) {
  268. fireMouseEvents(getClickType(getClickPosition(e.getPoint(), false)),
  269. MouseEventType.RELEASED, e);
  270. if (quickCopy) {
  271. textPane.copy((e.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK);
  272. SwingUtilities.invokeLater(this::clearSelection);
  273. }
  274. if (e.getButton() == MouseEvent.BUTTON1) {
  275. highlightEvent(MouseEventType.RELEASED, e);
  276. }
  277. }
  278. @Override
  279. public void mouseDragged(final MouseEvent e) {
  280. if (e.getModifiersEx() == InputEvent.BUTTON1_DOWN_MASK) {
  281. highlightEvent(MouseEventType.DRAG, e);
  282. }
  283. }
  284. @Override
  285. public void mouseEntered(final MouseEvent e) {
  286. //Ignore
  287. }
  288. @Override
  289. public void mouseExited(final MouseEvent e) {
  290. //Ignore
  291. }
  292. @Override
  293. public void mouseMoved(final MouseEvent e) {
  294. checkForLink();
  295. }
  296. /** Checks for a link under the cursor and sets appropriately. */
  297. private void checkForLink() {
  298. final AttributedCharacterIterator iterator = getIterator(getMousePosition());
  299. if (iterator != null
  300. && (iterator.getAttribute(IRCTextAttribute.HYPERLINK) != null
  301. || iterator.getAttribute(IRCTextAttribute.CHANNEL) != null
  302. || iterator.getAttribute(IRCTextAttribute.NICKNAME) != null)) {
  303. setCursor(HAND_CURSOR);
  304. return;
  305. }
  306. if (getCursor() == HAND_CURSOR) {
  307. setCursor(Cursor.getDefaultCursor());
  308. }
  309. }
  310. /**
  311. * Retrieves a character iterator for the text at the specified mouse position.
  312. *
  313. * @since 0.6.4
  314. * @param mousePosition The mouse position to retrieve text for
  315. *
  316. * @return A corresponding character iterator, or null if the specified mouse position doesn't
  317. * correspond to any text
  318. */
  319. private AttributedCharacterIterator getIterator(final Point mousePosition) {
  320. final LineInfo lineInfo = getClickPosition(mousePosition, false);
  321. if (lineInfo.getLine() != -1
  322. && document.getLine(lineInfo.getLine()) != null) {
  323. final AttributedCharacterIterator iterator =
  324. document.getStyledLine(lineInfo.getLine()).getIterator();
  325. if (lineInfo.getIndex() < iterator.getBeginIndex()
  326. || lineInfo.getIndex() > iterator.getEndIndex()) {
  327. return null;
  328. }
  329. iterator.setIndex(lineInfo.getIndex());
  330. return iterator;
  331. }
  332. return null;
  333. }
  334. /**
  335. * Sets the selection for the given event.
  336. *
  337. * @param type mouse event type
  338. * @param e responsible mouse event
  339. */
  340. protected void highlightEvent(final MouseEventType type, final MouseEvent e) {
  341. if (isVisible()) {
  342. final Point point = e.getLocationOnScreen();
  343. SwingUtilities.convertPointFromScreen(point, this);
  344. if (!contains(point)) {
  345. final Rectangle bounds = getBounds();
  346. final Point mousePos = e.getPoint();
  347. if (mousePos.getX() < bounds.getX()) {
  348. point.setLocation(bounds.getX() + SINGLE_SIDE_PADDING, point.getY());
  349. } else if (mousePos.getX() > bounds.getX() + bounds.getWidth()) {
  350. point.setLocation(bounds.getX() + bounds.getWidth()
  351. - SINGLE_SIDE_PADDING, point.getY());
  352. }
  353. if (mousePos.getY() < bounds.getY()) {
  354. point.setLocation(point.getX(), bounds.getY() + DOUBLE_SIDE_PADDING);
  355. } else if (mousePos.getY() > bounds.getY() + bounds.getHeight()) {
  356. point.setLocation(bounds.getX() + bounds.getWidth() - SINGLE_SIDE_PADDING,
  357. bounds.getY() + bounds.getHeight() - DOUBLE_SIDE_PADDING - 1);
  358. }
  359. }
  360. LineInfo info = getClickPosition(point, true);
  361. // TODO: These are fairly expensive if the user is moving around a lot; cache them.
  362. final Rectangle2D.Float first = getFirstLineRectangle();
  363. final Rectangle2D.Float last = getLastLineRectangle();
  364. if (info.getLine() == -1 && info.getPart() == -1 && contains(point)
  365. && document.getNumLines() != 0 && first != null && last != null) {
  366. if (first.getY() >= point.getY()) {
  367. info = getFirstLineInfo();
  368. } else if (last.getY() <= point.getY()) {
  369. info = getLastLineInfo();
  370. }
  371. }
  372. if (info.getLine() != -1 && info.getPart() != -1) {
  373. if (type == MouseEventType.CLICK) {
  374. selection.setStartLine(info.getLine());
  375. selection.setStartPos(info.getIndex());
  376. }
  377. selection.setEndLine(info.getLine());
  378. selection.setEndPos(info.getIndex());
  379. recalc();
  380. }
  381. }
  382. }
  383. /**
  384. * Returns the visible rectangle of the first line.
  385. *
  386. * @return First line's rectangle
  387. */
  388. private Rectangle2D.Float getFirstLineRectangle() {
  389. for (Map.Entry<LineInfo, Rectangle2D.Float> entry : lineAreas.entrySet()) {
  390. if (entry.getKey().getLine() == firstVisibleLine) {
  391. return entry.getValue();
  392. }
  393. }
  394. return null;
  395. }
  396. /**
  397. * Returns the last line's visible rectangle.
  398. *
  399. * @return Last line's rectangle
  400. */
  401. private Rectangle2D.Float getLastLineRectangle() {
  402. for (Map.Entry<LineInfo, Rectangle2D.Float> entry : lineAreas.entrySet()) {
  403. if (entry.getKey().getLine() == lastVisibleLine) {
  404. return entry.getValue();
  405. }
  406. }
  407. return null;
  408. }
  409. /**
  410. * Returns the LineInfo for the first visible line.
  411. *
  412. * @return First line's line info
  413. */
  414. private LineInfo getFirstLineInfo() {
  415. int firstLineParts = Integer.MAX_VALUE;
  416. for (LineInfo info : lineAreas.keySet()) {
  417. if (info.getLine() == firstVisibleLine && info.getPart() < firstLineParts) {
  418. firstLineParts = info.getPart();
  419. }
  420. }
  421. return new LineInfo(firstVisibleLine, firstLineParts);
  422. }
  423. /**
  424. * Returns the LineInfo for the last visible line.
  425. *
  426. * @return Last line's line info
  427. */
  428. private LineInfo getLastLineInfo() {
  429. int lastLineParts = -1;
  430. for (LineInfo info : lineAreas.keySet()) {
  431. if (info.getLine() == lastVisibleLine && info.getPart() > lastLineParts) {
  432. lastLineParts = info.getPart();
  433. }
  434. }
  435. return new LineInfo(lastVisibleLine + 1, lastLineParts);
  436. }
  437. /**
  438. *
  439. * Returns the line information from a mouse click inside the textpane.
  440. *
  441. * @param point mouse position
  442. * @param selection Are we selecting text?
  443. *
  444. * @return line number, line part, position in whole line
  445. */
  446. public LineInfo getClickPosition(final Point point, final boolean selection) {
  447. int lineNumber = -1;
  448. int linePart = -1;
  449. int pos = 0;
  450. if (point != null) {
  451. for (Map.Entry<LineInfo, Rectangle2D.Float> entry : lineAreas.entrySet()) {
  452. if (entry.getValue().contains(point)) {
  453. lineNumber = entry.getKey().getLine();
  454. linePart = entry.getKey().getPart();
  455. }
  456. }
  457. pos = getHitPosition(lineNumber, linePart, (int) point.getX(), (int) point.getY(),
  458. selection);
  459. }
  460. return new LineInfo(lineNumber, linePart, pos);
  461. }
  462. /**
  463. * Returns the character index for a specified line and part for a specific hit position.
  464. *
  465. * @param lineNumber Line number
  466. * @param linePart Line part
  467. * @param x X position
  468. * @param y Y position
  469. *
  470. * @return Hit position
  471. */
  472. private int getHitPosition(final int lineNumber, final int linePart,
  473. final float x, final float y, final boolean selection) {
  474. int pos = 0;
  475. for (Map.Entry<LineInfo, TextLayout> entry : lineLayouts.entrySet()) {
  476. if (entry.getKey().getLine() == lineNumber) {
  477. if (entry.getKey().getPart() < linePart) {
  478. pos += entry.getValue().getCharacterCount();
  479. } else if (entry.getKey().getPart() == linePart) {
  480. final TextHitInfo hit =
  481. entry.getValue().hitTestChar(x - DOUBLE_SIDE_PADDING, y);
  482. if (selection || x > entry.getValue().getBounds().getX()) {
  483. pos += hit.getInsertionIndex();
  484. } else {
  485. pos += hit.getCharIndex();
  486. }
  487. }
  488. }
  489. }
  490. return pos;
  491. }
  492. /**
  493. * Returns the selected range info.
  494. *
  495. * @return Selected range info
  496. */
  497. protected LinePosition getSelectedRange() {
  498. return selection.getNormalised();
  499. }
  500. /** Clears the selection. */
  501. protected void clearSelection() {
  502. selection.setEndLine(selection.getStartLine());
  503. selection.setEndPos(selection.getStartPos());
  504. recalc();
  505. }
  506. /**
  507. * Selects the specified region of text.
  508. *
  509. * @param position Line position
  510. */
  511. public void setSelectedRange(final LinePosition position) {
  512. selection = new LinePosition(position);
  513. recalc();
  514. }
  515. /**
  516. * Returns the first visible line.
  517. *
  518. * @return the line number of the first visible line
  519. */
  520. public int getFirstVisibleLine() {
  521. return firstVisibleLine;
  522. }
  523. /**
  524. * Returns the last visible line.
  525. *
  526. * @return the line number of the last visible line
  527. */
  528. public int getLastVisibleLine() {
  529. return lastVisibleLine;
  530. }
  531. /**
  532. * Returns the number of visible lines.
  533. *
  534. * @return Number of visible lines
  535. */
  536. public int getNumVisibleLines() {
  537. return lastVisibleLine - firstVisibleLine;
  538. }
  539. @Override
  540. public void componentResized(final ComponentEvent e) {
  541. recalc();
  542. }
  543. @Override
  544. public void componentMoved(final ComponentEvent e) {
  545. //Ignore
  546. }
  547. @Override
  548. public void componentShown(final ComponentEvent e) {
  549. //Ignore
  550. }
  551. @Override
  552. public void componentHidden(final ComponentEvent e) {
  553. //Ignore
  554. }
  555. @Override
  556. public void configChanged(final String domain, final String key) {
  557. updateCachedSettings();
  558. }
  559. @Override
  560. public String getToolTipText(final MouseEvent event) {
  561. final AttributedCharacterIterator iterator = getIterator(
  562. event.getPoint());
  563. if (iterator != null
  564. && iterator.getAttribute(IRCTextAttribute.TOOLTIP) != null) {
  565. return iterator.getAttribute(IRCTextAttribute.TOOLTIP).toString();
  566. }
  567. return super.getToolTipText(event);
  568. }
  569. /**
  570. * Fires mouse clicked events with the associated values.
  571. *
  572. * @param clickType Click type
  573. * @param eventType Mouse event type
  574. * @param event Triggering mouse event
  575. */
  576. private void fireMouseEvents(final ClickTypeValue clickType,
  577. final MouseEventType eventType, final MouseEvent event) {
  578. for (TextPaneListener listener : listeners.get(TextPaneListener.class)) {
  579. listener.mouseClicked(clickType, eventType, event);
  580. }
  581. }
  582. /**
  583. * Adds a textpane listener.
  584. *
  585. * @param listener Listener to add
  586. */
  587. public void addTextPaneListener(final TextPaneListener listener) {
  588. listeners.add(TextPaneListener.class, listener);
  589. }
  590. /**
  591. * Removes a textpane listener.
  592. *
  593. * @param listener Listener to remove
  594. */
  595. public void removeTextPaneListener(final TextPaneListener listener) {
  596. listeners.remove(TextPaneListener.class, listener);
  597. }
  598. }