001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * Unit.java
028: *
029: * Created on 8 febbraio 2003, 17.53
030: *
031: */
032:
033: package it.businesslogic.ireport.util;
034:
035: /**
036: *
037: * @author Administrator
038: */
039: public class Unit {
040:
041: public static final double PIXEL = 1.0;
042: public static final double INCHES = 72.0;
043: public static final double CENTIMETERS = 28.3464;
044: public static final double MILLIMETERS = 2.83464;
045:
046: /** Holds value of property unitName. */
047: private String unitName;
048: private String keyName;
049:
050: /** Holds value of property conversionValue. */
051: private double conversionValue;
052:
053: /** Creates a new instance of Unit */
054: public Unit(String unitName, double conversionValue) {
055: this (unitName, conversionValue, unitName);
056: }
057:
058: public Unit(String unitName, double conversionValue, String keyName) {
059: this .unitName = unitName;
060: this .conversionValue = conversionValue;
061: this .setKeyName(keyName);
062: }
063:
064: /** Getter for property unitName.
065: * @return Value of property unitName.
066: *
067: */
068: public String getUnitName() {
069: return this .unitName;
070: }
071:
072: /** Setter for property unitName.
073: * @param unitName New value of property unitName.
074: *
075: */
076: public void setUnitName(String unitName) {
077: this .unitName = unitName;
078: }
079:
080: /** Getter for property conversionValue.
081: * @return Value of property conversionValue.
082: *
083: */
084: public double getConversionValue() {
085: return this .conversionValue;
086: }
087:
088: /** Setter for property conversionValue.
089: * @param conversionValue New value of property conversionValue.
090: *
091: */
092: public void setConversionValue(double conversionValue) {
093: this .conversionValue = conversionValue;
094: }
095:
096: public static Unit[] getStandardUnits() {
097: Unit[] units = new Unit[4];
098:
099: units[0] = new Unit(it.businesslogic.ireport.util.I18n
100: .getString("pixels", "pixels"), Unit.PIXEL, "pixels");
101: units[1] = new Unit(it.businesslogic.ireport.util.I18n
102: .getString("inches", "inches"), Unit.INCHES, "inches");
103: units[2] = new Unit("cm", Unit.CENTIMETERS);
104: units[3] = new Unit("mm", Unit.MILLIMETERS);
105:
106: return units;
107: }
108:
109: public static int getUnitIndex(String unitName) {
110: Unit[] units = getStandardUnits();
111: for (int i = 0; i < units.length; ++i) {
112: if (units[i].getUnitName().equalsIgnoreCase(unitName))
113: return i;
114: }
115: return -1;
116: }
117:
118: public String toString() {
119: return getUnitName();
120: }
121:
122: static public double convertPixelsToInches(long pixels) {
123: return ((double) pixels) / INCHES;
124: }
125:
126: static public long convertInchesToPixels(double inches) {
127: return (long) (inches * INCHES);
128: }
129:
130: static public double convertPixelsToCentimeters(long pixels) {
131: return ((double) pixels) / CENTIMETERS;
132: }
133:
134: static public long convertCentimetersToPixels(double centimeters) {
135: return (long) (centimeters * CENTIMETERS);
136: }
137:
138: static public double convertPixelsToMillimeters(long pixels) {
139: return ((double) pixels) / MILLIMETERS;
140: }
141:
142: static public long convertMillimetersToPixels(double millimeters) {
143: return (long) (millimeters * CENTIMETERS);
144: }
145:
146: static public long convertToPixels(double value, double convert) {
147: return (long) (value * convert);
148: }
149:
150: public String getKeyName() {
151: return keyName;
152: }
153:
154: public void setKeyName(String keyName) {
155: this.keyName = keyName;
156: }
157: }
|