//  WebReader.java
// This program uses a JEditorPane to display the
// contents of a file on a Web server.// Java core packages
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;// Java extension packages
import javax.swing.*;
import javax.swing.event.*;public class WebReader extends JFrame {
private JTextField enterField;
private JEditorPane contentsArea;
private JButton backButton,fwdButton;
private JPanel contralPanel;
private URLList urllist;
   // set up GUI
   public WebReader()
   {
      super( "My Web Browser" );
      JMenu fileMenu = new JMenu("File");//set up File menu ane its menu item
      fileMenu.setMnemonic('F');      JMenuItem exitItem = new JMenuItem("Exit");
      exitItem.setMnemonic('X');
      exitItem.addActionListener(
       new ActionListener(){
       public void actionPerformed(ActionEvent event){
       System.exit(0);
       }
       }
      );
      fileMenu.add( exitItem );      JMenuBar bar = new JMenuBar();
      setJMenuBar( bar );
      bar.add( fileMenu );      JMenu editMenu = new JMenu("Edit");
      editMenu.setMnemonic('E');      JMenuItem nextItem = new JMenuItem( "Next" );
      nextItem.setMnemonic( 'N' );
      nextItem.addActionListener(
       new ActionListener(){
       public void actionPerformed(ActionEvent event){
       System.exit(0);
       }
       }
      );
      editMenu.add( nextItem );
      bar.add( editMenu );      Container container = getContentPane();      FlowLayout lay=new FlowLayout();
      lay.setAlignment( FlowLayout.LEFT );      contralPanel = new JPanel();
      contralPanel.setLayout( lay );      urllist = new URLList();      Icon back = new ImageIcon( "back.gif" );
      backButton = new JButton( "back",back );
      backButton.addActionListener(
       new ActionListener()
       {
       public void actionPerformed( ActionEvent event )
            {
             String strback=new String( urllist.back() );
             System.out.println( strback );
             if( !strback.equals( "Noop" ) )
             getThePage( strback );
            
            }
        }
      );
   contralPanel.add( backButton );      Icon fwd = new ImageIcon( "fwd.gif" );
      fwdButton = new JButton( "fwd",fwd );
      fwdButton.addActionListener(
       new ActionListener()
       {
       public void actionPerformed( ActionEvent event )
            {
             String strfwd=new String( urllist.forward() );
             System.out.println( strfwd );
             if( !strfwd.equals( "Noop" ) )
             getThePage( strfwd );
            
            }
        }
      );
      contralPanel.add( fwdButton );      // create enterField and register its listener
      enterField = new JTextField( "Enter file URL here",24 );      enterField.addActionListener(         new ActionListener() {            // get document specified by user
            public void actionPerformed( ActionEvent event )
            {
             urllist.push( event.getActionCommand() );
             getThePage( event.getActionCommand() );
            }         }  // end anonymous inner class      ); // end call to addActionListener      enterField.addMouseListener(
       new MouseListener(){
       public void mouseClicked( MouseEvent e )
       {
       enterField.selectAll();
       }       public void mousePressed( MouseEvent e )
       {
       }       public void mouseReleased( MouseEvent e )
       {
       }
       public void mouseEntered( MouseEvent e )
       {
       }
      
      
       public void mouseExited( MouseEvent e )
       {
       }
       }
      );      contralPanel.add( enterField );      container.add( contralPanel, BorderLayout.NORTH );
      // create contentsArea and register HyperlinkEvent listener
      contentsArea = new JEditorPane();
      contentsArea.setEditable( false );      contentsArea.addHyperlinkListener(         new HyperlinkListener() {            // if user clicked hyperlink, go to specified page
            public void hyperlinkUpdate( HyperlinkEvent event )
            {
               if ( event.getEventType() ==
                    HyperlinkEvent.EventType.ACTIVATED )
                    {
                     urllist.push( event.getURL().toString() );
                     getThePage( event.getURL().toString() );
                    }
            }         }  // end anonymous inner class      ); // end call to addHyperlinkListener      container.add(  new JScrollPane( contentsArea ) ,
         BorderLayout.CENTER );      setSize( 1000, 700 );
      setVisible( true );
   }   // load document; change mouse cursor to indicate status
   private void getThePage( String location )
   {
      // change mouse cursor to WAIT_CURSOR
      setCursor( Cursor.getPredefinedCursor(
         Cursor.WAIT_CURSOR ) );      if(!location.startsWith( "http://" ))
       location="http://" + location;      // load document into contentsArea and display location in
      // enterField
      try {
         contentsArea.setPage( location );
         enterField.setText( location );
      }      // process problems loading document
      catch ( IOException ioException ) {
         JOptionPane.showMessageDialog( this,
            "Error retrieving specified URL",
            "Bad URL", JOptionPane.ERROR_MESSAGE );
      }      setCursor( Cursor.getPredefinedCursor(
         Cursor.DEFAULT_CURSOR ) );
   }   // begin application execution
   public static void main( String args[] )
   {
      WebReader application = new WebReader();      application.setDefaultCloseOperation(
         JFrame.EXIT_ON_CLOSE );   }}  // end class WebReader
class URLList
{
private int now = 0;
private Vector vector; URLList()
{
vector = new Vector( 1 );
}
public void push(String url)
{
vector.add(url);
now = vector.size()-1;
} public String back()
{
System.out.println( "back  " + now );
if( now == 0 )
return( "Noop" );
else
{
now--;
System.out.println( "back----" + now );
return( (String)vector.get( now ));
}
} public String forward()
{
System.out.println( "fwd" + now + vector.size() );
if( ( vector.size() == 0 ) || ( now == ( vector.size()-1 ) ) )
return( "Noop" );
else
{
now++;
System.out.println( "fwd+++" + now );
return( (String)vector.get( now ));
}
}
public int getnow()
{
return now;
}
}