Game-of-Life-WSU-CEG4180  v0.1
RefactoringtheGameofLife
AlertBox.java
Go to the documentation of this file.
1 /* This page is part of the Game of Life source code */
2 
3 /*
4  * Easily show an alert.
5  * Copyright 2003 Edwin Martin <edwin@bitstorm.org>
6  *
7  */
8 package org.bitstorm.util;
9 
10 import java.awt.AWTEvent;
11 import java.awt.Button;
12 import java.awt.Dialog;
13 import java.awt.Dimension;
14 import java.awt.Event;
15 import java.awt.FlowLayout;
16 import java.awt.Frame;
17 import java.awt.GridLayout;
18 import java.awt.Image;
19 import java.awt.Label;
20 import java.awt.Panel;
21 import java.awt.Point;
22 import java.awt.Toolkit;
23 import java.awt.event.ActionEvent;
24 import java.awt.event.ActionListener;
25 import java.util.StringTokenizer;
26 
27 
28 /**
29  * AlertBox shows a message besides a warning symbol.
30  * @author Edwin Martin
31  *
32  */
33 public class AlertBox extends Dialog {
34  private Button okButton;
35 
36  /**
37  * Contructs a AlertBox.
38  *
39  * Use the newline character '\n' in the message to seperate lines.
40  *
41  * @param parent parent frame
42  * @param title title of the dialog box
43  * @param message the message to show
44  */
45  public AlertBox( Frame parent, String title, String message ) {
46  super( parent, title, false );
47 
48  Image alertImage = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("alert.gif"));
49  ImageComponent alertImageComponent = new ImageComponent( alertImage );
50  okButton = new Button(" OK ");
51  okButton.addActionListener(
52  new ActionListener() {
53  public void actionPerformed(ActionEvent e) {
54  close();
55  }
56  }
57  );
58  Panel buttonPanel = new Panel();
59  buttonPanel.setLayout( new FlowLayout( FlowLayout.CENTER ) );
60  buttonPanel.add( okButton );
61  StringTokenizer st = new StringTokenizer( message, "\n" );
62  Panel messagePanel = new Panel( new GridLayout( st.countTokens(), 1 ) );
63  while ( st.hasMoreTokens() ) {
64  messagePanel.add( new Label( st.nextToken() ) );
65  }
66  add( "West", alertImageComponent );
67  add( "Center", messagePanel );
68  add( "South", buttonPanel );
69  enableEvents(Event.WINDOW_DESTROY);
70  setResizable( false );
71  setModal( true );
72  pack();
73  Point p = parent.getLocation();
74  Dimension dim = parent.getSize();
75  setLocation( p.x+dim.width/2-150, p.y+dim.height/2-75 );
76  show();
77  }
78 
79  /**
80  * Close dialog box.
81  */
82  private void close() {
83  this.hide();
84  this.dispose();
85  }
86  /**
87  * Process close window button.
88  * @see java.awt.Component#processEvent(java.awt.AWTEvent)
89  */
90  public void processEvent( AWTEvent e ) {
91  if ( e.getID() == Event.WINDOW_DESTROY )
92  close();
93  }
94 
95 }
AlertBox(Frame parent, String title, String message)
Definition: AlertBox.java:45
void processEvent(AWTEvent e)
Definition: AlertBox.java:90