/*
 * $Log: dWindowSwitches.java,v $
 * Revision 1.5  1997/01/21 22:34:34  hma
 * done.
 *
# Revision 1.4  1997/01/20  20:25:42  hma
# gonna put functionality of adding and removing switches in this dialog.
#
# Revision 1.3  1996/10/23  17:00:06  hma
# call TextField.selectAll() when a switch is selected, so easy to edit.
#
# Revision 1.2  1996/10/23  16:54:07  hma
# add editing ability.
#
# Revision 1.1  1996/07/11  01:22:47  hma
# Initial revision
#
 */
//////////////////////////////////////////////////

package gui;

// Standard packages.
import java.applet.Applet;
import java.awt.*;
import java.util.*;
import java.net.URL;
import java.net.MalformedURLException;

// Network Design Tool Packages.
import network.*;
import general.GeneralProperties;
import general.Set;		// for network.switches
import gui.FramedPanel;
//////////////////////////////////////////////////
public class dWindowSwitches extends GridBagDialog {
  // swtArray references to the switch array of the network
  private Object[] swtArray;
  private GUICanvas canvas;
  private GeneralProperties gpData;
  private Network network;
  private List list;		// Scrolling list of switches
  private int sel = -1;		// item in the list selected
  private Button bAdd, bRemove, bOk, bUpdate, bHelp;
  private Canvas c;
  private TextField tfName, tfX, tfY, tfAlpha, tfOmega;
  private Panel pButtons, pEdit;

  private final String namePrefix = "Switch";
  private int nameCount = 0;

//--------------------------------------------------------
public dWindowSwitches(Frame parent, GeneralProperties gp) {
// REQUIRES: 
// RETURNS: nothing.
// PROMISES: 
// 1. Constructs graphics objects required for LinksWindow.
  
  super(parent, "Switches", false);
  gpData = gp;

  canvas = (GUICanvas)gpData.get("Canvas");
  network = (Network)gpData.get("Network");

  // add ok and cancel buttons to pButtons panel
  pButtons = new Panel();
  pButtons.setLayout(new GridLayout(1,5));
  pButtons.add(bAdd = new Button("Add"));
  pButtons.add(bRemove = new Button("Remove"));
  pButtons.add(bOk = new Button("Ok"));
  pButtons.add(bUpdate = new Button("Update"));
  pButtons.add(bHelp = new Button("Help"));
  gbc.weightx = 0;
  gbc.weighty = 0;
  gbc.fill = GridBagConstraints.NONE;
  gbc.anchor = GridBagConstraints.CENTER;
  add(pButtons, 0, 6, 1, 1);

  // add Edit Panel
  pEdit = new Panel();
  gbc.weightx = 100;
  gbc.fill = GridBagConstraints.HORIZONTAL;
  pEdit.setLayout ( new GridLayout (2, 5));
  pEdit.add (new Label("Name"));
  pEdit.add (new Label("X"));
  pEdit.add (new Label("Y"));
  pEdit.add (new Label("alpha"));
  pEdit.add (new Label("omega"));
  tfName = new TextField ();
  tfX = new TextField ();
  tfY = new TextField ();
  tfAlpha = new TextField ();
  tfOmega = new TextField ();
  pEdit.add (tfName);
  pEdit.add (tfX);
  pEdit.add (tfY);
  pEdit.add (tfAlpha);
  pEdit.add (tfOmega);
  add(pEdit, 0, 4, 1, 2);

  // add List of Switches
  list = new List(10, false);
  update();
  gbc.fill = GridBagConstraints.BOTH;
  gbc.weightx = 100;
  gbc.weighty = 100;
  add(list, 0, 0, 1, 4);  

  pack();
  show();
}


  /**
   * update the contents in the list from network.switches 
   */
  public void update() {
    network = (Network)gpData.get("Network");
    swtArray = network.switches.getArray();
    list.clear();
    for( int i = 0; i < swtArray.length; i++ )
      list.addItem( swtArray[i].toString() ); 
    list.select(sel);
  }


