1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 /******************************************************************************
25 * HtmlPanel.java
26 *
27 * A wrapper around the Swing HTML JEditorPane (from Swing Set 1.7)
28 ****************************************************************************/
29
30 package zeus.gui.help;
31
32 import javax.swing.*;
33 import javax.swing.event.*;
34 import javax.swing.text.*;
35
36 import java.awt.*;
37 import java.net.URL;
38 import java.net.MalformedURLException;
39 import java.io.IOException;
40
41
42 public class HtmlPanel extends JPanel implements HyperlinkListener {
43 protected JEditorPane html;
44 protected URL currentDoc;
45 protected HelpWindow parent;
46
47
48 public HtmlPanel(HelpWindow parent) {
49 this.parent = parent;
50 setLayout(new BorderLayout());
51
52 html = new JEditorPane();
53 html.setEditable(false);
54 html.addHyperlinkListener(this);
55 JScrollPane scroller = new JScrollPane();
56 scroller.setPreferredSize(new Dimension(360, 180));
57 scroller.setMinimumSize(new Dimension(360,180));
58 JViewport viewport = scroller.getViewport();
59 viewport.add(html);
60 viewport.setBackingStoreEnabled(true);
61 add(scroller, BorderLayout.CENTER);
62 }
63
64
65 public void hyperlinkUpdate(HyperlinkEvent e) {
66 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
67 URL url = e.getURL();
68 parent.addToHistory(url.toString());
69 linkActivated(e.getURL());
70 }
71 }
72
73
74 public void setPage(String doc) {
75 try {
76 linkActivated(new URL(doc));
77 }
78 catch (MalformedURLException e) {
79 System.out.println("Malformed URL: " + e);
80 }
81 catch (IOException e) {
82 System.out.println("IOException: " + e);
83 }
84 }
85
86
87 /***
88 * Follows the reference in an link. The given url is the requested reference.
89 * By default this calls <a href="#setPage">setPage</a>, and if an exception is
90 * thrown the original previous document is restored and a beep sounded. If an
91 * attempt was made to follow a link, but it represented a malformed url, this
92 * method will be called with a null argument.
93 *
94 * @param u the URL to follow
95 */
96 protected void linkActivated(URL url) {
97 currentDoc = url;
98 Cursor cursor = html.getCursor();
99 Cursor waitCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
100 html.setCursor(waitCursor);
101 SwingUtilities.invokeLater(new PageLoader(url,cursor));
102 }
103
104 /***
105 * temporary class that loads synchronously (although
106 * later than the request so that a cursor change can be done).
107 */
108 class PageLoader implements Runnable {
109
110 URL url;
111 Cursor cursor;
112
113 PageLoader(URL url, Cursor cursor) {
114 this.url = url;
115 this.cursor = cursor;
116 }
117
118 public void run() {
119 if (url == null) {
120 html.setCursor(cursor);
121
122 Container cont = html.getParent();
123 cont.repaint();
124 }
125 else {
126 Document doc = html.getDocument();
127 try {
128 html.setPage(url);
129 }
130 catch(IOException ioe) {
131
132 getToolkit().beep();
133 }
134 finally {
135
136 url = null;
137 SwingUtilities.invokeLater(this);
138 }
139 }
140 }
141 }
142 }