nl.lxtreme.ols.util.swing
Class SwingComponentUtils

java.lang.Object
  extended by nl.lxtreme.ols.util.swing.SwingComponentUtils

public final class SwingComponentUtils
extends Object

Provides some utility methods for use with Swing components.


Field Summary
static int LEFT_FACING
           
static int RIGHT_FACING
           
 
Method Summary
static boolean askConfirmation(Window aWindow, String aMessage)
          Asks the user for confirmation.
static boolean askConfirmation(Window aWindow, String aMessage, String aTitle)
          Asks the user for confirmation.
static JComponent createButtonPane(JButton... aButtons)
          Creates a button pane in which the given buttons are neatly aligned with proper spacings.
static JComboBox createChannelSelector(int aChannelCount)
          Creates a channel selector combobox, where only a valid channel can be selected.
static JComboBox createChannelSelector(int aChannelCount, int aDefaultSelectedIndex)
          Creates a channel selector combobox, where only a valid channel can be selected.
static KeyStroke createKeyMask(int aKeyStroke, int... aMasks)
          Convenience method to create a key mask.
static KeyStroke createMenuKeyMask(int aKeyStroke, int... aMasks)
          Convenience method to create a key mask for menu's.
static JComboBox createOptionalChannelSelector(int aChannelCount)
          Creates a channel selector combobox, where optionally a channel can be selected.
static JComboBox createOptionalChannelSelector(int aChannelCount, int aDefaultSelectedIndex)
          Creates a channel selector combobox, where optionally a channel can be selected.
static JLabel createRightAlignedLabel(String aText)
          Creates a JLabel which text is right aligned.
static void dispose(Window aWindow)
          Closes and disposes a given Window.
static void drawArrowHead(Graphics2D aG, int aXpos, int aYpos, int aFactor, int aArrowWidth, int aArrowHeight)
          Draws a single arrow head
static void drawDoubleHeadedArrow(Graphics aG, int aX1, int aY, int aX2)
          Draws a double headed arrow of 8x8.
static void drawDoubleHeadedArrow(Graphics aG, int aX1, int aY1, int aX2, int aY2)
          Draws a double headed arrow of 8x8.
static void drawDoubleHeadedArrow(Graphics aG, int aX1, int aY1, int aX2, int aY2, int aArrowWidth, int aArrowHeight)
          Draws a double headed arrow with arrow heads of a given width and height.
static void fixLabelWidth(JLabel aLabel, String aMinimalText)
          "Fixates" the preferred width of the given label to the given text.
static
<T> T
getAncestorOfClass(Class<T> aType, Component aComponent)
          Convenience method for searching above the given component in the component hierarchy and returns the first object of the given type it finds, or null if no such parent was found.
static Window getCurrentWindow()
          Tries to find the current focused window.
static JComponent getDeepestComponentAt(Component aParent, int aXpos, int aYpos)
          Returns the deepest visible descendent Component of parent that contains the location x, y.
static JComponent getDeepestComponentAt(MouseEvent aEvent)
          Returns the deepest visible descendent Component of parent that contains the location x, y.
static int getMenuShortcutKeyMask()
          Returns the key mask of the menu shortcut key.
static Window getOwningWindow(AWTEvent aEvent)
          Tries to find the owning window for the AWT-event's source.
static Window getOwningWindow(Component aComponent)
          Tries to find the owning window for the given component.
static int getStringWidth(Font aFont, String aString)
          Returns the string width for a given Font and string.
static int getStringWidth(String aString)
          Returns the string width for the default label font and string.
static void invokeOnEDT(Runnable aRunnable)
          Similar as to SwingUtilities.invokeLater(Runnable), but does a check first if the current running thread is already the EDT.
static boolean isActivelyShown(Component aComponent)
          Returns whether the given component is "actively" shown in screen, that is, it or any of its ancestors is focused.
static void loadWindowState(org.osgi.service.prefs.Preferences aProperties, Window aWindow)
          Tries to load/restore the window state of the given window.
static void registerKeyBinding(JComponent aComponent, char aKey, Action aAction)
          Registers a given keystroke to invoke a given action on the given component.
