import java.awt.*;
import java.util.*;

public class TextWindow extends Frame {
    private final static int BUTTON_WIDTH  = 72;    
    private final static int BUTTON_HEIGHT = 18;

    private final static int OFFSET = 13;
    
    protected TextArea area_;
    String text_;
    
    public TextWindow(String title, String text, int width, int height) {
        super(title);

        setBackground(Color.lightGray);

        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (screen.width - width) / 2;
        int y = (screen.height - height) / 2;
        y = y * 2 / 3;
        reshape(x, y, width, height);

        text_ = text;
        
        repaint();
        show();
    }

    public void paint (Graphics g) {
        if (area_ == null) {
            setLayout(null);
        
            area_ = new TextArea();
            add(area_);
            
            Insets insets = insets();
            int insetX = insets.left + insets.right;
            int insetY = insets.top + insets.bottom;
            int width = size().width;
            int height = size().height;
            
            area_.reshape(insets.left + OFFSET, insets.top + OFFSET,
                          width  - 2 * OFFSET - insetX,
                          height - 3 * OFFSET - insetY - BUTTON_HEIGHT);
            area_.setText(text_);
            area_.setEditable(false);
            
            Button close = new Button("Close");
            add(close);

            close.reshape((width - BUTTON_WIDTH) / 2,
                          area_.location().y + area_.size().height + OFFSET,
                          BUTTON_WIDTH, BUTTON_HEIGHT);
            
        }
    }

    public void setText(String text) {
        area_.setText(text);
        show();
    }

    public boolean action(Event evt, Object what) {
        if (evt.target instanceof Button) {
            hide();
            return true;
        }
        return false;
    }

    public boolean handleEvent(Event evt) {
        if (evt.id == Event.WINDOW_DESTROY) {
            hide();
            return true;
        }
        else
            return super.handleEvent(evt);
    }
}

