ca.bc.webarts.widgets
Class Util

java.lang.Object
  extended byca.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).
static java.lang.String CLASSPATH
          The VM classpath (used in some methods)..
static short DEFAULT_LOOKANDFEEL
          the Default LookAndFeel CONSTANT.
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 MAC_LOOKANDFEEL
          the Mac LookAndFeel CONSTANT.
static java.lang.String macClassName
          the Mac LookAndFeel class name
static short METAL_LOOKANDFEEL
          the Metal LookAndFeel CONSTANT.
static java.lang.String metalClassName
          the Java LookAndFeel class name
static short MOTIF_LOOKANDFEEL
          the Motif LookAndFeel CONSTANT.
static java.lang.String motifClassName
          the motif LookAndFeel class name
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.
static java.lang.String SYSTEM_LINE_SEPERATOR
          A holder for this clients System line termination separator.
static java.lang.String USERDIR
          The users home ditrectory.
static java.lang.String USERHOME
          The users home ditrectory.
static java.lang.String windowsClassName
          the Windoze LookAndFeel class name
static short WINDOZE_LOOKANDFEEL
          the Windoze LookAndFeel CONSTANT.
 
Constructor Summary
Util()
           
 
Method Summary
static java.lang.String absoluteToPwdRelativePath(java.io.File absPath)
          Converts an Absolute path to a relative path from the VM Working Dir.
static java.lang.String absoluteToPwdRelativePath(java.lang.String absPath)
          Converts an Absolute path to a relative path from the VM Working Dir.
static java.lang.String capsToSpacesInString(java.lang.String fName)
          Takes a string and adds a space in front of any capitalized char.
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 long countFilesInDir(java.lang.String dirName, boolean recurse)
          Counts the files in a dir (optionally recurses sub dirs)
static java.lang.String createCurrentDateStamp()
          Creates a timestamp for the current date.
static java.lang.String createCurrentDateTimeStamp()
          Creates a timestamp for the current date and time.
static java.lang.String createCurrentTimeStamp()
          Creates a timestamp for the current time in the form of 'hour + "-" + min + "-" + sec + "-" + millis'.
static java.lang.String createTimerTimeStamp(int startTimeMillis)
          Creates a Timer timestamp from the current time to the passed startTime in the form of
'hour + "-" + min + "-" + sec + "-" + millis'.
static long dirSize(java.lang.String fileName, boolean recurseSubdirs)
          Obtains the bytes of data contained in all files in this dir (and subdirs).
static void ensureFolderExists(java.io.File folder)
          Ensures that a folder exists.
static void ensureFolderExists(java.lang.String foldername)
           
static int executeNativeApp(java.lang.String executableLocation, java.lang.String[] appParms, java.lang.String outFilename)
          Executes the Specified Native OS application with the provided commandline parameters.
static int executeNativeApp(java.lang.String executableLocation, java.util.Vector appParms, java.lang.String outFilename)
          Executes the Specified Native OS application with the provided commandline parameters.
static java.util.Vector findFile(java.lang.String fileName, java.lang.String directory, boolean recurseSubdirs, boolean regExp, boolean verbose)
          Searches the provided directory (and subdirs) for the requested file.
static java.awt.Frame getAncestorFrame(java.awt.Component c)
          This method seeks out and returns the Parent Frame for a given component.
static java.util.Date getCurrentDate()
          gets the current date and time.
static java.net.URL getFileBaseURL(java.lang.String fileString)
          Creates a URL for the specified filename string.
static java.net.URL[] getFileBaseURLs(java.lang.String[] fileStrings)
          Creates a URL[] for the given Strings.
static java.io.File getFileDir(java.lang.String fileString)
          Creates a File representing the Dir 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.net.InetAddress getLocalhostName()
          gets the current VMs localhost address.
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()
          Inits the log and some other vars.
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 main(java.lang.String[] args)
           
static boolean moveFile(java.lang.String filename, java.lang.String foldername)
          Moves the specified file to the specified directory.
static boolean removeDir(java.lang.String fileName)
          Recursively deletes all the files in the spec'd directory and subdirectories, and then removes the dir.
static boolean removeFile(java.lang.String fileName)
          Deletes a file.
static boolean removeOldFiles(java.lang.String dirName, int numDays, boolean recurse)
          Deletes all files in the spec'd dir that are older than the spec'd number of days.
static java.lang.String removeParentRelativeReference(java.lang.String path)
          Removes a '/..' from the filename string and returns the corrected dir name reference.
static java.util.Hashtable searchInFile(java.lang.String searchStr, java.lang.String filename, boolean recurseSubdirs, boolean fileRegExp, boolean verbose)
          Searches the provided file/directory (and subdirs) for the search String.
static void setIconForApp(javax.swing.JFrame appFrame, java.lang.String imageFilename)
          SetsIcon for the passed in Frame
static void setIconForApp(javax.swing.JFrame appFrame, java.net.URL imageUrl)
          SetsIcon for the passed in Frame
