/*
 * $Log: dEditAddConstraint.java,v $
# Revision 1.2  1996/08/05  19:43:44  hma
# modify handleEvent so it does not destroy the parent window when run from netscape.
#
# Revision 1.1  1996/07/11  01:18:11  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 network.*;
import general.*;
import physical.PhysicalGraph;
import physical.Place;
import gui.FramedPanel;
import gui.GUICanvas;

//////////////////////////////////////////////////
public class dEditAddConstraint extends Dialog {

private GeneralProperties gpData;
private FramedPanel pMain;		   
private TextField tfName;	     // Inputs name of the place.
private TextField tfSet1;	     // Inputs X Coordinate of place.
private TextField tfSet2;	     // Inputs Y Coordinate of place.
private TextField tfOut;	     // Inputs alpha of place.
private TextField tfIn;	             // Inputs omega of place.
private static int DefNameCount = 1; // Keeps the count of the number of
				     // current constraints. 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 dEditAddConstraint(Frame parent, GeneralProperties gp) {
// REQUIRES: nothing.
// RETURNS: nothing.
// PROMISES: Creates and displays dialog box Edit|Add|Place.

  super(parent, "Add Constraint", true);

  gpData = gp;
  Network n = (Network)gpData.get("Network");
  DefNameCount = (DefNameCount < n.constraints.size()) ?
    n.constraints.size() : DefNameCount;
  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("Constraint Name: ", Label.LEFT));
  pMain.add(tfName = new TextField("Constraint"+DefNameCount));

  pMain.add(new Label("Set 1: ", Label.LEFT));
  pMain.add(tfSet1 = new TextField());
  pMain.add(new Label("Set 2: ", Label.LEFT));
  pMain.add(tfSet2 = new TextField());
  pMain.add(new Label("Out(1->2): ", Label.LEFT));
  pMain.add(tfOut = new TextField());
  pMain.add(new Label("In(2->1) : ", Label.LEFT));
  pMain.add(tfIn = new TextField());

  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.WINDOW_DESTROY) {
    dispose();
    return true;
  }
  if(e.id == Event.KEY_PRESS) {	    // To let components handle keyboard
				    // events. (required due to bug in java). 
    return false;
  }
  super.handleEvent(e);
  return true;
}

public boolean action(Event e, Object arg) {
  if(arg.equals("Ok")) {
    try {
      // A newer constraint has been added so,
      // increment DefNameCount.
      ++DefNameCount;
      Network n = (Network)gpData.get("Network");

      String set1string = "{ " + tfSet1.getText() + " }";
      String set2string = "{ " + tfSet2.getText() + " }";
      System.out.println("Set1string is " + set1string);
      System.out.println("Set2string is " + set2string);

      Set set1 = n.readSwitchSet(set1string);
      Set set2 = n.readSwitchSet(set2string);
      System.out.println("Set1 is " + set1);
      System.out.println("Set2 is " + set2);
      double out = (new Double(tfOut.getText())).doubleValue();
      double in = (new Double(tfIn.getText())).doubleValue();
      SetPairConstraint spc = new SetPairConstraint
	(tfName.getText(), set1, set2, out, in);
      System.out.println("new constraint is " + spc);

      n.addConstraint(spc); 
      System.out.println(spc);
      dispose();
      return true;
    }
    catch (NumberFormatException excep) {
      new dMessageBox((Frame)getParent(), "Error", "Illegal Input!", gpData,
		  false, null);
    }      
  }
  else if(arg.equals("Cancel")) {
    dispose();
    return true;
  }
  else if(arg.equals("Help")) {
    String HelpURL = (String)gpData.
      get("Help URL for Edit|Add|Constraint 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); 
    }
    return true;
  }
  else
    return super.action(e, arg);
  return true;
}
//************************************************
} // End of class dEditAddConstraint
//////////////////////////////////////////////////











