Game-of-Life-WSU-CEG4180  v0.1
RefactoringtheGameofLife
TextFileDialog.java
Go to the documentation of this file.
1 /* This page is part of the Game of Life source code */
2 
3 package org.bitstorm.util;
4 
5 
6 import java.awt.AWTEvent;
7 import java.awt.Button;
8 import java.awt.Color;
9 import java.awt.Dialog;
10 import java.awt.Event;
11 import java.awt.FlowLayout;
12 import java.awt.Frame;
13 import java.awt.Panel;
14 import java.awt.TextArea;
15 import java.awt.event.ActionEvent;
16 import java.awt.event.ActionListener;
17 import java.io.IOException;
18 
19 
20 /**
21  * Shows a text file in a dialog box.
22  * It has a vertical scrollbar and a close button.
23  *
24  * @author Edwin Martin
25  */
26 public class TextFileDialog extends Dialog {
27  /**
28  * Constructs a TextFileDialog.
29  * @param parent parent frame
30  * @param title window title
31  * @param filename filename to show (from jar-file)
32  */
33  public TextFileDialog( Frame parent, String title, String filename, int posX, int posY ) {
34  super( parent, title );
35  String manualText;
36  Button okButton = new Button(" Close ");
37  okButton.addActionListener(
38  new ActionListener() {
39  public void actionPerformed(ActionEvent e) {
40  close();
41  }
42  }
43  );
44 
45  EasyFile file = new EasyFile( this.getClass().getResourceAsStream( filename ) );
46  try {
47  manualText = file.readText();
48  } catch (IOException e) {
49  manualText = "";
50  }
51 
52  TextArea manualTextArea = new TextArea( manualText, 20, 50, TextArea.SCROLLBARS_VERTICAL_ONLY );
53  manualTextArea.setBackground( Color.white ); // improve readability
54  manualTextArea.setForeground( Color.black ); // we don't want white text on a white background
55  manualTextArea.setEditable(false);
56 
57  Panel buttonPanel = new Panel();
58  buttonPanel.setLayout( new FlowLayout( FlowLayout.CENTER ) );
59  buttonPanel.add( okButton );
60 
61  this.add( "Center", manualTextArea );
62  this.add( "South", buttonPanel );
63  this.pack();
64  this.enableEvents(Event.WINDOW_DESTROY);
65  this.setLocation( posX, posY );
66  this.show();
67  }
68 
69  /**
70  * Close dialog box.
71  */
72  private void close() {
73  this.hide();
74  this.dispose();
75  }
76 
77  /**
78  * Handle close window button.
79  * @see java.awt.Component#processEvent(java.awt.AWTEvent)
80  */
81  public void processEvent( AWTEvent e ) {
82  if ( e.getID() == Event.WINDOW_DESTROY )
83  close();
84  }
85 }
TextFileDialog(Frame parent, String title, String filename, int posX, int posY)