org.bibalex.daf.commongui
Class MessageBox

java.lang.Object
  extended by org.bibalex.daf.commongui.MessageBox
All Implemented Interfaces:
java.awt.event.ActionListener, java.awt.event.KeyListener, java.awt.event.WindowListener, java.util.EventListener

public class MessageBox
extends java.lang.Object
implements java.awt.event.ActionListener, java.awt.event.WindowListener, java.awt.event.KeyListener

Provides a reusable window that presents a message and choice buttons to the user. A modal dialog is used. Since a thread is used to set the dialog to visible, when the client calls ask() it will not block. The client may implement ActionListener, which has: public void actionPerformed(ActionEvent evt) if user response notification is desired.

Author:
Jack Harich

Field Summary
private  java.awt.Panel buttonPanel
           
private  java.lang.String closeWindowCommand
           
private  java.awt.Dialog dialog
           
private  java.awt.event.ActionEvent event
           
private  java.awt.Frame frame
           
private  boolean frameNotProvided
           
private  java.awt.Canvas imageCanvas
           
private  java.awt.event.ActionListener listener
           
private static int MAX_CHARS_PER_LINE
           
private  java.lang.String title
           
 
Constructor Summary
MessageBox()
          This constructor is used for no listener, such as for a simple okay dialog.
MessageBox(java.awt.event.ActionListener listener)
          This convenience constructor is used to delare the
 
Method Summary
 void actionPerformed(java.awt.event.ActionEvent evt)
           
 void addChoice(java.lang.String label)
          A convenience method that assumes the command is the same as the label.
 void addChoice(java.lang.String label, java.lang.String command)
          The @param label will be used for the button and the
private  void addMultilineString(java.lang.String message, java.awt.Container container)
           
 void ask(java.lang.String message)
          One of the "ask" methods must be the last call when using a MessageBox.
 void askOkay(java.lang.String message)
          Same as ask(String message) except adds an "Okay" button.
 void askOkCancel(java.lang.String message)
          Same as ask(String message) except adds "Ok" and "Cancel" buttons.
 void askYesNo(java.lang.String message)
          Same as ask(String message) except adds "Yes" and "No" buttons.
 void askYesNoCancel(java.lang.String message)
          Same as ask(String message) except adds "Yes", "No" and "Cancel" buttons.
 void askYesNoRetry(java.lang.String message)
          Same as ask(String message) except adds "Yes", "No" and "Retry" buttons.
private  java.awt.Panel createMultiLinePanel(java.lang.String message)
           
private  void enforceMinimumSize(java.awt.Component comp, int minWidth, int minHeight)
           
private  void fireCloseRequested()
           
private  java.awt.GridBagConstraints getDefaultConstraints()
           
 java.lang.String getResult()
          Retrieves the result of the message box.
 void keyPressed(java.awt.event.KeyEvent evt)
          Detects the Escape character and fires the Close request.
 void keyReleased(java.awt.event.KeyEvent evt)
          Empty Implementation
 void keyTyped(java.awt.event.KeyEvent evt)
          Empty Implementation
 void reportError(java.lang.String message)
          Same as ask(String message) except adds an "Okay" button, and an error Image.
 void run()
          This prevents the caller from blocking on ask(), which if this class is used on an awt event thread would cause a deadlock.
 void setActionListener(java.awt.event.ActionListener listener)
          This set the listener to be notified of button clicks and WindowClosing events.
 void setCloseWindowCommand(java.lang.String command)
          Sets the ActionCommand used in the ActionEvent when the user attempts to close the window.
 void setFrame(java.awt.Frame frame)
          If a Frame is provided then it is used to instantiate the modal Dialog.
 void setTitle(java.lang.String title)
          Sets the Title of the Message Box with the given String.
static void showError(java.lang.String errorMessage, boolean addDetails)
          Shows an error message to the user.
