dip.misc
Class Utils

java.lang.Object
  extended bydip.misc.Utils

public class Utils
extends java.lang.Object

Various static utilities used by GUI and non-GUI classes.

	NOTE:
		to go from a String resource to a URL, use 
		Utils.getURL()
			e.g., for a resource, called common/test.txt
		to use with Utils.getText():
			String RESOURCE = "resource/common/test.txt";
			
			String myText = Utils.getText(RESOURCE);
		to get the URL:
			URL url = Utils.getURL(RESOURCE);
			JeditorPane editorPane.setPage(url);
	


Field Summary
static javax.swing.border.Border EMPTY_BORDER_10
           
static javax.swing.border.Border EMPTY_BORDER_5
           
static java.lang.String FRAME_ICON
           
static java.lang.String UTILS_RES_ERR_DLG_TEXT
           
static java.lang.String UTILS_RES_ERR_DLG_TITLE
           
static java.lang.String UTILS_RES_NOT_FOUND
           
 
Method Summary
static java.io.File appendExtension(java.io.File file, java.lang.String ext)
          Appends the extension to the file, unless it was already appended.
static void centerIn(java.awt.Component inner, java.awt.Component outer)
          Centers the inner component within the outer component.
static void centerInScreen(java.awt.Component c)
          Centers the component in the screen.
static java.lang.String colorToHex(java.awt.Color color)
          Given a Color, convert it to a hex String.
static java.lang.String colorToHex(java.awt.Color color, boolean withPound)
          Converts a Color to its Hexadecimal equivalent, and prepends a '#' sign if withPound is true.
static java.lang.String colorToHTMLHex(java.awt.Color color)
          Converts a color to HTML hex color in the following format: #RRGGBB.
static javax.swing.JTextField createEmailTextField(int cols)
          Constructs a formatted text field that only allows valid email characters.
static javax.swing.JEditorPane createTextLabel(boolean blend)
          Creates a JEditorPane that has flowing, rich (HTML) text, but is not selectable or editable.
static javax.swing.JEditorPane createTextLabel(java.lang.String text, boolean blend)
          Creates a JEditorPane that has flowing, rich (HTML) text, but is not selectable or editable.
static javax.swing.JEditorPane createTextLabel(java.lang.String text, boolean blend, boolean isFocusable)
          Creates a JEditorPane that has flowing, rich (HTML) text, but is not selectable or editable.
static javax.swing.JTextField createURITextField(int cols)
          Constructs a formatted text field that only allows valid URI characters.
static java.lang.String format(java.lang.String format, java.lang.Object[] args)
          A less flexible and faster but easier-to-use message formatting.
static java.lang.ClassLoader getClassLoader()
          Gets the classloader used for this class
static java.lang.String getCommonString(java.lang.String key)
          Gets a common-bundle (non-localized) String.
static java.lang.String[] getCommonStringArray(java.lang.String key)
          Gets a common-bundle (non-localized) String array.
static javax.swing.Icon getIcon(java.lang.String name)
          Gets a resource as an Icon.
static java.awt.Image getImage(java.lang.String name)
          Gets a resource as an Image.
static java.awt.Image getImage(java.net.URL url)
          Returns an Image from the given URL.
static javax.swing.ImageIcon getImageIcon(java.lang.String name)
          Get an ImageIcon.
static java.io.InputStream getInputStream(java.lang.String name)
          Gets an InputStream to the named resource; throws an exception if the resource could not be found.
static java.io.InputStreamReader getInputStreamReader(java.lang.String name)
          Gets an InputStreamReader to the named resource.
static java.util.Locale getLocale()
          Get which Locale has been loaded or selected.
static int[] getLocalIntArray(java.lang.String key)
          Gets an array of resource-bundle ints, for internationalization.
static java.lang.String getLocalString(java.lang.String key)
          Gets a resource-bundle String; this is for internationalization.
static java.lang.String getLocalString(java.lang.String key, java.lang.Object arg1)
          Gets a resource-bundle String; this is for internationalization.
static java.lang.String getLocalString(java.lang.String key, java.lang.Object[] args)
          Gets a resource-bundle String; this is for internationalization.
static java.lang.String getLocalString(java.lang.String key, java.lang.Object arg1, java.lang.Object arg2)
          Gets a resource-bundle String; this is for internationalization.
