org.openide.util
Class Utilities

java.lang.Object
  |
  +--org.openide.util.Utilities

public final class Utilities
extends Object

Otherwise uncategorized useful static methods.


Inner Class Summary
static class Utilities.UnorderableException
          Exception indicating that a given list could not be partially-ordered.
 
Field Summary
static int OS_AIX
          Operating system is IBM AIX.
static int OS_DEC
          Deprecated. please use OS_TRU64 instead
static int OS_HP
          Operating system is HP-UX.
static int OS_IRIX
          Operating system is SGI IRIX.
static int OS_LINUX
          Operating system is Linux.
static int OS_MAC
          Operating system is Mac.
static int OS_OS2
          Operating system is OS/2.
static int OS_OTHER
          Operating system is unknown.
static int OS_SOLARIS
          Operating system is Solaris.
static int OS_SUNOS
          Operating system is Sun OS.
static int OS_TRU64
          Operating system is Compaq TRU64 Unix
static int OS_UNIX_MASK
          A mask for Unix platforms.
static int OS_VMS
          Operating system is Compaq OpenVMS
static int OS_WIN_OTHER
          Operating system is one of the Windows variants but we don't know which one it is
static int OS_WIN2000
          Operating system is Windows 2000.
static int OS_WIN95
          Operating system is Windows 95.
static int OS_WIN98
          Operating system is Windows 98.
static int OS_WINDOWS_MASK
          A mask for Windows platforms.
static int OS_WINNT
          Operating system is Windows NT.
static int TYPICAL_WINDOWS_TASKBAR_HEIGHT
          A height of the windows's taskbar
 
Method Summary
static int arrayHashCode(Object[] arr)
          Compute hash code of array.
static boolean compareObjects(Object o1, Object o2)
          Safe equality check.
static boolean compareObjectsImpl(Object o1, Object o2, int checkArraysDepth)
          Safe equality check with array recursion.
static String escapeParameters(String[] params)
          Complementary method to parseParameters
static BeanInfo getBeanInfo(Class clazz)
          Central method for obtaining BeanInfo for potential JavaBean classes.
static BeanInfo getBeanInfo(Class clazz, Class stopClass)
          Central method for obtaining BeanInfo for potential JavaBean classes, with a stop class.
static String getClassName(Class clazz)
          Assemble a human-presentable class name for a specified class.
static Component getFocusTraversableComponent(Component c)
          Find a focus-traverable component.
static Class getObjectType(Class c)
          Get the object type for given primitive type.
static int getOperatingSystem()
          Get the operating system on which the IDE is running.
static Class getPrimitiveType(Class c)
          Get the primitive type for given object type.
static Dimension getScreenSize()
           
static String getShortClassName(Class clazz)
          Assemble a human-presentable class name for a specified class (omitting the package).
static boolean isJavaIdentifier(String id)
          Test whether a given string is a valid Java identifier.
static boolean isLargeFrameIcons()
          Test whether the operating system supports icons on frames (windows).
static boolean isUnix()
          Test whether the IDE is running on some variant of Unix.
static boolean isWindows()
          Test whether the IDE is running on some variant of Windows.
static String keyToString(KeyStroke stroke)
          Converts a Swing key stroke descriptor to a familiar Emacs-like name.
static Image loadImage(String resourceID)
          Loads an image from the specified resource ID.
static Image mergeImages(Image image1, Image image2, int x, int y)
          This method merges two images into the new one.
static String[] parseParameters(String s)
          Parses parameters from a given string in shell-like manner.
static List partialSort(List l, Comparator c, boolean stable)
          Sort a list according to a specified partial order.
static String pureClassName(String fullName)
          Turn full name of an inner class into its pure form.
static String replaceString(String original, String replaceFrom, String replaceTo)
          Search-and-replace fixed string matches within a string.
static int showJFileChooser(JFileChooser chooser, Component parent, String approveButtonText)
          Utility method for avoiding of memory leak in JDK 1.3 / JFileChooser.showDialog(...)
static KeyStroke stringToKey(String s)
          Construct a new key description from a given universal string description.
static KeyStroke[] stringToKeys(String s)
          Convert a space-separated list of user-friendly key binding names to a list of Swing key strokes.
static Object[] toObjectArray(Object array)
          Convert an array of primitive types to an array of objects.
static Object toPrimitiveArray(Object[] array)
          Convert an array of objects to an array of primitive types.
static String translate(String className)
          Handles repackaging whole IDE for runtime compatibility.
