1 package net.suberic.util.swing; 2 import javax.swing.JEditorPane ; 3 import javax.swing.event.HyperlinkEvent ; 4 import javax.swing.event.MouseInputAdapter ; 5 import java.awt.event.MouseEvent ; 6 import java.awt.Point ; 7 import java.net.URL ; 8 import java.net.MalformedURLException ; 9 import javax.swing.text.Document ; 10 11 14 public class HyperlinkMouseHandler extends MouseInputAdapter { 15 16 public class URLSelection { 17 public URL url; 18 public int start; 19 public int end; 20 public JEditorPane editor; 21 22 public URLSelection(JEditorPane newEditor, URL 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 public void mouseMoved(MouseEvent e) { 39 JEditorPane editor = (JEditorPane ) e.getSource(); 40 if (!editor.isEditable()) { 41 Point pt = new Point (e.getX(), e.getY()); 42 URLSelection newSelection = getIndicatedURL(pt, editor); 43 if (newSelection != currentSelection) { 44 if (currentSelection != null) { 45 editor.fireHyperlinkUpdate(new HyperlinkEvent (currentSelection, HyperlinkEvent.EventType.EXITED, currentSelection.url)); 46 } 47 48 if (newSelection != null) { 49 editor.fireHyperlinkUpdate(new HyperlinkEvent (newSelection, HyperlinkEvent.EventType.ENTERED, newSelection.url)); 50 } 51 currentSelection = newSelection; 52 } 53 } 54 } 55 56 URLSelection getIndicatedURL(Point pt, JEditorPane editor) { 57 int pos = editor.viewToModel(pt); 58 if (pos >= 0) { 59 try { 60 Document 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 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 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 word = doc.getText(wordStart, wordLength); 119 if (word.indexOf("://") != -1) { 120 try { 121 URL urlSelected = new URL (word); 122 123 return new URLSelection(editor, urlSelected, wordStart, wordEnd); 124 } catch (MalformedURLException mue) { 125 } 126 } 127 } 128 } catch (javax.swing.text.BadLocationException ble) { 129 130 } 131 } 132 133 return null; 134 } 135 136 public void mouseClicked(MouseEvent e) { 137 JEditorPane editor = (JEditorPane ) e.getSource(); 138 if (!editor.isEditable()) { 139 Point pt = new Point (e.getX(), e.getY()); 140 URLSelection selection = getIndicatedURL(pt, editor); 141 if (selection != null) { 142 editor.fireHyperlinkUpdate(new HyperlinkEvent (selection, HyperlinkEvent.EventType.ACTIVATED, selection.url)); 143 } 144 } 145 } 146 147 } 148 | Popular Tags |