static java.lang.String getLocalString(java.lang.String key, java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3)
          Gets a resource-bundle String; this is for internationalization.
static java.lang.String[] getLocalStringArray(java.lang.String key)
          Gets an array of resource-bundle Strings, for internationalization.
static java.lang.String getLocalStringNoEx(java.lang.String key)
          Gets a resource-bundle String; this is for internationalization.
static java.net.URL getResourceBase()
          Gets the Resource Base (resource directory).
static java.lang.String getResourceBasePrefix()
          Get the resource base prefix.
static java.awt.Image getScaledImage(java.awt.Image src, float factor)
          Return a scaled image that is proportionally scaled via the given factor (e.g., 0.5f = 1/2 size).
static java.awt.Image getScaledImage(java.awt.Image src, int w, int h)
          Scale an Image to the given width and height (in pixels)
static java.awt.Dimension getScreenSize()
          Return the screen size.
static java.awt.Dimension getScreenSize(float fraction)
          Returns the screen size multiplied by the given fraction, preserving the aspect ratio, and returns the result.
static java.awt.Dimension getScreenSize(float fraction, float smallScreenFraction)
          Returns the screen size multiplied by the given fraction, preserving the aspect ratio, and returns the result.
static java.lang.String getText(java.lang.String name)
          Gets a resource as a String.
static java.lang.String getText(java.lang.String name, java.lang.Object arg1)
          Gets a resource as a String.
static java.lang.String getText(java.lang.String name, java.lang.Object[] args)
          Gets a resource as a String.
static java.net.URL getURL(java.lang.String name)
          Given a resource name, extract a URL for the given resource.
static boolean isOSX()
          Detect if we are running on Mac OS X
static boolean isWindows()
          Detect if we are running on Windows
static void loadLocale(java.util.Locale loc)
          Force a load of a locale
static java.awt.Color parseColor(java.lang.String value, java.awt.Color defaultValue)
          Given a color string, converts it to a color value.
static java.lang.String[] parseCSV(java.lang.String input)
          Parses a line of CSV text into a String array.
static java.lang.String[] parseCSVXE(java.lang.String input)
          Similar to parseCSV(), but if the input string (after trimming) is null or empty, returns an array of length 0.
static void popupError(javax.swing.JFrame parent, java.lang.String title, java.lang.String text)
          Popup an Error message dialog
static void popupInfo(javax.swing.JFrame parent, java.lang.String title, java.lang.String text)
          Popup an Info message dialog
static java.lang.String replaceAll(java.lang.String input, java.lang.String[] toFind, java.lang.String[] toReplace)
          Replaces all instances of "tofind" with "toReplace" in the given input String, and returns the String after replacement has occured.
static java.lang.String replaceAll(java.lang.String input, java.lang.String toFind, java.lang.String toReplace)
          Replaces all instances of "tofind" with "toReplace" in the given input String, and returns the String after replacement has occured.
static javax.swing.ImageIcon rescaleImageIcon(javax.swing.ImageIcon src, int w, int h)
          Scale an ImageIcon to the given width and height (in pixels)
static javax.swing.ImageIcon scaleDown(javax.swing.ImageIcon src, int maxW, int maxH)
          Scale an ImageIcon *down* to the given width and height (in pixels), keeping aspect ratio intact.
static void setFontColor(java.lang.StringBuffer sb, java.awt.Color color)
          Create a <font> tag with the given color.
static void setFontColor(java.lang.StringBuffer sb, int r, int g, int b)
          Create a <font> tag with the given color.
static java.awt.Image syncCreateImage(java.awt.Image img)
          Ensure that the entire image has been created.
static java.awt.Image syncCreateImage(java.awt.image.ImageProducer ip)
          Ensure that the entire image has been created.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_BORDER_5

public static final javax.swing.border.Border EMPTY_BORDER_5

EMPTY_BORDER_10

public static final javax.swing.border.Border EMPTY_BORDER_10

UTILS_RES_NOT_FOUND

public static final java.lang.String UTILS_RES_NOT_FOUND
See Also:
Constant Field Values

UTILS_RES_ERR_DLG_TITLE

public static final java.lang.String UTILS_RES_ERR_DLG_TITLE
See Also:
Constant Field Values

UTILS_RES_ERR_DLG_TEXT

public static final java.lang.String UTILS_RES_ERR_DLG_TEXT
See Also:
Constant Field Values