static String wrapString(String original, int width, boolean wrapWords, boolean removeNewLines)
          Deprecated. Use (String, int, BreakIterator, boolean) as it is friendlier to I18N.
static String wrapString(String original, int width, BreakIterator breakIterator, boolean removeNewLines)
          Wrap multi-line strings.
static String[] wrapStringToArray(String original, int width, boolean wrapWords, boolean removeNewLines)
          Deprecated. use wrapStringToArray(String, int, BreakIterator, boolean) since it is better for I18N
static String[] wrapStringToArray(String original, int width, BreakIterator breakIterator, boolean removeNewLines)
          Wrap multi-line strings (and get the individual lines).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

OS_WINNT

public static final int OS_WINNT
Operating system is Windows NT.

OS_WIN95

public static final int OS_WIN95
Operating system is Windows 95.

OS_WIN98

public static final int OS_WIN98
Operating system is Windows 98.

OS_SOLARIS

public static final int OS_SOLARIS
Operating system is Solaris.

OS_LINUX

public static final int OS_LINUX
Operating system is Linux.

OS_HP

public static final int OS_HP
Operating system is HP-UX.

OS_AIX

public static final int OS_AIX
Operating system is IBM AIX.

OS_IRIX

public static final int OS_IRIX
Operating system is SGI IRIX.

OS_SUNOS

public static final int OS_SUNOS
Operating system is Sun OS.

OS_TRU64

public static final int OS_TRU64
Operating system is Compaq TRU64 Unix

OS_DEC

public static final int OS_DEC
Deprecated. please use OS_TRU64 instead


OS_OS2

public static final int OS_OS2
Operating system is OS/2.

OS_MAC

public static final int OS_MAC
Operating system is Mac.

OS_WIN2000

public static final int OS_WIN2000
Operating system is Windows 2000.

OS_VMS

public static final int OS_VMS
Operating system is Compaq OpenVMS

OS_WIN_OTHER

public static final int OS_WIN_OTHER
Operating system is one of the Windows variants but we don't know which one it is

OS_OTHER

public static final int OS_OTHER
Operating system is unknown.

OS_WINDOWS_MASK

public static final int OS_WINDOWS_MASK
A mask for Windows platforms.

OS_UNIX_MASK

public static final int OS_UNIX_MASK
A mask for Unix platforms.

TYPICAL_WINDOWS_TASKBAR_HEIGHT

public static final int TYPICAL_WINDOWS_TASKBAR_HEIGHT
A height of the windows's taskbar
Method Detail

getOperatingSystem

public static final int getOperatingSystem()
Get the operating system on which the IDE is running.
Returns:
one of the OS_* constants (such as OS_WINNT)

isWindows

public static final boolean isWindows()
Test whether the IDE is running on some variant of Windows.
Returns:
true if Windows, false if some other manner of operating system

isUnix

public static final boolean isUnix()
Test whether the IDE is running on some variant of Unix. Linux is included as well as the commercial vendors.
Returns:
true some sort of Unix, false if some other manner of operating system

isJavaIdentifier

public static final boolean isJavaIdentifier(String id)
Test whether a given string is a valid Java identifier.
Parameters:
id - string which should be checked
Returns:
true if a valid identifier

getBeanInfo

public static BeanInfo getBeanInfo(Class clazz)
                            throws IntrospectionException
Central method for obtaining BeanInfo for potential JavaBean classes. This implementation provides additional functionality for Swing bean infos.
Parameters:
clazz - class of the bean to provide the BeanInfo for
Returns:
the bean info
Throws:
IntrospectionException - for the usual reasons
See Also:
Introspector.getBeanInfo(Class)

getBeanInfo

public static BeanInfo getBeanInfo(Class clazz,
                                   Class stopClass)
                            throws IntrospectionException
Central method for obtaining BeanInfo for potential JavaBean classes, with a stop class. This implementation provides additional functionality for Swing bean infos.
Parameters:
clazz - class of the bean to provide the BeanInfo for
stopClass - the stop class
Returns:
the bean info
Throws:
IntrospectionException - for the usual reasons
See Also:
Introspector.getBeanInfo(Class, Class)

wrapStringToArray

public static String[] wrapStringToArray(String original,
                                         int width,
                                         boolean wrapWords,
                                         boolean removeNewLines)
Deprecated. use wrapStringToArray(String, int, BreakIterator, boolean) since it is better for I18N

Wrap multi-line strings (and get the individual lines).
Parameters:
original - the original string to wrap
width - the maximum width of lines
wrapWords - if true, the lines are wrapped on word boundaries (if possible); if false, character boundaries are used
removeNewLines - if true, any newlines in the original string are ignored
Returns:
the lines after wrapping

