/*
 * $Log$
 */

package gui;

// Standard packages.
import java.awt.*;
import java.util.*;

import network.*;
import general.*;

public class dSelectLinks extends GridBagDialog {
  public static final String ONE_DIRECTION = "one direction";
  public static final String TWO_DIRECTION = "two directions";
  private Frame parent;
  private GridBagPanel pLinks;
  private Panel pButtons;
  private Button bOk, bCancel, bHelp, bSelAll;
  private Choice cDirection;
  private List lAllLinks, lSelectedLinks;

  private Hashtable hSelectedLinks;
  private Network n;
  private Set sAllLinks;

  public dSelectLinks(Frame parent, Network n) {
    super(parent, "Select Links", true);
    this.parent = parent;

    this.n = n;
    sAllLinks = n.links;
    lAllLinks = new List(10, false);
    lSelectedLinks = new List(10, false);
    Enumeration e = sAllLinks.elements();
    while (e.hasMoreElements()) {
      lAllLinks.addItem( ((Link)e.nextElement()).getName() );
    }
    
    pLinks = new GridBagPanel();
    pLinks.add(new Label("All Links"), 0, 0, 2, 1);
    pLinks.add(new Label("Selected Links"), 2, 0, 2, 1);
    pLinks.gbc.weightx = 1.0;
    pLinks.gbc.weighty = 1.0;
    pLinks.gbc.fill = GridBagConstraints.BOTH;
    pLinks.add(lAllLinks, 0, 1, 2, 3);
    pLinks.add(lSelectedLinks, 2, 1, 2, 3);
    gbc.weightx = 1.0;
    gbc.weighty = 1.0;
    gbc.fill = GridBagConstraints.BOTH;
    add(pLinks, 0, 0, 5, 4);
    hSelectedLinks = new Hashtable();

    
    pButtons = new Panel();
    pButtons.setLayout(new GridLayout(1,5));
    pButtons.add(bSelAll = new Button("Sel All"));
    pButtons.add(bOk = new Button("Ok"));
    pButtons.add(bCancel = new Button("Cancel"));
    pButtons.add(bHelp = new Button("Help"));
    cDirection = new Choice();
    cDirection.addItem(ONE_DIRECTION);
    cDirection.addItem(TWO_DIRECTION);
    cDirection.select(0);
    pButtons.add(cDirection);
    gbc.weightx = 0;
    gbc.weighty = 0;
    add(pButtons, 0, 4, 5, 1);
    pack();
  }

  public boolean action (Event evt, Object arg) {
    if (evt.target == bSelAll) {
      lSelectedLinks.clear();
      hSelectedLinks = new Hashtable();
      Enumeration e = sAllLinks.elements();
      while (e.hasMoreElements()) {
	Link theLink = (Link)e.nextElement();
	hSelectedLinks.put(theLink.getName(), theLink);
	lSelectedLinks.addItem(theLink.getName());
      }
      return true;
    }
    else if (evt.target == bCancel) {
      n.setLinksToDimension(null);
      dispose();
    }
    else if (evt.target == bHelp) {

    }
    else if (evt.target == bOk) {
      n.setLinksToDimension(hSelectedLinks);
      n.setLinkDirection(cDirection.getSelectedItem());
      dispose();
    }
    return false;
  }

  public boolean handleEvent(Event e) {
    if(e.id == Event.KEY_PRESS) { // to let components handle keyboard
				// events. (required due to bug in java). 
      return false;
    }
    else if (e.id == Event.LIST_SELECT) {
      if (e.target == lAllLinks) {
	String selection = lAllLinks.getSelectedItem();
	if (!hSelectedLinks.containsKey(selection)) {
	  hSelectedLinks.put(selection, sAllLinks.get(selection));
	  lSelectedLinks.addItem(selection);
	}
      }
      else if (e.target == lSelectedLinks) {
	int index = lSelectedLinks.getSelectedIndex();
	String selection = lSelectedLinks.getSelectedItem();
	lSelectedLinks.delItem(index);
	hSelectedLinks.remove(selection);
      }
    }
    else {
      super.handleEvent(e);
    }
    return true;
  }
//************************************************
} // End of class dSelectLinks
//////////////////////////////////////////////////
