com.sas.swing.visuals.util
Class Util

com.sas.swing.visuals.util.Util
All Implemented Interfaces:
Constants, javax.swing.SwingConstants

public abstract class Util
implements Constants

Utility functions.


Field Summary
static java.lang.String RB_KEY
           
static java.lang.String VERTICAL_LINE_WIDTH_STRING
           
 
Fields inherited from interface com.sas.swing.Constants
CENTER_BOTTOM, CENTER_LEADING, CENTER_LEFT, CENTER_RIGHT, CENTER_TOP, CENTER_TRAILING
 
Constructor Summary
Util()
           
 
Method Summary
static void addCommand(javax.swing.JMenu menu, com.sas.util.Command command)
          Alias for addCommand(menu, commands, null);
static void addCommand(javax.swing.JMenu menu, com.sas.util.Command command, java.lang.Object object)
           
static void addCommand(javax.swing.JPopupMenu menu, com.sas.util.Command command)
          Alias for addCommand(menu, commands, null);
static void addCommand(javax.swing.JPopupMenu menu, com.sas.util.Command command, java.lang.Object object)
           
static void addCommands(javax.swing.JMenu menu, com.sas.util.Command[] commands)
          Alias for addCommands(menu, commands, null);
static void addCommands(javax.swing.JMenu menu, com.sas.util.Command[] commands, java.lang.Object object)
           
static void addCommands(javax.swing.JPopupMenu menu, com.sas.util.Command[] commands)
          Alias for addCommands(menu, commands, null);
static void addCommands(javax.swing.JPopupMenu menu, com.sas.util.Command[] commands, java.lang.Object object)
           
static void apply(java.awt.Component rootComponent, com.sas.util.ApplyInterface operation)
          Applies a given operation to each of a set of components.
static java.lang.String[] breakupLines(java.lang.String text)
          StringTokenizer(text,"\n") really does a "\n+" which is not what we want.
static void center(java.awt.Window window)
          Center the dialog on the screen.
static void centerOver(java.awt.Window window, java.awt.Component component)
          Center the dialog over a component on the screen.
static void centerOverWindow(java.awt.Window window, java.awt.Component component)
          Center the dialog over a Window on the screen.
static java.awt.Point computePopupMenuLocation(int x, int y, int width, int height, java.awt.Component comp)
          Determines the location on the screen the popup menu should be displayed at.
static java.awt.Dimension computePopupMenuSize(javax.swing.JPopupMenu popMenu, java.awt.Component comp)
          Calculates the size a popup menu will be so that a user will be able to know where to locate the popup menu on the screen.
static void configureGroupBoxBorder(javax.swing.border.Border border, java.lang.String borderTitle)
          Sets the values on a Border object for it to look like an MS group box.
static void configureMnemonicForButton(javax.swing.AbstractButton button, java.lang.String text)
          Configures the mnemonic for the button given the text.
static void configureMnemonicForLabel(javax.swing.JLabel label, java.lang.String text)
          Configures the mnemonic for the label given the text.
static java.awt.Image convertIconToImage(java.awt.Component component, javax.swing.Icon icon)
          Converts the specified icon to an image.
static javax.swing.JLabel createLabelForComponent(java.awt.Component component, java.lang.String text)
          Creates an instance of JLabel and configures the mnemonic for the label given the text.
static void drawFocusRect(java.awt.Graphics g, int x, int y, int width, int height)
          WARNING: Changes current color setting.
static void drawFocusRect(java.awt.Graphics g, int x, int y, int width, int height, boolean useXOR)
          WARNING: Changes current color setting.
static boolean drawString(java.awt.Graphics g, java.lang.String text, int underlinedChar, int x, int y)
          Copied and modified from BasicGraphicsUtils Draw a string with the graphics g at location (x,y) just like g.drawString() would.
static java.lang.String getClippedText(java.lang.String text, java.awt.FontMetrics fm, int availTextWidth)
          Retruns a clipped version of the text passed in if necessary.
static java.lang.Object getDefaultModel(java.lang.Object comp)
          Get an instance of the default model for the given component.
static java.awt.Font getDialogFont()
           
