KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > util > Unit


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * Unit.java
28  *
29  * Created on 8 febbraio 2003, 17.53
30  *
31  */

32
33 package it.businesslogic.ireport.util;
34
35 /**
36  *
37  * @author Administrator
38  */

39 public class Unit {
40
41     public static final double PIXEL = 1.0;
42     public static final double INCHES = 72.0;
43     public static final double CENTIMETERS = 28.3464;
44     public static final double MILLIMETERS = 2.83464;
45
46     /** Holds value of property unitName. */
47     private String JavaDoc unitName;
48     private String JavaDoc keyName;
49
50     /** Holds value of property conversionValue. */
51     private double conversionValue;
52
53     /** Creates a new instance of Unit */
54     public Unit(String JavaDoc unitName, double conversionValue) {
55         this(unitName, conversionValue, unitName);
56     }
57     
58     public Unit(String JavaDoc unitName, double conversionValue, String JavaDoc keyName) {
59         this.unitName = unitName;
60         this.conversionValue = conversionValue;
61         this.setKeyName(keyName);
62     }
63
64     /** Getter for property unitName.
65      * @return Value of property unitName.
66      *
67      */

68     public String JavaDoc getUnitName() {
69         return this.unitName;
70     }
71
72     /** Setter for property unitName.
73      * @param unitName New value of property unitName.
74      *
75      */

76     public void setUnitName(String JavaDoc unitName) {
77         this.unitName = unitName;
78     }
79
80     /** Getter for property conversionValue.
81      * @return Value of property conversionValue.
82      *
83      */

84     public double getConversionValue() {
85         return this.conversionValue;
86     }
87
88     /** Setter for property conversionValue.
89      * @param conversionValue New value of property conversionValue.
90      *
91      */

92     public void setConversionValue(double conversionValue) {
93         this.conversionValue = conversionValue;
94     }
95
96     public static Unit[] getStandardUnits()
97     {
98         Unit[] units = new Unit[4];
99
100         units[0] = new Unit(it.businesslogic.ireport.util.I18n.getString("pixels", "pixels"),Unit.PIXEL, "pixels");
101         units[1] = new Unit(it.businesslogic.ireport.util.I18n.getString("inches", "inches"),Unit.INCHES, "inches");
102         units[2] = new Unit("cm", Unit.CENTIMETERS);
103         units[3] = new Unit("mm", Unit.MILLIMETERS);
104
105         return units;
106     }
107
108     public static int getUnitIndex(String JavaDoc unitName)
109     {
110         Unit[] units = getStandardUnits();
111         for (int i=0; i<units.length; ++i)
112         {
113             if (units[i].getUnitName().equalsIgnoreCase(unitName)) return i;
114         }
115         return -1;
116     }
117
118     public String JavaDoc toString()
119     {
120         return getUnitName();
121     }
122
123     static public double convertPixelsToInches(long pixels)
124     {
125         return ((double)pixels)/INCHES;
126     }
127
128     static public long convertInchesToPixels(double inches)
129     {
130         return (long)(inches*INCHES);
131     }
132
133     static public double convertPixelsToCentimeters(long pixels)
134     {
135         return ((double)pixels)/CENTIMETERS;
136     }
137
138     static public long convertCentimetersToPixels(double centimeters)
139     {
140         return (long)(centimeters*CENTIMETERS);
141     }
142
143     static public double convertPixelsToMillimeters(long pixels)
144     {
145         return ((double)pixels)/MILLIMETERS;
146     }
147
148     static public long convertMillimetersToPixels(double millimeters)
149     {
150         return (long)(millimeters*CENTIMETERS);
151     }
152
153     static public long convertToPixels(double value, double convert)
154     {
155         return (long)(value*convert);
156     }
157
158     public String JavaDoc getKeyName() {
159         return keyName;
160     }
161
162     public void setKeyName(String JavaDoc keyName) {
163         this.keyName = keyName;
164     }
165 }
166
Popular Tags