static void setTheLookAndFeel(short i, java.awt.Component comp)
          Calls the User Interface Manager and sets the look and feel setting based on the parameter it is passed.
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 spacesToCapsInString(java.lang.String fName)
          Removes any space chars ' ' from a String and Capitalizes the next char.
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, 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.


SYSTEM_LINE_SEPERATOR

public static final java.lang.String SYSTEM_LINE_SEPERATOR
A holder for this clients System line termination separator.


OPEN_DIALOG

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

See Also:
Constant Field Values

SAVE_DIALOG

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

See Also:
Constant Field Values

CLASSPATH

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


USERHOME

public static java.lang.String USERHOME
The users home ditrectory.


USERDIR

public static java.lang.String USERDIR
The users home ditrectory.


CLASSNAME

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


METAL_LOOKANDFEEL

public static final short METAL_LOOKANDFEEL
the Metal LookAndFeel CONSTANT.

See Also:
Constant Field Values

WINDOZE_LOOKANDFEEL

public static final short WINDOZE_LOOKANDFEEL
the Windoze LookAndFeel CONSTANT.

See Also:
Constant Field Values

MOTIF_LOOKANDFEEL

public static final short MOTIF_LOOKANDFEEL
the Motif LookAndFeel CONSTANT.

See Also:
Constant Field Values

MAC_LOOKANDFEEL

public static final short MAC_LOOKANDFEEL
the Mac LookAndFeel CONSTANT.

See Also:
Constant Field Values

DEFAULT_LOOKANDFEEL

public static final short DEFAULT_LOOKANDFEEL
the Default LookAndFeel CONSTANT. DEFAULT_LOOKANDFEEL = METAL_LOOKANDFEEL

See Also:
Constant Field Values

macClassName

public static java.lang.String macClassName
the Mac LookAndFeel class name


metalClassName

public static java.lang.String metalClassName
the Java LookAndFeel class name


motifClassName

public static java.lang.String motifClassName
the motif LookAndFeel class name


windowsClassName

public static java.lang.String windowsClassName
the Windoze LookAndFeel class name


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()
Inits the log and some other vars.


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.

ensureFolderExists

public static void ensureFolderExists(java.lang.String foldername)

moveFile

public static boolean moveFile(java.lang.String filename,
                               java.lang.String foldername)
Moves the specified file to the specified directory. It will create the target dir if it does not exist.

Parameters:
filename - is the absoulte pathname of the file to move.
foldername - is the directory to move it to.

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

setIconForApp

public static void setIconForApp(javax.swing.JFrame appFrame,
                                 java.lang.String imageFilename)
SetsIcon for the passed in Frame

Parameters:
imageFilename - - is the filename for the new icon
appFrame - The new iconForApp value

setIconForApp

public static void setIconForApp(javax.swing.JFrame appFrame,
                                 java.net.URL imageUrl)
SetsIcon for the passed in Frame

Parameters:
imageUrl - - is the URL for the new icon
appFrame - The new iconForApp value

createCurrentDateStamp

public static java.lang.String createCurrentDateStamp()
Creates a timestamp for the current date. It is in the form of 'year + "-" + dayNum
For example: 2003-245

Returns:
The CurrentTimeStamp value.

createCurrentDateTimeStamp

public static java.lang.String createCurrentDateTimeStamp()
Creates a timestamp for the current date and time. It is in the form of 'year + "-" + dayNum + "-" + hour + "-" + min + "-" + sec + "-" + millis'.
For example: 2003-245-14-34-52-224

Returns:
The CurrentTimeStamp value.

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.

createTimerTimeStamp

public static java.lang.String createTimerTimeStamp(int startTimeMillis)
Creates a Timer timestamp from the current time to the passed startTime in the form of
'hour + "-" + min + "-" + sec + "-" + millis'. THIS METHOD IS NOT COMPLETED. IT DOES NOT WORK YET.

Returns:
The Timer Timestamp 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 boolean 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.

removeFile

public static boolean removeFile(java.lang.String fileName)
Deletes a file.

Parameters:
fileName - The filename to delete.

removeOldFiles

public static boolean removeOldFiles(java.lang.String dirName,
                                     int numDays,
                                     boolean recurse)
Deletes all files in the spec'd dir that are older than the spec'd number of days.

Parameters:
dirName - The directory to parse for files to delete.
numDays - The age of the files to delete.
recurse - Do the removal recursively.

dirSize

public static long dirSize(java.lang.String fileName,
                           boolean recurseSubdirs)
Obtains the bytes of data contained in all files in this dir (and subdirs).

Parameters:
fileName - The filename of the directory to delete.
Returns:
the size of the dir in bytes.

searchInFile

public static java.util.Hashtable searchInFile(java.lang.String searchStr,
                                               java.lang.String filename,
                                               boolean recurseSubdirs,
                                               boolean fileRegExp,
                                               boolean verbose)
Searches the provided file/directory (and subdirs) for the search String.

Parameters:
searchStr - The searchString/regexp to search for.
filename - The filename/directoryName to search through.
recurseSubdirs - Flags if the sub-dirs are searched.
fileRegExp - Flags if 'fileName' should be regarded as a regular expression.
verbose - true will produce output to System.out just like the UNIX find cmd
Returns:
a hash of the full cannoical paths/lineNumbers to the found files.

