org.alchemy.core
Class AlcUtil

java.lang.Object
  extended by org.alchemy.core.AlcUtil
All Implemented Interfaces:
AlcConstants

public class AlcUtil
extends java.lang.Object
implements AlcConstants

Static utility methods used in Alchemy Used to manipulate strings, load images, and general stuff


Field Summary
 
Fields inherited from interface org.alchemy.core.AlcConstants
COLOR_UI_BG, COLOR_UI_BOX, COLOR_UI_END, COLOR_UI_HIGHLIGHT, COLOR_UI_LINE, COLOR_UI_LINE_ALPHA, COLOR_UI_START, CURSOR_ARROW, CURSOR_BLANK, CURSOR_CIRCLE, CURSOR_CROSS, CURSOR_EYEDROPPER, CURSOR_HAND, CURSOR_MOVE, CURSOR_TEXT, CURSOR_WAIT, DIR_DESKTOP, DIR_HOME, DIR_SEPARATOR, DIR_TEMP, FONT_LARGE, FONT_LARGE_BOLD, FONT_MEDIUM, FONT_MEDIUM_BOLD, FONT_SMALL, FONT_SMALL_BOLD, FONT_SMALLER, JAVA_VERSION, JAVA_VERSION_NAME, KEY_MODIFIER, LOCALE, MATH_DEG_TO_RAD, MATH_HALF_PI, MATH_PI, MATH_QUARTER_PI, MATH_RAD_TO_DEG, MATH_THIRD_PI, MATH_TWO_PI, MODULE_AFFECT, MODULE_CREATE, OS_LINUX, OS_MAC, OS_NAME, OS_OTHER, OS_WINDOWS, PEN_CURSOR, PEN_ERASER, PEN_STYLUS, STYLE_BOTH, STYLE_CLIP, STYLE_FILL, STYLE_STROKE, TOOLKIT, USER_NAME
 
Constructor Summary
AlcUtil()
           
 
Method Summary
static java.io.File addFileExtension(java.io.File file, java.lang.String ext)
          Checks a name for a file extension and adds one if not present
static java.net.URL appendStringToUrl(java.net.URL url, java.lang.String append)
          Function to append a string to the end of a given URL
static java.awt.Point calculateCenter(java.awt.Container popup)
          Calculate the centre of the screen with multiple monitors
static java.awt.Point calculateCenter(java.awt.Container popup, boolean palette)
          Calculate the centre of the screen with multiple monitors
static void copyFile(java.io.InputStream in, java.io.File dst)
          Copies the source file to destination file.
static java.lang.String dateStamp(java.lang.String format)
          Returns a string date stamp according to the format given
static void drawSoftRect(java.awt.Graphics g, int x, int y, int width, int height)
           
static java.awt.image.BufferedImage getBufferedImage(java.awt.Image image)
          Convert an Image into a BufferedImage
static java.awt.image.BufferedImage getBufferedImage(java.lang.String name)
          Returns a BufferedImage from a String, or null if the name was invalid.
static java.awt.image.BufferedImage getBufferedImage(java.net.URL imgUrl)
          Returns a BufferedImage from a URL, or null if the name was invalid.
static java.lang.String getClassName(java.lang.Object object)
          Returns just the class name -- no package info.
static int getColorBrightness(int rgb)
          Get the brightness of a color
static java.awt.Cursor getCursor(java.lang.String name)
          Create a custom cursor from a given image file
static java.awt.Image getImage(java.lang.String name)
          Returns an Image from the Alchemy data folder, or null if the name was invalid.
static java.awt.Image getImage(java.net.URL imgUrl)
          Returns an Image from a URL, or null if the name was invalid.
static javax.swing.ImageIcon getImageIcon(java.lang.String name)
          Returns an ImageIcon from the Alchemy data folder, or null if the name was invalid.
static javax.swing.ImageIcon getImageIcon(java.net.URL imgUrl)
          Returns an ImageIcon from a URL, or null if the name was invalid.