FRAME_ICON

public static final java.lang.String FRAME_ICON
See Also:
Constant Field Values
Method Detail

loadLocale

public static void loadLocale(java.util.Locale loc)
Force a load of a locale


getLocale

public static java.util.Locale getLocale()
Get which Locale has been loaded or selected.


getClassLoader

public static java.lang.ClassLoader getClassLoader()
Gets the classloader used for this class


getScreenSize

public static java.awt.Dimension getScreenSize()
Return the screen size.


getScreenSize

public static java.awt.Dimension getScreenSize(float fraction)
Returns the screen size multiplied by the given fraction, preserving the aspect ratio, and returns the result.

For example, getScreenSize(0.5f) would return a Dimension that was 400 x 300 if the screen size was 800 x 600 pixels.


getScreenSize

public static java.awt.Dimension getScreenSize(float fraction,
                                               float smallScreenFraction)
Returns the screen size multiplied by the given fraction, preserving the aspect ratio, and returns the result.

This is a hackish version, with two fractions. The second fraction is used for 'smaller' screens (defined as 800x600 or less).


centerInScreen

public static void centerInScreen(java.awt.Component c)
Centers the component in the screen. If component is larger than the screen in a particular axis, then that axis is centered to the component.


centerIn

public static void centerIn(java.awt.Component inner,
                            java.awt.Component outer)
Centers the inner component within the outer component. If the outer component is null, or if the inner component is larger than the outer component (in either axis), the component is centered to the screen using centerInScreen().


appendExtension

public static java.io.File appendExtension(java.io.File file,
                                           java.lang.String ext)
Appends the extension to the file, unless it was already appended. Will also use a '.' to separate if not contained in the extension.


setFontColor

public static void setFontColor(java.lang.StringBuffer sb,
                                java.awt.Color color)
Create a <font> tag with the given color.


setFontColor

public static void setFontColor(java.lang.StringBuffer sb,
                                int r,
                                int g,
                                int b)
Create a <font> tag with the given color.


popupError

public static void popupError(javax.swing.JFrame parent,
                              java.lang.String title,
                              java.lang.String text)
Popup an Error message dialog


popupInfo

public static void popupInfo(javax.swing.JFrame parent,
                             java.lang.String title,
                             java.lang.String text)
Popup an Info message dialog


getResourceBase

public static java.net.URL getResourceBase()
Gets the Resource Base (resource directory). This is needed for setting things like setBase() on HTML docuements.


getResourceBasePrefix

public static java.lang.String getResourceBasePrefix()
Get the resource base prefix.


getURL

public static java.net.URL getURL(java.lang.String name)
Given a resource name, extract a URL for the given resource.


getImageIcon

public static javax.swing.ImageIcon getImageIcon(java.lang.String name)
Get an ImageIcon. Return null if an ImageIcon could not be found.


getInputStream

public static java.io.InputStream getInputStream(java.lang.String name)
                                          throws java.io.IOException
Gets an InputStream to the named resource; throws an exception if the resource could not be found.

Throws:
java.io.IOException

getInputStreamReader

public static java.io.InputStreamReader getInputStreamReader(java.lang.String name)
                                                      throws java.io.IOException
Gets an InputStreamReader to the named resource.

Throws:
java.io.IOException

getText

public static java.lang.String getText(java.lang.String name)
Gets a resource as a String.

Suitable for small text files / HTML files. May not be appropriate for larger text files.

A null string is returned if an error occurs.


getText

public static java.lang.String getText(java.lang.String name,
                                       java.lang.Object arg1)
Gets a resource as a String.

Uses MessageFormat to replace the given argument with the argument provided.


getText

public static java.lang.String getText(java.lang.String name,
                                       java.lang.Object[] args)
Gets a resource as a String.

Uses MessageFormat to replace the given argument with the arguments provided.


getImage

public static java.awt.Image getImage(java.lang.String name)
Gets a resource as an Image.

Synchronous; may not suitable for large images.

null if error


getImage

public static java.awt.Image getImage(java.net.URL url)
Returns an Image from the given URL.

Synchronous; may not suitable for large images.

null if error


getIcon

public static javax.swing.Icon getIcon(java.lang.String name)
Gets a resource as an Icon.

Synchronous; may not suitable for large images.

null if error


syncCreateImage

