FileDocCategorySizeDatePackage
Chapter1.javaAPI DocExample5812Thu Oct 04 11:59:12 BST 2001None

Chapter1.java

/*
 * File: Chap1Solutions.java
 * Tim Balls : Feb 2001 1999
 *
 * Solutions to some of exercises from Chapter 1
 * Changes/additions to Browswer3 are marked with /**/
/*
 * Contains classes:
 *
 *    Ch1Sol1ab : solutions to Q1 (a) and (b)
 *    Ch1Sol2   : solution to Q2
 *       with an alternative form of error display
 */

import javax.swing.*;
import javax.swing.event.*;
import java.net.*;
import java.io.IOException;
import java.awt.event.*;
import java.awt.*;


class Ch1Sol1ab extends JFrame implements HyperlinkListener,
																ActionListener
{	public static void main( String[] args )
	{	if( args.length == 0 )
		{	new Ch1Sol1ab( "NoURL" );
		}
		else
		{	new Ch1Sol1ab( args[0] );
		}
	}

	private JEditorPane html = new JEditorPane();
	private URL url;
	private JTextField location;
/**/	private JLabel bottomLine;
	
	public Ch1Sol1ab( String site )
	{	setSize( 350, 450 );
		setTitle( "Scrolling Browser" );
		addWindowListener( new WindowAdapter() {
			public void windowClosing( WindowEvent e )
			{	System.exit( 0 );
			}
		});

		// Build more interesting interface
		location = new JTextField( site, 25 );  // 25 cols
		JPanel topLine = new JPanel();
		topLine.setLayout( new FlowLayout( FlowLayout.LEFT ) );
		topLine.add( new JLabel( "URL:" ) );
		topLine.add( location );
		location.addActionListener( this );
			
		// for the JFrame:
		this.getContentPane().setLayout( new BorderLayout() );
		this.getContentPane().add( topLine, BorderLayout.NORTH );

/**/	bottomLine = new JLabel( "" );
/**/  this.getContentPane().add( bottomLine, BorderLayout.SOUTH );

		if( !site.equals( "NoURL" ) )
		{	loadURL( site );
		}
		
		JScrollPane scroller = new JScrollPane();
		JViewport viewport = scroller.getViewport();
		viewport.add( html );
		
		this.getContentPane().add( scroller, BorderLayout.CENTER );
		html.setEditable( false ); // to allow links to be activated
		html.addHyperlinkListener( this );
		
		setVisible( true );
	}

	// When a hyperlink is "clicked" :-
	public void hyperlinkUpdate( HyperlinkEvent e )
	{	if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
/**/		{	loadURL( e.getURL().toString() );
		}
	}
	
	// When a new URL is entered (and return pressed)
	public void actionPerformed( ActionEvent e )
	{	String site = location.getText();
		loadURL( site );
		// big assumption - it is legal and works!!
	}

	// Attempt to load the html JEditorPane from the URL at "site"
	private void loadURL( String site )
	{	try
		{	url = new URL( site );
		}
		catch( MalformedURLException e )
/**/	{	bottomLine.setText( "Invalid URL specified" );
/**/	   return;
		}
		try
		{	html.setPage( url ); // load page
		}
		catch( IOException e )
/**/	{	bottomLine.setText( "Can't load url: " + url );
/**/		return;
		}
/**/	bottomLine.setText( "" );
/**/  // update text field (moved)
/**/  location.setText( site );	
	}

}


//**************************************************************

class Ch1Sol2 extends JFrame implements HyperlinkListener,
																ActionListener
{	public static void main( String[] args )
	{	if( args.length == 0 )
		{	new Ch1Sol2( "NoURL" );
		}
		else
		{	new Ch1Sol2( args[0] );
		}
	}

	private JEditorPane html = new JEditorPane();
	private URL url;
	private JTextField location;
/**/   private JButton backButton = new JButton( "Back" );
/**/   private URL lastURL = null;
		
	public Ch1Sol2( String site )
	{	setSize( 350, 450 );
		setTitle( "Scrolling Browser" );
		addWindowListener( new WindowAdapter() {
			public void windowClosing( WindowEvent e )
			{	System.exit( 0 );
			}
		});

		// Build more interesting interface
		location = new JTextField( site, 25 );  // 25 cols
		JPanel topLine = new JPanel();
		topLine.setLayout( new FlowLayout( FlowLayout.LEFT ) );
/**/  topLine.add( backButton );
		topLine.add( new JLabel( "URL:" ) );
		topLine.add( location );
		location.addActionListener( this );
			
		// for the JFrame:
		this.getContentPane().setLayout( new BorderLayout() );
		this.getContentPane().add( topLine, BorderLayout.NORTH );

		if( !site.equals( "NoURL" ) )
		{	loadURL( site );
		}

/**/  // add listener for the back button
      backButton.addActionListener( new ActionListener() {
         public void actionPerformed( ActionEvent e )
         {  if( lastURL != null )
            {  loadURL( lastURL.toString() );
            }
         }
/**/  });
 		
				
		JScrollPane scroller = new JScrollPane();
		JViewport viewport = scroller.getViewport();
		viewport.add( html );
		
		this.getContentPane().add( scroller, BorderLayout.CENTER );
		html.setEditable( false ); // to allow links to be activated
		html.addHyperlinkListener( this );
		
		setVisible( true );
	}

	// When a hyperlink is "clicked" :-
	public void hyperlinkUpdate( HyperlinkEvent e )
	{	if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
		{	loadURL( e.getURL().toString() );
		}
	}
	
	// When a new URL is entered (and return pressed)
	public void actionPerformed( ActionEvent e )
	{	String site = location.getText();
		loadURL( site );
		// big assumption - it is legal and works!!
	}

	// Attempt to load the html JEditorPane from the URL at "site"
	private void loadURL( String site )
/**/	{	URL currentURL = url;
	   try
		{	url = new URL( site );
		}
		catch( MalformedURLException e )
/**/	{	JOptionPane.showMessageDialog( this,
               "Invalid URL specified",
               "Invalid URL",
               JOptionPane.ERROR_MESSAGE );
/**/	   return;
		}
		try
		{	html.setPage( url ); // load page
		}
		catch( IOException e )
/**/	{	JOptionPane.showMessageDialog( this,
               "Can't load URL : " + url,
               "Loading error",
               JOptionPane.ERROR_MESSAGE );
/**/		return;
		}
/**/  // update text field (moved)
/**/  location.setText( site );
/**/  lastURL = currentURL;
	}

}