KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > experimental > swt > SWTUtils


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * -------------
28  * SWTUtils.java
29  * -------------
30  * (C) Copyright 2006, by Henry Proudhon and Contributors.
31  *
32  * Original Author: Henry Proudhon (henry.proudhon AT insa-lyon.fr);
33  * Contributor(s):
34  *
35  * Changes
36  * -------
37  * 1 Aug 2006 : New class (HP);
38  *
39  */

40
41 package org.jfree.experimental.swt;
42
43 import javax.swing.JPanel JavaDoc;
44
45 import org.eclipse.swt.SWT;
46 import org.eclipse.swt.graphics.Color;
47 import org.eclipse.swt.graphics.Device;
48 import org.eclipse.swt.graphics.Font;
49 import org.eclipse.swt.graphics.FontData;
50 import org.eclipse.swt.graphics.GC;
51
52 /**
53  * Utility class gathering some useful and general method.
54  * Mainly convert forth and back graphical stuff between
55  * awt and swt.
56  */

57 public class SWTUtils {
58     
59     private final static String JavaDoc Az = "ABCpqr";
60
61     /**
62      * Create a <code>FontData</code> object which encapsulate
63      * the essential data to create a swt font. The data is taken
64      * from the provided awt Font.
65      * <p>Generally speaking, given a font size, the returned swt font
66      * will display differently on the screen than the awt one.
67      * Because the SWT toolkit use native graphical ressources whenever
68      * it is possible, this fact is plateform dependent. To address
69      * this issue, it is possible to enforce the method to return
70      * a font with the same size (or at least as close as possible)
71      * as the awt one.
72      * <p>When the object is no more used, the user must explicitely
73      * call the dispose method on the returned font to free the
74      * operating system resources (the garbage collector won't do it).
75      *
76      * @param device The swt device to draw on (display or gc device).
77      * @param font The awt font from which to get the data.
78      * @param ensureSameSize A boolean used to enforce the same size
79      * (in pixels) between the awt font and the newly created swt font.
80      * @return a <code>FontData</code> object.
81      */

82     public static FontData toSwtFontData(Device device, java.awt.Font JavaDoc font,
83             boolean ensureSameSize) {
84         FontData fontData = new FontData();
85         fontData.setName(font.getFamily());
86         int style = SWT.NORMAL;
87         switch (font.getStyle()) {
88             case java.awt.Font.PLAIN:
89                 style |= SWT.NORMAL;
90                 break;
91             case java.awt.Font.BOLD:
92                 style |= SWT.BOLD;
93                 break;
94             case java.awt.Font.ITALIC:
95                 style |= SWT.ITALIC;
96                 break;
97             case (java.awt.Font.ITALIC + java.awt.Font.BOLD):
98                 style |= SWT.ITALIC | SWT.BOLD;
99                 break;
100         }
101         fontData.setStyle(style);
102         // convert the font size (in pt for awt) to height in pixels for swt
103
int height = (int) Math.round(font.getSize() * 72.0
104                 / device.getDPI().y);
105         fontData.setHeight(height);
106         // hack to ensure the newly created swt fonts will be rendered with the
107
// same height as the awt one
108
if (ensureSameSize) {
109             GC tmpGC = new GC(device);
110             JPanel JavaDoc DUMMY_PANEL = new JPanel JavaDoc();
111             Font tmpFont = new Font(device, fontData);
112             tmpGC.setFont(tmpFont);
113             if (tmpGC.textExtent(Az).x
114                     > DUMMY_PANEL.getFontMetrics(font).stringWidth(Az)) {
115                 while (tmpGC.textExtent(Az).x
116                         > DUMMY_PANEL.getFontMetrics(font).stringWidth(Az)) {
117                     tmpFont.dispose();
118                     height--;
119                     fontData.setHeight(height);
120                     tmpFont = new Font(device, fontData);
121                     tmpGC.setFont(tmpFont);
122                 }
123             }
124             else if (tmpGC.textExtent(Az).x
125                     < DUMMY_PANEL.getFontMetrics(font).stringWidth(Az)) {
126                 while (tmpGC.textExtent(Az).x
127                         < DUMMY_PANEL.getFontMetrics(font).stringWidth(Az)) {
128                     tmpFont.dispose();
129                     height++;
130                     fontData.setHeight(height);
131                     tmpFont = new Font(device, fontData);
132                     tmpGC.setFont(tmpFont);
133                 }
134             }
135             tmpFont.dispose();
136             tmpGC.dispose();
137         }
138         return fontData;
139     }
140     
141     /**
142      * Create an awt font by converting as much information
143      * as possible from the provided swt <code>FontData</code>.
144      * <p>Generally speaking, given a font size, an swt font will
145      * display differently on the screen than the corresponding awt
146      * one. Because the SWT toolkit use native graphical ressources whenever
147      * it is possible, this fact is plateform dependent. To address
148      * this issue, it is possible to enforce the method to return
149      * an awt font with the same height as the swt one.
150      *
151      * @param device The swt device being drawn on (display or gc device).
152      * @param fontData The swt font to convert.
153      * @param ensureSameSize A boolean used to enforce the same size
154      * (in pixels) between the swt font and the newly created awt font.
155      * @return An awt font converted from the provided swt font.
156      */

157     public static java.awt.Font JavaDoc toAwtFont(Device device, FontData fontData,
158             boolean ensureSameSize) {
159         int style;
160         switch (fontData.getStyle()) {
161             case SWT.NORMAL:
162                 style = java.awt.Font.PLAIN;
163                 break;
164             case SWT.ITALIC:
165                 style = java.awt.Font.ITALIC;
166                 break;
167             case SWT.BOLD:
168                 style = java.awt.Font.BOLD;
169                 break;
170             default:
171                 style = java.awt.Font.PLAIN;
172                 break;
173         }
174         int height = (int) Math.round(fontData.height * device.getDPI().y
175                 / 72.0);
176         // hack to ensure the newly created awt fonts will be rendered with the
177
// same height as the swt one
178
if (ensureSameSize) {
179             GC tmpGC = new GC(device);
180             Font tmpFont = new Font(device, fontData);
181             tmpGC.setFont(tmpFont);
182             JPanel JavaDoc DUMMY_PANEL = new JPanel JavaDoc();
183             java.awt.Font JavaDoc tmpAwtFont = new java.awt.Font JavaDoc(fontData.getName(),
184                     style, height);
185             if (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az)
186                     > tmpGC.textExtent(Az).x) {
187                 while (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az)
188                         > tmpGC.textExtent(Az).x) {
189                     height--;
190                     tmpAwtFont = new java.awt.Font JavaDoc(fontData.getName(), style,
191                             height);
192                 }
193             }
194             else if (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az)
195                     < tmpGC.textExtent(Az).x) {
196                 while (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az)
197                         < tmpGC.textExtent(Az).x) {
198                     height++;
199                     tmpAwtFont = new java.awt.Font JavaDoc(fontData.getName(), style,
200                             height);
201                 }
202             }
203             tmpFont.dispose();
204             tmpGC.dispose();
205         }
206         return new java.awt.Font JavaDoc(fontData.getName(), style, height);
207     }
208
209     /**
210      * Create an awt font by converting as much information
211      * as possible from the provided swt <code>Font</code>.
212      *
213      * @param device The swt device to draw on (display or gc device).
214      * @param font The swt font to convert.
215      * @return An awt font converted from the provided swt font.
216      */

