FileDocCategorySizeDatePackage
Chapter2.javaAPI DocExample7788Thu Oct 04 11:59:12 BST 2001None

Ch2Sol123

public class Ch2Sol123 extends JFrame

Fields Summary
private JEditorPane
html
private JComboBox
location
private JLabel
statusLine
private JButton
back
private JButton
forward
private HistoryList
historyList
Constructors Summary
public Ch2Sol123(String site)


	    
		setSize( 550, 600 );
		setTitle( "Chapter Two Solutions" );
		addWindowListener( new WindowAdapter() {
			public void windowClosing( WindowEvent e )
			{	System.exit( 0 );
			}
		});

		// Build more interesting interface
/**/	location = new JComboBox();
/**/  location.setEditable( true );
/**/	location.getEditor().setItem( site );
		
		JPanel topLine = new JPanel();
		topLine.setLayout( new FlowLayout( FlowLayout.LEFT ) );
      topLine.add( back );
      topLine.add( forward );
		topLine.add( new JLabel( "URL:" ) );
		topLine.add( location );		
		
		// for the JFrame:
		this.getContentPane().setLayout( new BorderLayout() );
		this.getContentPane().add( topLine, BorderLayout.NORTH );
		this.getContentPane().add( statusLine, BorderLayout.SOUTH );

		if( !site.equals( "NoURL" ) )
		{  if( loadURL( site ) )
		   {  try
		      {  historyList.add( new URL( site ) );
		      }
		      catch( Exception ex ) {} // it must work as loadURL returned true.
		      location.addItem( 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

		setVisible( true );		
		
/**/  // Event handlers - new code but much has simply been moved here

      // First pick up <return> pressed in editable section of the
      // JComboBox.  Need to add the new item to the JComboBox list.		
		location.addActionListener( new ActionListener() {
		   public void actionPerformed( ActionEvent e )
		   {  String newSite = location.getEditor().getItem().toString();
		      if( loadURL( newSite ) )
		      {  try
		         {  historyList.add( new URL( newSite ) );
		         }
		         catch( Exception ex ) {} // it must work as loadURL returned true.
		         int index = -1;
		         for( int item = 0; item < location.getItemCount(); item++ )
		         {  if( location.getItemAt( item ).equals( newSite ) )
		            {  index = item;
		            }
		         }
		         if( index == -1 ) // new item, not in list, so put at front
		         {  location.insertItemAt( newSite, 0 );
		         }
		      }
         }
      });

      // This one is needed to capture an item picked from the list
      location.addItemListener( new ItemListener() {
         public void itemStateChanged( ItemEvent e )
         {  if( e.getStateChange() == ItemEvent.SELECTED )
            {  boolean b = loadURL( e.getItem().toString() ); // must work!
            }
         }
      });
		
      // history list buttons:
      back.addActionListener( new ActionListener() {
      	public void actionPerformed( ActionEvent e )
         {	historyList.back();
            loadURL( historyList.getCurrent().toString() );    	
     	   }
     	});

      forward.addActionListener( new ActionListener() {
 	      public void actionPerformed( ActionEvent e )
	      {	historyList.forward();
	         loadURL( historyList.getCurrent().toString() );
         }
	   });

/**/  // hyperlinkListener moved to here		
		html.addHyperlinkListener( new HyperlinkListener() {
			public void hyperlinkUpdate( HyperlinkEvent e )
	      {	if( e.getEventType() == HyperlinkEvent.EventType.ACTIVATED )
		      {	if( loadURL( e.getURL().toString() ) )
		         {   historyList.add( e.getURL() );
		         }
		      }
		      if( e.getEventType() == HyperlinkEvent.EventType.ENTERED )
		      {	statusLine.setText( "" + e.getURL() );
		      }
		      if( e.getEventType() == HyperlinkEvent.EventType.EXITED )
		      {	statusLine.setText( "" );
		      }
	      }
      });		

/**/  // end of event handlers
		
		
	
Methods Summary
private voiderror(java.lang.String message)

  JOptionPane.showMessageDialog( this,
	                                  message,
	                                  "Error",
	                                  JOptionPane.ERROR_MESSAGE);
	
private booleanloadURL(java.lang.String site)

	URL url = null;
	   try
		{	url = new URL( site );
		}
		catch( MalformedURLException e )
		{	error( "Invalid URL specified" );
			return false;
		}
		try
		{	html.setPage( url ); // load page
		}
		catch( IOException e )
		{	error( "Can't load url: " + url );
			return false;
		}
/**/  // set it as the currently displayed item in the JComboBox
/**/  // but DO NOT add it to the list at this point
/**/  location.getEditor().setItem( site );
      return true;		
	
public static voidmain(java.lang.String[] args)

	if( args.length == 0 )
		{	new Ch2Sol123( "NoURL" );
		}
		else
		{	new Ch2Sol123( args[0] );
		}