static int getDisplayedMnemonic(java.lang.String label)
          Returns the first mnemonic in the given string.
static int getDisplayedMnemonicIndex(java.lang.String label)
          Returns the index of the first mnemonic in the given string.
static javax.swing.border.Border getGroupBoxBorder(java.lang.String title)
           
static javax.swing.border.Border getGroupBoxBorder(java.lang.String title, int top, int left, int bottom, int right)
           
static int getKeyMapping(java.lang.String key)
          Get the java.awt.event.KeyEvent key code corresponding to the given string.
static java.lang.String getLabelWithoutMnemonic(java.lang.String label)
          Returns a string with all mnemonics in the given string removed.
static java.awt.Container getParent(java.awt.Component comp, java.lang.Class containerClass)
          Returns the first Container ancestor of the given component that is of type containerClass.
static java.awt.Frame getParentAWTFrame(java.awt.Component comp)
          Get the parent frame of the given component.
static javax.swing.JFrame getParentFrame(java.awt.Component comp)
          Get the parent frame of the given component.
static java.lang.String getStringMapping(int key)
          Get the string corresponding to the given java.awt.event.KeyEvent key code.
protected static java.lang.String[] getWrappedText(int availableSize, java.lang.String[] lines, java.awt.FontMetrics fm, boolean wordWrap, int orientation)
          Returns an array of lines of text, which is a wrapped version of the lines passed in passed on the various properties passed in.
static void invertRect(java.awt.Graphics g, int x, int y, int width, int height)
          Draws an inverted rectangle.
static void invertRect(java.awt.Graphics g, java.awt.Rectangle rect)
          Draws an inverted rectangle.
static java.lang.String layoutCompoundLabel(javax.swing.JComponent c, java.awt.FontMetrics fm, java.lang.String text, javax.swing.Icon icon, int verticalAlignment, int horizontalAlignment, int verticalTextPosition, int horizontalTextPosition, java.awt.Rectangle viewR, java.awt.Rectangle iconR, java.awt.Rectangle textR, int textIconGap, int orientation, boolean lineWrap, boolean wrapStyleWord, int gapBetweenLines, boolean clipIt, int tabSize, int leadingContentOffset, int topContentOffset)
          Copied and modified from SwingUtilities, defines what the orientation is, line wrap policy is, and the gap between lines so that multiline text can be handled.
static void setEnabledOnContainer(java.awt.Container container, boolean enabled)
          This method sets the enabled state on all of it's descendents to either true or false depending on the value of the enabled parameter.
static void setLabelAndMnemonicOnAction(javax.swing.Action action, java.lang.String text)
          Sets the mnemonic and label for the given action given the text.
 

Field Detail

RB_KEY

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

VERTICAL_LINE_WIDTH_STRING

public static final java.lang.String VERTICAL_LINE_WIDTH_STRING
See Also:
Constant Field Values
Constructor Detail

Util

public Util()
Method Detail

setEnabledOnContainer

public static void setEnabledOnContainer(java.awt.Container container,
                                         boolean enabled)
This method sets the enabled state on all of it's descendents to either true or false depending on the value of the enabled parameter. It does not set the enabled state on itself.


center

public static void center(java.awt.Window window)
Center the dialog on the screen.


centerOver

public static void centerOver(java.awt.Window window,
                              java.awt.Component component)
Center the dialog over a component on the screen.


centerOverWindow

public static void centerOverWindow(java.awt.Window window,
                                    java.awt.Component component)
Center the dialog over a Window on the screen. The Window may be specified, or the containment hierarchy is walked until a Window is found.

Parameters:
window - The window to center.
component - The component who's Window should be used to center over.

getDialogFont

public static java.awt.Font getDialogFont()

configureGroupBoxBorder

public static void configureGroupBoxBorder(javax.swing.border.Border border,
                                           java.lang.String borderTitle)
Sets the values on a Border object for it to look like an MS group box. A null String can be passed in to specify no title. If the border is of type BaseBorder, then the common values are as follows: lightSource = LOWERED, borderWidth = 2, and textMargin = 2. If the border is of type TitledBorder, the the title is just set and will use the UI to set the look and feel.

Parameters:
border - the border to modify for a group box
borderTitle - the title for the border, or null for no title

