Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

TextPaneCanvas.java 23KB

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