static java.util.Collection<AlcShape> getPDFShapes(java.io.File file, boolean resetLocation)
          Get a set of vector paths (shapes) from a PDF file.
static AlcShape[] getPDFShapesAsArray(java.io.File file, boolean resetLocation)
          Get a set of vector paths (shapes) from a PDF file.
static AlcShape[] getShapes()
          Return an array of AlcShape from PDF files in the shapes folder If no shapes are found a dialog is displayed telling the user to add some shapes to their shapes folder
static java.net.URL getUrlPath(java.lang.String path)
          Returns a URL from a String, or null if the name was invalid.
static java.net.URL getUrlPath(java.lang.String path, java.lang.ClassLoader classLoader)
          Returns a URL from a String, or null if the name was invalid.
static java.lang.String hex(byte i)
          Converts a byte, char, int, or color to a String containing the equivalent hexadecimal notation.
static java.lang.String hex(char c)
           
static java.lang.String hex(int i)
           
static java.lang.String hex(int i, int digits)
           
static java.io.File[] listFilesAsArray(java.io.File directory, java.io.FilenameFilter filter, boolean recurse)
          List all files within a folder and all its sub folders
static void openPDF(java.io.File pdf)
          Open a local pdf in the default application
static void openURL(java.lang.String url)
          Launch a url in the default browser Adapted from: http://www.centerkey.com/java/browser/
static void printFloatArray(float[] array)
          Print a float array
static void printStringArray(java.lang.String[] array)
          Print a string array
static void registerWindowCloseKeys(javax.swing.JRootPane root, javax.swing.Action closeAction)
          Registers key events for a Ctrl-W and ESC with an ActionListener that will take care of disposing the window.
static boolean setClipboard(java.awt.datatransfer.Transferable contents, java.awt.datatransfer.ClipboardOwner owner)
          Sets the current contents of the clipboard to the specified transferable object and registers the specified clipboard owner as the owner of the new contents.
static boolean showConfirmDialog(java.lang.String title, java.lang.String message)
          Show a confirmation dialog specific to the OS style The title and message are taken from the localised Alchemy bundle
static boolean showConfirmDialog(java.lang.String winTitle, java.lang.String winMessage, java.lang.String macTitle, java.lang.String macMessage)
          Show a confirmation dialog specific to the OS style The title and message are taken from the localised Alchemy bundle
static boolean showConfirmDialogFromBundle(java.lang.String title, java.lang.String message)
          Show a confirmation dialog specific to the OS style The title and message are taken from the localised Alchemy bundle
static boolean showConfirmDialogFromBundle(java.lang.String winTitle, java.lang.String winMessage, java.lang.String macTitle, java.lang.String macMessage)
          Show a confirmation dialog specific to the OS style The title and message are taken from the localised Alchemy bundle
static java.io.File showFileChooser()
          Ask for a location with a file chooser.
static java.io.File showFileChooser(boolean foldersOnly)
          Ask for a location with a file chooser.
static java.io.File showFileChooser(boolean foldersOnly, java.awt.Component parent)
          Ask for a location with a file chooser.
static java.io.File showFileChooser(java.io.File defaultDir)
          Ask for a location with a file chooser.
static java.io.File showFileChooser(java.io.File defaultDir, boolean foldersOnly)
          Ask for a location with a file chooser.
static java.io.File showFileChooser(java.lang.String title)
          Ask for a location with a file chooser.
static java.io.File showFileChooser(java.lang.String title, boolean foldersOnly)
          Ask for a location with a file chooser.
static java.io.File showFileChooser(java.lang.String title, java.io.File defaultDir)
          Ask for a location with a file chooser.
static java.io.File showFileChooser(java.lang.String title, java.io.File defaultDir, boolean foldersOnly, java.awt.Component parent)
          Ask for a location with a file chooser.
static void showNoShapesDialog()
          Show a dialog informing the user there are no shapes loaded
static int unhex(java.lang.String s)
           
static java.lang.String zeroPad(int i, int len)
          Zero Pad an int
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AlcUtil

