View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.gui.component;
21  
22  import java.io.IOException;
23  import java.net.URL;
24  
25  import javax.swing.JEditorPane;
26  import javax.swing.event.HyperlinkEvent;
27  import javax.swing.event.HyperlinkListener;
28  import javax.swing.text.html.HTMLDocument;
29  import javax.swing.text.html.HTMLFrameHyperlinkEvent;
30  
31  /***
32   * Provides a pane with html support.
33   */
34  public class HTMLEditorPane extends JEditorPane
35  {
36      
37      //--- Constructor(s) ---
38  
39      public HTMLEditorPane(URL url) throws IOException
40      {
41  		super(url);
42  
43  		initialize(true);
44      }
45  
46      public HTMLEditorPane(boolean followHyperLinks)
47      {
48  		initialize(followHyperLinks);
49      }
50  
51      public HTMLEditorPane()
52  	{
53      	this(true);
54      }
55      
56      //--- Method(s) ---
57  
58      private void initialize(boolean followHyperLinks)
59      {
60  		setContentType("text/html");
61  		setEditable(false);
62  		
63  		if (followHyperLinks) {
64  			addHyperlinkListener(new FramesListener());
65  		}
66      }
67  
68  
69      public static void addHeader(StringBuffer sb)
70      {
71  		sb.append("<html>");
72  		sb.append("<head>");
73  		sb.append("<style type=\"text/css\">");
74  		sb.append("body { font-family:dialog,sans-serif,arial,helvetica,tahoma; }");
75  		sb.append("</style>");
76  		sb.append("</head>");
77  		sb.append("<body>");
78      }
79  
80      public static void addFooter(StringBuffer sb)
81      {
82  		sb.append("</body>");
83  		sb.append("</html>");
84      }
85  
86      /***
87       * Listens for links and filters out links pointing to different
88       * frames, this code was taken from jdoc.
89       */
90      private class FramesListener implements HyperlinkListener
91      {
92  		public void hyperlinkUpdate(HyperlinkEvent e)
93  		{
94  			if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
95  				JEditorPane pane = (JEditorPane)e.getSource();
96  				if (e instanceof HTMLFrameHyperlinkEvent) {
97  					HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent)e;
98  					HTMLDocument doc = (HTMLDocument)pane.getDocument();
99  					doc.processHTMLFrameHyperlinkEvent(evt);
100 				}
101 				else {
102 					try {
103 						pane.setPage(e.getURL());
104 					}
105 					catch (Throwable t) {
106 					}
107 				}
108 			}
109 		}
110     }
111 }