findFile

public static java.util.Vector findFile(java.lang.String fileName,
                                        java.lang.String directory,
                                        boolean recurseSubdirs,
                                        boolean regExp,
                                        boolean verbose)
Searches the provided directory (and subdirs) for the requested file.

Parameters:
fileName - The filename/regexp to search for.
directory - The filename of the directory to delete.
recurseSubdirs - Flags if the sub-dirs are searched.
regExp - Flags if 'fileName' should be regarded as a regular expression.
verbose - true will produce output to System.out just like the UNIX find cmd
Returns:
the full cannoical paths to the found files.

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

spacesToCapsInString

public static java.lang.String spacesToCapsInString(java.lang.String fName)
Removes any space chars ' ' from a String and Capitalizes the next char.

Parameters:
fName - a string perform the action
Returns:
the new representation

capsToSpacesInString

public static java.lang.String capsToSpacesInString(java.lang.String fName)
Takes a string and adds a space in front of any capitalized char. Basically the reverse of the method spacesToCapsInString.

Parameters:
fName - a string perform the action
Returns:
the new representation

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.

Parameters:
filename - the filename to look for
Returns:
the path description of the passed filename as found in the classpath.

getAncestorFrame

public static java.awt.Frame getAncestorFrame(java.awt.Component c)
This method seeks out and returns the Parent Frame for a given component.

Parameters:
c - The Component to be used as the basis of the frame search
Returns:
Frame The Frame that eventually holds the giuven Component.

setTheLookAndFeel

public static void setTheLookAndFeel(short i,
                                     java.awt.Component comp)
Calls the User Interface Manager and sets the look and feel setting based on the parameter it is passed.

Parameters:
i - The new TheLookAndFeel value

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.
openOrClose - Description of the Parameter
Returns:
the absolute path of the chosen file.

absoluteToPwdRelativePath

public static java.lang.String absoluteToPwdRelativePath(java.io.File absPath)
Converts an Absolute path to a relative path from the VM Working Dir.

Parameters:
absPath - is the File to use to to convert
Returns:
null if can't be done, otherwise it returns the relative path.

absoluteToPwdRelativePath

public static java.lang.String absoluteToPwdRelativePath(java.lang.String absPath)
Converts an Absolute path to a relative path from the VM Working Dir.

Parameters:
absPath - is the absolute path to convert
Returns:
null if can't be done, otherwise it returns the relative path.

removeParentRelativeReference

public static java.lang.String removeParentRelativeReference(java.lang.String path)
Removes a '/..' from the filename string and returns the corrected dir name reference. For Example : pass it "/a/b/c/../d" it returns "/a/b/d".

Parameters:
path - is the absolute path to convert
Returns:
null if can't be done, otherwise it returns the corrected path.

getFileDir

public static java.io.File getFileDir(java.lang.String fileString)
Creates a File representing the Dir for the specified filename string.

Parameters:
fileString - is the filename to get the dir for

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

getFileBaseURLs

public static java.net.URL[] getFileBaseURLs(java.lang.String[] fileStrings)
Creates a URL[] for the given Strings.

Parameters:
fileStrings - Description of Parameter
Returns:
The FileBaseURLs value

getLocalhostName

public static java.net.InetAddress getLocalhostName()
gets the current VMs localhost address.

Returns:
the current VMs localhost address, Null if it can't be gotten.

executeNativeApp

public static int executeNativeApp(java.lang.String executableLocation,
                                   java.lang.String[] appParms,
                                   java.lang.String outFilename)
Executes the Specified Native OS application with the provided commandline parameters. This method blocks until the Executed app is complete. It also cleans up its sub-process and garbage collects.

Parameters:
executableLocation - is the path to the executable to run
appParms - and extra commandline parameters to tag onto the end of the commandline that gets executed.
outFilename - an outputFile to dump the exe output.
Returns:
returns the return code from the executed app/process
See Also:
StreamGobbler, Runtime.getRuntime()

executeNativeApp

public static int executeNativeApp(java.lang.String executableLocation,
                                   java.util.Vector appParms,
                                   java.lang.String outFilename)
Executes the Specified Native OS application with the provided commandline parameters. This method blocks until the Executed app is complete. It also cleans up its sub-process and garbage collects.

Parameters:
executableLocation - is the path to the executable to run
appParms - and extra commandline parameters to tag onto the end of the commandline that gets executed.
outFilename - an outputFile to dump the exe output.
Returns:
returns the return code from the executed app/process
See Also:
StreamGobbler, Runtime.getRuntime()

getCurrentDate

public static java.util.Date getCurrentDate()
gets the current date and time.

Returns:
the current Date.

countFilesInDir

public static long countFilesInDir(java.lang.String dirName,
                                   boolean recurse)
Counts the files in a dir (optionally recurses sub dirs)

Parameters:
dirName -
recurse -
Returns:
the number of files (not counting the directories)

main

public static void main(java.lang.String[] args)