/*
 * $Log: dWindowConstraints.java,v $
# Revision 1.1  1996/07/11  01:22:15  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.GeneralProperties;
import gui.FramedPanel;
//////////////////////////////////////////////////
public class dWindowConstraints extends GridBagDialog {
  private GeneralProperties gpData;
  private List list;			    // Scrolling list of links.
  private Button bOk;
  private Button bUpdate;
  private Button bHelp;
  private Canvas c;
  private Panel pCostAndLowerBound, pButtons;

//************************************************
public dWindowConstraints(Frame parent, GeneralProperties gp) {
// REQUIRES: 
// RETURNS: nothing.
// PROMISES: 
// 1. Constructs graphics objects required for LinksWindow.
  
  super(parent, "Constraints", false);
  gpData = gp;

  Network n = (Network)gpData.get("Network");

  // add List of Constraints
  list = new List(10, false);
  update();
  gbc.fill = GridBagConstraints.BOTH;
  gbc.weightx = 100;
  gbc.weighty = 100;
  add(list, 0, 0, 1, 6);  

  // add ok and cancel buttons to pButtons panel
  pButtons = new Panel();
  pButtons.setLayout(new GridLayout(1,3));
  pButtons.add(bOk = new Button("Ok"));
  pButtons.add(bUpdate = new Button("Update"));
  pButtons.add(bHelp = new Button("Help"));
  gbc.weightx = 0;
  gbc.weighty = 0;
  gbc.fill = GridBagConstraints.NONE;
  gbc.anchor = GridBagConstraints.CENTER;
  add(pButtons, 0, 6, 1, 1);

  pack();
}

public void update() {
  Network n = (Network)gpData.get("Network");
  list.clear();
  for(Enumeration e=n.constraints.elements(); e.hasMoreElements(); ) {
    // Add SetPairConstraints to list 
    list.addItem( ((SetPairConstraint)e.nextElement()).toString() ); 
  }
}


//************************************************
public boolean action(Event evt, Object arg) {
  if (arg.equals("Ok")) {
    hide();
    return true;
  }
  else if (arg.equals("Update")) {
    update();
    return true;
  }
  else if (arg.equals("Help")) {
    String HelpURL = (String)gpData.
      get("Help URL for Window|Switches 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;
  }
  return false;
}

//************************************************
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.id == Event.WINDOW_DESTROY && 
     e.target == this)
    hide();
  else
    super.handleEvent(e);
  return true;			    // No need to pass event to parent as
				    // the dialog box is anyway model. 
}

//************************************************
} // end of class dWindowConstraints
//////////////////////////////////////////////////