getGroupBoxBorder

public static javax.swing.border.Border getGroupBoxBorder(java.lang.String title,
                                                          int top,
                                                          int left,
                                                          int bottom,
                                                          int right)

getGroupBoxBorder

public static javax.swing.border.Border getGroupBoxBorder(java.lang.String title)

getParentFrame

public static javax.swing.JFrame getParentFrame(java.awt.Component comp)
Get the parent frame of the given component. Walks the parent tree until their is no parent or the parent is of type javax.swing.JFrame.

Parameters:
comp - the component to find the parent frame for
Returns:
the frame ancestor of the component or null if no ancestor is of type javax.swing.JFrame

getParentAWTFrame

public static java.awt.Frame getParentAWTFrame(java.awt.Component comp)
Get the parent frame of the given component. Walks the parent tree until their is no parent or the parent is of type java.awt.Frame.

Parameters:
comp - the component to find the parent frame for
Returns:
the frame ancestor of the component or null if no ancestor is of type java.awt.Frame

getParent

public static java.awt.Container getParent(java.awt.Component comp,
                                           java.lang.Class containerClass)
Returns the first Container ancestor of the given component that is of type containerClass.

Parameters:
comp - the component to find the parent frame for
Returns:
the Container ancestor of the component or null if no ancestor is of type containerClass

getDefaultModel

public static java.lang.Object getDefaultModel(java.lang.Object comp)
Get an instance of the default model for the given component.

Parameters:
comp - the component to get the default model for
Returns:
an instance of the default model or null if the component does not have a model

addCommand

public static void addCommand(javax.swing.JMenu menu,
                              com.sas.util.Command command,
                              java.lang.Object object)

addCommand

public static void addCommand(javax.swing.JMenu menu,
                              com.sas.util.Command command)
Alias for addCommand(menu, commands, null);


addCommands

public static void addCommands(javax.swing.JMenu menu,
                               com.sas.util.Command[] commands)
Alias for addCommands(menu, commands, null);


addCommands

public static void addCommands(javax.swing.JMenu menu,
                               com.sas.util.Command[] commands,
                               java.lang.Object object)

addCommand

public static void addCommand(javax.swing.JPopupMenu menu,
                              com.sas.util.Command command,
                              java.lang.Object object)

addCommand

public static void addCommand(javax.swing.JPopupMenu menu,
                              com.sas.util.Command command)
Alias for addCommand(menu, commands, null);


addCommands

public static void addCommands(javax.swing.JPopupMenu menu,
                               com.sas.util.Command[] commands)
Alias for addCommands(menu, commands, null);


addCommands

public static void addCommands(javax.swing.JPopupMenu menu,
                               com.sas.util.Command[] commands,
                               java.lang.Object object)

breakupLines

public static java.lang.String[] breakupLines(java.lang.String text)
StringTokenizer(text,"\n") really does a "\n+" which is not what we want. We also want it be based on the line.separator property. So create our own version. We first attempt to divide by line.separator, then by "\n". Ideally, we'd prefer to just break up by line.separator, but we need to handle text that was defined in a properties file, with embedded \n.


getClippedText

public static java.lang.String getClippedText(java.lang.String text,
                                              java.awt.FontMetrics fm,
                                              int availTextWidth)
Retruns a clipped version of the text passed in if necessary.

Parameters:
text - the text to clip if necessary
fm - the font metrics for the graphics
availTextWidth - the number of pixels the text has to paint itself in
Returns:
A clipped version of the text if determined not to fit with the available with, else just the same text passed in will be returned

drawString

public static boolean drawString(java.awt.Graphics g,
                                 java.lang.String text,
                                 int underlinedChar,
                                 int x,
                                 int y)
Copied and modified from BasicGraphicsUtils Draw a string with the graphics g at location (x,y) just like g.drawString() would. The first occurence of underlineChar in text will be underlined. The matching is not case sensitive. Returns true if the underlined character was drawn.


layoutCompoundLabel

