/*
 * $Log: dEditDelConstraint.java,v $
# Revision 1.1  1996/07/11  01:20:05  hma
# Initial revision
#
# Revision 1.4  1996/06/10  16:49:56  inder
# minor changes.
#
# Revision 1.3  1996/06/06  05:17:55  inder
# made the button panel vertically unresizable.
#
# Revision 1.2  1996/05/13  11:18:57  inder
# works completely.
#
# Revision 1.1  1996/05/13  11:17:26  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.Place;
import physical.PhysicalGraph;
import network.*;
import gui.FramedPanel;
import gui.dMessageBox;
import gui.GUICanvas;
//////////////////////////////////////////////////
public class dEditDelConstraint extends Dialog {

private Network n;
private GeneralProperties gpData;
private FramedPanel pMain;		   
private Choice choice;
Hashtable htConstraint;		    // Keeps track of item in cPlace and
				    // correponding place.
private Label lSet1;
private Label lSet2;
private Label lOut;
private Label lIn;

private FramedPanel pButtons;	    // Holds buttons.
private Button bOk;		    // Ok button.
private Button bCancel;		    // Cancel button.
private Button bHelp;		    // Help button.
//************************************************
public dEditDelConstraint(Frame parent, GeneralProperties gp) {
// REQUIRES: At least one constraint exists in PhysicalGraph.
// RETURNS: nothing.
// PROMISES: Creates and displays dialog box Edit|Delete|Constraint. Removes
// selected constraint from the Network if "ok" button is pressed.

  super(parent, "Delete Constraint", 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));
  choice = new Choice();

  n = (Network)gpData.get("Network");
  htConstraint = new Hashtable();
  String s;
  SetPairConstraint c;
  int Index = 0;		    // Index in choice.
  for (Enumeration e = n.constraints.keys(); e.hasMoreElements(); ) {
    s = (String)e.nextElement();
    choice.addItem(s);
//    htPlaces.put(new Integer(Index), p);
//    ++Index;
  }

  choice.select(0);		    // Select first item.
  c = (SetPairConstraint)n.constraints.get(choice.getSelectedItem());
//  p = (Place)htPlaces.get(new Integer(0)); // Display data related to
					   // selected place.  
  pMain.add(new Label("Constraint : ", Label.LEFT));
  pMain.add(choice);
  pMain.add(new Label("Set1 : ", Label.LEFT));
  lSet1 = new Label( (c.getA()).toString(), Label.LEFT);
  pMain.add(lSet1);
  pMain.add(new Label("Set2 : ", Label.LEFT));
  pMain.add(lSet2 = new Label( (c.getB()).toString(), Label.LEFT));
  pMain.add(new Label("Out : ", Label.LEFT));
  pMain.add(lOut = new Label(""+c.getOut(), Label.LEFT));
  pMain.add(new Label("In : ", Label.LEFT));
  pMain.add(lIn = new Label(""+c.getIn(), Label.LEFT));

  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;
  }
  else if(e.target == choice) {
    // Display data related to selected place.
    SetPairConstraint c = (SetPairConstraint)n.constraints.get(choice.getSelectedItem()); 
    lSet1.setText( (c.getA()).toString());
    lSet2.setText( (c.getB()).toString());
    lOut.setText(""+c.getOut());
    lIn.setText(""+c.getIn());
  }
  else if(e.target == bOk) {
    String key = choice.getSelectedItem();
    n.removeConstraint(key);
    dispose();
  }
  else if(e.target == bCancel) {
    dispose();
  }
  else if(e.target == bHelp) {
    String HelpURL = (String)gpData.
      get("Help URL for Edit|Delete|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); 
    }
  }
  else
    return super.handleEvent(e);

  return true;
}
//************************************************
} // End of class dEditDelConstraint.
//////////////////////////////////////////////////


















































