/*
 * $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.Switch;
import network.Link;
import network.Network;
import gui.FramedPanel;
import gui.GUICanvas;
//////////////////////////////////////////////////
public class dNetworkAdjust extends Dialog {

public static String INCREASING_ORDER = "Increasing Order";
public static String DECREASING_ORDER = "Decreasing Order";

private GeneralProperties gpData;
private FramedPanel pMain;		   
private TextField tfRatio;	    // Inputs name of the link.
private Choice cOrder;		    // choice for increasing or decreasing order

private FramedPanel pButtons;	    // Holds buttons.
private Button bOk;		    // Ok button.
private Button bCancel;		    // Cancel button.
private Button bHelp;		    // Help button.
//************************************************
public dNetworkAdjust(Frame parent, GeneralProperties gp) {
// REQUIRES: nothing.
// RETURNS: nothing.
// PROMISES: creates dialog box Network|Adjust.

  super(parent, "Adjust Initial Network", 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(2, 2, gap, gap));

  cOrder = new Choice();
  cOrder.addItem(INCREASING_ORDER);
  cOrder.addItem(DECREASING_ORDER);
  cOrder.select(0);		  
  tfRatio = new TextField( ((Double)gpData.get("Ratio")).toString() );
  pMain.add(new Label("Add Links from Secondary Network by: ", Label.LEFT));
  pMain.add(cOrder);
  pMain.add(new Label("Add Links which reduce distance by factor of ", Label.LEFT));
  pMain.add(tfRatio);

  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) {
    try {
      gpData.put("Order", cOrder.getSelectedItem());
      gpData.put("Ratio", Double.valueOf(tfRatio.getText()));
      Network n = (Network)gpData.get("Network");
      n.adjustInitial(gpData);
      System.out.println("adjusted");
      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 Network|Adjust 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;			    // No need to pass event to parent as
				    // the dialog box is anyway model. 
}
//************************************************
} // End of class dEditAddLink
//////////////////////////////////////////////////