static void registerKeyBinding(JComponent aComponent, KeyStroke aKeyStroke, Action aAction)
          Registers a given keystroke to invoke a given action on the given component.
static void registerKeyBinding(JComponent aComponent, String aKeyStroke, Action aAction)
          Registers a given keystroke to invoke a given action on the given component.
static void registerKeystroke(JComponent aComponent, Action aAction, String aCommandName)
          Registers the keystroke of the given action as "command" of the given component.
static void saveWindowState(org.osgi.service.prefs.Preferences aProperties, Window aWindow)
          Saves the window state to the given properties map.
static void setupDialogContentPane(JDialog aDialog, Component aCenterComponent, Component aButtonPane)
          Sets up the given dialog's content pane by setting its border to provide a good default spacer, and setting the content pane to the given components.
static void setupDialogContentPane(JDialog aDialog, Component aCenterComponent, Component aButtonPane, JButton defaultButton)
          Sets up the given dialog's content pane by setting its border to provide a good default spacer, and setting the content pane to the given components.
static File showFileOpenDialog(Window aOwner, FileFilter... aFileFilters)
          Shows a file-open selection dialog for the current working directory.
static File showFileOpenDialog(Window aOwner, String aCurrentDirectory, FileFilter... aFileFilters)
          Shows a file-open selection dialog for the given working directory.
static File showFileSaveDialog(Window aOwner, FileFilter... aFileFilters)
          Shows a file-save selection dialog for the current working directory.
static File showFileSaveDialog(Window aOwner, String aCurrentDirectory, FileFilter... aFileFilters)
          Shows a file-save selection dialog for the given working directory.
static File showFileSelectionDialog(Window aOwner, FileFilter... aFileFilters)
          Shows a file selection dialog for the current working directory.
static File showFileSelectionDialog(Window aOwner, String aCurrentDirectory, FileFilter... aFileFilters)
          Shows a file selection dialog for the given working directory.
static String toCssString(Font aFont)
          Converts a given font to a font-clause that can be used in a CSS-file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LEFT_FACING

public static final int LEFT_FACING
See Also:
Constant Field Values

RIGHT_FACING

public static final int RIGHT_FACING
See Also:
Constant Field Values
Method Detail

askConfirmation

public static boolean askConfirmation(Window aWindow,
                                      String aMessage)
Asks the user for confirmation.

Parameters:
aWindow - the parent window of the confirmation dialog;
aMessage - the message to display in the confirmation dialog.
Returns:
true if the user acknowledged the confirmation, false otherwise.

askConfirmation

public static boolean askConfirmation(Window aWindow,
                                      String aMessage,
                                      String aTitle)
Asks the user for confirmation.

Parameters:
aWindow - the parent window of the confirmation dialog;
aMessage - the message to display in the confirmation dialog;
aTitle - the title to display in the confirmation dialog.
Returns:
true if the user acknowledged the confirmation, false otherwise.

createButtonPane

public static JComponent createButtonPane(JButton... aButtons)
Creates a button pane in which the given buttons are neatly aligned with proper spacings.

Parameters:
aButtons - the buttons to add to the created button pane, will be added in the given order.
Returns:
the button pane, never null.

createChannelSelector

public static JComboBox createChannelSelector(int aChannelCount)
Creates a channel selector combobox, where only a valid channel can be selected.

Parameters:
aChannelCount - the number of channels to include in the combobox options;
Returns:
a combobox with channel selector options.

createChannelSelector

public static JComboBox createChannelSelector(int aChannelCount,
                                              int aDefaultSelectedIndex)
Creates a channel selector combobox, where only a valid channel can be selected.

Parameters:
aChannelCount - the number of channels to include in the combobox options;
aDefaultSelectedIndex - the default selected index for the created combobox.
Returns:
a combobox with channel selector options.

createKeyMask

public static final KeyStroke createKeyMask(int aKeyStroke,
                                            int... aMasks)
Convenience method to create a key mask.

Parameters:
aKeyStroke - the key stroke to create a key mask for;
aMasks - the (optional) mask modifiers to use.
Returns:
a keystroke instance.

createMenuKeyMask

public static final KeyStroke createMenuKeyMask(int aKeyStroke,
                                                int... aMasks)
