ca.bc.webarts.widgets
Class Util

java.lang.Object
  |
  +--ca.bc.webarts.widgets.Util

public class Util
extends java.lang.Object

A set of useful utility methods/functions for reuse.

Author:
Tom Gutwin P.Eng

Field Summary
private static java.lang.String CLASSNAME
          A holder This classes name (used when logging).
private static java.lang.String CLASSPATH
          The VM classpath (used in some methods)..
private static boolean initUtil
          Class flag signifying if the initUtil method has been called
private static Log log_
          Gets a Log instance in case any calling classes are using it.
static short OPEN_DIALOG
          A constant to specify which dialog to open with the chooseFileDialog.
static short SAVE_DIALOG
          A constant to specify which dialog to open with the chooseFileDialog.
static java.lang.String SYSTEM_FILE_SEPERATOR
          A holder for this clients System File Separator.
 
Constructor Summary
Util()
           
 
Method Summary
static java.lang.String chooseAFilename(java.awt.Component parent, java.lang.String startPath)
          Encapsulates the showing of the FileChooser dialog and returns a String representing the absolute path.
static java.lang.String chooseAFilename(java.awt.Component parent, java.lang.String startPath, ExampleFileFilter[] filters, boolean filesAndDirs)
          Encapsulates the showing of the FileChooser dialog and returns a String representing the absolute path.
static java.lang.String chooseAFilename(java.awt.Component parent, java.lang.String startPath, ExampleFileFilter[] filters, boolean filesAndDirs, short openOrClose)
          Encapsulates the showing of the FileChooser dialog and returns a String representing the absolute path.
static java.lang.String createCurrentTimeStamp()
          Creates a timestamp for the current time in the form of 'hour + "-" + min + "-" + sec + "-" + millis'.
static void ensureFolderExists(java.io.File folder)
          Ensures that a folder exists.
static java.net.URL getFileBaseURL(java.lang.String fileString)
          Creates a URL for the specified filename string.
static java.lang.String getFilePathFromClasspath(java.lang.String filename)
          Searches the classpath for the specified filename and then returns the full path that is used for it.
static java.lang.String getFilePathFromClasspath(java.lang.String filename, java.lang.String fallBack)
          Searches the classpath for the specified filename and then returns the full path that is used for it.
static java.io.File initDirFile(java.lang.String s)
          Initisalizes File only if it is directory.It is represented by the passed in String.
private static void initUtil()
           
static java.awt.Image loadImage(java.lang.String imageFilename)
          Loads the requested Image filename from the classpath.
static java.awt.Image loadImage(java.lang.String imageFilename, java.lang.String jarFilename)
          Loads the requested Image filename from the specified jarfile.
static void removeDir(java.lang.String fileName)
          Recursively deletes all the files in the spec'd directory and subdirectories, and then removes the dir.
static boolean sleep(long waitTime)
          A method to simply abstract the Try/Catch required to put the current thread to sleep for the specified time in ms.
static java.lang.String toHEXString(byte b)
          A simple byte to Hex String converter.
static java.lang.String tokenReplace(java.lang.String sentence, java.lang.String token, java.lang.String replacement)
          A simple String token replacement routine.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

SYSTEM_FILE_SEPERATOR

public static final java.lang.String SYSTEM_FILE_SEPERATOR
A holder for this clients System File Separator.

OPEN_DIALOG

public static final short OPEN_DIALOG
A constant to specify which dialog to open with the chooseFileDialog.

SAVE_DIALOG

public static final short SAVE_DIALOG
A constant to specify which dialog to open with the chooseFileDialog.

CLASSNAME

private static java.lang.String CLASSNAME
A holder This classes name (used when logging).

CLASSPATH

private static java.lang.String CLASSPATH
The VM classpath (used in some methods)..

log_

private static Log log_
Gets a Log instance in case any calling classes are using it.

initUtil

private static boolean initUtil
Class flag signifying if the initUtil method has been called
Constructor Detail

Util

public Util()
Method Detail

initUtil

private static void initUtil()

ensureFolderExists

public static void ensureFolderExists(java.io.File folder)
Ensures that a folder exists.

Ensures that a folder exists

 
Description:
use it like this:
ensureFolderExists(new File(fileName).getParentFile());
Parameters:
folder - The File object to check.

toHEXString

public static java.lang.String toHEXString(byte b)
A simple byte to Hex String converter.
Parameters:
b - The byte to convert
Returns:
The resultant Hex String

createCurrentTimeStamp

