package gie;
import java.awt.*;
import gie.*;

public class InfoDialog extends Dialog {
    protected Button button, focusButton, selButton=null;
    protected MultiLineLabel label;
    protected Panel p;
  //protected parent;

    public InfoDialog(Frame parent, String title, String message, Button valid, boolean modal)
    {
        // Create a dialog with the specified title
        super(parent, title, modal);
        
        // Create and use a BorderLayout manager with specified margins
        this.setLayout(new BorderLayout(15, 15));
        
        // Create the message component and add it to the window
        label = new MultiLineLabel(message,40,20);
	
        this.add("Center", label);
        
        // Use a FlowLayout to center the button and give it margins.
	focusButton=valid;
        p = new Panel();
        p.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 15));
        p.add(valid);
        this.add("South", p);

    }
    
    public InfoDialog(Frame parent, String title, String message, Button valid){
      this(parent, title, message,valid, true);
    }
    
    public InfoDialog(Frame parent, String title, String message, String valid){
      this(parent, title, message,new Button(valid), true);
    }
    
    public InfoDialog(Frame parent, String title, String message, String valid, boolean modal){
      this(parent, title, message,new Button(valid), modal);
    }
    
    public InfoDialog(Frame parent, String title, String message){
      this(parent, title, message,"Okay", true);
    }

    // add button
    public void addButton(Button b){
      p.add(b);
    }
    public void addButton(Button b, boolean focus){
      if (focus)
	focusButton=b;
      p.add(b);
    }

    // Pop down the window when the button is clicked.
    public boolean action(Event e, Object arg)
    {
        if (e.target instanceof Button) {
	  this.hide();
	  if(answer!=null) answer.action(new Event(this,Event.WINDOW_DESTROY,e.target),e.target);
	  this.dispose();
	  return true;
        }
        else return false;
    }

  Component answer=null;
   public void show() {
     // Resize the window to the preferred size of its components
     this.pack();
     super.show();
   }

   public void showWithAnswer(Component parent) {
     answer=parent;
     this.show();
   }

    // When the window gets the keyboard focus, give it to the button.
    // This allows keyboard shortcuts to pop down the dialog.
    public boolean gotFocus(Event e, Object arg) {
        focusButton.requestFocus();
        return true;
    }
    
    public static void main(String[] args) {
        Frame f = new Frame("InfoDialog Test");
        f.resize(100, 100);
        f.show();
        InfoDialog d = new InfoDialog(f, "Help", 
                          "The host you are trying to contact\n" +
                          "is not currently responding.\n" +
                          "Please try again later.");
	d.addButton(new Button("Cancel"));
        d.show();
    }
}
