Class MessageUtil

java.lang.Object
ca.corbett.extras.MessageUtil

public final class MessageUtil extends Object
Simple utility class to show messages to the user via JOptionPane while also simultaneously logging them. This saves the caller from having to make two separate calls each time an error comes up.

General logging and messaging

Example usage:

     MessageUtil messageUtil = new MessageUtil(this, myLogger);
     messageUtil.error("Something went horribly wrong", someException);
 
This will show an error dialog to the user, and also log the message string to the given Logger. It is equivalent to the following code:
     JOptionPane.showMessageDialog(this, "Something went horribly wrong",
             "Error", JOptionPane.ERROR_MESSAGE);
     myLogger.log(Level.SEVERE, "Something went horribly wrong", someException);
 

Both the given parent component and the given Logger are optional. If no parent component is given, dialogs will have no parent. If no Logger is given, no logging will occur.

Asking questions

MessageUtil has shorthand methods for asking simple yes/no or yes/no/cancel questions via JOptionPane. These methods do not perform any logging. For example:

     int result = messageUtil.askYesNo("Confirm Delete",
              "Are you sure you want to delete this file?");
     if (result == MessageUtil.YES) {
         // User clicked Yes
         // ...
     }
 

This saves a small amount of code and improves readability.

Getting user input

MessageUtil also has shorthand methods for asking the user to select from a list of options, or to enter free-form text input. These, once again, are wrappers around JOptionPane methods that save a small amount of code and improve readability. Examples:

     // Get free-text entry with some default value already filled in:
     String color = messageUtil.askText("Select Color",
                                  "Enter your favorite color:",
                                  "Blue");
     if (color != null) {
         // User made a selection (null would mean they cancelled)
     }

     // Get selection from a list of options:
     String[] options = {"Red", "Green", "Blue"};
     String color = messageUtil.askSelect("Select Color",
                              "Choose your favorite color:",
                              options,
                              "Green");
     if (color != null) {
         // User made a selection (null would mean they cancelled)
     }
 

Nothing is logged when asking for user input.

