gov.nih.nlm.swing
Class SwingToolkit

java.lang.Object
  extended bygov.nih.nlm.swing.SwingToolkit
All Implemented Interfaces:
SwingConstants
Direct Known Subclasses:
MMSToolkit

public abstract class SwingToolkit
extends Object
implements SwingConstants

GUI utility class. It provides several constants for useful GUI elements, as well as methods for reporting errors, informing uses of information, or requesting feedback from a user.

Author:
MEME Group

Field Summary
protected static Map key_map
           
protected static Properties properties
           
 
Fields inherited from interface gov.nih.nlm.swing.SwingConstants
EMPTY_BORDER, EMPTY_BORDER_NO_TOP, EMPTY_INSETS, GRID_INSETS, HAS_POPUP_BORDER, IS_REQUIRED_BORDER, VIEW
 
Constructor Summary
SwingToolkit()
           
 
Method Summary
static boolean confirmRequest(Component parent, String request)
          Asks the user a (yes/no) question.
static boolean confirmRequest(String request)
          Asks the user a (yes/no) question.
static boolean confirmRequestWithOption(Component parent, String request, String key)
          Asks the user a (yes/no) question.
static boolean copy(File in, File out)
          Copies the input file to the output file.
static void displayTextFile(Component parent, String title, File file)
          Displays a text file.
static void displayTextFile(Component parent, String title, File file, int rows, int cols)
          Displays a text file.
static void displayURL(Component parent, String title, URL url)
          Displays the file at a given URL.
static void Exit(int return_value)
          Cleans up loose ends before calling System.exit(int).
static String getMultiLineUserInput(Component parent, String message)
          Asks the user a question that involves a potentially multi-line answer.
static String getMultiLineUserInput(String request)
          Asks the user a question that involves a potentially multi-line answer.
static JPanel getPanelForHTML(URL url)
          Returns a JPanel displaying the contents of the specified url URL.
static Properties getProperties()
          Returns the toolkit Properties.
static String getProperty(String name)
          Returns the property value for the specified name.
static String getProperty(String name, String _default)
          Returns the property value for the specified name.
static String getUserInput(Component parent, String message)
          Asks the user a question.
static String getUserInput(String request)
          Asks the user a question.
static Image makeColorTransparent(Image im, Color color, Color repl_color)
          Make a certain color transparent and return a new image.
static String md5(File in)
          Computes the MD5 of the specified file.
static void notifyUser(Component parent, String message)
          Notifies the user of something.
static void notifyUser(Component parent, String message, String title)
          Notifies the user of something.
static void notifyUser(String request)
          Notifies the user of something.
static void notifyUserWithOption(Component parent, String message, String title, String key)
          Notifies the user of something.
static BufferedImage readScaledImage(URL url, int w, boolean composite)
          Read a scaled image from the specified URL.
static void removeProperty(String name)
          Remove a property.
static void reportError(Component parent, String error, boolean fatal)
          Reports an error message to the user.
static void reportError(String error)
          Reports an error message to the user.
static void reportError(String error, boolean fatal)
          Reports an error message to the user.
static void setProperties(Properties props)
          Sets the toolkit properties.
static void setProperty(String name, String value)
          Sets a property.
static boolean unzip(String zip_file, String output_dir, String archive_subdir, Frame owner)
          Unzips the specified file into the specified directory using the specified operating system command.
static boolean unzip(String unzip_cmd, String zip_file, String output_dir, String archive_subdir, Frame owner)
          Unzips the specified file into the specified directory using the specified operating system command.
static boolean usingView()
          Indicates whether or not the application is using a GUI.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

properties

protected static Properties properties

key_map

protected static Map key_map
Constructor Detail

SwingToolkit

public SwingToolkit()
Method Detail

setProperty

public static void setProperty(String name,
                               String value)
Sets a property.

Parameters:
name - the property name
value - the property value

setProperties

public static void setProperties(Properties props)
Sets the toolkit properties. This can be used to override the properties set in the call to initialize.

Parameters:
props - a set of new Properties

getProperty

public static String getProperty(String name)
Returns the property value for the specified name.

Parameters:
name - the property name
Returns:
the property value, or null if it is not found

getProperty

public static String getProperty(String name,
                                 String _default)
