home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / plugin / jfc / Metalworks / src / MetalworksHelp.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  4.7 KB  |  159 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)MetalworksHelp.java    1.9 02/06/13
  38.  */
  39.  
  40. import javax.swing.*;
  41. import java.awt.*;
  42. import java.net.URL;
  43. import java.net.MalformedURLException;
  44. import java.io.*;
  45. import javax.swing.text.*;
  46. import javax.swing.event.*;
  47.  
  48. /*
  49.  * @version 1.9 06/13/02
  50.  * @author Steve Wilson
  51.  */
  52. public class MetalworksHelp extends JInternalFrame {
  53.   
  54.     public MetalworksHelp() {
  55.     super("Help", true, true, true, true);
  56.  
  57.            setFrameIcon( (Icon)UIManager.get("Tree.openIcon")); // PENDING(steve) need more general palce to get this icon
  58.     setBounds( 200, 25, 400, 400);
  59.     HtmlPane html = new HtmlPane();
  60.     setContentPane(html);
  61.     }
  62.  
  63. }
  64.  
  65.  
  66. class HtmlPane extends JScrollPane implements HyperlinkListener {
  67.     JEditorPane html;
  68.  
  69.     public HtmlPane() {
  70.     try {
  71.         File f = new File ("HelpFiles/toc.html");
  72.         String s = f.getAbsolutePath();
  73.         s = "file:"+s;
  74.         URL url = new URL(s);
  75.         html = new JEditorPane(s);
  76.         html.setEditable(false);
  77.         html.addHyperlinkListener(this);
  78.  
  79.         JViewport vp = getViewport();
  80.         vp.add(html);
  81.     } catch (MalformedURLException e) {
  82.         System.out.println("Malformed URL: " + e);
  83.     } catch (IOException e) {
  84.         System.out.println("IOException: " + e);
  85.     }    
  86.     }
  87.  
  88.     /**
  89.      * Notification of a change relative to a 
  90.      * hyperlink.
  91.      */
  92.     public void hyperlinkUpdate(HyperlinkEvent e) {
  93.     if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
  94.         linkActivated(e.getURL());
  95.     }
  96.     }
  97.  
  98.     /**
  99.      * Follows the reference in an
  100.      * link.  The given url is the requested reference.
  101.      * By default this calls <a href="#setPage">setPage</a>,
  102.      * and if an exception is thrown the original previous
  103.      * document is restored and a beep sounded.  If an 
  104.      * attempt was made to follow a link, but it represented
  105.      * a malformed url, this method will be called with a
  106.      * null argument.
  107.      *
  108.      * @param u the URL to follow
  109.      */
  110.     protected void linkActivated(URL u) {
  111.     Cursor c = html.getCursor();
  112.     Cursor waitCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
  113.     html.setCursor(waitCursor);
  114.     SwingUtilities.invokeLater(new PageLoader(u, c));
  115.     }
  116.  
  117.     /**
  118.      * temporary class that loads synchronously (although
  119.      * later than the request so that a cursor change
  120.      * can be done).
  121.      */
  122.     class PageLoader implements Runnable {
  123.     
  124.     PageLoader(URL u, Cursor c) {
  125.         url = u;
  126.         cursor = c;
  127.     }
  128.  
  129.         public void run() {
  130.         if (url == null) {
  131.         // restore the original cursor
  132.         html.setCursor(cursor);
  133.  
  134.         // PENDING(prinz) remove this hack when 
  135.         // automatic validation is activated.
  136.         Container parent = html.getParent();
  137.         parent.repaint();
  138.         } else {
  139.         Document doc = html.getDocument();
  140.         try {
  141.             html.setPage(url);
  142.         } catch (IOException ioe) {
  143.             html.setDocument(doc);
  144.             getToolkit().beep();
  145.         } finally {
  146.             // schedule the cursor to revert after
  147.             // the paint has happended.
  148.             url = null;
  149.             SwingUtilities.invokeLater(this);
  150.         }
  151.         }
  152.     }
  153.  
  154.     URL url;
  155.     Cursor cursor;
  156.     }
  157.  
  158. }
  159.