gnu.java.awt

Class AWTUtilities


public class AWTUtilities
extends Object

This class mirrors the javax.swing.SwingUtilities class. It provides commonly needed functionalities for AWT classes without the need to reference classes in the javax.swing package.

Method Summary

static Rectangle
calculateInsetArea(Rectangle base, Insets insets, Rectangle ret)
Calculates the portion of the base rectangle which is inside the insets.
static MouseEvent
convertMouseEvent(Component source, MouseEvent sourceEvent, Component destination)
Convert a mouse event which refrers to one component to another.
static Point
convertPoint(Component source, int x, int y, Component destination)
Converts a point (x,y) from the coordinate space of one component to another.
static void
convertPointFromScreen(Point p, Component c)
Converts a point from "screen" coordinates (such as the coordinate space mouse events are delivered in) to a component's local coordinate space.
static void
convertPointToScreen(Point p, Component c)
Converts a point from a component's local coordinate space to "screen" coordinates (such as the coordinate space mouse events are delivered in).
static Rectangle
convertRectangle(Component source, Rectangle rect, Component destination)
Converts a rectangle from the coordinate space of one component to another.
static Container
getAncestorNamed(String name, Component comp)
Returns the least ancestor of comp which has the specified name.
static Container
getAncestorOfClass(Class<T> c, Component comp)
Returns the least ancestor of comp which is an instance of the specified class.
static Component
getDeepestComponentAt(Component parent, int x, int y)
Returns the deepest descendent of parent which is both visible and contains the point (x,y).
static FontMetrics
getFontMetrics(Font font)
Returns the font metrics object for a given font.
static Rectangle
getLocalBounds(Component aComponent)
Calculates the bounds of a component in the component's own coordinate space.
static Component
getRoot(Component comp)
Returns the "root" of the component tree containint comp The root is defined as either the least ancestor of comp which is a Window, or the greatest ancestor of comp which is a Applet if no Window ancestors are found.
static List<E>
getVisibleChildren(Container c)
Returns the visible children of a Container.
static void
invokeAndWait(Runnable doRun)
Calls java.awt.EventQueue.invokeAndWait with the specified Runnable.
static void
invokeLater(Runnable doRun)
Calls java.awt.EventQueue.invokeLater with the specified Runnable.
static boolean
isDescendingFrom(Component a, Component b)
Return true if a descends from b, in other words if b is an ancestor of a.
static boolean
isEventDispatchThread()
Calls java.awt.EventQueue.isEventDispatchThread.
static boolean
isValidKey(int keyCode)
Returns whether the specified key code is valid.
static Window
windowForComponent(Component comp)
Equivalent to calling getAncestorOfClass(Window, comp).

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Method Details

calculateInsetArea

public static Rectangle calculateInsetArea(Rectangle base,
                                           Insets insets,
                                           Rectangle ret)
Calculates the portion of the base rectangle which is inside the insets.
Parameters:
base - The rectangle to apply the insets to
insets - The insets to apply to the base rectangle
ret - A rectangle to use for storing the return value, or null
Returns:
The calculated area inside the base rectangle and its insets, either stored in ret or a new Rectangle if ret is null
See Also:
AWTUtilities

convertMouseEvent

public static MouseEvent convertMouseEvent(Component source,
                                           MouseEvent sourceEvent,
                                           Component destination)
Convert a mouse event which refrers to one component to another. This includes changing the mouse event's coordinate space, as well as the source property of the event. If source is null, it is taken to refer to destination's root component. If destination is null, the new event will remain expressed in source's coordinate system.
Parameters:
source - The component the mouse event currently refers to
sourceEvent - The mouse event to convert
destination - The component the new mouse event should refer to
Returns:
A new mouse event expressed in terms of the destination component's coordinate space, and with the destination component as its source

convertPoint

public static Point convertPoint(Component source,
                                 int x,
                                 int y,
                                 Component destination)
Converts a point (x,y) from the coordinate space of one component to another. This is equivalent to converting the point from source space to screen space, then back from screen space to destination space. If exactly one of the two Components is null, it is taken to refer to the root ancestor of the other component. If both are null, no transformation is done.
Parameters:
source - The component which the point is expressed in terms of
x - Horizontal coordinate of point to transform
y - Vertical coordinate of point to transform
destination - The component which the return value will be expressed in terms of
Returns:
The point (x,y) converted from the coordinate space of the source component to the coordinate space of the destination component

convertPointFromScreen

public static void convertPointFromScreen(Point p,
                                          Component c)
Converts a point from "screen" coordinates (such as the coordinate space mouse events are delivered in) to a component's local coordinate space. This operation is equivalent to translating the point by the negation of the component's location (which is the origin of its coordinate space).
Parameters:
p - The point to convert
c - The component which the point should be expressed in terms of

