/*
 * $Log: dEditDelLink.java,v $
# Revision 1.4  1996/07/11  01:20:36  hma
# intermediate working version
#
# Revision 1.3  1996/06/10  16:50:06  inder
# minor changes.
#
# Revision 1.2  1996/06/06  05:16:46  inder
# made the button panel vertically unresizable.
#
# Revision 1.1  1996/05/13  16:45:53  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.dMessageBox;
import gui.GUICanvas;
//////////////////////////////////////////////////
public class dEditDelLink extends Dialog {

private GeneralProperties gpData;
private FramedPanel pMain;		   
private Choice cLinks;
Hashtable htLinks;		    // Keeps track of item in cLinks and
				    // correponding place.
private Label lLeftEP;
private Label lRightEP;

private FramedPanel pButtons;	    // Holds buttons.
private Button bOk;		    // Ok button.
private Button bCancel;		    // Cancel button.
private Button bHelp;		    // Help button.
//************************************************
public dEditDelLink(Frame parent, GeneralProperties gp) {
// REQUIRES: At least one link exists in network.
// RETURNS: nothing.
// PROMISES: Creates and displays dialog box Edit|Delete|Link. Removes
// selected link from the network if ok button is pressed.

  super(parent, "Delete 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));
  cLinks = new Choice();

  Network n = (Network)gpData.get("Network");
  htLinks = new Hashtable();
  Link l;
  int Index = 0;		    // Index in choice.
  for (Enumeration e=n.links.elements(); e.hasMoreElements(); ) {
    l = (Link) e.nextElement();
    cLinks.addItem(l.getName());
    htLinks.put(new Integer(Index), l);
    ++Index;
  }

  cLinks.select(0);		    // Select first item.
  l = (Link)htLinks.get(new Integer(0)); // Display data related to
					 // selected place. 
  pMain.add(new Label("Link : ", Label.LEFT));
  pMain.add(cLinks);
  pMain.add(new Label("Left Endpoint : ", Label.LEFT));
  pMain.add(lLeftEP = new Label(""+l.getL().getName(), Label.LEFT));
  pMain.add(new Label("Right Endpoint : ", Label.LEFT));
  pMain.add(lRightEP = new Label(""+l.getR().getName(), Label.LEFT));

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

  // 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 == cLinks) {
    Link l = (Link)htLinks.get(new Integer(cLinks.getSelectedIndex()));  
    lLeftEP.setText(""+l.getL().getName());
    lRightEP.setText(""+l.getR().getName());
  }
  else if(e.target == bOk) {
    Link l = (Link)htLinks.get(new Integer(cLinks.getSelectedIndex())); 
    Network n = (Network)gpData.get("Network");
    n.removeLink(l);
    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|Delete|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;
}
//************************************************
} // End of class dEditDelLink.
//////////////////////////////////////////////////