public AlcUtil()
Method Detail

addFileExtension

public static java.io.File addFileExtension(java.io.File file,
                                            java.lang.String ext)
Checks a name for a file extension and adds one if not present

Parameters:
file - The file to add the extenstion to
ext - The extension to add (without the leading dot)
Returns:
The new file with extension

getClassName

public static java.lang.String getClassName(java.lang.Object object)
Returns just the class name -- no package info.

Parameters:
object - The object
Returns:
The name of the class

appendStringToUrl

public static java.net.URL appendStringToUrl(java.net.URL url,
                                             java.lang.String append)
Function to append a string to the end of a given URL

Parameters:
url - The url
append - The string to append
Returns:
The new url with string appended

dateStamp

public static java.lang.String dateStamp(java.lang.String format)
                                  throws java.lang.IllegalArgumentException
Returns a string date stamp according to the format given

Parameters:
format - Format for the date stamp see: SimpleDateFormat
Returns:
A string containing a formatted date
Throws:
java.lang.IllegalArgumentException - if the given pattern is invalid

zeroPad

public static java.lang.String zeroPad(int i,
                                       int len)
Zero Pad an int

Parameters:
i - The number to pad
len - The length required
Returns:
The padded number

getUrlPath

public static java.net.URL getUrlPath(java.lang.String path)
Returns a URL from a String, or null if the name was invalid.

Parameters:
path - The name to the resource
Returns:
URL to the resource or null if invalid

getUrlPath

public static java.net.URL getUrlPath(java.lang.String path,
                                      java.lang.ClassLoader classLoader)
Returns a URL from a String, or null if the name was invalid.

Parameters:
path - The name to the resource
classLoader - The classloader
Returns:
URL to the resource or null if invalid

getImageIcon

public static javax.swing.ImageIcon getImageIcon(java.lang.String name)
Returns an ImageIcon from the Alchemy data folder, or null if the name was invalid.

Parameters:
name - The name to the image
Returns:
ImageIcon or null if invalid

getImageIcon

public static javax.swing.ImageIcon getImageIcon(java.net.URL imgUrl)
Returns an ImageIcon from a URL, or null if the name was invalid.

Parameters:
imgUrl - The URL to the image
Returns:
ImageIcon or null if invalid

getImage

public static java.awt.Image getImage(java.lang.String name)
Returns an Image from the Alchemy data folder, or null if the name was invalid.

Parameters:
name - The name to the image
Returns:
Image or null if invalid

getImage

public static java.awt.Image getImage(java.net.URL imgUrl)
Returns an Image from a URL, or null if the name was invalid.

Parameters:
imgUrl - The URL to the image
Returns:
Image or null if invalid

getBufferedImage

public static java.awt.image.BufferedImage getBufferedImage(java.net.URL imgUrl)
Returns a BufferedImage from a URL, or null if the name was invalid.

Parameters:
imgUrl - The URL to the image
Returns:
BufferedImage or null if invalid

getBufferedImage

public static java.awt.image.BufferedImage getBufferedImage(java.lang.String name)
Returns a BufferedImage from a String, or null if the name was invalid.

Parameters:
name - The name to the image
Returns:
BufferedImage or null if invalid

getBufferedImage

public static java.awt.image.BufferedImage getBufferedImage(java.awt.Image image)
Convert an Image into a BufferedImage

Parameters:
image - The Image to be converted
Returns:
The Buffered Image or null if invalid

getCursor

public static java.awt.Cursor getCursor(java.lang.String name)
Create a custom cursor from a given image file

Parameters:
name - The image file to use as the cursor
Returns:
A Custom cursor or the default cursor if the image can not be found

getShapes

public static AlcShape[] getShapes()
Return an array of AlcShape from PDF files in the shapes folder If no shapes are found a dialog is displayed telling the user to add some shapes to their shapes folder

Returns:
An array of AlcShapes or null if no shapes found

showNoShapesDialog