public static java.lang.String layoutCompoundLabel(javax.swing.JComponent c,
                                                   java.awt.FontMetrics fm,
                                                   java.lang.String text,
                                                   javax.swing.Icon icon,
                                                   int verticalAlignment,
                                                   int horizontalAlignment,
                                                   int verticalTextPosition,
                                                   int horizontalTextPosition,
                                                   java.awt.Rectangle viewR,
                                                   java.awt.Rectangle iconR,
                                                   java.awt.Rectangle textR,
                                                   int textIconGap,
                                                   int orientation,
                                                   boolean lineWrap,
                                                   boolean wrapStyleWord,
                                                   int gapBetweenLines,
                                                   boolean clipIt,
                                                   int tabSize,
                                                   int leadingContentOffset,
                                                   int topContentOffset)
Copied and modified from SwingUtilities, defines what the orientation is, line wrap policy is, and the gap between lines so that multiline text can be handled. Calls layoutCompoundLabel passing in true for replaceTab. Compute and return the location of the icons origin, the location of origin of the text baseline, and a possibly clipped version of the compound labels string. Locations are computed relative to the viewR rectangle.


getWrappedText

protected static java.lang.String[] getWrappedText(int availableSize,
                                                   java.lang.String[] lines,
                                                   java.awt.FontMetrics fm,
                                                   boolean wordWrap,
                                                   int orientation)
Returns an array of lines of text, which is a wrapped version of the lines passed in passed on the various properties passed in.

Parameters:
availableSize - the available size the text has to paint itself
lines - the lines of text which will be wrapped as needed
fm - the font metrics for the graphics that is being used
wordWrap - whether to wrap the lines of text on word boundaries or not
orientation - whether the text is being painted vertically or horizontally
Returns:
and array of strings which are the wrapped lines of text

computePopupMenuSize

public static java.awt.Dimension computePopupMenuSize(javax.swing.JPopupMenu popMenu,
                                                      java.awt.Component comp)
Calculates the size a popup menu will be so that a user will be able to know where to locate the popup menu on the screen.

Parameters:
popMenu - the popup menu whoose size the user wants to know
comp - the component the popup menu will be displayed upon, used so if the popup menu does not have a font set, it will use the font form the component if possible
Returns:
the size the popup menu will display on the screen

computePopupMenuLocation

public static java.awt.Point computePopupMenuLocation(int x,
                                                      int y,
                                                      int width,
                                                      int height,
                                                      java.awt.Component comp)
Determines the location on the screen the popup menu should be displayed at. It will move the popup menu up if going off the bottom of the screen, or it will move the popup menu to the left if the menu is running of the right side of the screen.

Parameters:
x - the initial popup menu x location, used to calculate if the popup menu will fit on the screen as is
y - the initial popup menu y location, used to calculate if the popup menu will fit on the screen as is
width - the width of the popup menu
height - the hieight of the popup menu
comp - the component the popup menu is displaying on
Returns:
the x an dy location the popup menu should be displayed at so it is entirely visible to the user

getStringMapping

public static java.lang.String getStringMapping(int key)
Get the string corresponding to the given java.awt.event.KeyEvent key code.

Parameters:
key - the KeyEvent key code
Returns:
the string matching the KeyEvent.

getKeyMapping

public static int getKeyMapping(java.lang.String key)
Get the java.awt.event.KeyEvent key code corresponding to the given string.

Parameters:
key - the string of the KeyEvent key code name
Returns:
the KeyEvent key code matching the string.

getDisplayedMnemonic

public static int getDisplayedMnemonic(java.lang.String label)
Returns the first mnemonic in the given string. Mnemonics are noted by & before the character that is to be the menonic. If more then one mnemonic is found in the string, only the first mnemonic will be returned. If the string is to contain a &, then 2 &s should be inserted and that will note that this is not the mnemonic. Other special strings are also considered when returning the correct mnemonic. The following strings are the special cases: &&, >, <, &, &copy, and &#. Here is an example:

String item = bundle.getString("name.txt"); // returns "&Name:" String text = Util.getLabelWithoutMnemonic(item); int index = Util.getDisplayedMnemonicIndex(item); JLabel label = new JLabel(text); JTextField field = new JTextField(); label.setLabelFor(field); if (index > -1) { int mnemonic = Util.getDisplayedMnemonic(item); label.setDisplayedMnemonic(mnemonic); label.setDisplayedMnemonicIndex(index); } This will set the mnemonic index to 0, mnemonic to 'N', and label to "Name". Also, since the label is set as the label for the JTextField, when the user executes the mnemonic, the textfield will gain focus.

