/*
 * $Log: dEditSwitch.java,v $
# Revision 1.1  1996/07/17  03:56:53  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 general.GeneralProperties;
import network.Switch;
import gui.FramedPanel;
//////////////////////////////////////////////////
public class dEditSwitch extends GridBagDialog {

private Switch s;
private GeneralProperties gpData;
private FramedPanel pMain;		   
private TextField tfName;	    // Inputs name of the place.
private TextField tfCoordX;	    // Inputs X Coordinate of place.
private TextField tfCoordY;	    // Inputs Y Coordinate of place.
private TextField tfAlpha;	    // Inputs alpha of place.
private TextField tfOmega;	    // Inputs omega of place.
private FramedPanel pButtons;	    // Holds buttons.
private Button bOk;		    // Ok button.
private Button bCancel;		    // Cancel button.
private Button bHelp;		    // Help button.

//************************************************
public dEditSwitch(Frame parent, Switch swt, GeneralProperties gp) {
// REQUIRES: nothing.
// RETURNS: nothing.
// PROMISES: Creates and displays dialog box Edit|Add|Place.

  super(parent, "Edit Switch", true);

  s = swt;
  gpData = gp;
  int gap = ((Integer)gpData.get("Inter GUI component gap")).intValue();
  // Making the Main panel.
  pMain = new FramedPanel(FramedPanel.SINGLE_BORDER);
  pMain.setLayout(new GridLayout(5, 2, gap, gap));
  pMain.add(new Label("Switch Name: "));
  pMain.add(tfName = new TextField(s.getName()));

  pMain.add(new Label("X Coordinate: ", Label.LEFT));
  pMain.add(tfCoordX = new TextField("" + s.getX()));
  pMain.add(new Label("Y Coordinate: ", Label.LEFT));
  pMain.add(tfCoordY = new TextField("" + s.getY()));
  pMain.add(new Label("Alpha: ", Label.LEFT));
  pMain.add(tfAlpha = new TextField("" + s.getAlpha()));
  pMain.add(new Label("Omega: ", Label.LEFT));
  pMain.add(tfOmega = new TextField("" + s.getOmega()));
  gbc.fill = GridBagConstraints.BOTH;
  gbc.weightx = 100;
  gbc.weighty = 100;
  add(pMain, 0, 0, 1, 5);

  // 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;
  gbc.anchor = GridBagConstraints.CENTER;
  add(pButtons, 0, 5, 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) {
    try{
      s.setLocation(( new Double(tfCoordX.getText()) ).doubleValue(),
		    ( new Double(tfCoordY.getText()) ).doubleValue());
      s.setAlpha( (new Double(tfAlpha.getText())).doubleValue() );
      s.setOmega( (new Double(tfOmega.getText())).doubleValue() );
      s.setName( tfName.getText() );
      dispose();
    }
    catch(NumberFormatException nfe) {
      new dMessageBox((Frame)getParent(), "Error", "Illegal Input!", gpData,
		  false, null);
    }
  }
  else if(e.target == bCancel) {
    dispose();
  }
  else if(e.target == bHelp) {
    String HelpURL = (String)gpData.
      get("Help URL for Edit|Switch|EditSwitch 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 dEditSwitchEdit
//////////////////////////////////////////////////