public static java.awt.Image syncCreateImage(java.awt.image.ImageProducer ip)
Ensure that the entire image has been created.

This method ensures that the Image desired has been fully realized (i.e., loaded or created) before returning a reference to it. If an error occurs, the returned Image will be null.

Parameters:
ip - ImageProducer image source.
Returns:
the completed Image object.

syncCreateImage

public static java.awt.Image syncCreateImage(java.awt.Image img)
Ensure that the entire image has been created.

This method ensures that the Image desired has been fully realized (i.e., loaded or created) before returning a reference to it. If an error occurs, the returned Image will be null.

Parameters:
img - the image source.
Returns:
the completed Image object.

getLocalString

public static java.lang.String getLocalString(java.lang.String key)
Gets a resource-bundle String; this is for internationalization.

If resource is missing, a popup-error message is displayed.

.


getLocalStringArray

public static java.lang.String[] getLocalStringArray(java.lang.String key)
Gets an array of resource-bundle Strings, for internationalization.

The Strings must be comma-separated. Spaces before/after are trimmed. Quotes have no special meaning.

.


getLocalIntArray

public static int[] getLocalIntArray(java.lang.String key)
Gets an array of resource-bundle ints, for internationalization.

The Integers must be comma-separated. Spaces before/after are trimmed. Quotes have no special meaning.

.


getLocalString

public static java.lang.String getLocalString(java.lang.String key,
                                              java.lang.Object arg1)
Gets a resource-bundle String; this is for internationalization. Objects passed in are for MessageFormat arguments.

If resource is missing, a popup-error message is displayed.

.


getLocalString

public static java.lang.String getLocalString(java.lang.String key,
                                              java.lang.Object arg1,
                                              java.lang.Object arg2)
Gets a resource-bundle String; this is for internationalization. Objects passed in are for MessageFormat arguments.

If resource is missing, a popup-error message is displayed.

.


getLocalString

public static java.lang.String getLocalString(java.lang.String key,
                                              java.lang.Object arg1,
                                              java.lang.Object arg2,
                                              java.lang.Object arg3)
Gets a resource-bundle String; this is for internationalization. Objects passed in are for MessageFormat arguments.

If resource is missing, a popup-error message is displayed.

.


getLocalString

public static java.lang.String getLocalString(java.lang.String key,
                                              java.lang.Object[] args)
Gets a resource-bundle String; this is for internationalization. Objects passed in are for MessageFormat arguments.

If resource is missing, a popup-error message is displayed.

.


getLocalStringNoEx

public static java.lang.String getLocalStringNoEx(java.lang.String key)
Gets a resource-bundle String; this is for internationalization.

. If there is NO resource present, returns 'null'.


parseColor

public static java.awt.Color parseColor(java.lang.String value,
                                        java.awt.Color defaultValue)
Given a color string, converts it to a color value. This is typically used when parsing configuration files and preferences. This has been tightened to be SVG CSS (non keyword) compliant. Case insensitive.

Acceptable forms are:

If parsing fails, defaultValue is returned.


colorToHex

public static java.lang.String colorToHex(java.awt.Color color)
Given a Color, convert it to a hex String. Includes alpha value. Null input is illegal.

The format of the output string is: "RRGGBBAA". No '#' is prepended. The String length is always 8 characters, zero-padded as necessary.


colorToHex

public static java.lang.String colorToHex(java.awt.Color color,
                                          boolean withPound)
Converts a Color to its Hexadecimal equivalent, and prepends a '#' sign if withPound is true.


colorToHTMLHex

public static java.lang.String colorToHTMLHex(java.awt.Color color)
Converts a color to HTML hex color in the following format: #RRGGBB. Alpha values are not used.


createTextLabel

public static javax.swing.JEditorPane createTextLabel(boolean blend)
Creates a JEditorPane that has flowing, rich (HTML) text, but is not selectable or editable. This will also set the document base to be the same as Utils.getResourceBase().

This is non-focusable by default.

Parameters:
blend - match background of component if true.

createTextLabel

public static javax.swing.JEditorPane createTextLabel(java.lang.String text,
                                                      boolean blend)
Creates a JEditorPane that has flowing, rich (HTML) text, but is not selectable or editable. This will also set the document base to be the same as Utils.getResourceBase().

This is non-focusable by default.

Parameters:
text - the initial text.
blend - match background of component if true.

