The JarBrowser source code

(Back to the index page: index.html)

/*
 * JarBrowser.java
 *
 * Created on 4. November 2002, 21:10
 */
import java.awt.event.*;
import java.io.*;
import java.net.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.html.*;

/**
 * A browser for browsing jar-packed web sites.
 * At the heart of the JarBrowser is the URL, 
 * which is passed to a very simple {@link Browser}.
 *
 * @author Margrit Höhme, 2002 
 * (<A HREF="mailto:java@mhoehme.de">java@mhoehme.de</A>)
 */
public class JarBrowser extends JFrame
{
    /**
     * The main method.
     * @param args The command line arguments can be used to pass in the URL of 
     *        a jar file, or parts of the URL.
     */
    public static void main (String[] args)
        throws MalformedURLException
    {
        URL url = new URL
           ("jar:file:/C:\\projects\\jarbrowser\\site.jar!/index.html");
        Browser browser = new Browser ();
        browser.setUrl (url);
    }
}

/**
 * A simple web browser.
 */
class Browser extends JFrame
{
    private JEditorPane pane;
    
    /** 
     * Creates a new instance of Browser.
     */
    public Browser () 
    {
        setSize (400, 300);
        setTitle ("JarBrowser by Margrit H\u00F6hme, 2002");
        pane = new JEditorPane();
        getContentPane().add (new JScrollPane (pane));
        addWindowListener (new WindowAdapter () {
            public void windowClosing (WindowEvent e) {
                e.getWindow().setVisible (false);
                e.getWindow().dispose();
            }
          });
        pane.setEditorKit(new HTMLEditorKit());
        pane.setEditable(false);
        pane.addHyperlinkListener(new HyperlinkListener () {
            public void hyperlinkUpdate (HyperlinkEvent e) {
                if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
                {
                    URL url = e.getURL();
                    if (url != null)
                    {
                        setUrl (url);
                    }
                }
            }
        });
        setVisible (true);
    }
    
    /**
     * Load the specified web page.
     * @param url the url of the web page.
     */
    public void setUrl (URL url)
    {
        try
        {
            pane.setPage(url);
        }
        catch (Throwable t)
        {
            System.err.println ("URL: "+url);
            t.printStackTrace(System.err);
        }
    }
}