Jump to content

Grinding Java - Searching the WWW in Java:grinding4.java

From EDM2
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import ProgressBar;

/**
 * this class reports the status of a search in progress.
 * it is a simple dialog which every search engine has access to and it
 * reports the progress of a search.
**/
class ProgressIndicator extends Dialog implements ActionListener
{
   /**
    * The constructor builds the UI for showing the progress of the Search
   **/
   public ProgressIndicator(Frame owner)
   {
      super(owner);
      this.owner = owner;
      setLayout(new BorderLayout());

      Panel p = new Panel();
      p.setLayout(new BorderLayout());
      p.add("South",availableMemory);
      p.add("North", data);
      add("North",p);

      availableMemory.setSize(20,100); // add a meter for the available
                                       // amount of memory.
      add("South", stopButton);

      p = new Panel();
      p.setLayout(new BorderLayout());
      p.add("South", checking);
      p.add("North", new Label("Checking the following URLS:                  "));
      // notice the size of the 2 strings is to force a certain size on the
      // panel which will the force that size on the Lists.

      add("East", p);

      p = new Panel();
      p.setLayout(new BorderLayout());
      p.add("South", pending);
      p.add("North", new Label("The following URLS are pending search:        "));

      add("West", p);

      stopButton.addActionListener(this);
      pack();
      show();
   }

   /**
    * shows progress information on the top of the dialog. I used this for
    * debugging but figured it's interesting.
   **/
   public void addInformation(String newData)
   {
      availableMemory.setPercentage(
           (int)(((double)WWWUtility.runtimeInformation.freeMemory()) /
                  WWWUtility.runtimeInformation.totalMemory() * 100));
      data.setText(newData);
   }

   /**
    * Adds a URL to the list of the URLs we are currently checking.
   **/
   public void addToListOfChecking(String data)
   {
      checking.add(data);
   }

   /**
    * Remove a URL from the list of the URLs we are currently checking.
   **/
   public void removeFromListOfChecking(String data)
   {
      checking.remove(data);
   }

   /**
    * Remove a URL from the list of the URLs we are currently checking.
   **/
   public void addToListOfPending(String data)
   {
      pending.add(data);
   }

   /**
    * Adds a URL to the list of the URLs we are currently checking.
   **/
   public void removeFromListOfPending(String data)
   {
      pending.remove(data);
   }

   /**
    * Invoked when actions occur.
    * This method is a part of the ActionListener interface.
   **/
   public void actionPerformed(ActionEvent e)
   {
      if(e.getSource() == stopButton)
          distributeEvent(new ActionEvent(this, EVT_STOP_BUTTON_PRESSED, "STOP"));
   }

   /**
    * adds an ActionListener to which to distribute news of
    * user input to the ProgressIndicator.
   **/
   public void addActionListener(ActionListener listener)
   {
      listeners.addElement(listener);
   }

   /**
    * removes an ActionListener so news will not be distributed
    * to that class.
   **/
   public void removeActionListener(ActionListener listener)
   {
      listeners.removeElement(listener);
   }

   /**
    * this method distributes the event among all the listeners.
   **/
   private void distributeEvent(ActionEvent e)
   {
      for (int iterator = 0 ; iterator < listeners.size() ; iterator++)
          ((ActionListener)listeners.elementAt(iterator)).actionPerformed(e);
   }

   public static final int EVT_STOP_BUTTON_PRESSED = 1; // the id of the event
                                           // when the stop button is pressed.

        private ProgressBar availableMemory =
                new ProgressBar(Color.black,Color.blue,Color.white);
        private Vector listeners = new Vector();//list of the action listeners.
        private Frame owner;
        private Label data = new Label("Progress"); // the information
                // displayed on the top of the dialog.
        private Button stopButton = new Button("Stop"); // the stop button,
                // to stop the search.
        private List checking = new List(15); // list of URLs which are being searched.
        private List pending = new List(15);  // list of URLs which are pending search.
}