  /**
   * add a new switch to the network.
   */
  public void addSwitch() {
    network.addSwitch();
    update();			// update the display
  }

  /**
   * remove the selected switch from the network.
   */
  public void removeSwitch() {
   if (sel < 0 || sel >= list.countItems())
     return;
   network.removeSwitch( (Switch)swtArray[sel] );
   update();
  }

  /** change the name of the selected switch */
  public boolean changeName() {
    Switch swt = (Switch)swtArray[sel];
    String newName = tfName.getText();
    return network.changeName(swt, newName);
  }

public boolean action(Event evt, Object arg) {
  if (evt.target == bAdd) {
    addSwitch();
    canvas.repaint();
  }
  else if (evt.target == bRemove) {
    removeSwitch();
    canvas.repaint();
  }
  else if (evt.target == bOk) {
    hide();
    return true;
  }
  else if (evt.target == bUpdate) {
    update();
    return true;
  }
  else if (evt.target == bHelp) {
    String HelpURL = (String)gpData.
      get("Help URL for Window|Switches Dialog Box"); 
    if( ((Boolean)gpData.get("In Applet?")).equals(Boolean.TRUE) ) {
      // Open connection to URL & display help. 
      try {
	Applet app = (Applet)gpData.get("Applet");
	app.getAppletContext().showDocument(new URL(HelpURL)); 
      }
      catch(MalformedURLException mfue) {
	new dMessageBox((Frame)getParent(), "Help", "Help URL " + HelpURL
			+ " doesn't exist!", gpData, false, null);
      }
    }
    else {
      // Display Help Message in a Dialog Box.
      String HelpMsg = "Please visit " + HelpURL + " for help.";
      new dMessageBox((Frame)getParent(), "Help", HelpMsg, gpData, false,
		      null); 
    }
    return true;
  }
  else if (evt.target instanceof TextField) {
    Switch sltdSwt = (Switch)swtArray[sel];
    if (evt.target == tfName) {
      if ( !changeName() ) {
	String ErrMsg = "Can not change name. Maybe this name is used by another switch";
	new dMessageBox((Frame)getParent(), "Error", ErrMsg, gpData, false,
			null); 	
      }
    }
    else if (evt.target == tfX)
      sltdSwt.setX ( (Double.valueOf(tfX.getText())).doubleValue() );
    else if (evt.target == tfY)
      sltdSwt.setY ( (Double.valueOf(tfY.getText())).doubleValue() );
    else if (evt.target == tfAlpha)
      sltdSwt.setAlpha ( (Double.valueOf(tfAlpha.getText())).doubleValue() );
    else if (evt.target == tfOmega)
      sltdSwt.setOmega ( (Double.valueOf(tfOmega.getText())).doubleValue() );
    list.replaceItem(sltdSwt.toString(), sel);
    canvas.repaint();
  }
  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.WINDOW_DESTROY && 
     e.target == this)
    hide();
  else if (e.id == Event.LIST_SELECT) {
    sel = list.getSelectedIndex();
    Switch sltdSwt = (Switch)swtArray[sel];
    if (sel != -1) {
      tfName.setText (sltdSwt.getName());
      tfName.selectAll();
      tfX.setText (String.valueOf(sltdSwt.getX()));
      tfX.selectAll();
      tfY.setText (String.valueOf(sltdSwt.getY()));
      tfY.selectAll();
      tfAlpha.setText (String.valueOf(sltdSwt.getAlpha()));
      tfAlpha.selectAll();
      tfOmega.setText (String.valueOf(sltdSwt.getOmega()));
      tfOmega.selectAll();
    }
  }
  else
    super.handleEvent(e);
  return true;			    // No need to pass event to parent as
				    // the dialog box is anyway model. 
}

//************************************************
} // end of class dWindowSwitches

