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.

WrapEditorKit.java 6.1KB

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