public static void showNoShapesDialog()
Show a dialog informing the user there are no shapes loaded


getPDFShapesAsArray

public static AlcShape[] getPDFShapesAsArray(java.io.File file,
                                             boolean resetLocation)
Get a set of vector paths (shapes) from a PDF file. Does not return clipping paths or any shape that is bigger or the same size as the page This is a rather long and hacky way using the swing labs PDFRenderer library. It uses reflection to access private variables but seems to be working... for now anyway.

Parameters:
file - The PDF file to retrive the shapes from
resetLocation - Reset the location of each path to 0,0
Returns:
An array of AlcShapes from the PDF, else null

getPDFShapes

public static java.util.Collection<AlcShape> getPDFShapes(java.io.File file,
                                                          boolean resetLocation)
Get a set of vector paths (shapes) from a PDF file.

Parameters:
file - The PDF file to retrive the shapes from
resetLocation - Reset the location of each path to 0,0
Returns:
A Collection of AlcShapes from the PDF, else null

copyFile

public static void copyFile(java.io.InputStream in,
                            java.io.File dst)
                     throws java.io.IOException
Copies the source file to destination file. If the destination file does not exist, it is created.

Parameters:
in - The source file as an InputStream
dst - The destination file
Throws:
java.io.IOException

listFilesAsArray

public static java.io.File[] listFilesAsArray(java.io.File directory,
                                              java.io.FilenameFilter filter,
                                              boolean recurse)
List all files within a folder and all its sub folders

Parameters:
directory - The directory to list
filter - The filter to use
recurse - To list all sub folders or not
Returns:
An array of files

calculateCenter

public static java.awt.Point calculateCenter(java.awt.Container popup)
Calculate the centre of the screen with multiple monitors

Parameters:
popup - The popup window
Returns:
The centred location as a Point

calculateCenter

public static java.awt.Point calculateCenter(java.awt.Container popup,
                                             boolean palette)
Calculate the centre of the screen with multiple monitors

Parameters:
popup - The popup window
palette - Popup in the window of the palette
Returns:
The centred location as a Point

openURL

public static void openURL(java.lang.String url)
Launch a url in the default browser Adapted from: http://www.centerkey.com/java/browser/

Parameters:
url - The url to be launched

openPDF

public static void openPDF(java.io.File pdf)
Open a local pdf in the default application

Parameters:
pdf - A file pointing to the pdf to open

setClipboard

public static boolean setClipboard(java.awt.datatransfer.Transferable contents,
                                   java.awt.datatransfer.ClipboardOwner owner)
Sets the current contents of the clipboard to the specified transferable object and registers the specified clipboard owner as the owner of the new contents. Shows warning message if an exception occured

Parameters:
contents -
owner -
Returns:
boolean false if an exception occured

showConfirmDialog

public static boolean showConfirmDialog(java.lang.String title,
                                        java.lang.String message)
Show a confirmation dialog specific to the OS style The title and message are taken from the localised Alchemy bundle

Parameters:
title - Title of the dialog
message - Message of the dialog
Returns:
True if OK, else false if Cancel

showConfirmDialogFromBundle

public static boolean showConfirmDialogFromBundle(java.lang.String title,
                                                  java.lang.String message)
Show a confirmation dialog specific to the OS style The title and message are taken from the localised Alchemy bundle

Parameters:
title - Title of the dialog
message - Message of the dialog
Returns:
True if OK, else false if Cancel

showConfirmDialog

public static boolean showConfirmDialog(java.lang.String winTitle,
                                        java.lang.String winMessage,
                                        java.lang.String macTitle,
                                        java.lang.String macMessage)
Show a confirmation dialog specific to the OS style The title and message are taken from the localised Alchemy bundle

Parameters:
winTitle - Title of the windows dialog
winMessage - Message of the windows dialog
macTitle - Title of the mac dialog
macMessage - Message of the mac dialog
Returns:
True if OK, else false if Cancel

showConfirmDialogFromBundle