Returns the property value for the specified name. If the property does not exist, the specified default value is returned instead.

Parameters:
name - the property name
_default - the default value if the property value is null
Returns:
the property value, or the default value if the property value is null

removeProperty

public static void removeProperty(String name)
Remove a property.

Parameters:
name - the property name.

getProperties

public static Properties getProperties()
Returns the toolkit Properties.

Returns:
the toolkit Properties

usingView

public static boolean usingView()
Indicates whether or not the application is using a GUI. This is based on the value of the SwingConstants.VIEW property.

Returns:
true if the current application is using a view, false otherwise

reportError

public static void reportError(String error)
Reports an error message to the user. The error is either written to STDERR or shown in a dialog box, depending upon the return value of usingView().

Parameters:
error - an error message

reportError

public static void reportError(String error,
                               boolean fatal)
Reports an error message to the user. The error is either written to STDERR or shown in a dialog box, depending upon the return value of usingView().

Parameters:
error - an error message
fatal - a flag indicating whether or not the error is fatal

reportError

public static void reportError(Component parent,
                               String error,
                               boolean fatal)
Reports an error message to the user. The error is either written to STDERR or shown in a dialog box, depending upon the return value of usingView().

Parameters:
parent - the parent component
error - an error message
fatal - a flag indicating whether or not the error is fatal

confirmRequest

public static boolean confirmRequest(String request)
Asks the user a (yes/no) question. Calls confirmRequest(Component, String) with a null parent

Parameters:
request - the (yes/no) question
Returns:
true if yes, false if no

confirmRequest

public static boolean confirmRequest(Component parent,
                                     String request)
Asks the user a (yes/no) question. If usingView() returns true, this method opens a dialog box with a message and a (yes/no) prompt for the user. If usingView() returns false, it prompts the user on the command line.

Parameters:
parent - the parent Component of the dialog box
request - the (yes/no) question
Returns:
true if yes; false if no

confirmRequestWithOption

public static boolean confirmRequestWithOption(Component parent,
                                               String request,
                                               String key)
Asks the user a (yes/no) question. If usingView() returns true, this method opens a dialog box with a message and a (yes/no) prompt for the user. If usingView() returns false, it prompts the user on the command line.

Parameters:
parent - the parent Component of the dialog box
request - the (yes/no) question
key - used to differentiate this request from others
Returns:
true if yes; false if no

notifyUser

public static void notifyUser(String request)
Notifies the user of something. Calls notifyUser(Component, String) with a null parent.

Parameters:
request - the message

notifyUser

public static void notifyUser(Component parent,
                              String message)
Notifies the user of something. If usingView() returns true, this method opens a dialog box informing the user of the state of something . It returns upon an OK click or close. If usingView() returns false, it notifies the user on the command line.

Parameters:
parent - the parent Component
message - the message

notifyUser

public static void notifyUser(Component parent,
                              String message,
                              String title)
Notifies the user of something. If usingView() returns true, this method opens a dialog box informing the user of the state of something . It returns upon an OK click or close. If usingView() returns false, it notifies the user on the command line.

Parameters:
parent - the parent Component
message - the message
title - for the dialog box

notifyUserWithOption

public static void notifyUserWithOption(Component parent,
                                        String message,
                                        String title,
                                        String key)
Notifies the user of something. If usingView() returns true, this method opens a dialog box informing the user of the state of something . It returns upon an OK click or close. If usingView() returns false, it notifies the user on the command line. Will not display if user has previously checked the JCheckBox "Don't show this message again." from the calling instance indicated by the key parameter.

Parameters:
parent - the parent Component
message - the message
title - for the dialog box
key - to differentiate calling instances

displayTextFile

public static void displayTextFile(Component parent,
                                   String title,
                                   File file)
Displays a text file. If usingView() returns true, this method opens a dialog box displaying the given File. It returns upon an OK click or close. If usingView() returns false, it displays the text on the command line.

Parameters:
parent - the parent Component
title - title for the display
file - the message

displayTextFile

public static void displayTextFile(Component parent,
                                   String title,
                                   File file,
                                   int rows,
                                   int cols)
Displays a text file. If usingView() returns true, this method opens a dialog box displaying the given File. It returns upon an OK click or close. If usingView() returns false, it displays the text on the command line.