wrapStringToArray

public static String[] wrapStringToArray(String original,
                                         int width,
                                         BreakIterator breakIterator,
                                         boolean removeNewLines)
Wrap multi-line strings (and get the individual lines).
Parameters:
original - the original string to wrap
width - the maximum width of lines
breakIterator - breaks original to chars, words, sentences, depending on what instance you provide.
removeNewLines - if true, any newlines in the original string are ignored
Returns:
the lines after wrapping

wrapString

public static String wrapString(String original,
                                int width,
                                BreakIterator breakIterator,
                                boolean removeNewLines)
Wrap multi-line strings.
Parameters:
original - the original string to wrap
width - the maximum width of lines
wrapWords - if true, the lines are wrapped on word boundaries (if possible); if false, character boundaries are used
removeNewLines - if true, any newlines in the original string are ignored
Returns:
the whole string with embedded newlines

wrapString

public static String wrapString(String original,
                                int width,
                                boolean wrapWords,
                                boolean removeNewLines)
Deprecated. Use (String, int, BreakIterator, boolean) as it is friendlier to I18N.

Wrap multi-line strings.
Parameters:
original - the original string to wrap
width - the maximum width of lines
wrapWords - if true, the lines are wrapped on word boundaries (if possible); if false, character boundaries are used
removeNewLines - if true, any newlines in the original string are ignored
Returns:
the whole string with embedded newlines

replaceString

public static String replaceString(String original,
                                   String replaceFrom,
                                   String replaceTo)
Search-and-replace fixed string matches within a string.
Parameters:
original - the original string
replaceFrom - the substring to be find
replaceTo - the substring to replace it with
Returns:
a new string with all occurrences replaced

pureClassName

public static final String pureClassName(String fullName)
Turn full name of an inner class into its pure form.
Parameters:
fullName - e.g. some.pkg.SomeClass$Inner
Returns:
e.g. Inner

isLargeFrameIcons

public static final boolean isLargeFrameIcons()
Test whether the operating system supports icons on frames (windows).
Returns:
true if it does not

arrayHashCode

public static int arrayHashCode(Object[] arr)
Compute hash code of array. Asks all elements for their own code and composes the values.
Parameters:
arr - array of objects, can contain nulls
Returns:
the hash code
See Also:
Object.hashCode()

compareObjects

public static boolean compareObjects(Object o1,
                                     Object o2)
Safe equality check. The supplied objects are equal if: This method is null-safe, so if one of the parameters is true and the second not, it returns false.
Parameters:
o1 - the first object to compare
o2 - the second object to compare
Returns:
true if the objects are equal

compareObjectsImpl

public static boolean compareObjectsImpl(Object o1,
                                         Object o2,
                                         int checkArraysDepth)
Safe equality check with array recursion.
Parameters:
o1 - the first object to compare
o2 - the second object to compare
checkArraysDepth - the depth to which arrays should be compared for equality (negative for infinite depth, zero for no comparison of elements, one for shallow, etc.)
Returns:
true if the objects are equal
See Also:
compareObjects(Object, Object)

getClassName

public static String getClassName(Class clazz)
Assemble a human-presentable class name for a specified class. Arrays are represented as e.g. java.lang.String[].
Parameters:
clazz - the class to name
Returns:
the human-presentable name

getShortClassName

public static String getShortClassName(Class clazz)
Assemble a human-presentable class name for a specified class (omitting the package). Arrays are represented as e.g. String[].
Parameters:
clazz - the class to name
Returns:
the human-presentable name

toPrimitiveArray

public static Object toPrimitiveArray(Object[] array)
Convert an array of objects to an array of primitive types. E.g. an Integer[] would be changed to an int[].
Parameters:
array - the wrapper array
Returns:
a primitive array
Throws:
IllegalArgumentException - if the array element type is not a primitive wrapper

toObjectArray

public static Object[] toObjectArray(Object array)
Convert an array of primitive types to an array of objects. E.g. an int[] would be turned into an Integer[].
Parameters:
array - the primitive array
Returns:
a wrapper array
Throws:
IllegalArgumentException - if the array element type is not primitive

getObjectType

public static Class getObjectType(Class c)
Get the object type for given primitive type.
Parameters:
c - primitive type (e.g. int)
Returns:
object type (e.g. Integer)

getPrimitiveType

