/*
* JFreeChart : a free chart library for the Java(tm) platform
*
*
* (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library 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 Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* -------------
* SWTUtils.java
* -------------
* (C) Copyright 2006, 2007, by Henry Proudhon and Contributors.
*
* Original Author: Henry Proudhon (henry.proudhon AT ensmp.fr);
* Contributor(s): Rainer Blessing;
* David Gilbert (david.gilbert@object-refinery.com);
* Christoph Beck.
*
* Changes
* -------
* 01-Aug-2006 : New class (HP);
* 16-Jan-2007 : Use FontData.getHeight() instead of direct field access (RB);
* 31-Jan-2007 : Moved the dummy JPanel from SWTGraphics2D.java,
* added a new convert method for mouse events (HP);
* 12-Jul-2007 : Improved the mouse event conversion with buttons
* and modifiers handling, patch sent by Christoph Beck (HP);
* 27-Aug-2007 : Modified toAwtMouseEvent signature (HP);
* 27-Nov-2007 : Moved convertToSWT() method from SWTGraphics2D and added
* convertAWTImageToSWT() (DG);
* 01-Jul-2008 : Simplify AWT/SWT font style conversions (HP);
*
*/
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.DirectColorModel;
import java.awt.image.IndexColorModel;
import java.awt.image.WritableRaster;
import javax.swing.JPanel;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
/**
* Utility class gathering some useful and general method.
* Mainly convert forth and back graphical stuff between
* awt and swt.
*/
public class SWTUtils {
private final static String Az = "ABCpqr";
/** A dummy JPanel used to provide font metrics. */
protected static final JPanel DUMMY_PANEL = new JPanel();
/**
* 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.
*
* @param device The swt device to draw on (display or gc device).
* @param color The awt color to match.
* @return a swt color object.
*/
public static Color toSwtColor(Device device, java.awt.Color color) {
return new org.eclipse.swt.graphics.Color(device,
color.getRed(), color.getGreen(), color.getBlue());
}
/**
* Transform an awt Rectangle2d instance into a swt one.
* The coordinates are rounded to integer for the swt object.
* @param rect2d The awt rectangle to map.
* @return an swt <code>Rectangle</code> object.
*/
public static Rectangle toSwtRectangle(Rectangle2D rect2d) {
return new Rectangle(
(int) Math.round(rect2d.getMinX()),
(int) Math.round(rect2d.getMinY()),
(int) Math.round(rect2d.getWidth()),
(int) Math.round(rect2d.getHeight()));
}
/**
* Transform a swt Rectangle instance into an awt one.
* @param rect the swt <code>Rectangle</code>
* @return a Rectangle2D.Double instance with
* the eappropriate location and size.
*/
public static Rectangle2D toAwtRectangle(Rectangle rect) {
Rectangle2D rect2d = new Rectangle2D.Double();
rect2d.setRect(rect.x, rect.y, rect.width, rect.height);
return rect2d;
}
/**
* Returns an AWT point with the same coordinates as the specified
* SWT point.
*
* @param p the SWT point (<code>null</code> not permitted).
*
* @return An AWT point with the same coordinates as <code>p</code>.
*
* @see #toSwtPoint(java.awt.Point)
*/
public static Point2D toAwtPoint(Point p) {
return new java.awt.Point(p.x, p.y);
}
/**
* Returns an SWT point with the same coordinates as the specified
* AWT point.
*
* @param p the AWT point (<code>null</code> not permitted).
*
* @return An SWT point with the same coordinates as <code>p</code>.
*
* @see #toAwtPoint(Point)
*/
public static Point toSwtPoint(java.awt.Point p) {
return new Point(p.x, p.y);
}
/**
* Returns an SWT point with the same coordinates as the specified AWT
* point (rounded to integer values).
*
* @param p the AWT point (<code>null</code> not permitted).
*
* @return An SWT point with the same coordinates as <code>p</code>.
*
* @see #toAwtPoint(Point)
*/
public static Point toSwtPoint(java.awt.geom.Point2D p) {
return new Point((int) Math.round(p.getX()),
(int) Math.round(p.getY()));
}
}
|