217     public static java.awt.Font JavaDoc toAwtFont(Device device, Font font) {
218         FontData fontData = font.getFontData()[0];
219         return toAwtFont(device, fontData, true);
220     }
221
222     /**
223      * Creates an awt color instance to match the rgb values
224      * of the specified swt color.
225      *
226      * @param color The swt color to match.
227      * @return an awt color abject.
228      */

229     public static java.awt.Color JavaDoc toAwtColor(Color color) {
230         return new java.awt.Color JavaDoc(color.getRed(), color.getGreen(),
231                 color.getBlue());
232     }
233     
234     /**
235      * Creates a swt color instance to match the rgb values
236      * of the specified awt paint. For now, this method test
237      * if the paint is a color and then return the adequate
238      * swt color. Otherwise plain black is assumed.
239      *
240      * @param device The swt device to draw on (display or gc device).
241      * @param paint The awt color to match.
242      * @return a swt color object.
243      */

244     public static Color toSwtColor(Device device, java.awt.Paint JavaDoc paint) {
245         java.awt.Color JavaDoc color;
246         if (paint instanceof java.awt.Color JavaDoc) {
247             color = (java.awt.Color JavaDoc) paint;
248         }
249         else {
250             try {
251                 throw new Exception JavaDoc("only color is supported at present... "
252                         + "setting paint to uniform black color" );
253             }
254             catch (Exception JavaDoc e) {
255                 e.printStackTrace();
256                 color = new java.awt.Color JavaDoc(0, 0, 0);
257             }
258         }
259         return new org.eclipse.swt.graphics.Color(device,
260                 color.getRed(), color.getGreen(), color.getBlue());
261     }
262
263     /**
264      * Creates a swt color instance to match the rgb values
265      * of the specified awt color. alpha channel is not supported.
266      * Note that the dispose method will need to be called on the
267      * returned object.
268      *
269      * @param device The swt device to draw on (display or gc device).
270      * @param color The awt color to match.
271      * @return a swt color object.
272      */

273     public static Color toSwtColor(Device device, java.awt.Color JavaDoc color) {
274         return new org.eclipse.swt.graphics.Color(device,
275                 color.getRed(), color.getGreen(), color.getBlue());
276     }
277 }
278
Popular Tags