Convenience method to create a key mask for menu's.

Parameters:
aKeyStroke - the key stroke to create a menu key mask for;
aMasks - the (optional) mask modifiers to use.
Returns:
a keystroke instance.

createOptionalChannelSelector

public static JComboBox createOptionalChannelSelector(int aChannelCount)
Creates a channel selector combobox, where optionally a channel can be selected.

Parameters:
aChannelCount - the number of channels to include in the combobox options.
Returns:
a combobox with channel selector options.

createOptionalChannelSelector

public static JComboBox createOptionalChannelSelector(int aChannelCount,
                                                      int aDefaultSelectedIndex)
Creates a channel selector combobox, where optionally a channel can be selected.

Parameters:
aChannelCount - the number of channels to include in the combobox options;
aDefaultSelectedIndex - the default selected index for the created combobox.
Returns:
a combobox with channel selector options.

createRightAlignedLabel

public static final JLabel createRightAlignedLabel(String aText)
Creates a JLabel which text is right aligned.

Parameters:
aText - the text of the JLabel to create, may be null.
Returns:
a JLabel instance, never null.

dispose

public static void dispose(Window aWindow)
Closes and disposes a given Window.

Parameters:
aWindow - the window to close, if null, this method doesn't do anything.

drawArrowHead

public static final void drawArrowHead(Graphics2D aG,
                                       int aXpos,
                                       int aYpos,
                                       int aFactor,
                                       int aArrowWidth,
                                       int aArrowHeight)
Draws a single arrow head

Parameters:
aG - the canvas to draw on;
aXpos - the X position of the arrow head;
aYpos - the (center) Y position of the arrow head;
aFactor - +1 to have a left-facing arrow head, -1 to have a right-facing arrow head;
aArrowWidth - the total width of the arrow head;
aArrowHeight - the total height of the arrow head.

drawDoubleHeadedArrow

public static final void drawDoubleHeadedArrow(Graphics aG,
                                               int aX1,
                                               int aY,
                                               int aX2)
Draws a double headed arrow of 8x8.

Parameters:
aG - the canvas to draw on;
aX1 - the starting X position of the arrow;
aY - the starting Y position of the arrow;
aX2 - the ending X position of the arrow.

drawDoubleHeadedArrow

public static final void drawDoubleHeadedArrow(Graphics aG,
                                               int aX1,
                                               int aY1,
                                               int aX2,
                                               int aY2)
Draws a double headed arrow of 8x8.

Parameters:
aG - the canvas to draw on;
aX1 - the starting X position of the arrow;
aY1 - the starting Y position of the arrow;
aX2 - the ending X position of the arrow;
aY2 - the ending Y position of the arrow.

drawDoubleHeadedArrow

public static final void drawDoubleHeadedArrow(Graphics aG,
                                               int aX1,
                                               int aY1,
                                               int aX2,
                                               int aY2,
                                               int aArrowWidth,
                                               int aArrowHeight)
Draws a double headed arrow with arrow heads of a given width and height.

Parameters:
aG - the canvas to draw on;
aX1 - the starting X position of the arrow;
aY1 - the starting Y position of the arrow;
aX2 - the ending X position of the arrow;
aY2 - the ending Y position of the arrow;
aArrowWidth - the total width of the arrow head;
aArrowHeight - the total height of the arrow head.

fixLabelWidth

public static final void fixLabelWidth(JLabel aLabel,
                                       String aMinimalText)
"Fixates" the preferred width of the given label to the given text.

Parameters:
aLabel - the label to fixate, cannot be null;
aMinimalText - the text to use as minimal width indicator.

getAncestorOfClass

public static <T> T getAncestorOfClass(Class<T> aType,
                                       Component aComponent)
Convenience method for searching above the given component in the component hierarchy and returns the first object of the given type it finds, or null if no such parent was found.

The reason this method exists is for tidyness of the calling code, as no longer a explicit cast is needed.

Parameters:
aType - the type of the parent to find, cannot be null;
aComponent - the component to search in the hierarchy, cannot be null.
Returns:
the requested ancestor, or null if not found.
See Also:
SwingUtilities.getAncestorOfClass(Class, Component)