public static boolean showConfirmDialogFromBundle(java.lang.String winTitle,
                                                  java.lang.String winMessage,
                                                  java.lang.String macTitle,
                                                  java.lang.String macMessage)
Show a confirmation dialog specific to the OS style The title and message are taken from the localised Alchemy bundle

Parameters:
winTitle - Title of the windows dialog
winMessage - Message of the windows dialog
macTitle - Title of the mac dialog
macMessage - Message of the mac dialog
Returns:
True if OK, else false if Cancel

showFileChooser

public static java.io.File showFileChooser()
Ask for a location with a file chooser.

Returns:
file/folder selected by the user

showFileChooser

public static java.io.File showFileChooser(java.lang.String title)
Ask for a location with a file chooser.

Parameters:
title - the name of the popup title
Returns:
file/folder selected by the user

showFileChooser

public static java.io.File showFileChooser(java.io.File defaultDir)
Ask for a location with a file chooser.

Parameters:
defaultDir - the default directory
Returns:
file/folder selected by the user

showFileChooser

public static java.io.File showFileChooser(boolean foldersOnly)
Ask for a location with a file chooser.

Parameters:
foldersOnly - to select only folders or not
Returns:
file/folder selected by the user

showFileChooser

public static java.io.File showFileChooser(boolean foldersOnly,
                                           java.awt.Component parent)
Ask for a location with a file chooser.

Parameters:
foldersOnly - to select only folders or not
parent - the parent component of the dialog, can be null; see showDialog for details
Returns:
file/folder selected by the user

showFileChooser

public static java.io.File showFileChooser(java.io.File defaultDir,
                                           boolean foldersOnly)
Ask for a location with a file chooser.

Parameters:
defaultDir - the default directory
foldersOnly - to select only folders or not
Returns:
file/folder selected by the user

showFileChooser

public static java.io.File showFileChooser(java.lang.String title,
                                           boolean foldersOnly)
Ask for a location with a file chooser.

Parameters:
title - the name of the popup title
foldersOnly - to select only folders or not
Returns:
file/folder selected by the user

showFileChooser

public static java.io.File showFileChooser(java.lang.String title,
                                           java.io.File defaultDir)
Ask for a location with a file chooser.

Parameters:
title - the name of the popup title
defaultDir - the default directory
Returns:
file/folder selected by the user

showFileChooser

public static java.io.File showFileChooser(java.lang.String title,
                                           java.io.File defaultDir,
                                           boolean foldersOnly,
                                           java.awt.Component parent)
Ask for a location with a file chooser.

Parameters:
title - the name of the popup title
foldersOnly - to select only folders or not
defaultDir - the default directory
parent - the parent component of the dialog, can be null; see showDialog for details
Returns:
file/folder selected by the user

registerWindowCloseKeys

public static void registerWindowCloseKeys(javax.swing.JRootPane root,
                                           javax.swing.Action closeAction)
Registers key events for a Ctrl-W and ESC with an ActionListener that will take care of disposing the window.

Parameters:
root - The window
closeAction - The action to be called

drawSoftRect

public static void drawSoftRect(java.awt.Graphics g,
                                int x,
                                int y,
                                int width,
                                int height)

getColorBrightness

public static int getColorBrightness(int rgb)
Get the brightness of a color

Parameters:
rgb - An rgb color (bit-shifted int format)
Returns:
The brightness as an int

hex

public static java.lang.String hex(byte i)
Converts a byte, char, int, or color to a String containing the equivalent hexadecimal notation. For example color(0, 102, 153, 255) will convert to the String "FF006699".

Parameters:
i -
Returns:

hex

public static java.lang.String hex(char c)

hex

public static java.lang.String hex(int i)

hex

public static java.lang.String hex(int i,
                                   int digits)

unhex

public static int unhex(java.lang.String s)

printFloatArray

public static void printFloatArray(float[] array)
Print a float array

Parameters:
array - Float array

printStringArray

public static void printStringArray(java.lang.String[] array)
Print a string array

Parameters:
array - String array