import java.awt.*; import java.awt.event.*; import java.applet.Applet; /** * Used to pop up dialog boxes with messages to the users. * Good for error messages and notifications. This can be used * as a help dialog box. * * @author Chris Juffre, cjuffre@cs.uml.edu * @since 1.0 * @version 1.1, updated May 1, 2002 to support parent applet focus * @version 1.2, updated May 9, 2002 to allow text wrapping and choice of text box or label, and changed reference parent reference from Applet to Component */ public class DialogBox extends Frame implements ActionListener { /** * The close button. * @since 1.1 */ private Button btClose; /** * The Parent of this Frame * @since 1.1 */ private Component parent; /** * The button to bring up the help. * @since 1.2 */ private Button btHelp; /** * Constructs the Dialog Box with either a text area or a label to * display information. * * @param inTitle The title of the window set by the caller. * @param inMessage The text of the message set by the caller. * @param ParentApp The parent of this frame * @param tArea If true use a TextArea, if false use a label. * @since 1.0 * @version 1.2, Updated on May 9 to incorporate use of TextArea OR Label */ public DialogBox(String inTitle, String inMessage, Component ParentApp, boolean tArea) { /* Title of the dialog box frame. */ super(inTitle); /* Sets the parent applet */ this.parent = ParentApp; /* Allows us to close this window. */ enableEvents(AWTEvent.WINDOW_EVENT_MASK); /* Make the layout a FlowLayout type. */ setLayout( new FlowLayout()); /* Sets up the text area to store desired message in. */ if(tArea) { TextArea message = new TextArea("", 20, 60, TextArea.SCROLLBARS_VERTICAL_ONLY); message.setEditable(false); message.setText(inMessage); add(message); } else add(new Label(inMessage)); /* Simple close window button */ btClose = new Button("Close Window"); btClose.addActionListener(this); add(btClose); /* Simple close window button if the title of this is not "help" case-insensitive. */ if(inTitle.trim().compareToIgnoreCase("help") != 0) { btHelp = new Button("Help"); btHelp.addActionListener(this); add(btHelp); } /* Set new location. */ setLocation(new Point((parent.getWidth() / 2) - (getWidth() / 2), (parent.getHeight() / 2) - (getHeight() / 2))); /* Makes it a size that is comparable to the size of the applet since it is a sub window */ pack(); setVisible(true); } /** * Called when window events are fired. * @param e The WindowEvent fired. * @since 1.0 */ public void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); /* If window is to be closed through the 'x' in the top right hand corner of the frame this will be called. This will dispose of all the resources used by this window. It also sets the focus back to the parent applet. */ if(e.getID() == WindowEvent.WINDOW_CLOSING) { dispose(); parent.requestFocus(); } } /** * Called when action events are fired. * @param event The ActionEvent fired. * @since 1.0 * @version 1.2, May 9th, to incorporate help button on the frame */ public void actionPerformed(ActionEvent e) { /* If the Close button is pressed then we are done with this dialog box. This will dispose of all the resources used by this window. Also sets the focus back to the parent applet. */ if(e.getSource() == btClose) { dispose(); parent.requestFocus(); } if(e.getSource() == btHelp) { new DialogBox("Help", RouletteConstants.HELP_TEXT, this, true).setVisible(true); } } }