Parameters:
the - string that will be parsed to return the frist mnemonic contain within it
Returns:
and integer that maps to the mnemonic contained within the string, or -1 if a mnemonic is not found

getDisplayedMnemonicIndex

public static int getDisplayedMnemonicIndex(java.lang.String label)
Returns the index of the first mnemonic in the given string. The index returned is the index of the character in the string with the & removed, that is, the string with the mnemonic removed. Mnemonics are noted by & before the character that is to be the menonic. If more then one mnemonic is found in the string, only the first mnemonic will be returned. If the string is to contain a &, then 2 &s should be inserted and that will note that this is not the mnemonic. Other special strings are also considered when returning the correct mnemonic. The following strings are the special cases: &&, >, <, &, &copy, and &#. Here is an example:

String item = bundle.getString("saveAs.txt"); // returns "Save &As" String label = Util.getLabelWithoutMnemonic(item); int index = Util.getDisplayedMnemonicIndex(item); JButton button = new JButton(label); if (index > -1) { int mnemonic = Util.getDisplayedMnemonic(item); button.setMnemonic(mnemonic); button.setDisplayedMnemonicIndex(index); } This will set the mnemonic index to 5, mnemonic to 'A', and label to "Save As". If just setMnemoic was called, then the 'a' in "Save" would be the mnemonic character, instead of the 'A' in "As".

Parameters:
the - string that will be parsed to return the frist mnemonic contain within it
Returns:
and integer that denotes the index of the mnemonic in the string, or -1 if a mnemonic is not found

getLabelWithoutMnemonic

public static java.lang.String getLabelWithoutMnemonic(java.lang.String label)
Returns a string with all mnemonics in the given string removed. Mnemonics are noted by & before the character that is to be the mnemonic. If the string is to contain a &, then 2 &s should be inserted and that will note that this is not the mnemonic. Other special strings are also considered when returning the correct mnemonic. The following strings are the special cases: &&, >, <, &, &copy, and &#. Here is an example:

String item = bundle.getString("saveAs.txt"); // returns "Save &As" String label = Util.getLabelWithoutMnemonic(item); int index = Util.getDisplayedMnemonicIndex(item); JButton button = new JButton(label); if (index > -1) { int mnemonic = Util.getDisplayedMnemonic(item); button.setMnemonic(mnemonic); button.setDisplayedMnemonicIndex(index); } This will set the label to "Save As", as the & will be removed from the string.

Parameters:
the - string that will be parsed to have its mnemonics removed
Returns:
a string with all mnemonics removed, if no mnemonics are found, the same string will be returned This method will remove only '&' character. If you want to remove mnemonic character as well then please use getDisplayLabel(String label).

configureMnemonicForLabel

public static void configureMnemonicForLabel(javax.swing.JLabel label,
                                             java.lang.String text)
Configures the mnemonic for the label given the text. This method uses getLabelWithoutMnemonic, getDisplayedMnemonicIndex, and getDisplayedMnemonic to set the mnemonic.

Parameters:
label - The label the mnemoinc is to be set on
text - the text for the label, should contain an & before the character that the mnemonic is suppose to be
See Also:
getLabelWithoutMnemonic(String), getDisplayedMnemonicIndex(String), getDisplayedMnemonic(String)

configureMnemonicForButton

public static void configureMnemonicForButton(javax.swing.AbstractButton button,
                                              java.lang.String text)
Configures the mnemonic for the button given the text. This method uses getLabelWithoutMnemonic, getDisplayedMnemonicIndex, and getDisplayedMnemonic to set the mnemonic.

Parameters:
button - The button the mnemoinc is to be set on
text - the text for the button, should contain an & before the character that the mnemonic is suppose to be
See Also:
getLabelWithoutMnemonic(String), getDisplayedMnemonicIndex(String), getDisplayedMnemonic(String)

setLabelAndMnemonicOnAction

