001: /*
002: * Copyright (c) 2002-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.forms.util;
032:
033: import java.awt.Component;
034: import java.awt.FontMetrics;
035: import java.awt.Toolkit;
036:
037: /**
038: * An abstract implementation of the {@link UnitConverter} interface that
039: * minimizes the effort required to convert font-dependent sizes to pixels.
040: *
041: * @author Karsten Lentzsch
042: * @version $Revision: 1.2 $
043: *
044: * @see DefaultUnitConverter
045: * @see com.jgoodies.forms.layout.Size
046: * @see com.jgoodies.forms.layout.Sizes
047: */
048: abstract public class AbstractUnitConverter implements UnitConverter {
049:
050: private static final int DTP_RESOLUTION = 72;
051:
052: // Unit Converter Implementation
053: // *********************************************
054:
055: /**
056: * Converts Inches and returns pixels using the specified resolution.
057: *
058: * @param in
059: * the Inches
060: * @param component
061: * the component that provides the graphics object
062: * @return the given Inches as pixels
063: */
064: public int inchAsPixel(double in, Component component) {
065: return inchAsPixel(in, getScreenResolution(component));
066: }
067:
068: /**
069: * Converts Millimeters and returns pixels using the resolution of the given
070: * component's graphics object.
071: *
072: * @param mm
073: * Millimeters
074: * @param component
075: * the component that provides the graphics object
076: * @return the given Millimeters as pixels
077: */
078: public int millimeterAsPixel(double mm, Component component) {
079: return millimeterAsPixel(mm, getScreenResolution(component));
080: }
081:
082: /**
083: * Converts Centimeters and returns pixels using the resolution of the given
084: * component's graphics object.
085: *
086: * @param cm
087: * Centimeters
088: * @param component
089: * the component that provides the graphics object
090: * @return the given Centimeters as pixels
091: */
092: public int centimeterAsPixel(double cm, Component component) {
093: return centimeterAsPixel(cm, getScreenResolution(component));
094: }
095:
096: /**
097: * Converts DTP Points and returns pixels using the resolution of the given
098: * component's graphics object.
099: *
100: * @param pt
101: * DTP Points
102: * @param component
103: * the component that provides the graphics object
104: * @return the given Points as pixels
105: */
106: public int pointAsPixel(int pt, Component component) {
107: return pointAsPixel(pt, getScreenResolution(component));
108: }
109:
110: /**
111: * Converts horizontal dialog units and returns pixels. Honors the
112: * resolution, dialog font size, platform, and l&f.
113: *
114: * @param dluX
115: * the horizontal dialog units
116: * @param c
117: * a Component that provides the font and graphics
118: * @return the given horizontal dialog units as pixels
119: */
120: public int dialogUnitXAsPixel(int dluX, Component c) {
121: return dialogUnitXAsPixel(dluX, getDialogBaseUnitsX(c));
122: }
123:
124: /**
125: * Converts vertical dialog units and returns pixels. Honors the resolution,
126: * dialog font size, platform, and l&f.
127: *
128: * @param dluY
129: * the vertical dialog units
130: * @param c
131: * a Component that provides the font and graphics
132: * @return the given vertical dialog units as pixels
133: */
134: public int dialogUnitYAsPixel(int dluY, Component c) {
135: return dialogUnitYAsPixel(dluY, getDialogBaseUnitsY(c));
136: }
137:
138: // Abstract Behavior *****************************************************
139:
140: /**
141: * Gets and returns the horizontal dialog base units. Implementations are
142: * encouraged to cache previously computed dialog base units.
143: *
144: * @param component
145: * a Component that provides the font and graphics
146: * @return the horizontal dialog base units
147: */
148: abstract protected double getDialogBaseUnitsX(Component component);
149:
150: /**
151: * Gets and returns the vertical dialog base units. Implementations are
152: * encouraged to cache previously computed dialog base units.
153: *
154: * @param component
155: * a Component that provides the font and graphics
156: * @return the vertical dialog base units
157: */
158: abstract protected double getDialogBaseUnitsY(Component component);
159:
160: // Convenience Methods ***************************************************
161:
162: /**
163: * Converts Inches and returns pixels using the specified resolution.
164: *
165: * @param in
166: * the Inches
167: * @param dpi
168: * the resolution
169: * @return the given Inches as pixels
170: */
171: protected final int inchAsPixel(double in, int dpi) {
172: return (int) Math.round(dpi * in);
173: }
174:
175: /**
176: * Converts Millimeters and returns pixels using the specified resolution.
177: *
178: * @param mm
179: * Millimeters
180: * @param dpi
181: * the resolution
182: * @return the given Millimeters as pixels
183: */
184: protected final int millimeterAsPixel(double mm, int dpi) {
185: return (int) Math.round(dpi * mm * 10 / 254);
186: }
187:
188: /**
189: * Converts Centimeters and returns pixels using the specified resolution.
190: *
191: * @param cm
192: * Centimeters
193: * @param dpi
194: * the resolution
195: * @return the given Centimeters as pixels
196: */
197: protected final int centimeterAsPixel(double cm, int dpi) {
198: return (int) Math.round(dpi * cm * 100 / 254);
199: }
200:
201: /**
202: * Converts DTP Points and returns pixels using the specified resolution.
203: *
204: * @param pt
205: * DTP Points
206: * @param dpi
207: * the resolution in dpi
208: * @return the given Points as pixels
209: */
210: protected final int pointAsPixel(int pt, int dpi) {
211: return Math.round(dpi * pt / DTP_RESOLUTION);
212: }
213:
214: /**
215: * Converts horizontal dialog units and returns pixels.
216: *
217: * @param dluX
218: * the horizontal dialog units
219: * @param dialogBaseUnitsX
220: * the horizontal dialog base units
221: * @return the given dialog base units as pixels
222: */
223: protected int dialogUnitXAsPixel(int dluX, double dialogBaseUnitsX) {
224: return (int) Math.round(dluX * dialogBaseUnitsX / 4);
225: }
226:
227: /**
228: * Converts vertical dialog units and returns pixels.
229: *
230: * @param dluY
231: * the vertical dialog units
232: * @param dialogBaseUnitsY
233: * the vertical dialog base units
234: * @return the given dialog base units as pixels
235: */
236: protected int dialogUnitYAsPixel(int dluY, double dialogBaseUnitsY) {
237: return (int) Math.round(dluY * dialogBaseUnitsY / 8);
238: }
239:
240: // Helper Code ************************************************************
241:
242: /**
243: * Computes and returns the average character width of the specified test
244: * string using the given FontMetrics. The test string shall represent an
245: * "average" text.
246: *
247: * @param metrics
248: * used to compute the test string's width
249: * @param testString
250: * the string that shall represent an "average" text
251: * @return the test string's average character width.
252: */
253: protected double computeAverageCharWidth(FontMetrics metrics,
254: String testString) {
255: int width = metrics.stringWidth(testString);
256: double average = (double) width / testString.length();
257: // System.out.println("Average width of '" + testString + "'=" +
258: // average);
259: return average;
260: }
261:
262: /**
263: * Returns the components screen resolution or the default screen resolution
264: * if the component is null or has no toolkit assigned yet.
265: *
266: * @param c
267: * the component to ask for a toolkit
268: * @return the component's screen resolution
269: */
270: protected int getScreenResolution(Component c) {
271: if (c == null)
272: return getDefaultScreenResolution();
273:
274: Toolkit toolkit = c.getToolkit();
275: return toolkit != null ? toolkit.getScreenResolution()
276: : getDefaultScreenResolution();
277: }
278:
279: private static int defaultScreenResolution = -1;
280:
281: /**
282: * Computes and returns the default resolution.
283: *
284: * @return the default screen resolution
285: */
286: protected int getDefaultScreenResolution() {
287: if (defaultScreenResolution == -1) {
288: defaultScreenResolution = Toolkit.getDefaultToolkit()
289: .getScreenResolution();
290: }
291: return defaultScreenResolution;
292: }
293:
294: }
|