org.openbp.swing
Class SwingUtil

java.lang.Object
  extended by org.openbp.swing.SwingUtil

public class SwingUtil
extends java.lang.Object

This class contains various static utility methods.


Field Summary
static int BOTTOM
          Flag for drawMultilineString(Graphics, String, int, Rectangle, boolean): Indicates that the text should be displayed at the bottom of the drawing area .
static int CENTER
          Flag for drawMultilineString(Graphics, String, int, Rectangle, boolean): Indicates that the text should be centered.
static int LEFT
          Flag for drawMultilineString(Graphics, String, int, Rectangle, boolean): Indicates that the text should be left justified.
static int MIDDLE
          Flag for drawMultilineString(Graphics, String, int, Rectangle, boolean): Indicates that the text should be displayed at the middle of the drawing area .
static java.lang.String RESOURCE_COMMON
          Name of the resource component for general client resources
static int RIGHT
          Flag for drawMultilineString(Graphics, String, int, Rectangle, boolean): Indicates that the text should be right justified.
static int TOP
          Flag for drawMultilineString(Graphics, String, int, Rectangle, boolean): Indicates that the text should be displayed at the top of the drawing area .
 
Method Summary
static int applyScale(int coordinate, boolean scaleToDoc, Scalable view)
          Applies the current scale factor of the view to a coordinate.
static java.awt.Point applyScale(java.awt.Point p, boolean scaleToDoc, Scalable view)
          Applies the current scale factor of the view to a point.
static java.awt.Rectangle applyScale(java.awt.Rectangle r, boolean scaleToDoc, Scalable view)
          Applies the current scale factor of the view to a rectangle.
static void computeMultilineStringBounds(java.awt.FontMetrics fm, java.lang.String s, int alignment, java.awt.Rectangle r)
          Calculates the dimensions of the text.
static boolean containsListener(javax.swing.event.EventListenerList listenerList, java.lang.Class type, java.util.EventListener listener)
          Checks if the specified listener is already registered as listener of the specified type in a listener list.
static java.awt.Rectangle convertBoundsToGlassCoords(java.awt.Component comp)
          Converts the given, component local rectangle into coordinates relative to an objects glassPane.
static java.awt.Point convertFromGlassCoords(java.awt.Point p, java.awt.Component comp)
          Converts the given, component local coordinates into coordinates relative to an objects glassPane.
static java.awt.Rectangle convertRectToGlassCoords(java.awt.Rectangle r, java.awt.Component comp)
          Converts the component local rectangle into coordinates relative to an objects glassPane.
static java.awt.Point convertToGlassCoords(java.awt.Point p, java.awt.Component comp)
          Converts the given, component local coordinates into coordinates relative to an objects glassPane.
static void drawMultilineString(java.awt.Graphics g, java.lang.String s, int alignment, java.awt.Rectangle r, boolean print)
          Draws a multi line text and/or calculates the dimensions of the text when drawn into the given Graphics object.
static void drawString(java.awt.Graphics g, java.lang.String s, int alignment, java.awt.Rectangle r)
          Draws a string into a Graphics object.
static boolean focusComponent(java.awt.Component comp)
          Tries to focus the given component.
static void forceMouseMove(java.awt.Point pos)
          Forces a mouse movement by 1 pixel to the right and back that will generate mouse movement events.
static java.awt.Dialog getDialog(java.awt.Component c)
          Gets the dialog that contains the given component.
static java.awt.Component getGlassPane(java.awt.Component comp)
          Returns the GlassPane that belongs to a given Component.
static javax.swing.RootPaneContainer getRootPaneContainer(java.awt.Component c)
          Gets the root pane container of a component.
static javax.swing.JScrollPane getScrollPaneAncestor(java.awt.Component c)
          Gets the scroll pane that wraps a given component.
static void inflateRectangle(java.awt.Rectangle r, int x, int y)
          Inflates the rectangle by the specified x/y value.
static void processPendingEvents()
          Processes any events pending in the event queue.
static void show(javax.swing.JDialog dlg)
          Shows a modal dialog.
static void show(javax.swing.JFrame frame)
          Shows a frame.
static void show(javax.swing.JWindow window)
          Shows a window.
static void startApplication(java.awt.Window mainWindow, boolean provideExitHandler)
          Displays the main frame of an application in the center of the screen.
static void waitCursorOff(java.awt.Component comp)
          Turns the waits cursor off.
static void waitCursorOn(java.awt.Component comp)
          Turns the waits cursor on.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

RESOURCE_COMMON

public static final java.lang.String RESOURCE_COMMON
Name of the resource component for general client resources

See Also:
Constant Field Values

LEFT

public static final int LEFT
Flag for drawMultilineString(Graphics, String, int, Rectangle, boolean): Indicates that the text should be left justified.

See Also:
Constant Field Values

CENTER

public static final int CENTER
Flag for drawMultilineString(Graphics, String, int, Rectangle, boolean): Indicates that the text should be centered.

