View Javadoc

1   /*
2   * The contents of this file are subject to the BT "ZEUS" Open Source 
3   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
4   * except in compliance with the Licence. You may obtain a copy of the Licence
5   * from $ZEUS_INSTALL/licence.html or alternatively from
6   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7   * 
8   * Except as stated in Clause 7 of the Licence, software distributed under the
9   * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
10  * implied. See the Licence for the specific language governing rights and 
11  * limitations under the Licence.
12  * 
13  * The Original Code is within the package zeus.*.
14  * The Initial Developer of the Original Code is British Telecommunications
15  * public limited company, whose registered office is at 81 Newgate Street, 
16  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
17  * public limited company are Copyright 1996-9. All Rights Reserved.
18  * 
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
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    // The follow hyperlink method
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    // Used to move between pages without altering history
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); // restore the original cursor
121 	   // PENDING(prinz) remove this hack when automatic validation is activated.
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 	      // html.setDocument(doc);
132 	      getToolkit().beep();
133 	   }
134 	   finally {
135 	      // schedule the cursor to revert after the paint has happended.
136 	      url = null;
137 	      SwingUtilities.invokeLater(this);
138 	   }
139 	}
140      }
141   }
142 }