Added
Link Here
|
1 |
/* |
2 |
* Sun Public License Notice |
3 |
* |
4 |
* The contents of this file are subject to the Sun Public License |
5 |
* Version 1.0 (the "License"). You may not use this file except in |
6 |
* compliance with the License. A copy of the License is available at |
7 |
* http://www.sun.com/ |
8 |
* |
9 |
* The Original Code is NetBeans. The Initial Developer of the Original |
10 |
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun |
11 |
* Microsystems, Inc. All Rights Reserved. |
12 |
*/ |
13 |
|
14 |
package org.netbeans.lib.editor.hyperlink; |
15 |
|
16 |
import java.awt.Color; |
17 |
import java.awt.Cursor; |
18 |
import java.awt.event.KeyEvent; |
19 |
import java.awt.event.KeyListener; |
20 |
import java.awt.event.MouseEvent; |
21 |
import java.awt.event.MouseListener; |
22 |
import java.awt.event.MouseMotionListener; |
23 |
import java.beans.PropertyChangeEvent; |
24 |
import java.beans.PropertyChangeListener; |
25 |
import java.util.ArrayList; |
26 |
import java.util.Collections; |
27 |
import java.util.HashMap; |
28 |
import java.util.Iterator; |
29 |
import java.util.List; |
30 |
import javax.swing.SwingUtilities; |
31 |
import javax.swing.text.Document; |
32 |
import javax.swing.text.JTextComponent; |
33 |
import org.netbeans.editor.*; |
34 |
import org.netbeans.lib.editor.hyperlink.spi.HyperlinkProvider; |
35 |
|
36 |
/** |
37 |
* |
38 |
* @author Jan Lahoda |
39 |
*/ |
40 |
public class HyperlinkOperation implements MouseListener, MouseMotionListener, PropertyChangeListener, KeyListener { |
41 |
|
42 |
private JTextComponent component; |
43 |
private Document currentDocument; |
44 |
private HyperlinkLayer layer; |
45 |
private String mimeType; |
46 |
private boolean componentHadMouseCursor; |
47 |
private Cursor oldComponentsMouseCursor; |
48 |
private boolean hyperlinkUp; |
49 |
|
50 |
public static HyperlinkOperation create(JTextComponent component, String mimeType) { |
51 |
return new HyperlinkOperation(component, mimeType); |
52 |
} |
53 |
|
54 |
/** Creates a new instance of HoveringImpl */ |
55 |
private HyperlinkOperation(JTextComponent component, String mimeType) { |
56 |
this.component = component; |
57 |
this.mimeType = mimeType; |
58 |
this.oldComponentsMouseCursor = null; |
59 |
this.componentHadMouseCursor = false; |
60 |
this.hyperlinkUp = false; |
61 |
|
62 |
component.addPropertyChangeListener("document", this); |
63 |
} |
64 |
|
65 |
private void documentUpdated() { |
66 |
currentDocument = component.getDocument(); |
67 |
|
68 |
if (currentDocument instanceof BaseDocument) { |
69 |
BaseDocument bd = (BaseDocument) currentDocument; |
70 |
|
71 |
bd.addLayer(layer = new HyperlinkLayer(), 1100); |
72 |
|
73 |
component.addMouseListener(this); |
74 |
component.addMouseMotionListener(this); |
75 |
component.addKeyListener(this); |
76 |
} |
77 |
} |
78 |
|
79 |
public void mouseMoved(MouseEvent e) { |
80 |
if ((e.getModifiersEx() & MouseEvent.CTRL_DOWN_MASK) == MouseEvent.CTRL_DOWN_MASK) { |
81 |
int position = component.viewToModel(e.getPoint()); |
82 |
|
83 |
if (position < 0) { |
84 |
unHyperlink(true); |
85 |
|
86 |
return ; |
87 |
} |
88 |
|
89 |
performHyperlinking(position); |
90 |
} else { |
91 |
unHyperlink(true); |
92 |
} |
93 |
} |
94 |
|
95 |
public void mouseDragged(MouseEvent e) { |
96 |
//ignored |
97 |
} |
98 |
|
99 |
private void performHyperlinking(int position) { |
100 |
HyperlinkProvider provider = findProvider(position); |
101 |
|
102 |
if (provider != null) { |
103 |
int[] offsets = provider.getHyperlinkSpan(component.getDocument(), position); |
104 |
|
105 |
if (offsets != null) |
106 |
makeHyperlink(offsets[0], offsets[1]); |
107 |
} else { |
108 |
unHyperlink(true); |
109 |
} |
110 |
} |
111 |
|
112 |
private void performAction(int position) { |
113 |
HyperlinkProvider provider = findProvider(position); |
114 |
|
115 |
if (provider != null) { |
116 |
provider.performClickAction(component.getDocument(), position); |
117 |
} |
118 |
} |
119 |
|
120 |
private HyperlinkProvider findProvider(int position) { |
121 |
List/*<HyperlinkProvider>*/ providers = HyperlinkProviderManager.getDefault().getHyperlinkProviders(mimeType); |
122 |
|
123 |
for (Iterator/*<HyperlinkProvider>*/ i = providers.iterator(); i.hasNext(); ) { |
124 |
HyperlinkProvider provider = (HyperlinkProvider) i.next(); |
125 |
|
126 |
if (provider.isHyperlinkPoint(component.getDocument(), position)) { |
127 |
return provider; |
128 |
} |
129 |
} |
130 |
|
131 |
return null; |
132 |
} |
133 |
|
134 |
private synchronized void makeHyperlink(final int start, final int end) { |
135 |
boolean makeCursorSnapshot = true; |
136 |
|
137 |
if (hyperlinkUp) { |
138 |
unHyperlink(false); |
139 |
makeCursorSnapshot = false; |
140 |
} |
141 |
|
142 |
hyperlinkUp = true; |
143 |
|
144 |
layer.hyperlinkStart = start; |
145 |
layer.hyperlinkEnd = end; |
146 |
|
147 |
SwingUtilities.invokeLater(new Runnable() { |
148 |
public void run() { |
149 |
component.getUI().damageRange(component, start, end); |
150 |
} |
151 |
}); |
152 |
|
153 |
if (makeCursorSnapshot) { |
154 |
if (componentHadMouseCursor = component.isCursorSet()) { |
155 |
oldComponentsMouseCursor = component.getCursor(); |
156 |
} else { |
157 |
oldComponentsMouseCursor = null; |
158 |
} |
159 |
component.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
160 |
} |
161 |
} |
162 |
|
163 |
private synchronized void unHyperlink(boolean removeCursor) { |
164 |
if (!hyperlinkUp) |
165 |
return ; |
166 |
|
167 |
final int start = layer.hyperlinkStart; |
168 |
final int end = layer.hyperlinkEnd; |
169 |
|
170 |
layer.hyperlinkStart = -1; |
171 |
layer.hyperlinkEnd = -1; |
172 |
|
173 |
if (start != (-1) && end != (-1)) { |
174 |
SwingUtilities.invokeLater(new Runnable() { |
175 |
public void run() { |
176 |
component.getUI().damageRange(component, start, end); |
177 |
} |
178 |
}); |
179 |
} |
180 |
|
181 |
if (componentHadMouseCursor && removeCursor) { |
182 |
component.setCursor(oldComponentsMouseCursor); |
183 |
componentHadMouseCursor = false; |
184 |
oldComponentsMouseCursor = null; |
185 |
} |
186 |
|
187 |
hyperlinkUp = false; |
188 |
} |
189 |
|
190 |
public void propertyChange(PropertyChangeEvent evt) { |
191 |
if (currentDocument != component.getDocument()) |
192 |
documentUpdated(); |
193 |
} |
194 |
|
195 |
public void keyTyped(KeyEvent e) { |
196 |
//ignored |
197 |
} |
198 |
|
199 |
public void keyReleased(KeyEvent e) { |
200 |
if (e.getKeyCode() == KeyEvent.VK_CONTROL) |
201 |
unHyperlink(true); |
202 |
} |
203 |
|
204 |
public void keyPressed(KeyEvent e) { |
205 |
//ignored |
206 |
} |
207 |
|
208 |
public void mouseReleased(MouseEvent e) { |
209 |
//ignored |
210 |
} |
211 |
|
212 |
public void mousePressed(MouseEvent e) { |
213 |
//ignored |
214 |
} |
215 |
|
216 |
public void mouseExited(MouseEvent e) { |
217 |
//ignored |
218 |
} |
219 |
|
220 |
public void mouseEntered(MouseEvent e) { |
221 |
//ignored |
222 |
} |
223 |
|
224 |
public void mouseClicked(MouseEvent e) { |
225 |
if ((e.getModifiersEx() & MouseEvent.CTRL_DOWN_MASK) == MouseEvent.CTRL_DOWN_MASK) { |
226 |
int position = component.viewToModel(e.getPoint()); |
227 |
|
228 |
if (position < 0) { |
229 |
return ; |
230 |
} |
231 |
|
232 |
performAction(position); |
233 |
} |
234 |
} |
235 |
|
236 |
private static class HyperlinkLayer extends DrawLayer.AbstractLayer { |
237 |
|
238 |
private int hyperlinkStart = (-1); |
239 |
private int hyperlinkEnd = (-1); |
240 |
|
241 |
public static final String NAME = "hyperlink-layer"; |
242 |
|
243 |
public static final int VISIBILITY = 1050; |
244 |
|
245 |
private boolean initialized = false; |
246 |
|
247 |
public HyperlinkLayer() { |
248 |
super(NAME); |
249 |
|
250 |
this.initialized = false; |
251 |
} |
252 |
|
253 |
private void checkDocument(Document doc) { |
254 |
} |
255 |
|
256 |
public boolean extendsEOL() { |
257 |
return true; |
258 |
} |
259 |
|
260 |
public synchronized void init(final DrawContext ctx) { |
261 |
if (!initialized) { |
262 |
Document doc = ctx.getEditorUI().getDocument(); |
263 |
|
264 |
initialized = true; |
265 |
checkDocument(doc); |
266 |
} |
267 |
|
268 |
if (isActive()) |
269 |
setNextActivityChangeOffset(hyperlinkStart); |
270 |
} |
271 |
|
272 |
private boolean isActive() { |
273 |
return hyperlinkStart != (-1) && hyperlinkEnd != (-1); |
274 |
} |
275 |
|
276 |
public boolean isActive(DrawContext ctx, MarkFactory.DrawMark mark) { |
277 |
return isActive(); |
278 |
} |
279 |
|
280 |
private boolean isIn(int offset) { |
281 |
return offset >= hyperlinkStart && offset < hyperlinkEnd; |
282 |
} |
283 |
|
284 |
private static Coloring hoverColoring = new Coloring(null, 0, Color.BLUE, null, Color.BLUE, null); |
285 |
|
286 |
public void updateContext(DrawContext ctx) { |
287 |
if (!isActive()) |
288 |
return ; |
289 |
|
290 |
int currentOffset = ctx.getFragmentOffset(); |
291 |
|
292 |
if (isIn(currentOffset)) { |
293 |
hoverColoring.apply(ctx); |
294 |
|
295 |
if (isIn(currentOffset + ctx.getFragmentLength())) |
296 |
setNextActivityChangeOffset(currentOffset + ctx.getFragmentLength()); |
297 |
} |
298 |
} |
299 |
|
300 |
} |
301 |
|
302 |
} |