See Also:
Constant Field Values

RIGHT

public static final int RIGHT
Flag for drawMultilineString(Graphics, String, int, Rectangle, boolean): Indicates that the text should be right justified.

See Also:
Constant Field Values

TOP

public static final int TOP
Flag for drawMultilineString(Graphics, String, int, Rectangle, boolean): Indicates that the text should be displayed at the top of the drawing area .

See Also:
Constant Field Values

MIDDLE

public static final int MIDDLE
Flag for drawMultilineString(Graphics, String, int, Rectangle, boolean): Indicates that the text should be displayed at the middle of the drawing area .

See Also:
Constant Field Values

BOTTOM

public static final int BOTTOM
Flag for drawMultilineString(Graphics, String, int, Rectangle, boolean): Indicates that the text should be displayed at the bottom of the drawing area .

See Also:
Constant Field Values
Method Detail

startApplication

public static void startApplication(java.awt.Window mainWindow,
                                    boolean provideExitHandler)
Displays the main frame of an application in the center of the screen. A main method of a primitive test program would consist of just one line:

Parameters:
mainWindow - Main application window (usually a JFrame or JDialog)
provideExitHandler - true Automatically provides a WindowClosing handler that exits the application.
false Does not provide an exit handler.

processPendingEvents

public static void processPendingEvents()
Processes any events pending in the event queue. The method will yield the current thread if there are any events left to process in the system event queue. This will give the event dispatcher thread the opportunity to process the pending events.
However, it is not guaranteed that the event queue will be empty when the method returns.


focusComponent

public static boolean focusComponent(java.awt.Component comp)
Tries to focus the given component. In difference to Component.requestFocus (), the method tries to find a descendant component that is focusable. Note that a true result does merely indicate that a focusable component has been found, not the actual passing of the focus.

Parameters:
comp - The component to receive the focus
Returns:
true if an egligable component has been found
false Otherwise

convertToGlassCoords

public static java.awt.Point convertToGlassCoords(java.awt.Point p,
                                                  java.awt.Component comp)
Converts the given, component local coordinates into coordinates relative to an objects glassPane. Throws an IllegalArgumentException if comp has no RootPaneContainer-ancestor or is null.

Parameters:
p - Coordinates to convert or null for (0/0)
comp - Component the coordinates are relative to
Returns:
The converted coordinates

convertFromGlassCoords

public static java.awt.Point convertFromGlassCoords(java.awt.Point p,
                                                    java.awt.Component comp)
Converts the given, component local coordinates into coordinates relative to an objects glassPane. Throws an IllegalArgumentException if comp has no RootPaneContainer-ancestor or is null.

Parameters:
p - Coordinates to convert or null for (0/0)
comp - Component the coordinates should be relative to
Returns:
The converted coordinates

convertRectToGlassCoords

public static java.awt.Rectangle convertRectToGlassCoords(java.awt.Rectangle r,
                                                          java.awt.Component comp)
Converts the component local rectangle into coordinates relative to an objects glassPane. Throws an IllegalArgumentException if comp has no RootPaneContainer-ancestor or is null.

Parameters:
r - Coordinates to convert
comp - Component the coordinates are relative to
Returns:
The converted coordinates

convertBoundsToGlassCoords

public static java.awt.Rectangle convertBoundsToGlassCoords(java.awt.Component comp)
Converts the given, component local rectangle into coordinates relative to an objects glassPane. Throws an IllegalArgumentException if comp has no RootPaneContainer-ancestor or is null.

Parameters:
comp - Component
Returns:
The converted coordinates

getGlassPane

public static java.awt.Component getGlassPane(java.awt.Component comp)
Returns the GlassPane that belongs to a given Component. Throws an IllegalArgumentException if comp has no RootPaneContainer-ancestor or is null.

Parameters:
comp - Component
Returns:
The glass pane

applyScale

public static java.awt.Rectangle applyScale(java.awt.Rectangle r,
                                            boolean scaleToDoc,
                                            Scalable view)
Applies the current scale factor of the view to a rectangle.

Parameters:
r - rectangle to be scaled
scaleToDoc - true Assumes that the coordinates are component coordinates that should be translated to document coordinates. false Assumes that the coordinates are document coordinates that should be translated to component coordinates.
view - Scalable view that provides the scaling factor
Returns:
The scaled rectangle

applyScale

public static java.awt.Point applyScale(java.awt.Point p,
                                        boolean scaleToDoc,
                                        Scalable view)
Applies the current scale factor of the view to a point.

Parameters:
p - Point to be scaled
scaleToDoc - true Assumes that the coordinates are component coordinates that should be translated to document coordinates. false Assumes that the coordinates are document coordinates that should be translated to component coordinates.
view - Scalable view that provides the scaling factor
Returns:
The scaled point

applyScale

public static int applyScale(int coordinate,
                             boolean scaleToDoc,
                             Scalable view)