getCurrentWindow

public static final Window getCurrentWindow()
Tries to find the current focused window.

Returns:
the current focused window, or null if no such window could be found.

getDeepestComponentAt

public static JComponent getDeepestComponentAt(Component aParent,
                                               int aXpos,
                                               int aYpos)
Returns the deepest visible descendent Component of parent that contains the location x, y. If parent does not contain the specified location, then null is returned. If parent is not a container, or none of parent's visible descendents contain the specified location, parent is returned.

Parameters:
aParent - the root component to begin the search
aXpos - the x target location
aYpos - the y target location

getDeepestComponentAt

public static JComponent getDeepestComponentAt(MouseEvent aEvent)
Returns the deepest visible descendent Component of parent that contains the location x, y. If parent does not contain the specified location, then null is returned. If parent is not a container, or none of parent's visible descendents contain the specified location, parent is returned.

Parameters:
aParent - the root component to begin the search
aXpos - the x target location
aYpos - the y target location

getMenuShortcutKeyMask

public static final int getMenuShortcutKeyMask()
Returns the key mask of the menu shortcut key.

Returns:
a key mask, >= 0.

getOwningWindow

public static final Window getOwningWindow(AWTEvent aEvent)
Tries to find the owning window for the AWT-event's source.

Parameters:
aEvent - the AWT event to find the owning window for, may be null.
Returns:
the owning window, or null if no such window could be found, or a null event was given.

getOwningWindow

public static final Window getOwningWindow(Component aComponent)
Tries to find the owning window for the given component.

Parameters:
aComponent - the AWT event to find the owning window for, may be null.
Returns:
the owning window, or null if no such window could be found, or a null component was given.

getStringWidth

public static int getStringWidth(Font aFont,
                                 String aString)
Returns the string width for a given Font and string.

Parameters:
aFont - the font to create the string width;
aString - the string to get the width for.
Returns:
a string width, >= 0.

getStringWidth

public static int getStringWidth(String aString)
Returns the string width for the default label font and string.

Parameters:
aString - the string to get the width for.
Returns:
a string width, >= 0.

invokeOnEDT

public static void invokeOnEDT(Runnable aRunnable)
Similar as to SwingUtilities.invokeLater(Runnable), but does a check first if the current running thread is already the EDT. If so, it will directly call the Runnable.run() method, otherwise leave it up to SwingUtilities.invokeLater(Runnable) to invoke it on a later moment.

Parameters:
aRunnable - the runnable to call on the EDT, cannot be null.

isActivelyShown

public static final boolean isActivelyShown(Component aComponent)
Returns whether the given component is "actively" shown in screen, that is, it or any of its ancestors is focused.

Parameters:
aComponent - the component to determine whether it is actively shown on screen, may be null.
Returns:
true if the given component is actively shown, false otherwise.

loadWindowState

public static void loadWindowState(org.osgi.service.prefs.Preferences aProperties,
                                   Window aWindow)
Tries to load/restore the window state of the given window.

Parameters:
aProperties - the properties to read from;
aWindow - the window to load the state for.

registerKeyBinding

public static void registerKeyBinding(JComponent aComponent,
                                      char aKey,
                                      Action aAction)
Registers a given keystroke to invoke a given action on the given component.

Parameters:
aComponent - the component to register the keystroke for;
aKeyStroke - the keystroke (as plain char) to register;
aAction - the action to invoke when the keystroke is typed.

registerKeyBinding

public static void registerKeyBinding(JComponent aComponent,
                                      KeyStroke aKeyStroke,
                                      Action aAction)
Registers a given keystroke to invoke a given action on the given component.

Parameters:
aComponent - the component to register the keystroke for;
aKeyStroke - the keystroke to register;
aAction - the action to invoke when the keystroke is typed.

registerKeyBinding

public static void registerKeyBinding(JComponent aComponent,
                                      String aKeyStroke,
                                      Action aAction)
Registers a given keystroke to invoke a given action on the given component.

Parameters:
aComponent - the component to register the keystroke for;
aKeyStroke - the keystroke (as String) to register;
aAction - the action to invoke when the keystroke is typed.

registerKeystroke

