1 32 33 package it.businesslogic.ireport.util; 34 35 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 47 private String unitName; 48 private String keyName; 49 50 51 private double conversionValue; 52 53 54 public Unit(String unitName, double conversionValue) { 55 this(unitName, conversionValue, unitName); 56 } 57 58 public Unit(String unitName, double conversionValue, String keyName) { 59 this.unitName = unitName; 60 this.conversionValue = conversionValue; 61 this.setKeyName(keyName); 62 } 63 64 68 public String getUnitName() { 69 return this.unitName; 70 } 71 72 76 public void setUnitName(String unitName) { 77 this.unitName = unitName; 78 } 79 80 84 public double getConversionValue() { 85 return this.conversionValue; 86 } 87 88 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 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 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 getKeyName() { 159 return keyName; 160 } 161 162 public void setKeyName(String keyName) { 163 this.keyName = keyName; 164 } 165 } 166 | Popular Tags |