//////////////////////////////////////////////////
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 physical.PhysicalGraph;
import physical.Place;
import network.Switch;
import network.Network;
import gui.FramedPanel;
import gui.GUICanvas;
//////////////////////////////////////////////////
public class dEditAddSwitch extends Dialog {

private GeneralProperties gpData;
private FramedPanel pMain;		   
private Hashtable htPlaces;
private TextField tfName;	    // Inputs name of the switch.
private static int DefNameCount = 1; // Keeps the count of the number of
				     // current switch. Used for generating
				     // default name. 
private Choice cPlace;		    // Place where this switch is placed.
private Label lCoordX;
private Label lCoordY;
private Label lAlpha;
private Label lOmega;

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 dEditAddSwitch(Frame parent, GeneralProperties gp) {
// REQUIRES: nothing.
// RETURNS: nothing.
// PROMISES: Creates and displays dialog box Edit|Add Switch.

  super(parent, "Add Switch", 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(6, 2, gap, gap));
  pMain.add(new Label("Switch Name: ", Label.LEFT));
  pMain.add(tfName = new TextField("Switch"+DefNameCount));

  cPlace = new Choice();
  PhysicalGraph pg = (PhysicalGraph)gpData.get("Physical Graph");
  htPlaces = new Hashtable();
  Place p;
  int Index = 0;		    // Index in choice.
  for (Enumeration e=pg.places.elements(); e.hasMoreElements(); ) {
    p = (Place) e.nextElement();
    cPlace.addItem( p.getName() );
    htPlaces.put(new Integer(Index), p);
    ++Index;
  }

  cPlace.select(0);		    // Select first item.
  p = (Place)htPlaces.get(new Integer(0)); // Display data related to
					   // selected place.  


  pMain.add(new Label("Place : ", Label.LEFT));
  pMain.add(cPlace);
  pMain.add(new Label("X Coordinate : ", Label.LEFT));
  lCoordX = new Label(""+p.x, Label.LEFT);
  pMain.add(lCoordX);
  pMain.add(new Label("Y Coordinate : ", Label.LEFT));
  pMain.add(lCoordY = new Label(""+p.y, Label.LEFT));
  pMain.add(new Label("Alpha : ", Label.LEFT));
  pMain.add(lAlpha = new Label(""+p.alpha, Label.LEFT));
  pMain.add(new Label("Omega : ", Label.LEFT));
  pMain.add(lOmega = new Label(""+p.omega, Label.LEFT));

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

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

  gbcOverall.insets = new Insets(0, gap, gap, gap);
  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;
  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 == cPlace) {
    // Display data related to selected place.
    Place p = (Place)htPlaces.get(new Integer(cPlace.getSelectedIndex())); 
    lCoordX.setText(""+p.x);
    lCoordY.setText(""+p.y);
    lAlpha.setText(""+p.alpha);
    lOmega.setText(""+p.omega);
  }
  else if(e.target == bOk) {
    ++DefNameCount;		    // A newer switch has been added so,
				    // increment DefNameCount.
    Place p = (Place)htPlaces.get(new Integer(cPlace.getSelectedIndex()));
    Network n = (Network)gpData.get("Network");
    n.addSwitch(new Switch(tfName.getText(), p));
    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;
}
//************************************************
} // End of class dEditAddSwitch
//////////////////////////////////////////////////
