org.jfree.experimental.swt
Class SWTUtils

java.lang.Object
  extended by org.jfree.experimental.swt.SWTUtils

public class SWTUtils
extends java.lang.Object

Utility class gathering some useful and general method. Mainly convert forth and back graphical stuff between awt and swt.


Field Summary
protected static javax.swing.JPanel DUMMY_PANEL
          A dummy JPanel used to provide font metrics.
 
Constructor Summary
SWTUtils()
           
 
Method Summary
static java.awt.Color toAwtColor(Color color)
          Creates an awt color instance to match the rgb values of the specified swt color.
static java.awt.Font toAwtFont(Device device, Font font)
          Create an awt font by converting as much information as possible from the provided swt Font.
static java.awt.Font toAwtFont(Device device, FontData fontData, boolean ensureSameSize)
          Create an awt font by converting as much information as possible from the provided swt FontData.
static java.awt.event.MouseEvent toAwtMouseEvent(org.eclipse.swt.events.MouseEvent event)
          Creates an AWT MouseEvent from a swt event.
static java.awt.geom.Point2D toAwtPoint(Point p)
          Returns an AWT point with the same coordinates as the specified SWT point.
static java.awt.geom.Rectangle2D toAwtRectangle(Rectangle rect)
          Transform a swt Rectangle instance into an awt one.
static Color toSwtColor(Device device, java.awt.Color color)
          Creates a swt color instance to match the rgb values of the specified awt color. alpha channel is not supported.
static Color toSwtColor(Device device, java.awt.Paint paint)
          Creates a swt color instance to match the rgb values of the specified awt paint.
static FontData toSwtFontData(Device device, java.awt.Font font, boolean ensureSameSize)
          Create a FontData object which encapsulate the essential data to create a swt font.
static Point toSwtPoint(java.awt.Point p)
          Returns an SWT point with the same coordinates as the specified AWT point.
static Point toSwtPoint(java.awt.geom.Point2D p)
          Returns an SWT point with the same coordinates as the specified AWT point (rounded to integer values).
static Rectangle toSwtRectangle(java.awt.geom.Rectangle2D rect2d)
          Transform an awt Rectangle2d instance into a swt one.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DUMMY_PANEL

protected static final javax.swing.JPanel DUMMY_PANEL
A dummy JPanel used to provide font metrics.

Constructor Detail

SWTUtils

public SWTUtils()
Method Detail

toSwtFontData

public static FontData toSwtFontData(Device device,
                                     java.awt.Font font,
                                     boolean ensureSameSize)
Create a FontData object which encapsulate the essential data to create a swt font. The data is taken from the provided awt Font.

Generally speaking, given a font size, the returned swt font will display differently on the screen than the awt one. Because the SWT toolkit use native graphical resources whenever it is possible, this fact is platform dependent. To address this issue, it is possible to enforce the method to return a font with the same size (or at least as close as possible) as the awt one.

When the object is no more used, the user must explicitly call the dispose method on the returned font to free the operating system resources (the garbage collector won't do it).

Parameters:
device - The swt device to draw on (display or gc device).
font - The awt font from which to get the data.
ensureSameSize - A boolean used to enforce the same size (in pixels) between the awt font and the newly created swt font.
Returns:
a FontData object.

toAwtFont

public static java.awt.Font toAwtFont(Device device,
                                      FontData fontData,
                                      boolean ensureSameSize)
Create an awt font by converting as much information as possible from the provided swt FontData.

Generally speaking, given a font size, an swt font will display differently on the screen than the corresponding awt one. Because the SWT toolkit use native graphical ressources whenever it is possible, this fact is platform dependent. To address this issue, it is possible to enforce the method to return an awt font with the same height as the swt one.

Parameters:
device - The swt device being drawn on (display or gc device).
fontData - The swt font to convert.
ensureSameSize - A boolean used to enforce the same size (in pixels) between the swt font and the newly created awt font.
Returns:
An awt font converted from the provided swt font.

toAwtFont

public static java.awt.Font toAwtFont(Device device,
                                      Font font)
Create an awt font by converting as much information as possible from the provided swt Font.

Parameters:
device - The swt device to draw on (display or gc device).
font - The swt font to convert.
Returns:
An awt font converted from the provided swt font.

toAwtColor

public static java.awt.Color toAwtColor(Color color)
Creates an awt color instance to match the rgb values of the specified swt color.

Parameters:
color - The swt color to match.
Returns:
an awt color abject.

toSwtColor

public static Color toSwtColor(Device device,
                               java.awt.Paint paint)
Creates a swt color instance to match the rgb values of the specified awt paint. For now, this method test if the paint is a color and then return the adequate swt color. Otherwise plain black is assumed.

Parameters:
device - The swt device to draw on (display or gc device).
paint - The awt color to match.
Returns:
a swt color object.

toSwtColor

public static Color toSwtColor(Device device,
                               java.awt.Color color)
Creates a swt color instance to match the rgb values of the specified awt color. alpha channel is not supported. Note that the dispose method will need to be called on the returned object.

Parameters:
device - The swt device to draw on (display or gc device).
color - The awt color to match.
Returns:
a swt color object.

toSwtRectangle

public static Rectangle toSwtRectangle(java.awt.geom.Rectangle2D rect2d)
Transform an awt Rectangle2d instance into a swt one. The coordinates are rounded to integer for the swt object.

Parameters:
rect2d - The awt rectangle to map.
Returns:
an swt Rectangle object.

toAwtRectangle

public static java.awt.geom.Rectangle2D toAwtRectangle(Rectangle rect)
Transform a swt Rectangle instance into an awt one.

Parameters:
rect - the swt Rectangle
Returns:
a Rectangle2D.Double instance with the eappropriate location and size.

toAwtPoint

public static java.awt.geom.Point2D toAwtPoint(Point p)
Returns an AWT point with the same coordinates as the specified SWT point.

Parameters:
p - the SWT point (null not permitted).
Returns:
An AWT point with the same coordinates as p.
See Also:
toSwtPoint(java.awt.Point)

toSwtPoint

public static Point toSwtPoint(java.awt.Point p)
Returns an SWT point with the same coordinates as the specified AWT point.

Parameters:
p - the AWT point (null not permitted).
Returns:
An SWT point with the same coordinates as p.
See Also:
toAwtPoint(Point)

toSwtPoint

public static Point toSwtPoint(java.awt.geom.Point2D p)
Returns an SWT point with the same coordinates as the specified AWT point (rounded to integer values).

Parameters:
p - the AWT point (null not permitted).
Returns:
An SWT point with the same coordinates as p.
See Also:
toAwtPoint(Point)

toAwtMouseEvent

public static java.awt.event.MouseEvent toAwtMouseEvent(org.eclipse.swt.events.MouseEvent event)
Creates an AWT MouseEvent from a swt event. This method helps passing SWT mouse event to awt components.

Parameters:
event - The swt event.
Returns:
A AWT mouse event based on the given SWT event.