Monitor.java :  » Search-Engine » BDDBot » bdd » search » Java Open Source

Java Open Source » Search Engine » BDDBot 
BDDBot » bdd » search » Monitor.java
package bdd.search;

import java.awt.Frame;
import java.awt.Panel;
import java.awt.List;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.Checkbox;
import java.awt.GridLayout;
import java.awt.Button;
import java.awt.Event;
import java.net.URL;
import bdd.search.spider.Crawler;
import bdd.search.query.QueryWebServer;

/** Written by Tim Macinta 1997                           <br>
 *  Distributed under the GNU Public License
 *       (a copy of which is enclosed with the source).   <br>
 *                                                        <br> 
 *  Provides a graphical monitor to watch the progress of
 *  the crawler and the search engine.
 */

public class Monitor extends Frame {

  EnginePrefs prefs;  // preferences
  List query_list;    // list of queries
  List url_list;      // list of indexed URLs
  List error_list;    // list of URLs with errors
  Checkbox track_queries;
  Checkbox track_urls;
  Button clear_urls;
  Button clear_queries;
  Button start_crawler;
  Label current_url;
  Label bytes_indexed;

  /** Starts a monitor with the custom web server running on the default
   *  port.  */
  public Monitor() {
    this(EnginePrefs.port);
  }

  /** Starts a monitor with the custom web server running on the
   *  specified port.         */
  public Monitor(int port) {
    super("BDDBot");
    prefs = new EnginePrefs();
    Panel p = new Panel();
    setLayout(new GridLayout(0, 1));
    
    // set up query monitor

    add(p);
    p.setLayout(new BorderLayout());
    Panel p2 = new Panel();
    p2.setLayout(new FlowLayout());
    p.add("North", p2);
    p.add("Center", query_list = new List());
    p2.add(track_queries = new Checkbox("Watch Queries", null, true));
    p2.add(clear_queries = new Button("Clear List"));
    p2.add(start_crawler = new Button("Start Crawler"));
    p.add("South", new Label("_______________________________", Label.CENTER));

    // set up spider monitor
    
    add(p = new Panel());
    p.setLayout(new BorderLayout());
    p.add("North", p2 = new Panel());
    p2.setLayout(new GridLayout(0, 1));
    p2.add(current_url = new Label("Current URL:"));
    p2.add(bytes_indexed = new Label("Total Bytes:  0"));
    p.add("Center", url_list = new List());
    add(p = new Panel());
    p.setLayout(new BorderLayout());
    p.add("Center", error_list = new List());
    p2 = new Panel();
    p2.setLayout(new FlowLayout());
    p2.add(track_urls = new Checkbox("Watch Indexed URLs", null, true));
    p2.add(clear_urls = new Button("Clear URL List"));
    p.add("South", p2);
    
    // work around a bug in the Java 1.0 List

    for (int i = 0; i < 200; i++) {
      query_list.addItem("");
      url_list.addItem("");
      error_list.addItem("");
    }
    resize(408, 469);
    show();                    // display this monitor
    clearQueryList();
    clearURLList();
    clearErrorList();

    // register this monitor and start the web server

    prefs.monitor = this;
    new QueryWebServer(port, prefs);
  }

  public boolean action(Event evt, Object what) {
    if (evt.target == clear_urls) {
      clearURLList();
      clearErrorList();
    } else if (evt.target == clear_queries) {
      clearQueryList();
    } else if (evt.target == start_crawler) {
      start_crawler.disable();
      Crawler.main(prefs.getStartingFile(), prefs);
    }
    return super.action(evt, what);
  }

  /** Call this method when a new URL is being indexed.  */
  public void indexing(URL current_url) {
    if (track_urls.getState()) {
      String u = current_url.toString();
      url_list.addItem(u);
      this.current_url.setText("Current URL:  "+u);
      this.url_list.makeVisible(this.url_list.countItems()-1);
    }
  }
  
  /** Call this method when a new query is being placed.  */
  public void querying(String words) {
    if (track_queries.getState()) {
      query_list.addItem(words);
      if (query_list.countItems() > 201) {
  query_list.delItem(2);
      }
      query_list.makeVisible(query_list.countItems()-1);
    }
  }
  
  /** Call this method to report an error during crawling. */
  public void reportError(URL bad_url) {
    if (track_urls.getState()) {
      error_list.addItem(bad_url.toString());
      error_list.makeVisible(error_list.countItems()-1);
    }
  }

  /** Call this method when a crawler has finished crawling. */
  public void crawlerDone(Crawler c) {
    start_crawler.enable();
  }

  public void clearURLList() {
    url_list.clear();
    url_list.addItem("Processed");
    url_list.addItem("---------");
  }

  public void clearErrorList() {
    error_list.clear();
    error_list.addItem("Errors");
    error_list.addItem("------");
  }

  public void clearQueryList() {
    query_list.clear();
    query_list.addItem("Queries");
    query_list.addItem("-------");
  }

  public void bytesIndexed(long total_bytes) {
    this.bytes_indexed.setText("Total Bytes:  "+total_bytes);
  }

  public static void main(String arg[]) {
    new Monitor();
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.