public static void setLabelAndMnemonicOnAction(javax.swing.Action action,
                                               java.lang.String text)
Sets the mnemonic and label for the given action given the text. The mnemonic is the character following an & in the text string.

Parameters:
action - the action that is going to have the Action.NAME, Action.MNEMONIC_KEY and com.sas.swing.util.Action.MNEMONIC_INDEX properties set.
text - the string that contains the mnemonic
See Also:
getLabelWithoutMnemonic(String), getDisplayedMnemonicIndex(String), getDisplayedMnemonic(String)

createLabelForComponent

public static javax.swing.JLabel createLabelForComponent(java.awt.Component component,
                                                         java.lang.String text)
Creates an instance of JLabel and configures the mnemonic for the label given the text. This method delegates to configureMnemonicForLabel for setting up the mnemonic. The label is also sets as teh component for the given component.

Parameters:
component - The componet the label is created for
text - the text for the label, should contain an & before the character that the mnemonic is suppose to be
Returns:
an instance of JLabel with the label and mnemonic set on it
See Also:
configureMnemonicForLabel(JLabel, String)

convertIconToImage

public static java.awt.Image convertIconToImage(java.awt.Component component,
                                                javax.swing.Icon icon)
Converts the specified icon to an image. The image size will be based on the width and values on the icon. The component argument is used to create an image object from (see (int,int) Component.createImage()).

If the icon is an instance of ImageIcon, then ImageIcon.getImage() is returned.

Parameters:
component - the component to create an image object from
icon - the icon to convert to an image
Returns:
an Image object for the specified icon or null
See Also:
ImageIcon

apply

public static void apply(java.awt.Component rootComponent,
                         com.sas.util.ApplyInterface operation)
Applies a given operation to each of a set of components.

Parameters:
rootComponent - the root of a hierarchy of components; operation will be applied to it and each of its children
operation - an object that implements com.sas.util.ApplyInterface; rootComponent and each of its children will be passed in turn as the argument to the apply(Object) method
See Also:
ChangeCursorCommand

invertRect

public static void invertRect(java.awt.Graphics g,
                              int x,
                              int y,
                              int width,
                              int height)
Draws an inverted rectangle. This methods uses the XOR drawing mode of java.awt.Graphics to invert the rectangle. Calling invert twice should restore the original state of the display.

Parameters:
g - the Graphics object to draw into
x - the x coordinate of the rectangle
y - the y coordinate of the rectangle
width - the width of the rectangle
height - the height of the rectangle

invertRect

public static void invertRect(java.awt.Graphics g,
                              java.awt.Rectangle rect)
Draws an inverted rectangle. This methods uses the XOR drawing mode of java.awt.Graphics to invert the rectangle. Calling invert twice should restore the original state of the display.

Parameters:
g - the Graphics object to draw into
rect - the bounding rectangle to invert

drawFocusRect

public static final void drawFocusRect(java.awt.Graphics g,
                                       int x,
                                       int y,
                                       int width,
                                       int height)
WARNING: Changes current color setting. Draws a dotted rectangle along the given bounds. This methods uses the XOR drawing mode of java.awt.Graphics to draw the rectangle. Therefore the focus rectangle should be displayed regardless of the color of the area being drawn into.

Parameters:
g - the graphics object to draw into
x - the logical x coordinate of the rectangle
y - the logical y coordinate of the rectangle
width - the logical width of the rectangle
height - the logical height of the rectangle

drawFocusRect

public static final void drawFocusRect(java.awt.Graphics g,
                                       int x,
                                       int y,
                                       int width,
                                       int height,
                                       boolean useXOR)
WARNING: Changes current color setting. Draws a dotted rectangle along the given bounds. This methods uses the XOR drawing mode of java.awt.Graphics to draw the rectangle if useXOR is true. Therefore the focus rectangle should be displayed regardless of the color of the area being drawn into.

Parameters:
g - the graphics object to draw into
x - the logical x coordinate of the rectangle
y - the logical y coordinate of the rectangle
width - the logical width of the rectangle
height - the logical height of the rectangle
useXOR - use XOR to draw the focus rectangle



Copyright © 2009 SAS Institute Inc. All Rights Reserved.