001    /*
002     * InfoDialog
003     * 
004     * Copyright (c) 2000, 2001, 2002, 2003 Marco Schmidt.
005     * All rights reserved.
006     */
007    
008    package net.sourceforge.jiu.gui.awt.dialogs;
009    
010    import java.awt.BorderLayout;
011    import java.awt.Button;
012    import java.awt.Dialog;
013    import java.awt.Dimension;
014    import java.awt.Frame;
015    import java.awt.Panel;
016    import java.awt.Rectangle;
017    import java.awt.TextArea;
018    import java.awt.Toolkit;
019    import java.awt.event.ActionEvent;
020    import java.awt.event.ActionListener;
021    
022    /**
023     * A modal AWT dialog that displays text in a non-editable text area component
024     * (so that it can be selected and easily copied to the system's clipboard).
025     * Provides an OK button so that user can remove the dialog.
026     * @author Marco Schmidt
027     */
028    public class InfoDialog extends Dialog implements ActionListener
029    {
030            private Button ok;
031            private TextArea textArea;
032    
033            /**
034             * Creates an InfoDialog, a modal dialog to display a text message, centered on the desktop.
035             * @param owner the Frame this dialog will belong to
036             * @param title the text that will be displayed in the title bar of the dialog
037             * @param text the message text that will be displayed in the main part of the dialog
038             */
039            public InfoDialog(Frame owner, String title, String text)
040            {
041                    super(owner, title, true);
042                    ok = new Button("OK");
043                    ok.addActionListener(this);
044                    Panel panel = new Panel();
045                    panel.add(ok);
046                    add(panel, BorderLayout.SOUTH);
047                    textArea = new TextArea(text);
048                    textArea.setEditable(false);
049                    textArea.setSize(textArea.getMinimumSize());
050                    //ScrollPane scrollPane = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);
051                    //scrollPane.add(textArea);
052                    add(textArea);
053                    pack();
054                    center();
055            }
056    
057            /**
058             * Hides (closes) this dialog if the OK button was source of the action event
059             * (e.g. if the button was pressed).
060             */
061            public void actionPerformed(ActionEvent e)
062            {
063                    if (e.getSource() == ok)
064                    {
065                            setVisible(false);
066                    }
067            }
068    
069            /**
070             * Centers the dialog on screen.
071             */
072            public void center()
073            {
074                    Rectangle rect = getBounds();
075                    int width = rect.width;
076                    int height = rect.height;
077                    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
078                    setLocation((screenSize.width / 2) - (width / 2),
079                            (screenSize.height / 2) - (height / 2));
080            }
081    }