KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > forms > util > AbstractUnitConverter


1 /*
2  * Copyright (c) 2003 JGoodies Karsten Lentzsch. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * o Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * o Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * o Neither the name of JGoodies Karsten Lentzsch nor the names of
15  * its contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.jgoodies.forms.util;
32
33 import java.awt.Component JavaDoc;
34 import java.awt.FontMetrics JavaDoc;
35 import java.awt.Toolkit JavaDoc;
36
37 /**
38  * An abstract implementation of the {@link UnitConverter} that minimizes
39  * the effort required to convert font-dependent sizes to pixels.
40  *
41  * @author Karsten Lentzsch
42  * @version $Revision: 1.2 $
43  * @see DefaultUnitConverter
44  * @see com.jgoodies.forms.layout.Size
45  * @see com.jgoodies.forms.layout.Sizes
46  */

47 abstract public class AbstractUnitConverter implements UnitConverter {
48
49     private static final int DTP_RESOLUTION = 72;
50     
51
52     // Unit Converter Implementation *********************************************
53

54     /**
55      * Converts Inches and answers pixels using the specified resolution.
56      *
57      * @param in the Inches
58      * @param component the component that provides the graphics object
59      * @return the given Inches as pixels
60      */

61     public int inchAsPixel(double in, Component JavaDoc component) {
62         return inchAsPixel(in, getScreenResolution(component));
63     }
64
65     /**
66      * Converts Millimeters and answers pixels using the resolution of the
67      * given component's graphics object.
68      *
69      * @param mm Millimeters
70      * @param component the component that provides the graphics object
71      * @return the given Millimeters as pixels
72      */

73     public int millimeterAsPixel(double mm, Component JavaDoc component) {
74         return millimeterAsPixel(mm, getScreenResolution(component));
75     }
76
77     /**
78      * Converts Centimeters and answers pixels using the resolution of the
79      * given component's graphics object.
80      *
81      * @param cm Centimeters
82      * @param component the component that provides the graphics object
83      * @return the given Centimeters as pixels
84      */

85     public int centimeterAsPixel(double cm, Component JavaDoc component) {
86         return centimeterAsPixel(cm, getScreenResolution(component));
87     }
88
89     /**
90      * Converts DTP Points and answers pixels using the resolution of the
91      * given component's graphics object.
92      *
93      * @param pt DTP Points
94      * @param component the component that provides the graphics object
95      * @return the given Points as pixels
96      */

97     public int pointAsPixel(int pt, Component JavaDoc component) {
98         return pointAsPixel(pt, getScreenResolution(component));
99     }
100     
101     /**
102      * Converts horizontal dialog units and answers pixels.
103      * Honors the resolution, dialog font size, platform, and l&f.
104      *
105      * @param dluX the horizontal dialog units
106      * @param c a Component that provides the font and graphics
107      * @return the given horizontal dialog units as pixels
108      */

109     public int dialogUnitXAsPixel(int dluX, Component JavaDoc c) {
110         return dialogUnitXAsPixel(dluX, getDialogBaseUnitsX(c));
111     }
112     
113     /**
114      * Converts vertical dialog units and answers pixels.
115      * Honors the resolution, dialog font size, platform, and l&f.
116      *
117      * @param dluY the vertical dialog units
118      * @param c a Component that provides the font and graphics
119      * @return the given vertical dialog units as pixels
120      */

121     public int dialogUnitYAsPixel(int dluY, Component JavaDoc c) {
122         return dialogUnitYAsPixel(dluY, getDialogBaseUnitsY(c));
123     }
124     
125     
126     // Abstract Behavior *****************************************************
127

128     /**
129      * Gets and answers the horizontal dialog base units.
130      * Implementations are encouraged to cache previously computed
131      * dialog base units.
132      *
133      * @param component a Component that provides the font and graphics
134      * @return the horizontal dialog base units
135      */

136     abstract protected double getDialogBaseUnitsX(Component JavaDoc component);
137     
138     /**
139      * Gets and answers the vertical dialog base units.
140      * Implementations are encouraged to cache previously computed
141      * dialog base units.
142      *
143      * @param component a Component that provides the font and graphics
144      * @return the vertical dialog base units
145      */

146     abstract protected double getDialogBaseUnitsY(Component JavaDoc component);
147     
148     
149     // Convenience Methods ***************************************************
150

151     /**
152      * Converts Inches and answers pixels using the specified resolution.
153      *
154      * @param in the Inches
155      * @param dpi the resolution
156      * @return the given Inches as pixels
157      */

158     protected final int inchAsPixel(double in, int dpi) {
159         return (int) Math.round(dpi * in);
160     }
161
162     /**
163      * Converts Millimeters and answers pixels using the specified resolution.
164      *
165      * @param mm Millimeters
166      * @param dpi the resolution
167      * @return the given Millimeters as pixels
168      */

169     protected final int millimeterAsPixel(double mm, int dpi) {
170         return (int) Math.round(dpi * mm * 10 / 254);
171     }
172
173     /**
174      * Converts Centimeters and answers pixels using the specified resolution.
175      *
176      * @param cm Centimeters
177      * @param dpi the resolution
178      * @return the given Centimeters as pixels
179      */

180     protected final int centimeterAsPixel(double cm, int dpi) {
181         return (int) Math.round(dpi * cm * 100 / 254);
182     }
183
184     /**
185      * Converts DTP Points and answers pixels using the specified resolution.
186      *
187      * @param pt DTP Points
188      * @param dpi the resolution in dpi
189      * @return the given Points as pixels
190      */

191     protected final int pointAsPixel(int pt, int dpi) {
192         return Math.round(dpi * pt / DTP_RESOLUTION);
193     }
194     
195     /**
196      * Converts horizontal dialog units and answers pixels.
197      *
198      * @param dluX the horizontal dialog units
199      * @param dialogBaseUnitsX the horizontal dialog base units
200      * @return the given dialog base units as pixels
201      */

202     protected int dialogUnitXAsPixel(int dluX, double dialogBaseUnitsX) {
203         return (int) Math.round(dluX * dialogBaseUnitsX / 4);
204     }
205
206     /**
207      * Converts vertical dialog units and answers pixels.
208      *
209      * @param dluY the vertical dialog units
210      * @param dialogBaseUnitsY the vertical dialog base units
211      * @return the given dialog base units as pixels
212      */

213     protected int dialogUnitYAsPixel(int dluY, double dialogBaseUnitsY) {
214         return (int) Math.round(dluY * dialogBaseUnitsY / 8);
215     }
216     
217     
218     // Helper Code ************************************************************
219

220     protected double computeAverageCharWidth(FontMetrics JavaDoc metrics,
221      String JavaDoc testString) {
222          int width = metrics.stringWidth(testString);
223          double average = (double) width / testString.length();
224          //System.out.println("Average width of '" + testString + "'=" + average);
225
return average;
226      }
227     
228     /**
229      * Returns the components screen resolution or the default screen
230      * resolution if the component is null or has no toolkit assigned yet.
231      *
232      * @param c the component to ask for a toolkit
233      * @return the component's screen resolution
234      */

235     protected int getScreenResolution(Component JavaDoc c) {
236         if (c == null)
237             return getDefaultScreenResolution();
238             
239         Toolkit JavaDoc toolkit = c.getToolkit();
240         return toolkit != null
241             ? toolkit.getScreenResolution()
242             : getDefaultScreenResolution();
243     }
244     
245     
246     private static int defaultScreenResolution = -1;
247     
248     /**
249      * Computes and answers the default resolution.
250      *
251      * @return the default screen resolution
252      */

253     protected int getDefaultScreenResolution() {
254         if (defaultScreenResolution == -1) {
255             defaultScreenResolution =
256                 Toolkit.getDefaultToolkit().getScreenResolution();
257         }
258         return defaultScreenResolution;
259     }
260
261
262     
263 }
Popular Tags