ehsbe CommonsTM
Version 1.1.0rc3

com.ehsbe.commons.gui
Class Util

java.lang.Object
  extended by com.ehsbe.commons.gui.Util

public class Util
extends Object

Some GUI utils

Version:
$Revision: 462 $
Author:
Jens Elkner

Field Summary
static String HELP_ID_KEY
          the property name for help key IDs used in JComponent Classes
static KeyStroke KEY_F1
          common key to use for displaying help
static KeyStroke KEY_HELP
          common key to use for displaying help
 
Constructor Summary
Util()
           
 
Method Summary
static void center(Component c)
          Set the location of the given component so, that it is centered on the screen.
static Component findFocusOwner(Component comp)
          Get the child component which has focus with respects of specified component.
static String getHelpID(Object comp)
          Convinience method find the help ID for the given component.
static Window getWindowForComponent(Component parentComponent)
          Returns the specified component's toplevel Frame or Dialog.
static void makeTreeCheckable(JTree tree)
          Convinience method, which makes a tree checkable by adding an appropriate mouse listener as well as a default tree cell renderer and key listener.
static void registerKbdAction(KeyStroke ks, Action action, JComponent comp, int condition)
          Register an action to be invoked, if the given component receives an event for the given keystroke and condition.
static void registerTooltipComponent(JComponent comp)
          Workaround for static tooltips - the JComponents original setTooltip does not register a component, if its getTooltip() return a none- null value.
static void restoreLocationAndSize(Component comp, String prefix, Class<?> pkgClass, int width, int height, int x, int y)
          Restore the size and location of the given component from stored user preferences.
static void saveLocationAndSize(Component comp, String prefix, Class<?> pkgClass, Point location)
          Save the location and size of the given component to the user preference node, described by the given class with the given key prefix.
static void setHelpID(Object comp, String id)
          Convinience method to assign a help ID to the specified component.
static void showGeneralError(String msg, Component comp)
          Convinience method to show an general error dialog.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HELP_ID_KEY

public static final String HELP_ID_KEY
the property name for help key IDs used in JComponent Classes

See Also:
Constant Field Values

KEY_F1

public static final KeyStroke KEY_F1
common key to use for displaying help


KEY_HELP

public static final KeyStroke KEY_HELP
common key to use for displaying help

Constructor Detail

Util

public Util()
Method Detail

getWindowForComponent

public static final Window getWindowForComponent(Component parentComponent)
                                          throws HeadlessException
Returns the specified component's toplevel Frame or Dialog.

Parameters:
parentComponent - the Component to check for a Frame or Dialog
Returns:
the Frame or Dialog that contains the component, or the default frame if the component is null, or does not have a valid Frame or Dialog parent
Throws:
HeadlessException - if GraphicsEnvironment.isHeadless returns true
See Also:
GraphicsEnvironment.isHeadless()

saveLocationAndSize

public static final void saveLocationAndSize(Component comp,
                                             String prefix,
                                             Class<?> pkgClass,
                                             Point location)
Save the location and size of the given component to the user preference node, described by the given class with the given key prefix.

Parameters:
comp - information source. Ignored if null.
prefix - preference key prefix to use.
pkgClass - class to use for obtaining user preferences.
location - use this location, if the component is not visible anymore (in which case one can not determine the location on the screen anymore). Ignored if null.

restoreLocationAndSize

public static final void restoreLocationAndSize(Component comp,
                                                String prefix,
                                                Class<?> pkgClass,
                                                int width,
                                                int height,
                                                int x,
                                                int y)
Restore the size and location of the given component from stored user preferences.

Parameters:
comp - component to size and position on the screen
prefix - preference key prefix to use.
pkgClass - class to use for obtaining user preferences.
width - default width to use, if there is no appropriate key or the stored value is < 10
height - default height to use, if there is no appropriate key or the stored value is < 10
x - default x position on the screen to set, if there is no appropriate key
y - default y position on the screen to set, if there is no appropriate key

center

public static void center(Component c)
Set the location of the given component so, that it is centered on the screen.

Parameters:
c - component to center. Ignored if null.

registerKbdAction

public static void registerKbdAction(KeyStroke ks,
                                     Action action,
                                     JComponent comp,
                                     int condition)
Register an action to be invoked, if the given component receives an event for the given keystroke and condition.

Does nothing, if the given keystroke or component is null.

The keystroke and action are registered with the action's command key value.

Parameters:
ks - keystroke to register
action - action to associate with the given keystroke. If null the action for the given keystroke is removed from the components action map and the keystroke is removed from the components input map.
comp - where to bind the keystroke
condition - on which condition the action should be fired. Values are the same as for JComponent.getInputMap(int)
Throws:
IllegalArgumentException - if condition has an invalid value

setHelpID

public static void setHelpID(Object comp,
                             String id)
Convinience method to assign a help ID to the specified component.

Does nothing, if the specified component is null. If the given component is a JComponent, the help ID gets put into the client property map with the key HELP_ID_KEY. Otherwise it gets put into a static local map of this class.

Parameters:
comp - component to which assign the given id
id - ID to assign. If null, the currently assigned ID (if any) is unassigned wrt. the given component.

getHelpID

public static String getHelpID(Object comp)
Convinience method find the help ID for the given component. If the given component has no assigned help ID, the container hierarchy gets traversed updwards until a component has been found, which has a help ID assigned or has no parent.

One needs to register, i.e. associate the help ID with the component before this works (see setHelpID(Object, String)).

Parameters:
comp - component to use for help ID lookup. Ignored if null.
Returns:
null if no help ID has been found, the help ID otherwise.

findFocusOwner

public static Component findFocusOwner(Component comp)
Get the child component which has focus with respects of specified component.

Parameters:
comp - the component to use as root for the focus lookup
Returns:
null if no focus owning child has been found and the given component has itself not the focus, the focus owning component otherwise.

showGeneralError

public static void showGeneralError(String msg,
                                    Component comp)
Convinience method to show an general error dialog.

Parameters:
msg - message to display
comp - where to anchor the dialog.

makeTreeCheckable

public static void makeTreeCheckable(JTree tree)
Convinience method, which makes a tree checkable by adding an appropriate mouse listener as well as a default tree cell renderer and key listener.

Parameters:
tree - tree to make checkable
See Also:
CheckTreeCellRenderer, CheckableTreeMouseAdapter

registerTooltipComponent

public static final void registerTooltipComponent(JComponent comp)
Workaround for static tooltips - the JComponents original setTooltip does not register a component, if its getTooltip() return a none- null value. So one has the choice to check, whether the shared TooltipManager instance is already listening on the component and if so simply do nothing (this is what this method does) or always register the component on the tooltip manager for each set, which would be a little bit more resource intensive.

Parameters:
comp - component to register with the tooltipmanager, if not already done.

ehsbe CommonsTM
Version 1.1.0rc3

Copyright ©2005-2009 EHSBE, Walther-Rathenau-Str. 58, 39104 Magdeburg, Sachsen-Anhalt, Germany. All Rights Reserved.