KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > util > swing > HyperlinkMouseHandler


1 package net.suberic.util.swing;
2 import javax.swing.JEditorPane JavaDoc;
3 import javax.swing.event.HyperlinkEvent JavaDoc;
4 import javax.swing.event.MouseInputAdapter JavaDoc;
5 import java.awt.event.MouseEvent JavaDoc;
6 import java.awt.Point JavaDoc;
7 import java.net.URL JavaDoc;
8 import java.net.MalformedURLException JavaDoc;
9 import javax.swing.text.Document JavaDoc;
10
11 /**
12  * This is a class which tracks hyperlink action for a JEditorPane.
13  */

14 public class HyperlinkMouseHandler extends MouseInputAdapter JavaDoc {
15
16     public class URLSelection {
17     public URL JavaDoc url;
18     public int start;
19     public int end;
20     public JEditorPane JavaDoc editor;
21     
22     public URLSelection(JEditorPane JavaDoc newEditor, URL JavaDoc newUrl, int newStart, int newEnd) {
23         url = newUrl;
24         start = newStart;
25         end = newEnd;
26         editor = newEditor;
27     }
28     }
29
30     private int lineLength;
31     private URLSelection currentSelection = null;
32
33     public HyperlinkMouseHandler(int newLineLength) {
34     lineLength = newLineLength;
35     }
36
37     // track the moving of the mouse.
38
public void mouseMoved(MouseEvent JavaDoc e) {
39     JEditorPane JavaDoc editor = (JEditorPane JavaDoc) e.getSource();
40     if (!editor.isEditable()) {
41         Point JavaDoc pt = new Point JavaDoc(e.getX(), e.getY());
42         URLSelection newSelection = getIndicatedURL(pt, editor);
43         if (newSelection != currentSelection) {
44         if (currentSelection != null) {
45             editor.fireHyperlinkUpdate(new HyperlinkEvent JavaDoc(currentSelection, HyperlinkEvent.EventType.EXITED, currentSelection.url));
46         }
47         
48         if (newSelection != null) {
49             editor.fireHyperlinkUpdate(new HyperlinkEvent JavaDoc(newSelection, HyperlinkEvent.EventType.ENTERED, newSelection.url));
50         }
51         currentSelection = newSelection;
52         }
53     }
54     }
55     
56     URLSelection getIndicatedURL(Point JavaDoc pt, JEditorPane JavaDoc editor) {
57     int pos = editor.viewToModel(pt);
58     if (pos >= 0) {
59         try {
60         Document JavaDoc doc = editor.getDocument();
61         int docLength = doc.getLength();
62         
63         int wordStart = 0;
64         
65         int startOffset = pos;
66         int relativePosition;
67         boolean startFound = false;
68         
69         while ( ! startFound ) {
70             startOffset = startOffset - lineLength;
71             relativePosition = lineLength;
72             
73             if (startOffset < 0) {
74             relativePosition = relativePosition + startOffset;
75             startOffset = 0;
76             }
77             
78             String JavaDoc possibleText = doc.getText(startOffset, relativePosition);
79             char[] charArray = possibleText.toCharArray();
80             for (int i = relativePosition - 1; (! startFound && i >= 0 ) ; i--) {
81             if (Character.isWhitespace(charArray[i]) || charArray[i] == '(' || charArray[i] == ')' || charArray[i] == '<' || charArray[i] == '>') {
82                 startFound = true;
83                 wordStart = startOffset + i + 1;
84             }
85             }
86             
87             if (startOffset == 0)
88             startFound = true;
89         }
90         
91         int wordEnd = docLength - 1;
92         startOffset = pos - 1 - lineLength;
93         int length = lineLength;
94         boolean endFound = false;
95         
96         while ( ! endFound ) {
97             startOffset = startOffset + lineLength;
98             
99             if (startOffset + lineLength > docLength) {
100             length = docLength - startOffset;
101             }
102             
103             String JavaDoc possibleText = doc.getText(startOffset, length);
104             char[] charArray = possibleText.toCharArray();
105             for (int i = 0; (! endFound && i < length ) ; i++) {
106             if (Character.isWhitespace(charArray[i]) || charArray[i] == '(' || charArray[i] == ')' || charArray[i] == '<' || charArray[i] == '>') {
107                 endFound = true;
108                 wordEnd = startOffset + i - 1;
109             }
110             }
111             
112             if (startOffset + length >= docLength)
113             endFound = true;
114         }
115         
116         int wordLength = wordEnd - wordStart + 1;
117         if (wordLength > 3) {
118             String JavaDoc word = doc.getText(wordStart, wordLength);
119             if (word.indexOf("://") != -1) {
120             try {
121                 URL JavaDoc urlSelected = new URL JavaDoc(word);
122                 
123                 return new URLSelection(editor, urlSelected, wordStart, wordEnd);
124             } catch (MalformedURLException JavaDoc mue) {
125             }
126             }
127         }
128         } catch (javax.swing.text.BadLocationException JavaDoc ble) {
129
130         }
131     }
132     
133     return null;
134     }
135
136     public void mouseClicked(MouseEvent JavaDoc e) {
137     JEditorPane JavaDoc editor = (JEditorPane JavaDoc) e.getSource();
138     if (!editor.isEditable()) {
139         Point JavaDoc pt = new Point JavaDoc(e.getX(), e.getY());
140         URLSelection selection = getIndicatedURL(pt, editor);
141         if (selection != null) {
142         editor.fireHyperlinkUpdate(new HyperlinkEvent JavaDoc(selection, HyperlinkEvent.EventType.ACTIVATED, selection.url));
143         }
144     }
145     }
146     
147 }
148
Popular Tags