/Users/lyon/j4p/src/gui/html/BrowserListener.java

1    package gui.html; 
2     
3    import javax.swing.*; 
4    import javax.swing.event.HyperlinkEvent; 
5    import javax.swing.event.HyperlinkListener; 
6    import javax.swing.text.html.HTMLDocument; 
7    import javax.swing.text.html.HTMLFrameHyperlinkEvent; 
8    import java.net.URL; 
9     
10   public class BrowserListener 
11           implements HyperlinkListener { 
12       public void hyperlinkUpdate(HyperlinkEvent e) { 
13           if (e.getEventType() == 
14                   HyperlinkEvent.EventType.ACTIVATED) { 
15               processActivated(e); 
16           } 
17       } 
18    
19       private void processActivated(HyperlinkEvent e) { 
20           JEditorPane jep = 
21                   (JEditorPane) e.getSource(); 
22           if (e instanceof HTMLFrameHyperlinkEvent) 
23               processHyperLinkEvent(e, jep); 
24           else { 
25               setPage(jep, e); 
26           } 
27       } 
28    
29       private void setPage(JEditorPane jep, 
30                            HyperlinkEvent e) { 
31           URL url = e.getURL(); 
32           System.out.println(url); 
33           try { 
34               jep.setPage(url); 
35           } catch (Throwable t) { 
36               System.out.println("could not open:" + 
37                       url); 
38               //jep.setText(net.Browser.toString(url+"")); 
39               //t.printStackTrace(); 
40           } 
41       } 
42    
43    
44       private void processHyperLinkEvent( 
45               HyperlinkEvent e, JEditorPane pane) { 
46           HTMLFrameHyperlinkEvent hle = 
47                   (HTMLFrameHyperlinkEvent) e; 
48           HTMLDocument htmlDoc = (HTMLDocument) pane.getDocument(); 
49           htmlDoc.processHTMLFrameHyperlinkEvent(hle); 
50       } 
51   } 
52