static void showMessage(java.lang.String message)
          Shows a normal message to the user.
 void useImageCanvas(java.awt.Canvas imageCanvas)
          This is handy for providing a small image that will be displayed to the left of the message.
 void useImageCanvas(java.lang.String fileName)
          This loads the image from the specified @param fileName, which must be in the same directory as this class.
 void windowActivated(java.awt.event.WindowEvent evt)
          Empty Implementation
 void windowClosed(java.awt.event.WindowEvent evt)
          Empty Implementation
 void windowClosing(java.awt.event.WindowEvent evt)
           
 void windowDeactivated(java.awt.event.WindowEvent evt)
          Empty Implementation
 void windowDeiconified(java.awt.event.WindowEvent evt)
          Empty Implementation
 void windowIconified(java.awt.event.WindowEvent evt)
          Empty Implementation
 void windowOpened(java.awt.event.WindowEvent evt)
          Empty Implementation
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

listener

private java.awt.event.ActionListener listener

dialog

private java.awt.Dialog dialog

closeWindowCommand

private java.lang.String closeWindowCommand

title

private java.lang.String title

frame

private java.awt.Frame frame

frameNotProvided

private boolean frameNotProvided

buttonPanel

private java.awt.Panel buttonPanel

imageCanvas

private java.awt.Canvas imageCanvas

event

private java.awt.event.ActionEvent event

MAX_CHARS_PER_LINE

private static final int MAX_CHARS_PER_LINE
See Also:
Constant Field Values
Constructor Detail

MessageBox

public MessageBox(java.awt.event.ActionListener listener)
This convenience constructor is used to delare the

Parameters:
listener - that will be notified when a button is clicked. The listener must implement ActionListener.

MessageBox

public MessageBox()
This constructor is used for no listener, such as for a simple okay dialog.

Method Detail

showError

public static void showError(java.lang.String errorMessage,
                             boolean addDetails)
Shows an error message to the user.

Parameters:
errorMessage - The message to be displayed to the user.
addDetails - Whether to added details from the log file or not.

showMessage

public static void showMessage(java.lang.String message)
Shows a normal message to the user.

Parameters:
message - The message to be displayed to the user.

run

public void run()
This prevents the caller from blocking on ask(), which if this class is used on an awt event thread would cause a deadlock.


actionPerformed

public void actionPerformed(java.awt.event.ActionEvent evt)
Specified by:
actionPerformed in interface java.awt.event.ActionListener
See Also:
ActionListener.actionPerformed(java.awt.event.ActionEvent)

windowClosing

public void windowClosing(java.awt.event.WindowEvent evt)
Specified by:
windowClosing in interface java.awt.event.WindowListener
See Also:
WindowListener.windowClosing(java.awt.event.WindowEvent)

windowClosed

public void windowClosed(java.awt.event.WindowEvent evt)
Empty Implementation

Specified by:
windowClosed in interface java.awt.event.WindowListener
See Also:
WindowListener.windowClosed(java.awt.event.WindowEvent)

windowDeiconified

public void windowDeiconified(java.awt.event.WindowEvent evt)
Empty Implementation

Specified by:
windowDeiconified in interface java.awt.event.WindowListener
See Also:
WindowListener.windowDeiconified(java.awt.event.WindowEvent)

windowIconified

public void windowIconified(java.awt.event.WindowEvent evt)
Empty Implementation

Specified by:
windowIconified in interface java.awt.event.WindowListener
See Also:
WindowListener.windowIconified(java.awt.event.WindowEvent)

windowOpened

public void windowOpened(java.awt.event.WindowEvent evt)
Empty Implementation

Specified by:
windowOpened in interface java.awt.event.WindowListener
See Also:
WindowListener.windowOpened(java.awt.event.WindowEvent)

windowActivated

public void windowActivated(java.awt.event.WindowEvent evt)
Empty Implementation

Specified by:
windowActivated in interface java.awt.event.WindowListener
See Also:
WindowListener.windowActivated(java.awt.event.WindowEvent)

windowDeactivated

public void windowDeactivated(java.awt.event.WindowEvent evt)
Empty Implementation

Specified by:
windowDeactivated in interface java.awt.event.WindowListener
See Also:
WindowListener.windowDeactivated(java.awt.event.WindowEvent)

keyTyped

public void keyTyped(java.awt.event.KeyEvent evt)
Empty Implementation

Specified by:
keyTyped in interface java.awt.event.KeyListener
See Also:
KeyListener.keyTyped(java.awt.event.KeyEvent)

keyPressed

public void keyPressed(java.awt.event.KeyEvent evt)
Detects the Escape character and fires the Close request.

