1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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 }