Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

WrapEditorKit.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * @author Stanislav Lapitsky
  3. * @version 1.0
  4. *
  5. * Extended for Hyperlink events
  6. */
  7. package com.dmdirc.addons.ui_swing.components.text;
  8. import com.dmdirc.events.LinkChannelClickedEvent;
  9. import com.dmdirc.events.LinkNicknameClickedEvent;
  10. import com.dmdirc.events.LinkUrlClickedEvent;
  11. import com.dmdirc.interfaces.EventBus;
  12. import com.dmdirc.interfaces.WindowModel;
  13. import com.dmdirc.ui.messages.IRCTextAttribute;
  14. import java.awt.Cursor;
  15. import java.awt.event.MouseEvent;
  16. import java.awt.event.MouseListener;
  17. import java.awt.event.MouseMotionListener;
  18. import javax.swing.JEditorPane;
  19. import javax.swing.SwingUtilities;
  20. import javax.swing.text.Element;
  21. import javax.swing.text.StyledDocument;
  22. import javax.swing.text.StyledEditorKit;
  23. import javax.swing.text.ViewFactory;
  24. /**
  25. * @author Stanislav Lapitsky
  26. * @version 1.0
  27. *
  28. * Extended for Hyperlink events
  29. */
  30. public class WrapEditorKit extends StyledEditorKit implements MouseListener, MouseMotionListener {
  31. /** A version number for this class. */
  32. private static final long serialVersionUID = 1;
  33. /** Wrap column factory. */
  34. private final ViewFactory defaultFactory = new WrapColumnFactory();
  35. /** Hand cursor. */
  36. private static final Cursor HAND_CURSOR = new Cursor(Cursor.HAND_CURSOR);
  37. /** Are we wrapping text? */
  38. private final boolean wrap;
  39. /** Associated Component. */
  40. private JEditorPane editorPane;
  41. /** Event bus to fire link click events on. */
  42. private final EventBus eventBus;
  43. /** The window this editor kit is used in. */
  44. private final WindowModel window;
  45. /**
  46. * Initialises a new wrapping editor kit.
  47. *
  48. * @param wrapping true iif the text needs to wrap
  49. * @param eventBus Event bus to raise hyperlink events on
  50. * @param window Window as source for hyperlink events
  51. */
  52. public WrapEditorKit(final boolean wrapping, final EventBus eventBus, final WindowModel window) {
  53. this.window = window;
  54. this.eventBus = eventBus;
  55. wrap = wrapping;
  56. }
  57. @Override
  58. public void install(final JEditorPane c) {
  59. super.install(c);
  60. editorPane = c;
  61. c.addMouseListener(this);
  62. c.addMouseMotionListener(this);
  63. }
  64. @Override
  65. public void deinstall(final JEditorPane c) {
  66. c.removeMouseListener(this);
  67. c.removeMouseMotionListener(this);
  68. editorPane = null;
  69. super.deinstall(c);
  70. }
  71. @Override
  72. public ViewFactory getViewFactory() {
  73. if (wrap) {
  74. return super.getViewFactory();
  75. } else {
  76. return defaultFactory;
  77. }
  78. }
  79. @Override
  80. public void mouseMoved(final MouseEvent e) {
  81. if (editorPane == null) {
  82. return;
  83. }
  84. if (!editorPane.isEditable() && (characterElementAt(e).getAttributes()
  85. .getAttribute(IRCTextAttribute.HYPERLINK) != null
  86. || characterElementAt(e).getAttributes().getAttribute(
  87. IRCTextAttribute.CHANNEL) != null
  88. || characterElementAt(e).getAttributes().getAttribute(
  89. IRCTextAttribute.NICKNAME) != null)) {
  90. editorPane.setCursor(HAND_CURSOR);
  91. return;
  92. }
  93. editorPane.setCursor(Cursor.getDefaultCursor());
  94. }
  95. @Override
  96. public void mouseReleased(final MouseEvent e) {
  97. if (!SwingUtilities.isLeftMouseButton(e) || editorPane == null) {
  98. return;
  99. }
  100. if (!editorPane.isEditable()) {
  101. Object target = characterElementAt(e).getAttributes().getAttribute(
  102. IRCTextAttribute.HYPERLINK);
  103. if (target != null) {
  104. eventBus.publishAsync(new LinkUrlClickedEvent(window, (String) target));
  105. }
  106. target = characterElementAt(e).getAttributes().getAttribute(IRCTextAttribute.CHANNEL);
  107. if (target != null) {
  108. eventBus.publishAsync(new LinkChannelClickedEvent(window, (String) target));
  109. }
  110. target = characterElementAt(e).getAttributes().getAttribute(IRCTextAttribute.NICKNAME);
  111. if (target != null) {
  112. eventBus.publishAsync(new LinkNicknameClickedEvent(window, (String) target));
  113. }
  114. }
  115. }
  116. @Override
  117. public void mouseClicked(final MouseEvent e) {
  118. //Ignore
  119. }
  120. @Override
  121. public void mousePressed(final MouseEvent e) {
  122. //Ignore
  123. }
  124. @Override
  125. public void mouseEntered(final MouseEvent e) {
  126. //Ignore
  127. }
  128. @Override
  129. public void mouseExited(final MouseEvent e) {
  130. //Ignore
  131. }
  132. @Override
  133. public void mouseDragged(final MouseEvent e) {
  134. //Ignore
  135. }
  136. /**
  137. * Returns the character element for the position of the mouse event.
  138. *
  139. * @param e Mouse event to get position from
  140. *
  141. * @return Character element at mouse event
  142. */
  143. private Element characterElementAt(final MouseEvent e) {
  144. return ((StyledDocument) editorPane.getDocument()).getCharacterElement(
  145. editorPane.getUI().viewToModel(editorPane, e.getPoint()));
  146. }
  147. }