public static java.lang.String createCurrentTimeStamp()
Creates a timestamp for the current time in the form of 'hour + "-" + min + "-" + sec + "-" + millis'.
Returns:
The CurrentTimeStamp value.

tokenReplace

public static java.lang.String tokenReplace(java.lang.String sentence,
                                            java.lang.String token,
                                            java.lang.String replacement)
A simple String token replacement routine. Replaces all occurences of the token parameter with the replacement value in the passed in sentence parameter.
Parameters:
sentence - The String to perform the token replacement on
token - the token String to seartch for and replace
replacement - the tokens replacement value
Returns:
The new token replaced string

removeDir

public static void removeDir(java.lang.String fileName)
Recursively deletes all the files in the spec'd directory and subdirectories, and then removes the dir.
Parameters:
fileName - The filename of the directory to delete.

initDirFile

public static java.io.File initDirFile(java.lang.String s)
Initisalizes File only if it is directory.It is represented by the passed in String.
Parameters:
s - the directory name to init as a File
Returns:
An instatiated File object if the passed string is a dir, null if not

getFilePathFromClasspath

public static java.lang.String getFilePathFromClasspath(java.lang.String filename,
                                                        java.lang.String fallBack)
Searches the classpath for the specified filename and then returns the full path that is used for it.
Parameters:
filename - to look for in the classpath
fallBack - if the file is not found in classpath
Returns:
the path description of the passed filename as found in the classpath.

getFilePathFromClasspath

public static java.lang.String getFilePathFromClasspath(java.lang.String filename)
Searches the classpath for the specified filename and then returns the full path that is used for it.
Returns:
the path description of the passed filename as found in the classpath.

loadImage

public static java.awt.Image loadImage(java.lang.String imageFilename,
                                       java.lang.String jarFilename)
Loads the requested Image filename from the specified jarfile.
Parameters:
imageFilename - the filename in the jarfile to use as the basis for the Image to retrieve.
jarFilename - the jar file to look through for the image.
Returns:
Image the Image loaded from the filename spec'd as an imput param, It returns null if it cannot retrieve the Image.

loadImage

public static java.awt.Image loadImage(java.lang.String imageFilename)
Loads the requested Image filename from the classpath.
Parameters:
imageFilename - the filename in the jarfile to use as the basis for the Image to retrieve.
Returns:
Image the Image loaded from the filename spec'd as an imput param, It returns null if it cannot retrieve the Image.

sleep

public static boolean sleep(long waitTime)
A method to simply abstract the Try/Catch required to put the current thread to sleep for the specified time in ms.
Parameters:
waitTime - the sleep time in milli seconds (ms).
Returns:
boolean value specifying if the sleep completed (true) or was interupted (false).

chooseAFilename

public static java.lang.String chooseAFilename(java.awt.Component parent,
                                               java.lang.String startPath)
Encapsulates the showing of the FileChooser dialog and returns a String representing the absolute path.

NO file filters will be used and NO directory selection is allowed.

Parameters:
parent - the owner of this dialog (generally just send this)
startPath - the dir to start the file chooser dialog from
Returns:
the absolute path of the chosen file.

chooseAFilename

public static java.lang.String chooseAFilename(java.awt.Component parent,
                                               java.lang.String startPath,
                                               ExampleFileFilter[] filters,
                                               boolean filesAndDirs)
Encapsulates the showing of the FileChooser dialog and returns a String representing the absolute path.

This method requires all the config parms specified by the ExampleFileFilter.

Parameters:
parent - the owner of this dialog (generally just send this)
startPath - the dir to start the file chooser dialog from
filters - an array holding the Filters to use in the dialog
filesAndDirs - specs if the user will be allowed to choose both files or dirs.
Returns:
the absolute path of the chosen file.

chooseAFilename

public static java.lang.String chooseAFilename(java.awt.Component parent,
                                               java.lang.String startPath,
                                               ExampleFileFilter[] filters,
                                               boolean filesAndDirs,
                                               short openOrClose)
Encapsulates the showing of the FileChooser dialog and returns a String representing the absolute path.

This method requires all the config parms specified by the ExampleFileFilter.

Parameters:
parent - the owner of this dialog (generally just send this)
startPath - the dir to start the file chooser dialog from
filters - an array holding the Filters to use in the dialog
filesAndDirs - specs if the user will be allowed to choose both files or dirs.
Returns:
the absolute path of the chosen file.

getFileBaseURL

public static java.net.URL getFileBaseURL(java.lang.String fileString)
Creates a URL for the specified filename string.
Parameters:
fileString - is the filename to turn into a file://URL
Returns:
the converted file://URL