|
@@ -1,25 +1,169 @@
|
1
|
1
|
/*
|
2
|
2
|
* @author Stanislav Lapitsky
|
3
|
3
|
* @version 1.0
|
|
4
|
+ *
|
|
5
|
+ * Extended for Hyperlink events
|
4
|
6
|
*/
|
5
|
7
|
|
6
|
8
|
package com.dmdirc.addons.ui_swing.components.text;
|
7
|
9
|
|
|
10
|
+import com.dmdirc.ui.messages.IRCTextAttribute;
|
|
11
|
+
|
|
12
|
+import java.awt.Cursor;
|
|
13
|
+import java.awt.event.MouseEvent;
|
|
14
|
+import java.awt.event.MouseListener;
|
|
15
|
+import java.awt.event.MouseMotionListener;
|
|
16
|
+
|
|
17
|
+import javax.swing.JEditorPane;
|
|
18
|
+import javax.swing.SwingUtilities;
|
|
19
|
+import javax.swing.event.HyperlinkEvent;
|
|
20
|
+import javax.swing.text.Element;
|
|
21
|
+import javax.swing.text.StyledDocument;
|
8
|
22
|
import javax.swing.text.StyledEditorKit;
|
9
|
23
|
import javax.swing.text.ViewFactory;
|
10
|
24
|
|
11
|
25
|
/**
|
12
|
26
|
* @author Stanislav Lapitsky
|
13
|
27
|
* @version 1.0
|
|
28
|
+ *
|
|
29
|
+ * Extended for Hyperlink events
|
14
|
30
|
*/
|
15
|
|
-public class WrapEditorKit extends StyledEditorKit {
|
|
31
|
+public class WrapEditorKit extends StyledEditorKit implements MouseListener,
|
|
32
|
+ MouseMotionListener {
|
16
|
33
|
|
17
|
34
|
private static final long serialVersionUID = 1;
|
|
35
|
+ /** Wrap column factory. */
|
18
|
36
|
private ViewFactory defaultFactory = new WrapColumnFactory();
|
|
37
|
+ /** Hand cursor. */
|
|
38
|
+ private static final Cursor HAND_CURSOR = new Cursor(Cursor.HAND_CURSOR);
|
|
39
|
+ /** Associated Component. */
|
|
40
|
+ private JEditorPane editorPane;
|
|
41
|
+
|
|
42
|
+ /** {@inheritDoc} */
|
|
43
|
+ @Override
|
|
44
|
+ public void install(final JEditorPane c) {
|
|
45
|
+ super.install(c);
|
|
46
|
+ editorPane = c;
|
|
47
|
+ c.addMouseListener(this);
|
|
48
|
+ c.addMouseMotionListener(this);
|
|
49
|
+ }
|
|
50
|
+
|
|
51
|
+ /** {@inheritDoc} */
|
|
52
|
+ @Override
|
|
53
|
+ public void deinstall(final JEditorPane c) {
|
|
54
|
+ c.removeMouseListener(this);
|
|
55
|
+ c.removeMouseMotionListener(this);
|
|
56
|
+ editorPane = null;
|
|
57
|
+ super.deinstall(c);
|
|
58
|
+ }
|
19
|
59
|
|
20
|
60
|
/** {@inheritDoc} */
|
21
|
61
|
@Override
|
22
|
62
|
public ViewFactory getViewFactory() {
|
23
|
63
|
return defaultFactory;
|
24
|
64
|
}
|
25
|
|
-}
|
|
65
|
+
|
|
66
|
+ /**
|
|
67
|
+ * {@inheritDoc}
|
|
68
|
+ *
|
|
69
|
+ * @param e Mouse Event
|
|
70
|
+ */
|
|
71
|
+ @Override
|
|
72
|
+ public void mouseMoved(final MouseEvent e) {
|
|
73
|
+ if (editorPane == null) {
|
|
74
|
+ return;
|
|
75
|
+ }
|
|
76
|
+ if (!editorPane.isEditable()) {
|
|
77
|
+ Object target = characterElementAt(e).getAttributes().getAttribute(
|
|
78
|
+ IRCTextAttribute.HYPERLINK);
|
|
79
|
+ if (target != null) {
|
|
80
|
+ editorPane.setCursor(HAND_CURSOR);
|
|
81
|
+ return;
|
|
82
|
+ }
|
|
83
|
+ }
|
|
84
|
+ editorPane.setCursor(Cursor.getDefaultCursor());
|
|
85
|
+ }
|
|
86
|
+
|
|
87
|
+ /**
|
|
88
|
+ * {@inheritDoc}
|
|
89
|
+ *
|
|
90
|
+ * @param e Mouse Event
|
|
91
|
+ */
|
|
92
|
+ @Override
|
|
93
|
+ public void mouseReleased(final MouseEvent e) {
|
|
94
|
+ if (!SwingUtilities.isLeftMouseButton(e) || editorPane == null) {
|
|
95
|
+ return;
|
|
96
|
+ }
|
|
97
|
+ if (!editorPane.isEditable()) {
|
|
98
|
+ Object target = characterElementAt(e).getAttributes().getAttribute(
|
|
99
|
+ IRCTextAttribute.HYPERLINK);
|
|
100
|
+ if (target != null) {
|
|
101
|
+ editorPane.fireHyperlinkUpdate(new HyperlinkEvent(editorPane,
|
|
102
|
+ HyperlinkEvent.EventType.ACTIVATED, null,
|
|
103
|
+ (String) target));
|
|
104
|
+ }
|
|
105
|
+ }
|
|
106
|
+ }
|
|
107
|
+
|
|
108
|
+ /**
|
|
109
|
+ * {@inheritDoc}
|
|
110
|
+ *
|
|
111
|
+ * @param e Mouse Event
|
|
112
|
+ */
|
|
113
|
+ @Override
|
|
114
|
+ public void mouseClicked(final MouseEvent e) {
|
|
115
|
+ //Ignore
|
|
116
|
+ }
|
|
117
|
+
|
|
118
|
+ /**
|
|
119
|
+ * {@inheritDoc}
|
|
120
|
+ *
|
|
121
|
+ * @param e Mouse Event
|
|
122
|
+ */
|
|
123
|
+ @Override
|
|
124
|
+ public void mousePressed(final MouseEvent e) {
|
|
125
|
+ //Ignore
|
|
126
|
+ }
|
|
127
|
+
|
|
128
|
+ /**
|
|
129
|
+ * {@inheritDoc}
|
|
130
|
+ *
|
|
131
|
+ * @param e Mouse Event
|
|
132
|
+ */
|
|
133
|
+ @Override
|
|
134
|
+ public void mouseEntered(final MouseEvent e) {
|
|
135
|
+ //Ignore
|
|
136
|
+ }
|
|
137
|
+
|
|
138
|
+ /**
|
|
139
|
+ * {@inheritDoc}
|
|
140
|
+ *
|
|
141
|
+ * @param e Mouse Event
|
|
142
|
+ */
|
|
143
|
+ @Override
|
|
144
|
+ public void mouseExited(final MouseEvent e) {
|
|
145
|
+ //Ignore
|
|
146
|
+ }
|
|
147
|
+
|
|
148
|
+ /**
|
|
149
|
+ * {@inheritDoc}
|
|
150
|
+ *
|
|
151
|
+ * @param e Mouse Event
|
|
152
|
+ */
|
|
153
|
+ @Override
|
|
154
|
+ public void mouseDragged(final MouseEvent e) {
|
|
155
|
+ //Ignore
|
|
156
|
+ }
|
|
157
|
+
|
|
158
|
+ /**
|
|
159
|
+ * Returns the character element for the positition of the mouse event.
|
|
160
|
+ *
|
|
161
|
+ * @param e Mouse event to get position from
|
|
162
|
+ *
|
|
163
|
+ * @return Character element at mouse event
|
|
164
|
+ */
|
|
165
|
+ private Element characterElementAt(final MouseEvent e) {
|
|
166
|
+ return ((StyledDocument) editorPane.getDocument()).getCharacterElement(
|
|
167
|
+ editorPane.getUI().viewToModel(editorPane, e.getPoint()));
|
|
168
|
+ }
|
|
169
|
+}
|