public static void registerKeystroke(JComponent aComponent,
                                     Action aAction,
                                     String aCommandName)
Registers the keystroke of the given action as "command" of the given component.

This code is based on the Sulky-tools, found at <http://github.com/huxi/sulky>.

Parameters:
aComponent - the component that should react on the keystroke, cannot be null;
aAction - the action of the keystroke, cannot be null;
aCommandName - the name of the command to register the keystore under.

saveWindowState

public static void saveWindowState(org.osgi.service.prefs.Preferences aProperties,
                                   Window aWindow)
Saves the window state to the given properties map.

Parameters:
aProperties - the properties to fill;
aWindow - the window to save the state for.

setupDialogContentPane

public static void setupDialogContentPane(JDialog aDialog,
                                          Component aCenterComponent,
                                          Component aButtonPane)
Sets up the given dialog's content pane by setting its border to provide a good default spacer, and setting the content pane to the given components.

Parameters:
aDialog - the dialog to setup, cannot be null;
aCenterComponent - the component that should appear at the center of the dialog;
aButtonPane - the component that should appear at the bottom of the dialog (typically the buttons).

setupDialogContentPane

public static void setupDialogContentPane(JDialog aDialog,
                                          Component aCenterComponent,
                                          Component aButtonPane,
                                          JButton defaultButton)
Sets up the given dialog's content pane by setting its border to provide a good default spacer, and setting the content pane to the given components.

Parameters:
aDialog - the dialog to setup, cannot be null;
aCenterComponent - the component that should appear at the center of the dialog;
aButtonPane - the component that should appear at the bottom of the dialog
defaultButton - the default button for this dialog; can be null for "none".
See Also:
JRootPane.setDefaultButton(javax.swing.JButton)

showFileOpenDialog

public static final File showFileOpenDialog(Window aOwner,
                                            FileFilter... aFileFilters)
Shows a file-open selection dialog for the current working directory.

Parameters:
aOwner - the owning window to show the dialog in.
Returns:
the selected file, or null if the user aborted the dialog.

showFileOpenDialog

public static final File showFileOpenDialog(Window aOwner,
                                            String aCurrentDirectory,
                                            FileFilter... aFileFilters)
Shows a file-open selection dialog for the given working directory.

Parameters:
aOwner - the owning window to show the dialog in;
aCurrentDirectory - the working directory to start the dialog in, can be null.
Returns:
the selected file, or null if the user aborted the dialog.

showFileSaveDialog

public static final File showFileSaveDialog(Window aOwner,
                                            FileFilter... aFileFilters)
Shows a file-save selection dialog for the current working directory.

Parameters:
aOwner - the owning window to show the dialog in.
Returns:
the selected file, or null if the user aborted the dialog.

showFileSaveDialog

public static final File showFileSaveDialog(Window aOwner,
                                            String aCurrentDirectory,
                                            FileFilter... aFileFilters)
Shows a file-save selection dialog for the given working directory.

Parameters:
aOwner - the owning window to show the dialog in;
aCurrentDirectory - the working directory to start the dialog in, can be null.
Returns:
the selected file, or null if the user aborted the dialog.

showFileSelectionDialog

public static final File showFileSelectionDialog(Window aOwner,
                                                 FileFilter... aFileFilters)
Shows a file selection dialog for the current working directory.

Parameters:
aOwner - the owning window to show the dialog in.
Returns:
the selected file, or null if the user aborted the dialog.

showFileSelectionDialog

public static final File showFileSelectionDialog(Window aOwner,
                                                 String aCurrentDirectory,
                                                 FileFilter... aFileFilters)
Shows a file selection dialog for the given working directory.

Parameters:
aOwner - the owning window to show the dialog in;
aCurrentDirectory - the working directory to start the dialog in, can be null.
Returns:
the selected file, or null if the user aborted the dialog.

toCssString

public static String toCssString(Font aFont)
Converts a given font to a font-clause that can be used in a CSS-file.

Parameters:
aFont - the font convert to CSS, cannot be null.
Returns:
a CSS clause for the given font, never null.
Throws:
IllegalArgumentException - in case the given font was null.


Copyright © 2012 L'Xtreme IT consultancy. All Rights Reserved.