Specified by:
keyPressed in interface java.awt.event.KeyListener
See Also:
KeyListener.keyPressed(java.awt.event.KeyEvent)

keyReleased

public void keyReleased(java.awt.event.KeyEvent evt)
Empty Implementation

Specified by:
keyReleased in interface java.awt.event.KeyListener
See Also:
KeyListener.keyReleased(java.awt.event.KeyEvent)

fireCloseRequested

private void fireCloseRequested()

setActionListener

public void setActionListener(java.awt.event.ActionListener listener)
This set the listener to be notified of button clicks and WindowClosing events.

Parameters:
listener - The ActionListener to be notified.

setTitle

public void setTitle(java.lang.String title)
Sets the Title of the Message Box with the given String.

Parameters:
title - The String to be used as the title.

setFrame

public void setFrame(java.awt.Frame frame)
If a Frame is provided then it is used to instantiate the modal Dialog. Otherwise a temporary Frame is used. Providing a Frame will have the effect of putting the focus back on that Frame when the MessageBox is closed or a button is clicked.

Parameters:
frame - The Frame used to use in the Modal Dialog mode.

getResult

public java.lang.String getResult()
Retrieves the result of the message box.

Returns:
the string resulting from the actionPerformed event.

setCloseWindowCommand

public void setCloseWindowCommand(java.lang.String command)
Sets the ActionCommand used in the ActionEvent when the user attempts to close the window. The window may be closed by clicking on "X", choosing Close from the window menu, or pressing the Escape key. The default command is "CloseRequested", which is just what a Close choice button would probably have as a command.

Parameters:
command - A String representing the Command to use upon close.

useImageCanvas

public void useImageCanvas(java.awt.Canvas imageCanvas)
This is handy for providing a small image that will be displayed to the left of the message.

Parameters:
imageCanvas - The Canvas to use.

useImageCanvas

public void useImageCanvas(java.lang.String fileName)
This loads the image from the specified @param fileName, which must be in the same directory as this class. For example @param fileName might be "LightBulb.gif".

Parameters:
fileName - The path of the Image to load.

addChoice

public void addChoice(java.lang.String label,
                      java.lang.String command)
The @param label will be used for the button and the

Parameters:
label - The label to add on the Button.
command - will be returned to the listener.

addChoice

public void addChoice(java.lang.String label)
A convenience method that assumes the command is the same as the label.

Parameters:
label - The label of the new Choice (Button).

ask

public void ask(java.lang.String message)
One of the "ask" methods must be the last call when using a MessageBox. This is the simplest "ask" method. It presents the provided @param message. If the messgae length exceeds the MAX_CHARS_PER_LINE, the message is splitted into lines.

Parameters:
message - A String representing the message to be asked.

askOkay

public void askOkay(java.lang.String message)
Same as ask(String message) except adds an "Okay" button.

Parameters:
message - A String representing the message to be asked.

reportError

public void reportError(java.lang.String message)
Same as ask(String message) except adds an "Okay" button, and an error Image.

Parameters:
message - A String representing the message to be asked.

askYesNo

public void askYesNo(java.lang.String message)
Same as ask(String message) except adds "Yes" and "No" buttons.

Parameters:
message - A String representing the message to be asked.

askYesNoCancel

public void askYesNoCancel(java.lang.String message)
Same as ask(String message) except adds "Yes", "No" and "Cancel" buttons.

Parameters:
message - A String representing the message to be asked.

askYesNoRetry

public void askYesNoRetry(java.lang.String message)
Same as ask(String message) except adds "Yes", "No" and "Retry" buttons.

Parameters:
message - A String representing the message to be asked.

askOkCancel

public void askOkCancel(java.lang.String message)
Same as ask(String message) except adds "Ok" and "Cancel" buttons.

Parameters:
message - A String representing the message to be asked.

createMultiLinePanel

private java.awt.Panel createMultiLinePanel(java.lang.String message)

addMultilineString

private void addMultilineString(java.lang.String message,
                                java.awt.Container container)

getDefaultConstraints

private java.awt.GridBagConstraints getDefaultConstraints()

enforceMinimumSize

private void enforceMinimumSize(java.awt.Component comp,
                                int minWidth,
                                int minHeight)