///////////////////////////////////////////////////////////////////////////////
package gui;
import java.applet.Applet;
import java.awt.*;
import java.util.*;
import java.net.URL;
import java.net.MalformedURLException;

import gui.FramedPanel;
///////////////////////////////////////////////////////////////////////////////
public class dMessageBox extends Dialog {

// private GeneralProperties gpData;
private static final int LineWidth = 100;
private static final int WrapSize = 5;
private String HelpURL;
private String HelpMessage;
private FramedPanel pMsg;	    // holds msg.
private FramedPanel pButtons;	    // Holds buttons.
private Button bDismiss;		    // Dismiss button.
private Button bHelp;		    // Help button.
//-----------------------------------------------------------------------------
public static void DisplayMessageBox(Frame parent, String title, String msg, String helpString, boolean wrap) {
    new dMessageBox(parent, title, msg, helpString, wrap);
}
//-----------------------------------------------------------------------------
public static void DisplayMessageBox(Frame parent, String title, String msg, String helpString) {
    new dMessageBox(parent, title, msg, helpString, true);
}
//-----------------------------------------------------------------------------
public dMessageBox(Frame parent, String title, String msg, String helpString) {
    this(parent, title, msg, helpString, true); // default constructor always wraps.
}
//-----------------------------------------------------------------------------
public dMessageBox(Frame parent, String title, String msg, String helpString, boolean Wrap) {
// GeneralProperties gp, boolean ShowHelp, String helpURL) {
// REQUIRES: nothing.
// RETURNS: nothing.
// PROMISES: Creates and shows a dialog box with title and displays msg in it.

  super(parent, title, false);

//  gpData = gp;
//  HelpURL = helpURL;
  setFont(new Font("Courier", Font.PLAIN, 12));
  GridBagLayout gblOverall = new GridBagLayout();
  setLayout(gblOverall);
  GridBagConstraints gbcOverall = new GridBagConstraints();
  gbcOverall.weightx = 1.0;
  gbcOverall.weighty = 1.0;
  int gap = 5;
  gbcOverall.insets = new Insets(gap, gap, 0, gap);
  gbcOverall.gridwidth = GridBagConstraints.REMAINDER;
  gbcOverall.fill = GridBagConstraints.BOTH;

  pMsg = new FramedPanel(FramedPanel.NO_BORDER, new Insets(0, 0, 0, 0));
  pMsg.setLayout(new GridLayout(1, 1));


  // First parse string to get lines separated by \n's
  StringTokenizer t = new StringTokenizer(msg, "\n");
  int num_lines = t.countTokens();
  String lines[] = new String[num_lines];
  for(int i=0; i<num_lines; ++i)
    lines[i] = t.nextToken();

  int NumCols = 0;
  for(int i=0; i<num_lines; ++i)
    if(lines[i].length() > NumCols)
        NumCols = lines[i].length();

  if(NumCols > LineWidth)
     NumCols = LineWidth;
  NumCols += 2;
  int NumRows = num_lines+2>25 ? 25 : num_lines+2;
  TextArea ta = new TextArea(NumRows+3, NumCols);

  ta.appendText("\n");   // For the screen with the lower resolution
  if(Wrap == false)
    ta.appendText(msg);
  else {
     for(int i=0; i<num_lines; ++i) {
        int len = lines[i].length();
        char st[] = new char[len+100];
        lines[i].getChars(0, len, st, 0);
        int count;
        for(int start=0; start<len; start+=count) {
            if(len-start <= LineWidth)
                count = len - start;
            else
                for(count=LineWidth-WrapSize; (start+count)<len && !Character.isSpace(st[start+count]); ++count);
            ta.appendText((new String(st, start, count)) + "\n");
        }
     }
  }
  ta.setEditable(false);
  pMsg.add(ta);
  pMsg.resize(pMsg.preferredSize());

  gblOverall.setConstraints(pMsg, gbcOverall);
  add(pMsg);

  // Add ok and help buttons at the bottom in a separate panel.
  pButtons = new FramedPanel();
  pButtons.add(bDismiss = new Button("Dismiss"));
  HelpMessage = helpString;
  if(helpString != null)
    pButtons.add(bHelp = new Button("Help")); // Add help button.

  gbcOverall.insets = new Insets(gap, gap, gap, gap);
  gbcOverall.weighty = 0.0;
  gbcOverall.fill = GridBagConstraints.HORIZONTAL;
  gblOverall.setConstraints(pButtons, gbcOverall);
  add(pButtons);

  pack();
  resize(preferredSize());
  show();
}
//-----------------------------------------------------------------------------
public boolean handleEvent(Event e) {
// REQUIRES: nothing.
// RETURNS: true if the event is handled within it, false otherwise.
// PROMISES: Changes web page to HelpURL if InApplet is true. otherwise
// displays HelpMsg.

  if(e.id == Event.KEY_PRESS) {	    // To let components handle keyboard
				    // events. (required due to bug in java).
    return false;
  }
  else if(e.target == bDismiss) {
    dispose();
  }
  else if(e.id == Event.WINDOW_DESTROY) {
    dispose();
  }
  else if(e.target == bHelp) {
    new dMessageBox((Frame)getParent(), "Help", HelpMessage, null);
/*    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 dMessageBox
///////////////////////////////////////////////////////////////////////////////
