/*
 * $Log: dWindowLinks.java,v $
# Revision 1.16  1996/07/11  01:22:29  hma
# intermediate working version
#
# Revision 1.2  1996/03/25  18:54:55  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 network.*;
import general.GeneralProperties;
import general.Set;
import gui.FramedPanel;
//////////////////////////////////////////////////
public class dWindowLinks extends GridBagDialog {
  public  List list;			    // Scrolling list of links.
  private Label lCost;
  private Button bOk;
  private Button bUpdate;
  private Button bStop;
  private Button bHelp;
  private Canvas c;
  private Panel pCost, pButtons;
  private GeneralProperties gpData;

//************************************************
public dWindowLinks(Frame parent, GeneralProperties gp) {
  // not modal
  super(parent, "Link List", false);

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

  // add cost pCost
  //pCost = new Panel();
  lCost = new Label("Link Cost = " + n.cost); 
  //pCost.add(lCost);
  gbc.fill = GridBagConstraints.NONE;
  gbc.anchor = GridBagConstraints.CENTER;
  add(lCost, 0, 0, 1, 1);

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

  list = new List(10, false);
  updateList();
  gbc.fill = GridBagConstraints.BOTH;
  gbc.weightx = 100;
  gbc.weighty = 100;
  add(list, 0, 2, 1, 6);  
  pack();
}

public void updateItem(Link link) {
  String name = "[" + link.getName();
  for (int i = 0; i < list.countItems(); i++) {
    if (list.getItem(i).startsWith(name)) {
      list.replaceItem(link.toString(), i);
      list.select(i);
      list.makeVisible(i);
      break;
    }
  }
}

public void updateList() {
  list.clear();
  Network n = (Network)gpData.get("Network");
  Link l;
  for(Enumeration e=n.links.elements(); e.hasMoreElements(); ) {
    l = (Link)e.nextElement();
    list.addItem( l.toString() ); // Add link to list l.
  }
}

//************************************************
public boolean action(Event evt, Object arg) {
  if (arg.equals("Ok")) {
    hide();
    return true;
  }
  else if (arg.equals("Stop")) {
    GUIManager gm = (GUIManager)gpData.get("GUIManager");
    if (gm.calcLinkDimensionThreadIsAlive()) {
      gm.stopCalcLinkDimensionThread();
      lCost.setText("Link Dimensioning Stopped");
      layout();
    }
    return true;
  }
  else if (arg.equals("Update")) {
    update();
    return true;
  }
  else if (arg.equals("Help")) {
    String HelpURL = (String)gpData.
      get("Help URL for Window|Links 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. 
}

public void setRunning() {
  lCost.setText("Dimensioning Links");
  lCost.invalidate();
}

public void disableUpdateButton() {
  bUpdate.disable();
}

public void enableUpdateButton() {
  bUpdate.enable();
}

// change the value of cost and list of links to reflect the recent change
public void update() {
  Network n = (Network)gpData.get("Network");
  GUIManager gm = (GUIManager)gpData.get("GUIManager");
  if (n.cost == 0 && n.links.size() != 0) {
    if (gm.calcLinkDimensionThreadIsAlive())
      lCost.setText("Dimensioning Links...");
    else
      lCost.setText("Link Cost not calculated");
  }
  else
    lCost.setText("Link Cost = " + n.cost);
  updateList();
  layout();
}

//************************************************
} // end of class dWindowLinks
//////////////////////////////////////////////////
