/*
 * $Log$
 */
//////////////////////////////////////////////////
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.*;
import gui.FramedPanel;

/**
 * For two switches u and v, define f(u,v) = omega(v)/(sum_w omega(w)), w is over all
 * vertices except u. also define g(u,v) = alpha(u)/(sum_w alpha(w)).
 * The traffic from u to v is limited to C*min(f(u,v)*alpha(u), g(u,v)*omega(v)).
 * Here C is a relax factor. When C = 1, all traffics are strictly specified, and
 * a complete graph is the best solution. When C = infiniti, it's not constrainting
 * anything.
 */
public class dEditConstraintsPair extends GridBagDialog {

  // the relax factor on pairwise constraints
  private static double RELAX_FACTOR = 1.0;

  private GeneralProperties gpData;
  private FramedPanel pMain;
  private FramedPanel pButtons;
  private TextField tfRelaxFactor;
  private Button bOk;		    // Ok button.
  private Button bCancel;	    // Cancel button.
  private Button bHelp;		    // Help button.

  //************************************************
  public dEditConstraintsPair(Frame parent, GeneralProperties gp) {
    // REQUIRES: nothing.
    // RETURNS: nothing.
    // PROMISES: Creates and displays dialog box Edit|Constraints|Pair.

    super(parent, "Pairwise Percentage Traffic Constraint", true);

    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(1, 2, gap, gap));

    pMain.add(new Label("Relax Factor: ", Label.LEFT));
    pMain.add(tfRelaxFactor = new TextField("" + RELAX_FACTOR));

    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 100;
    gbc.weighty = 100;
    add(pMain, 0, 0, 1, 1);

    // 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, 2, 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{
	Network n = (Network)gpData.get("Network");
	double relaxFactor = ( new Double(tfRelaxFactor.getText()) ).doubleValue();
	n.addPairwiseTrafficConstraint(relaxFactor);
	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|Constraints|Pair 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 dEditConstraintsPair
//////////////////////////////////////////////////