Applies the current scale factor of the view to a coordinate.

Parameters:
coordinate - X or Y coordinate to be scaled
scaleToDoc - true Assumes that the coordinates are component coordinates that should be translated to document coordinates. false Assumes that the coordinates are document coordinates that should be translated to component coordinates.
view - Scalable view that provides the scaling factor
Returns:
The resulting coordinate

waitCursorOn

public static void waitCursorOn(java.awt.Component comp)
Turns the waits cursor on. The wait cursor is implemented by showing the glass pane and setting its cursor to the system wait cursor. The current status of the glass pane is saved and can be restored using waitCursorOff(java.awt.Component). Subsequent calls of waitCursorOn do not overwrite the saved state, waitCursorOff will reset the glass pane to the first saved state.

Parameters:
comp - Component that can be used to determine the glass pane

waitCursorOff

public static void waitCursorOff(java.awt.Component comp)
Turns the waits cursor off. This will reset the changes to the glass pane caused by a call to waitCursorOn(java.awt.Component). If there is not information present about the previous state of the glass pane (i. e. because waitCursorOff has already been called), the method will do nothing.

Parameters:
comp - Component that can be used to determine the glass pane

forceMouseMove

public static void forceMouseMove(java.awt.Point pos)
Forces a mouse movement by 1 pixel to the right and back that will generate mouse movement events. This is used in the first line to update mouse position-dependant cursor shapes programatically.

Parameters:
pos - Current mouse positon in screen coordinates

containsListener

public static boolean containsListener(javax.swing.event.EventListenerList listenerList,
                                       java.lang.Class type,
                                       java.util.EventListener listener)
Checks if the specified listener is already registered as listener of the specified type in a listener list.

Parameters:
listenerList - Listener list to search
type - Type of the listener to be added
listener - Listener to be added
Returns:
true The listener is already registered.
false No such listener has been found.

drawMultilineString

public static void drawMultilineString(java.awt.Graphics g,
                                       java.lang.String s,
                                       int alignment,
                                       java.awt.Rectangle r,
                                       boolean print)
Draws a multi line text and/or calculates the dimensions of the text when drawn into the given Graphics object.

Parameters:
g - Graphics context to paint into
s - Text to draw
alignment - Text alignment (LEFT/CENTER/RIGHT | TOP/MIDDLE/BOTTOM)
r - Rectangle containing the drawing region
print - true Draws the text
false Calculates the text dimensions only If r.width > 0 then the text lines are wrapped to this width.
If r.width is 0 or print is false, the method writes the width of the longest line back into r.width.
Note: The height of the text is always written back into r.height!

computeMultilineStringBounds

public static void computeMultilineStringBounds(java.awt.FontMetrics fm,
                                                java.lang.String s,
                                                int alignment,
                                                java.awt.Rectangle r)
Calculates the dimensions of the text.

Parameters:
fm - Font metrics of the current font
s - Text to draw
alignment - Text alignment (LEFT/CENTER/RIGHT | TOP/MIDDLE/BOTTOM)
r - Rectangle containing the drawing region The method writes the width of the longest line back into r.width.
The height of the text is written back into r.height.

drawString

public static void drawString(java.awt.Graphics g,
                              java.lang.String s,
                              int alignment,
                              java.awt.Rectangle r)
Draws a string into a Graphics object.

Parameters:
g - Graphics context to paint into
s - Text to draw
alignment - Text alignment (LEFT/CENTER/RIGHT)
r - Bounding rectangle (width has to be greater 0 when the alignment is CENTER or RIGHT)

show

public static void show(javax.swing.JDialog dlg)
Shows a modal dialog.

Parameters:
dlg - Dialog to display

show

public static void show(javax.swing.JFrame frame)
Shows a frame.

Parameters:
frame - Dialog to display

show

public static void show(javax.swing.JWindow window)
Shows a window.

Parameters:
window - Dialog to display

getRootPaneContainer

public static javax.swing.RootPaneContainer getRootPaneContainer(java.awt.Component c)
Gets the root pane container of a component.

Parameters:
c - The component
Returns:
The container or null

getScrollPaneAncestor

public static javax.swing.JScrollPane getScrollPaneAncestor(java.awt.Component c)
Gets the scroll pane that wraps a given component.

Parameters:
c - Given component
Returns:
The scroll pane or null if the component is not wrapped in a scroll pane

getDialog

public static java.awt.Dialog getDialog(java.awt.Component c)
Gets the dialog that contains the given component.

Parameters:
c - The component
Returns:
The dialog or null the component is not part of a dialog

inflateRectangle

public static void inflateRectangle(java.awt.Rectangle r,
                                    int x,
                                    int y)
Inflates the rectangle by the specified x/y value.

Parameters:
r - Rectangle to enlarge
x - Horizontal value; if negative, the rectangle will be deflated
y - Vertical value; if negative, the rectangle will be deflated


Copyright © 2011. All Rights Reserved.