/*
 * Aleph Toolkit
 *
 * Copyright 1999, Brown University, Providence, RI.
 * 
 *                         All Rights Reserved
 * 
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose other than its incorporation into a
 * commercial product is hereby granted without fee, provided that the
 * above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Brown University not be used in
 * advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission.
 * 
 * BROWN UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ANY
 * PARTICULAR PURPOSE.  IN NO EVENT SHALL BROWN UNIVERSITY BE LIABLE FOR
 * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

package aleph.gui;

import aleph.Host;
import java.awt.Checkbox;
import java.awt.CheckboxMenuItem;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Enumeration;
import java.util.Vector;

/**
 * Menu that manages the known hosts.
 * @see aleph.gui.Console
 *
 * @author Maurice Herlihy
 * @date January 1999
 **/

public class HostMenu extends Menu {

  private int    firstHost;	// index of first host in menup
  private Vector knownHosts    = new Vector();
  private State  state;

  /**
   * Sole constructor.
   **/
  public HostMenu (State state) {
    super("Hosts", true);
    this.state = state;

    // Recheck whether hosts are live.  Select all live hosts.
    MenuItem refresh = new MenuItem("Refresh");
    refresh.addActionListener(new ActionListener() { // anonymous class
      public void actionPerformed (ActionEvent e) { // Quoi faire?
	HostMenu.this.state.selectedHosts.setSize(0); // emtpy the vector
	HostMenu.this.state.liveHosts.setSize(0);
	for (int i = firstHost; i < getItemCount(); i++) {
	  HostCheckbox hostCheckbox = (HostCheckbox) getItem(i);
	  hostCheckbox.refresh();
	  if (hostCheckbox.isEnabled()) {
	    HostMenu.this.state.selectedHosts.addElement(hostCheckbox.host);
	    HostMenu.this.state.liveHosts.addElement(hostCheckbox.host);
          }
	}
      }
    });
    add(refresh);		// add this item to menu

    // Learn about a new host
    MenuItem addHost = new MenuItem("Add");
    addHost.addActionListener(new ActionListener() { // anonymous class
      public void actionPerformed (ActionEvent e) {
	HostDialog dialog = new HostDialog(HostMenu.this.state.frame);
	dialog.pack();
	dialog.setVisible(true);
	Host host = dialog.getHost();	// Get host from user.
	if (host != null) {	// user didn't cancel
	  if (HostMenu.this.knownHosts.indexOf(host) < 0) { // not a duplicate
	    HostMenu.this.knownHosts.addElement(host);
	    HostCheckbox hostCheckbox = new HostCheckbox(host);
	    add(hostCheckbox);
	    if (hostCheckbox.isEnabled()) {// host is live
	      HostMenu.this.state.selectedHosts.addElement(host);
	      HostMenu.this.state.liveHosts.addElement(host);
            }
	  } else {
	    new ErrorDialog(HostMenu.this.state.frame, "duplicate host " + host);
	  }
	}
	dialog.dispose();
      }
    });
    add(addHost);

    addSeparator();
    firstHost = getItemCount();

    // now fill in one button for each known host and select the live ones
    for (Enumeration enum = Host.allHosts(); enum.hasMoreElements(); ) {
      Host host = (Host) enum.nextElement();
      HostCheckbox hostCheckbox = new HostCheckbox(host);
      add(hostCheckbox);
      if (hostCheckbox.isEnabled()) {
	state.selectedHosts.addElement(hostCheckbox.host);
	state.liveHosts.addElement(hostCheckbox.host);
      }
    }
  }

  /**
   * Inner class representing host button
   **/
  private class HostCheckbox extends CheckboxMenuItem {
    public Host host;
    public HostCheckbox (Host host) {
      super(host.toString());
      this.host = host;
      // update selectedHosts when user selects or deselects
      addItemListener(new ItemListener() {
      public void itemStateChanged (ItemEvent e) {
        if (getState())
          HostMenu.this.state.selectedHosts.addElement(HostCheckbox.this.host);
        else
          HostMenu.this.state.selectedHosts.removeElement(HostCheckbox.this.host);
      }});
      if (host.ping()) {	// live one
	setState(true);
	setEnabled(true);
      } else {			// no answer
	setState(false);
	setEnabled(false);
      }
    }
    public void refresh () {
      if (host.ping()) {	// live one
	setEnabled(true);
      } else {			// no answer
	setState(false);
	setEnabled(false);
      }
    }
    public boolean equals (Object anObject) {
      if ((anObject != null) && (anObject instanceof HostCheckbox)) {
	HostCheckbox other = (HostCheckbox) anObject;
	return (other.host.equals(this.host));
      } else {
	return false;
      }
    }
  }
}