convertPointToScreen

public static void convertPointToScreen(Point p,
                                        Component c)
Converts a point from a component's local coordinate space to "screen" coordinates (such as the coordinate space mouse events are delivered in). This operation is equivalent to translating the point by the location of the component (which is the origin of its coordinate space).
Parameters:
p - The point to convert
c - The component which the point is expressed in terms of
See Also:
convertPointFromScreen

convertRectangle

public static Rectangle convertRectangle(Component source,
                                         Rectangle rect,
                                         Component destination)
Converts a rectangle from the coordinate space of one component to another. This is equivalent to converting the rectangle from source space to screen space, then back from screen space to destination space. If exactly one of the two Components is null, it is taken to refer to the root ancestor of the other component. If both are null, no transformation is done.
Parameters:
source - The component which the rectangle is expressed in terms of
rect - The rectangle to convert
destination - The component which the return value will be expressed in terms of
Returns:
A new rectangle, equal in size to the input rectangle, but with its position converted from the coordinate space of the source component to the coordinate space of the destination component

getAncestorNamed

public static Container getAncestorNamed(String name,
                                         Component comp)
Returns the least ancestor of comp which has the specified name.
Parameters:
name - The name to search for
comp - The component to search the ancestors of
Returns:
The nearest ancestor of comp with the given name, or null if no such ancestor exists

getAncestorOfClass

public static Container getAncestorOfClass(Class<T> c,
                                           Component comp)
Returns the least ancestor of comp which is an instance of the specified class.
Parameters:
c - The class to search for
comp - The component to search the ancestors of
Returns:
The nearest ancestor of comp which is an instance of the given class, or null if no such ancestor exists

getDeepestComponentAt

public static Component getDeepestComponentAt(Component parent,
                                              int x,
                                              int y)
Returns the deepest descendent of parent which is both visible and contains the point (x,y). Returns parent when either parent is not a container, or has no children which contain (x,y). Returns null when either (x,y) is outside the bounds of parent, or parent is null.
Parameters:
parent - The component to search the descendents of
x - Horizontal coordinate to search for
y - Vertical coordinate to search for
Returns:
A component containing (x,y), or null
See Also:
java.awt.Container.findComponentAt

getFontMetrics

public static FontMetrics getFontMetrics(Font font)
Returns the font metrics object for a given font. The metrics can be used to calculate crude bounding boxes and positioning information, for laying out components with textual elements.
Parameters:
font - The font to get metrics for
Returns:
The font's metrics
See Also:
GlyphMetrics

getLocalBounds

public static Rectangle getLocalBounds(Component aComponent)
Calculates the bounds of a component in the component's own coordinate space. The result has the same height and width as the component's bounds, but its location is set to (0,0).
Parameters:
aComponent - The component to measure
Returns:
The component's bounds in its local coordinate space

getRoot

public static Component getRoot(Component comp)
Returns the "root" of the component tree containint comp The root is defined as either the least ancestor of comp which is a Window, or the greatest ancestor of comp which is a Applet if no Window ancestors are found.
Parameters:
comp - The component to search for a root
Returns:
The root of the component's tree, or null

getVisibleChildren

public static List<E> getVisibleChildren(Container c)
Returns the visible children of a Container. This method is commonly needed in LayoutManagers, because they only have to layout the visible children of a Container.
Parameters:
c - the Container from which to extract the visible children
Returns:
the visible children of c

invokeAndWait

public static void invokeAndWait(Runnable doRun)
            throws InterruptedException,
                   InvocationTargetException
Calls java.awt.EventQueue.invokeAndWait with the specified Runnable.

invokeLater

public static void invokeLater(Runnable doRun)
Calls java.awt.EventQueue.invokeLater with the specified Runnable.

isDescendingFrom

public static boolean isDescendingFrom(Component a,
                                       Component b)
Return true if a descends from b, in other words if b is an ancestor of a.
Parameters:
a - The child to search the ancestry of
b - The potential ancestor to search for
Returns:
true if a is a descendent of b, false otherwise

isEventDispatchThread

public static boolean isEventDispatchThread()
Calls java.awt.EventQueue.isEventDispatchThread.

isValidKey

public static boolean isValidKey(int keyCode)
Returns whether the specified key code is valid.

windowForComponent

public static Window windowForComponent(Component comp)
Equivalent to calling getAncestorOfClass(Window, comp).
Parameters:
comp - The component to search for an ancestor window
Returns:
An ancestral window, or null if none exists

AWTUtilities.java -- Common utility methods for AWT and Swing. Copyright (C) 2005, 2007 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.