Source of SiteSelector.java


  1: // Fig. 24.2: SiteSelector.java
  2: // This program loads a document from a URL.
  3: import java.net.MalformedURLException;
  4: import java.net.URL;
  5: import java.util.HashMap;
  6: import java.util.ArrayList;
  7: import java.awt.BorderLayout;
  8: import java.applet.AppletContext;
  9: import javax.swing.JApplet;
 10: import javax.swing.JLabel;
 11: import javax.swing.JList;
 12: import javax.swing.JScrollPane;
 13: import javax.swing.event.ListSelectionEvent;
 14: import javax.swing.event.ListSelectionListener;
 15: 
 16: public class SiteSelector extends JApplet 
 17: {
 18:    private HashMap< Object, URL > sites; // site names and URLs
 19:    private ArrayList< String > siteNames; // site names
 20:    private JList siteChooser; // list of sites to choose from
 21: 
 22:    // read HTML parameters and set up GUI
 23:    public void init()
 24:    {
 25:       sites = new HashMap< Object, URL >(); // create HashMap
 26:       siteNames = new ArrayList< String >(); // create ArrayList
 27: 
 28:       // obtain parameters from HTML document
 29:       getSitesFromHTMLParameters(); 
 30: 
 31:       // create GUI components and layout interface
 32:       add( new JLabel( "Choose a site to browse" ), BorderLayout.NORTH );
 33: 
 34:       siteChooser = new JList( siteNames.toArray() ); // populate JList
 35:       siteChooser.addListSelectionListener(
 36:          new ListSelectionListener() // anonymous inner class
 37:          {
 38:             // go to site user selected
 39:             public void valueChanged( ListSelectionEvent event )
 40:             {
 41:                // get selected site name
 42:                Object object = siteChooser.getSelectedValue();
 43: 
 44:                // use site name to locate corresponding URL
 45:                URL newDocument = sites.get( object );
 46: 
 47:                // get applet container
 48:                AppletContext browser = getAppletContext();
 49: 
 50:                // tell applet container to change pages
 51:                browser.showDocument( newDocument );
 52:             } // end method valueChanged
 53:          } // end anonymous inner class
 54:       ); // end call to addListSelectionListener
 55: 
 56:       add( new JScrollPane( siteChooser ), BorderLayout.CENTER );
 57:    } // end method init
 58: 
 59:    // obtain parameters from HTML document
 60:    private void getSitesFromHTMLParameters()
 61:    {
 62:       String title; // site title
 63:       String location; // location of site
 64:       URL url; // URL of location
 65:       int counter = 0; // count number of sites
 66: 
 67:       title = getParameter( "title" + counter ); // get first site title
 68: 
 69:       // loop until no more parameters in HTML document
 70:       while ( title != null ) 
 71:       {
 72:          // obtain site location
 73:          location = getParameter( "location" + counter );
 74:             
 75:          try // place title/URL in HashMap and title in ArrayList
 76:          {
 77:             url = new URL( location ); // convert location to URL
 78:             sites.put( title, url ); // put title/URL in HashMap
 79:             siteNames.add( title ); // put title in ArrayList
 80:          } // end try
 81:          catch ( MalformedURLException urlException ) 
 82:          {
 83:             urlException.printStackTrace();
 84:          } // end catch
 85: 
 86:          counter++;
 87:          title = getParameter( "title" + counter ); // get next site title
 88:       } // end while
 89:    } // end method getSitesFromHTMLParameters
 90: } // end class SiteSelector
 91: 
 92: /**************************************************************************
 93:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 94:  * Pearson Education, Inc. All Rights Reserved.                           *
 95:  *                                                                        *
 96:  * DISCLAIMER: The authors and publisher of this book have used their     *
 97:  * best efforts in preparing the book. These efforts include the          *
 98:  * development, research, and testing of the theories and programs        *
 99:  * to determine their effectiveness. The authors and publisher make       *
100:  * no warranty of any kind, expressed or implied, with regard to these    *
101:  * programs or to the documentation contained in these books. The authors *
102:  * and publisher shall not be liable in any event for incidental or       *
103:  * consequential damages in connection with, or arising out of, the       *
104:  * furnishing, performance, or use of these programs.                     *
105:  *************************************************************************/