/*
 * $Log: dEditAddPlace.java,v $
# Revision 1.11  1996/07/11  01:19:05  hma
# intermediate working version
#
# Revision 1.10  1996/06/10  16:33:47  inder
# minor changes.
#
# Revision 1.9  1996/06/06  05:14:12  inder
# made the button panel vertically unresizable.
#
# Revision 1.8  1996/05/13  08:13:15  inder
# keyboard events now passed to components.
#
# Revision 1.7  1996/05/13  01:01:56  inder
# minor typos.
#
# Revision 1.6  1996/05/12  09:39:35  inder
# added input of alpha and omega.
#
# Revision 1.5  1996/05/12  07:23:56  inder
# reflected changes in the constructor of dMessageBox.
#
# Revision 1.4  1996/05/12  06:11:53  inder
# removed static variable gap.
#
# Revision 1.3  1996/05/12  06:01:20  inder
# Now extracts required data from gpData.
#
# Revision 1.2  1996/05/12  05:35:39  inder
# Completed creation.
#
# Revision 1.1  1996/05/10  20:10:25  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 physical.PhysicalGraph;
import physical.Place;
import gui.FramedPanel;
import gui.GUICanvas;
//////////////////////////////////////////////////
public class dEditAddPlace extends Dialog {

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 static int DefNameCount = 1; // Keeps the count of the number of
				     // current place. Used for generating
				     // default name. 
private FramedPanel pButtons;	    // Holds buttons.
private Button bOk;		    // Ok button.
private Button bCancel;		    // Cancel button.
private Button bHelp;		    // Help button.
//************************************************
public dEditAddPlace(Frame parent, GeneralProperties gp) {
// REQUIRES: nothing.
// RETURNS: nothing.
// PROMISES: Creates and displays dialog box Edit|Add|Place.

  super(parent, "Add Place", 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(5, 2, gap, gap));
  pMain.add(new Label("Place Name: ", Label.LEFT));
  pMain.add(tfName = new TextField("Place"+DefNameCount));
  // Default position of coordinates is same as the last mouse click.
  Point ptDefault = (Point)gpData.get("Coordinates of Last Mouse Click");
  pMain.add(new Label("X Coordinate: ", Label.LEFT));
  pMain.add(tfCoordX = new TextField(""+ptDefault.x));
  pMain.add(new Label("Y Coordinate: ", Label.LEFT));
  pMain.add(tfCoordY = new TextField(""+ptDefault.y));
  pMain.add(new Label("Alpha: ", Label.LEFT));
  String st = "" + 
    ((Double) gpData.get("Default Value of Alpha for a Place")).doubleValue();
  pMain.add(tfAlpha = new TextField(st));
  pMain.add(new Label("Omega: ", Label.LEFT));
  st = "" + 
    ((Double) gpData.get("Default Value of Omega for a Place")).doubleValue(); 
  pMain.add(tfOmega = new TextField(st));

  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 == bOk) {
    ++DefNameCount;		    // A newer place has been added so,
				    // increment DefNameCount.
    try{
      double x = (new Double(tfCoordX.getText())).doubleValue();
      double y = (new Double(tfCoordY.getText())).doubleValue();
      double alpha = (new Double(tfAlpha.getText())).doubleValue();
      double omega = (new Double(tfOmega.getText())).doubleValue();

      ((PhysicalGraph)gpData.get("Physical Graph"))
	.addPlace(new Place(tfName.getText(), x, y, alpha, omega)); 
      GUICanvas c = (GUICanvas)gpData.get("Canvas");
      c.paint(c.getGraphics());
      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|Add|Place 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 dEditAddPlace
//////////////////////////////////////////////////