public static Class getPrimitiveType(Class c)
Get the primitive type for given object type.
Parameters:
c - object type (e.g. Integer)
Returns:
primitive type (e.g. int)

getFocusTraversableComponent

public static Component getFocusTraversableComponent(Component c)
Find a focus-traverable component.
Parameters:
c - the component to look in
Returns:
the same component if traversable, else a child component if present, else null
See Also:
Component.isFocusTraversable()

parseParameters

public static String[] parseParameters(String s)
Parses parameters from a given string in shell-like manner. Users of the Bourne shell (e.g. on Unix) will already be familiar with the behavior. For example, when using NbProcessDescriptor you should be able to: Caveat: even after parsing, Windows programs (such as the Java launcher) may not fully honor certain characters, such as quotes, in command names or arguments. This is because programs under Windows frequently perform their own parsing and unescaping (since the shell cannot be relied on to do this). On Unix, this problem should not occur.
Parameters:
s - a string to parse
Returns:
an array of parameters

escapeParameters

public static String escapeParameters(String[] params)
Complementary method to parseParameters
See Also:
parseParameters(java.lang.String)

keyToString

public static String keyToString(KeyStroke stroke)
Converts a Swing key stroke descriptor to a familiar Emacs-like name.
Parameters:
stroke - key description
Returns:
name of the key (e.g. CS-F1 for control-shift-function key one)
See Also:
stringToKey(java.lang.String)

stringToKey

public static KeyStroke stringToKey(String s)
Construct a new key description from a given universal string description. Provides mapping between Emacs-like textual key descriptions and the KeyStroke object used in Swing.

This format has following form:

[C][A][S][M]-identifier

Where:

Every modifier before the hyphen must be pressed. identifier can be any text constant from KeyEvent but without the leading VK_ characters. So KeyEvent.VK_ENTER is described as ENTER.
Parameters:
s - the string with the description of the key
Returns:
key description object, or null if the string does not represent any valid key

stringToKeys

public static KeyStroke[] stringToKeys(String s)
Convert a space-separated list of user-friendly key binding names to a list of Swing key strokes.
Parameters:
s - the string with keys
Returns:
array of key strokes, or null if the string description is not valid
See Also:
stringToKey(java.lang.String)

getScreenSize

public static final Dimension getScreenSize()
Returns:
size of the screen. The size is modified for Windows OS - some pointes are subtracted to reflect a presence of the taskbar

showJFileChooser

public static final int showJFileChooser(JFileChooser chooser,
                                         Component parent,
                                         String approveButtonText)
Utility method for avoiding of memory leak in JDK 1.3 / JFileChooser.showDialog(...)
Parameters:
parent -  
approveButtonText -  

partialSort

public static List partialSort(List l,
                               Comparator c,
                               boolean stable)
                        throws Utilities.UnorderableException
Sort a list according to a specified partial order. Note that in the current implementation, the comparator will be called exactly once for each distinct pair of list elements, ignoring order, so caching its results is a waste of time.
Parameters:
l - the list to sort (will not be modified)
c - a comparator to impose the partial order; "equal" means that the elements are not ordered with respect to one another, i.e. may be only a partial order
stable - whether to attempt a stable sort, meaning that the position of elements will be disturbed as little as possible; might be slightly slower
Returns:
the partially-sorted list
Throws:
Utilities.UnorderableException - if the specified partial order is inconsistent on this list

translate

public static String translate(String className)
Handles repackaging whole IDE for runtime compatibility. It should not be used by new code except during a package rename.
Parameters:
name - fully qualified name of a class to translate
Throws:
NullPointerException - iff name is null

mergeImages

public static final Image mergeImages(Image image1,
                                      Image image2,
                                      int x,
                                      int y)
This method merges two images into the new one. The second image is drawn over the first one with its top-left corner at x, y. Images mustn't be of the same size. New image will have a size of max(second image size + top-left corner, first image size). Method is used mostly when second image contains transparent pixels (e.g. for badging). If both images are null, it makes default transparent 16x16 image.
Parameters:
image1 - underlying image
image2 - second image
x - x position of top-left corner
y - y position of top-left corner
Returns:
new merged image

loadImage

public static final Image loadImage(String resourceID)
Loads an image from the specified resource ID. The image is loaded using the "system" classloader registered in Lookup, reverting to the systemClassLoader if no loader is registered.
Parameters:
resourceID - resource path of the icon
Returns:
icon's Image, or null, if the icon cannot be loaded.


Built on December 12 2001.  |  Portions Copyright 1997-2001 Sun Microsystems, Inc. All rights reserved.