createTextLabel

public static javax.swing.JEditorPane createTextLabel(java.lang.String text,
                                                      boolean blend,
                                                      boolean isFocusable)
Creates a JEditorPane that has flowing, rich (HTML) text, but is not selectable or editable. This will also set the document base to be the same as Utils.getResourceBase().

Parameters:
text - the initial text.
blend - match background of component if true.
isFocusable - true if this component can receive focus events

getScaledImage

public static java.awt.Image getScaledImage(java.awt.Image src,
                                            int w,
                                            int h)
Scale an Image to the given width and height (in pixels)


rescaleImageIcon

public static javax.swing.ImageIcon rescaleImageIcon(javax.swing.ImageIcon src,
                                                     int w,
                                                     int h)
Scale an ImageIcon to the given width and height (in pixels)


scaleDown

public static javax.swing.ImageIcon scaleDown(javax.swing.ImageIcon src,
                                              int maxW,
                                              int maxH)
Scale an ImageIcon *down* to the given width and height (in pixels), keeping aspect ratio intact. No scaling is done if image is smaller then the desired max (specified) width/height


getScaledImage

public static java.awt.Image getScaledImage(java.awt.Image src,
                                            float factor)
Return a scaled image that is proportionally scaled via the given factor (e.g., 0.5f = 1/2 size).


format

public static java.lang.String format(java.lang.String format,
                                      java.lang.Object[] args)
A less flexible and faster but easier-to-use message formatting. A number enclosed within braces is replaced with the argument at that index. The toString() method is used to convert the Object argument into text.


createEmailTextField

public static javax.swing.JTextField createEmailTextField(int cols)
Constructs a formatted text field that only allows valid email characters. These are defined as ASCII alphanumerics (a-z, A-Z, 0-9), plus the characters !,.,-,_,@. This is not (by any means) RFC822 compliant, but allows most email addresses through.


createURITextField

public static javax.swing.JTextField createURITextField(int cols)
Constructs a formatted text field that only allows valid URI characters. These are defined as ASCII alphanumerics (a-z, A-Z, 0-9), plus many additional characters, including: ;,/,?,:,@,&,=,+,$,,,-,_,.,!,~,*,',|,%


parseCSV

public static java.lang.String[] parseCSV(java.lang.String input)
Parses a line of CSV text into a String array.

This quoted text, unquoted text, and escaped quotes. Excel-style quote support could be added, but has not been tested adequately. Unescaped quotes are also acceptable in this parser. Commas with only whitespace between them return empty ("") Strings. Whitespace is removed around unquoted items.

For example:
"hello", goodbye , """test",, "a line, a \"quote\""
Will parse into:
hello|goodbye|""test||a line, a "quote"|

Should never return null. NOTE that a entry of "" will return "" (array length 1). An array length of 0 is never returned.


parseCSVXE

public static java.lang.String[] parseCSVXE(java.lang.String input)
Similar to parseCSV(), but if the input string (after trimming) is null or empty, returns an array of length 0. XE for "eXtended Edition".


getCommonString

public static java.lang.String getCommonString(java.lang.String key)
Gets a common-bundle (non-localized) String. If the key is not found, an exception is thrown.


getCommonStringArray

public static java.lang.String[] getCommonStringArray(java.lang.String key)
Gets a common-bundle (non-localized) String array. The string array is parsed with the parseCSV() method. If the key is not found, an exception is thrown.


replaceAll

public static java.lang.String replaceAll(java.lang.String input,
                                          java.lang.String toFind,
                                          java.lang.String toReplace)
Replaces all instances of "tofind" with "toReplace" in the given input String, and returns the String after replacement has occured. If no modification to input has occured, the same reference will be returned.


replaceAll

public static java.lang.String replaceAll(java.lang.String input,
                                          java.lang.String[] toFind,
                                          java.lang.String[] toReplace)
Replaces all instances of "tofind" with "toReplace" in the given input String, and returns the String after replacement has occured. If no modification to input has occured, the same reference will be returned.

toFind and toReplace must have the same lengths.

The order of replacement is the order of the toFind array.


isOSX

public static boolean isOSX()
Detect if we are running on Mac OS X


isWindows

public static boolean isWindows()
Detect if we are running on Windows



Copyright 2002-2004 Zachary DelProposto / jDip Development Team. All Rights Reserved.