Since:
2022-04-11
Author:
scorbo2
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
     
    static final int
     
    static final int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a MessageUtil with the given parent Component.
    MessageUtil(Component parent, Logger logger)
    Creates a MessageUtil with the given parent Component and the given Logger.
  • Method Summary

    Modifier and Type
    Method
    Description
    askSelect(String message, String[] options, String initialSelectionValue)
    Shorthand for using JOptionPane to ask the user to select from a list of options.
    askSelect(String title, String message, String[] options, String initialSelectionValue)
    Shorthand for using JOptionPane to ask the user to select from a list of options.
    askText(String message, String initialValue)
    Shorthand for using JOptionPane to ask the user for free-form text input.
    askText(String title, String message, String initialValue)
    Shorthand for using JOptionPane to ask the user for free-form text input.
    int
    askYesNo(String message)
    Shorthand for using JOptionPane to ask the user a simple yes or no question.
    int
    askYesNo(String title, String message)
    Shorthand for using JOptionPane to ask the user a simple yes or no question.
    int
    Shorthand for using JOptionPane to ask the user a yes, no, or cancel question.
    int
    askYesNoCancel(String title, String message)
    Shorthand for using JOptionPane to ask the user a yes, no, or cancel question.
    void
    error(String message)
    Shows an error message to the user, and also logs it if a Logger was provided.
    void
    error(String title, String message)
    Shows an error message to the user, and also logs it if a Logger was provided.
    void
    error(String title, String message, Throwable ex)
    Shows an error message to the user, and also logs it if a Logger was provided.
    void
    error(String message, Throwable ex)
    Shows an error message to the user, and also logs it if a Logger was provided.
    Returns the Logger currently being used.
    Returns the configured parent Component, if one is set.
    void
    info(String message)
    Shows an info message to the user, and also logs it if a Logger was provided.
    void
    info(String title, String message)
    Shows an info message to the user, and also logs it if a Logger was provided.
    void
    Used to specify a default dialog title in the case where error() is invoked without a given title.
    void
    Used to specify a default dialog title in the case where info() is invoked without a given title.
    void
    Used to specify a default dialog title in the case where input dialogs are invoked without a given title.
    void
    Used to specify a default dialog title in the case where askYesNo() or askYesNoCancel() is invoked without a given title.
    void
    Used to specify a default dialog title in the case where warning() is invoked without a given title.
    void
    warning(String message)
    Shows a warning message to the user, and also logs it if a Logger was provided.
    void
    warning(String title, String message)
    Shows a warning message to the user, and also logs it if a Logger was provided.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • MessageUtil

      public MessageUtil(Component parent)
      Creates a MessageUtil with the given parent Component.
      Parameters:
      parent - The parent Component to be used with message dialogs.
    • MessageUtil

      public MessageUtil(Component parent, Logger logger)
      Creates a MessageUtil with the given parent Component and the given Logger.
      Parameters:
      parent - The parent Component to be used with message dialogs.
      logger - The Logger which will receive INFO and SEVERE messages.
  • Method Details

    • getParent

      public Component getParent()
      Returns the configured parent Component, if one is set. May be null.
      Returns:
      The parent Component, or null.
    • getLogger

      public Logger getLogger()
      Returns the Logger currently being used. May be null if none was set.
      Returns:
      A Logger instance, or null.
    • setDefaultErrorTitle

      public void setDefaultErrorTitle(String title)
      Used to specify a default dialog title in the case where error() is invoked without a given title. Does not affect logging. If unset, the default is "Error".
      Parameters:
      title - The new title to use for error dialogs.
    • setDefaultWarningTitle

      public void setDefaultWarningTitle(String title)
      Used to specify a default dialog title in the case where warning() is invoked without a given title. Does not affect logging. If unset, the default is "Warning".
      Parameters:
      title - The new title to use for warning dialogs.
    • setDefaultInfoTitle

      public void setDefaultInfoTitle(String title)
      Used to specify a default dialog title in the case where info() is invoked without a given title. Does not affect logging. If unset, the default is "Info".
      Parameters:
      title - The new title to use for info dialogs.
    • setDefaultQuestionTitle

      public void setDefaultQuestionTitle(String title)
      Used to specify a default dialog title in the case where askYesNo() or askYesNoCancel() is invoked without a given title. If unset, the default is "Confirm".
      Parameters:
      title - The new title to use for question dialogs.
    • setDefaultInputTitle

      public void setDefaultInputTitle(String title)
      Used to specify a default dialog title in the case where input dialogs are invoked without a given title. If unset, the default is "Select".
      Parameters:
      title - The new title to use for input dialogs.
    • error

      public void error(String message)
      Shows an error message to the user, and also logs it if a Logger was provided.
      Parameters:
      message - The error message to show.
    • error

      public void error(String title, String message)
      Shows an error message to the user, and also logs it if a Logger was provided.
      Parameters:
      title - The title for the dialog.
      message - The error message to show.
    • error

      public void error(String message, Throwable ex)
      Shows an error message to the user, and also logs it if a Logger was provided.
      Parameters:
      message - The error message to show.
      ex - An Exception which will be handed to the Logger.
    • error

      public void error(String title, String message, Throwable ex)
      Shows an error message to the user, and also logs it if a Logger was provided.
      Parameters:
      title - The title for the dialog.
      message - The error message to show.
      ex - An Exception which will be handed to the Logger.
    • info

      public void info(String message)
      Shows an info message to the user, and also logs it if a Logger was provided.
      Parameters:
      message - The info message to show.
    • info

      public void info(String title, String message)
      Shows an info message to the user, and also logs it if a Logger was provided.
      Parameters:
      title - The title for the dialog.
      message - The info message to show.
    • warning

      public void warning(String message)
      Shows a warning message to the user, and also logs it if a Logger was provided.
      Parameters:
      message - The warning message to show.
    • warning

      public void warning(String title, String message)
      Shows a warning message to the user, and also logs it if a Logger was provided.
      Parameters:
      title - The title for the dialog.
      message - The warning message to show.
    • askYesNo

      public int askYesNo(String message)
      Shorthand for using JOptionPane to ask the user a simple yes or no question. Nothing is logged. A default dialog title of "Confirm" is used.
      Parameters:
      message - The question message.
      Returns:
      One of JOptionPane.YES_OPTION or JOptionPane.NO_OPTION (shorthand: MessageUtil.YES or MessageUtil.NO).
    • askYesNoCancel

      public int askYesNoCancel(String message)
      Shorthand for using JOptionPane to ask the user a yes, no, or cancel question. Nothing is logged. A default dialog title of "Confirm" is used.
      Parameters:
      message - The question message.
      Returns:
      One of JOptionPane.YES_OPTION, JOptionPane.NO_OPTION, or JOptionPane.CANCEL_OPTION (shorthand: MessageUtil.YES, MessageUtil.NO, or MessageUtil.CANCEL).
    • askYesNo

      public int askYesNo(String title, String message)
      Shorthand for using JOptionPane to ask the user a simple yes or no question. Nothing is logged.
      Parameters:
      title - The title for the dialog.
      message - The question message.
      Returns:
      One of JOptionPane.YES_OPTION or JOptionPane.NO_OPTION (shorthand: MessageUtil.YES or MessageUtil.NO).
    • askYesNoCancel

      public int askYesNoCancel(String title, String message)
      Shorthand for using JOptionPane to ask the user a yes, no, or cancel question. Nothing is logged.
      Parameters:
      title - The title for the dialog.
      message - The question message.
      Returns:
      One of JOptionPane.YES_OPTION, JOptionPane.NO_OPTION, or JOptionPane.CANCEL_OPTION (shorthand: MessageUtil.YES, MessageUtil.NO, or MessageUtil.CANCEL).
    • askSelect

      public String askSelect(String message, String[] options, String initialSelectionValue)
      Shorthand for using JOptionPane to ask the user to select from a list of options. A default dialog title of "Select" is used. Nothing is logged.
      Parameters:
      message - The prompt message.
      options - An array of possible options in String form.
      initialSelectionValue - The option that should be initially selected when the dialog appears.
      Returns:
      The selected option as a String, or null if the user cancelled.
    • askSelect

      public String askSelect(String title, String message, String[] options, String initialSelectionValue)
      Shorthand for using JOptionPane to ask the user to select from a list of options. Nothing is logged.
      Parameters:
      title - The title for the dialog.
      message - The prompt message.
      options - An array of possible options in String form.
      initialSelectionValue - The option that should be initially selected when the dialog appears.
      Returns:
      The selected option as a String, or null if the user cancelled.
    • askText

      public String askText(String message, String initialValue)
      Shorthand for using JOptionPane to ask the user for free-form text input. A default dialog title of "Select" is used. Nothing is logged.
      Parameters:
      message - The prompt message.
      initialValue - The initial value to pre-fill in the input field.
      Returns:
      The entered text as a String, or null if the user cancelled.
    • askText

      public String askText(String title, String message, String initialValue)
      Shorthand for using JOptionPane to ask the user for free-form text input. Nothing is logged.
      Parameters:
      title - The title for the dialog.
      message - The prompt message.
      initialValue - The initial value to pre-fill in the input field.
      Returns:
      The entered text as a String, or null if the user cancelled.