/*
 * $Log: dEditAddLink.java,v $
# Revision 1.16  1996/07/11  01:18:35  hma
# intermediate working version
#
# Revision 1.15  1996/06/10  16:49:21  inder
# minor changes.
#
# Revision 1.14  1996/06/06  05:15:01  inder
# made the button panel vertically unresizable.
#
# Revision 1.13  1996/05/13  15:58:39  inder
# Now creates a link and adds it to Network.
#
# Revision 1.12  1996/05/13  08:14:32  inder
# keyboard events now passed to components.
#
# Revision 1.11  1996/05/13  00:44:12  inder
# minor changes.
#
# Revision 1.10  1996/05/12  07:27:30  inder
# relected changes in the constructor of dMessageBox.
#
# Revision 1.9  1996/05/12  06:31:18  inder
# Now, all required data structures are extracted from gpData.
#
# Revision 1.8  1996/05/06  20:01:42  inder
# Now help URL is correctly visited by browser.
#
# Revision 1.7  1996/05/06  19:18:22  inder
# Fixed problem of MainWindow disappearing when this dialog box is
# displayed.
#
# Revision 1.6  1996/04/22  06:12:31  inder
# Changed layout to GridBagLayout.
#
# Revision 1.5  1996/04/15  05:11:32  inder
# minor changes.
#
# Revision 1.4  1996/04/08  19:10:19  inder
# removed pack() statement from show() because it was collapsing the
# dialog box thus making it invisible.
#
# Revision 1.3  1996/04/06  22:51:10  inder
# added Ok,Cancel and Help buttons.
#
# Revision 1.2  1996/04/06  19:34:00  inder
# removed import of network.Network.
#
# Revision 1.1  1996/04/06  19:32:45  inder
# 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 general.GeneralProperties;
import network.Switch;
import network.Link;
import network.Network;
import gui.FramedPanel;
import gui.GUICanvas;
//////////////////////////////////////////////////
public class dEditAddLink extends Dialog {

private GeneralProperties gpData;
private FramedPanel pMain;		   
private TextField tfName;	    // Inputs name of the link.
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 Hashtable htSwitches;
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 dEditAddLink(Frame parent, GeneralProperties gp) {
// REQUIRES: nothing.
// RETURNS: nothing.
// PROMISES: creates dialog box Edit|Add|Link.

  super(parent, "Add Link", true);
  gpData = gp;

  GridBagLayout gblOverall = new GridBagLayout();
  setLayout(gblOverall);
  GridBagConstraints gbcOverall = new GridBagConstraints();
  gbcOverall.weightx = 1.0;
  gbcOverall.weighty = 1.0;
  int gap = ((Integer)gpData.get("Inter GUI component gap")).intValue();
  gbcOverall.insets = new Insets(gap, gap, gap, gap);
  gbcOverall.gridwidth = GridBagConstraints.REMAINDER;
  gbcOverall.fill = GridBagConstraints.BOTH;

  // Making the Main panel.
  pMain = new FramedPanel(FramedPanel.SINGLE_BORDER);
  pMain.setLayout(new GridLayout(3, 2, gap, gap));
  pMain.add(new Label("Link Name: ", Label.LEFT));
  pMain.add(tfName = new TextField("Link"+DefNameCount));

  cLeftEP = new Choice();
  cRightEP = new Choice();
  
  int Index = 0;		    // Index in choices.
  Network n = (Network)gpData.get("Network");
  htSwitches = new Hashtable();
  Switch s;
  for (Enumeration e=n.switches.elements(); e.hasMoreElements(); ) {
    s = (Switch) e.nextElement();
    cLeftEP.addItem(s.getName());
    cRightEP.addItem(s.getName());
    htSwitches.put(new Integer(Index), s);
    ++Index;
  }
  cLeftEP.select(0);		    // Select first switch.
  cRightEP.select(cRightEP.countItems()-1); // Select last switch.

  pMain.add(new Label("Left Switch: ", Label.LEFT));
  pMain.add(cLeftEP);
  pMain.add(new Label("Right Switch: ", Label.LEFT));
  pMain.add(cRightEP);

  gblOverall.setConstraints(pMain, gbcOverall);
  add(pMain);

  // 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"));

  gbcOverall.weighty = 0.0;
  gbcOverall.fill = GridBagConstraints.HORIZONTAL;
  gbcOverall.insets = new Insets(0, gap, gap, gap);
  gblOverall.setConstraints(pButtons, gbcOverall);
  add(pButtons);

  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) {
    ++DefNameCount;		    // A newer link has been added so,
				    // increment DefNameCount.
    Switch sLeft = (Switch)htSwitches
      .get(new Integer(cLeftEP.getSelectedIndex()));
    Switch sRight = (Switch)htSwitches
      .get(new Integer(cRightEP.getSelectedIndex()));

    Network n = (Network)gpData.get("Network");
    n.addLink(new Link(tfName.getText(), sLeft, sRight));
    GUICanvas c = (GUICanvas)gpData.get("Canvas");
    c.paint(c.getGraphics());
    dispose();
  }
  else if(e.target == bCancel) {
    dispose();
  }
  else if(e.target == bHelp) {
    String HelpURL = (String)gpData.
      get("Help URL for Edit|Add|Link 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 dEditAddLink
//////////////////////////////////////////////////