Parameters:
parent - the parent Component
title - title for the display
file - the message
rows - the number of rows to be displayed at one time without scrolling
cols - the number of columns to be displayed at one time without scrolling

displayURL

public static void displayURL(Component parent,
                              String title,
                              URL url)
Displays the file at a given URL. If usingView() returns true, this method opens a dialog box displaying the given URL. It returns upon an OK click or close. If usingView() returns false, nothing happens.

Parameters:
parent - the parent Component
title - title for the display
url - URL to be displayed

getUserInput

public static String getUserInput(String request)
Asks the user a question. Calls getUserInput(Component, String) with a null parent.

Parameters:
request - the message
Returns:
the user response

getUserInput

public static String getUserInput(Component parent,
                                  String message)
Asks the user a question. If usingView() returns true, this method opens a dialog box with the specified question and "OK"/"Cancel" buttons. If usingView() returns false, a response from the user is solicited on the command line.

Parameters:
parent - the parent Component
message - the message
Returns:
the user response

getMultiLineUserInput

public static String getMultiLineUserInput(String request)
Asks the user a question that involves a potentially multi-line answer. Calls getMultiLineUserInput(Component, String) with a null parent

Parameters:
request - the request
Returns:
the user response

getMultiLineUserInput

public static String getMultiLineUserInput(Component parent,
                                           String message)
Asks the user a question that involves a potentially multi-line answer. If usingView() returns true, this method opens a dialog box asking the user a question and soliciting a multi-line response. If usingView() returns false, a response from the user is solicited on the command line.

Parameters:
parent - the parent Component
message - the message
Returns:
the user response

copy

public static boolean copy(File in,
                           File out)
                    throws IOException
Copies the input file to the output file. This method makes use of a progress monitor (if usingView() returnes true).

Parameters:
in - the input File
out - the output File
Returns:
true if the copying was successful, false otherwise.
Throws:
IOException

md5

public static String md5(File in)
                  throws IOException,
                         NoSuchAlgorithmException
Computes the MD5 of the specified file.

Parameters:
in - the File
Returns:
the MD5 value, or null if cancelled.
Throws:
IOException
NoSuchAlgorithmException

unzip

public static boolean unzip(String zip_file,
                            String output_dir,
                            String archive_subdir,
                            Frame owner)
                     throws IOException
Unzips the specified file into the specified directory using the specified operating system command. This method makes use of a progress monitor (if usingView() returns true).

Parameters:
zip_file - file to be unzipped
output_dir - location for unzipped files
archive_subdir - the portion of the archive to extract
owner - parent Component
Returns:
true if the unzipping was successful, false otherwise.
Throws:
IOException

unzip

public static boolean unzip(String unzip_cmd,
                            String zip_file,
                            String output_dir,
                            String archive_subdir,
                            Frame owner)
                     throws IOException
Unzips the specified file into the specified directory using the specified operating system command. This method makes use of a progress monitor (if usingView() returns true).

Parameters:
unzip_cmd - the OS command to use to unzip
zip_file - file to be unzipped
output_dir - location for unzipped files
archive_subdir - the portion of the archive to extract
owner - parent Component
Returns:
true if the unzipping was successful, false otherwise.
Throws:
IOException

Exit

public static void Exit(int return_value)
Cleans up loose ends before calling System.exit(int). All applications should call SwingToolkit.Exit(0) instead of calling System.exit(0).

Parameters:
return_value - the exit code. 0 typically means no error.

getPanelForHTML

public static JPanel getPanelForHTML(URL url)
Returns a JPanel displaying the contents of the specified url URL.

Parameters:
url - to display
Returns:
a JPanel

makeColorTransparent

public static Image makeColorTransparent(Image im,
                                         Color color,
                                         Color repl_color)
Make a certain color transparent and return a new image.

Parameters:
im - The original Image
color - the Color to be transparent
repl_color - Color replace color
Returns:
the new Image

readScaledImage

public static BufferedImage readScaledImage(URL url,
                                            int w,
                                            boolean composite)
                                     throws IOException
Read a scaled image from the specified URL.

Parameters:
url - the URL
w - scale to this width
composite - true if images should be faded
Returns:
BufferedImage matching the params
Throws:
IOException - if the URL cannot be accessed


Copyright ©2005