//////////////////////////////////////////////////
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 general.GeneralProperties;
import network.Switch;
import network.Link;
import network.Network;
import gui.FramedPanel;
import gui.GUICanvas;
//////////////////////////////////////////////////
public class dEditLink extends GridBagDialog {

private GeneralProperties gpData;
private FramedPanel pMain;		   
private TextField tfName;	    // Inputs name of the link.
private TextField tfCapL2R;
private TextField tfCapR2L;
private TextField tfLength;
private static int DefNameCount = 1; // Keeps the count of the number of
				     // current link. Used for generating
				     // default name. 
private Choice cLeftEP;		    // Switch at Left end point.
private Choice cRightEP;	    // Switch at Right endpoint.
private Vector switchVec;
private Link link;
private FramedPanel pCostFn;	    // Displays cost function for the
				    // link.

private FramedPanel pButtons;	    // Holds buttons.
private Button bOk;		    // Ok button.
private Button bCancel;		    // Cancel button.
private Button bHelp;		    // Help button.
//---------------------------------------------------------
public dEditLink(Frame parent, Link l, GeneralProperties gp) {
// REQUIRES: nothing.
// RETURNS: nothing.
// PROMISES: creates dialog box Edit|Add|Link.

  super(parent, "Edit Link", true);
  gpData = gp;
  link = l;

  // Making the Main panel.
  pMain = new FramedPanel(FramedPanel.SINGLE_BORDER);
  pMain.setLayout(new GridLayout(6, 2));
  pMain.add(new Label("Link Name: ", Label.LEFT));
  pMain.add(tfName = new TextField(link.getName()));

  cLeftEP = new Choice();
  cRightEP = new Choice();
  
  Network n = (Network)gpData.get("Network");
  n.updateSwitchVec();
  switchVec = n.switchVec;
  for ( int i = 0; i < switchVec.size(); i++ ) {
    cLeftEP.addItem( ((Switch)switchVec.elementAt(i)).getName() );
    cRightEP.addItem( ((Switch)switchVec.elementAt(i)).getName() );
  }
  cLeftEP.select( switchVec.indexOf(link.getL()) );  // Select left switch.
  cRightEP.select( switchVec.indexOf(link.getR()) ); // Select right switch.

  pMain.add(new Label("Left Switch: ", Label.LEFT));
  pMain.add(cLeftEP);
  pMain.add(new Label("Right Switch: ", Label.LEFT));
  pMain.add(cRightEP);
  pMain.add(new Label("Length:", Label.LEFT));
  tfLength = new TextField("" + link.getLength());
  pMain.add( tfLength );
  pMain.add(new Label("CapL2R:", Label.LEFT));
  tfCapL2R = new TextField("" + link.getL2R());
  pMain.add( tfCapL2R );
  pMain.add(new Label("CapR2L", Label.LEFT));
  tfCapR2L = new TextField("" + link.getR2L());
  pMain.add( tfCapR2L );

  gbc.fill = GridBagConstraints.BOTH;
  gbc.weightx = 100;
  gbc.weighty = 100;
  add(pMain, 0, 0, 1, 6);

  /*
  // Creating panel for cost function.
  pCostFn = new FramedPanel(FramedPanel.SINGLE_BORDER);
  pCostFn.add(new Label("Link Cost Function : To be Completed"));
  // Need to complete switch cost function panel. 

  gblOverall.setConstraints(pCostFn, gbcOverall);
  add(pCostFn);
  */

  // Add ok, cancel and help buttons at the bottom in a separate panel.
  pButtons = new FramedPanel(FramedPanel.SINGLE_BORDER);
  pButtons.add(bOk = new Button("Ok"));
  pButtons.add(bCancel = new Button("Cancel"));
  pButtons.add(bHelp = new Button("Help"));

  gbc.fill = GridBagConstraints.HORIZONTAL;
  add(pButtons, 0, 4, 1, 1);

  pack();
  show();
}
//************************************************
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.target == bOk) {
    Switch sLeft = (Switch)
      switchVec.elementAt( (new Integer(cLeftEP.getSelectedIndex())).intValue() );
    Switch sRight = (Switch)
      switchVec.elementAt( (new Integer(cRightEP.getSelectedIndex())).intValue() );
    Network n = (Network)gpData.get("Network");
    double length = (new Double(tfLength.getText())).doubleValue();
    double capL2R = (new Double(tfCapL2R.getText())).doubleValue();
    double capR2L = (new Double(tfCapR2L.getText())).doubleValue();
    if (sLeft.equals(link.getL()) && sRight.equals(link.getR())) {
      link.setName(tfName.getText());
      link.setLength(length);
      link.setL2R(capL2R);
      link.setR2L(capR2L);
    }
    else {
      n.removeLink(n.links, link);
      Link lk = new Link(tfName.getText(), sLeft, sRight, length);
      lk.setL2R(capL2R);
      lk.setR2L(capR2L);
      n.addLink(lk);
    }
    
    dispose();
  }
  else if(e.target == bCancel) {
    dispose();
  }
  else if(e.target == bHelp) {
    String HelpURL = (String)gpData.
      get("Help URL for Edit|Link|EditLink 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); 
    }
  }
  else
    super.handleEvent(e);

  return true;			    // No need to pass event to parent as
				    // the dialog box is anyway model. 
}
//************************************************
} // End of class dEditLink
//////////////////////////////////////////////////
