1 22 package info.monitorenter.gui.chart.labelformatters; 23 24 import info.monitorenter.gui.chart.IAxis; 25 import info.monitorenter.util.Range; 26 import info.monitorenter.util.units.AUnit; 27 import info.monitorenter.util.units.UnitFactory; 28 import info.monitorenter.util.units.UnitSystemSI; 29 30 import java.text.NumberFormat ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.Map ; 34 35 49 public class LabelFormatterAutoUnits extends ALabelFormatter { 50 51 61 private static final Map UNITS_2_POWER = new HashMap (); 62 63 static { 64 Iterator itUnits = UnitFactory.getInstance().getUnits(UnitSystemSI.getInstance()).iterator(); 65 AUnit unit; 66 double factor = 0; 67 int power; 68 while (itUnits.hasNext()) { 69 power = 0; 70 unit = (AUnit) itUnits.next(); 71 factor = unit.getFactor(); 72 if (factor > 1) { 73 while (factor > 1) { 74 factor /= 10; 75 power++; 76 } 77 78 } else if (factor < 1) { 79 while (factor < 1) { 80 factor *= 10; 81 power--; 82 } 83 } 84 LabelFormatterAutoUnits.UNITS_2_POWER.put(unit, new Integer (power)); 85 } 86 } 87 88 91 private ALabelFormatter m_delegate; 92 93 104 private AUnit m_unit = UNIT_UNCHANGED; 105 106 114 public LabelFormatterAutoUnits(final ALabelFormatter delegate) { 115 super(); 116 this.m_delegate = delegate; 117 } 118 119 136 private final void chooseUnit(final double min, final double max) { 137 double range = this.getAxis().getRange().getExtent(); 138 if (range == 0) { 139 range = 1; 140 } 141 this.m_unit = UnitFactory.getInstance().getUnit(range, UnitSystemSI.getInstance()); 142 if (range / this.m_unit.getFactor() < 3) { 143 this.m_unit = this.m_unit.getNexLowerUnit(); 144 } 145 } 146 147 150 public boolean equals(final Object obj) { 151 return this.m_delegate.equals(obj); 152 } 153 154 157 public String format(final double value) { 158 double tmp = value / this.m_unit.getFactor(); 159 return this.m_delegate.format(tmp); 160 } 161 162 165 public IAxis getAxis() { 166 return this.m_delegate.getAxis(); 167 } 168 169 172 public int getMaxAmountChars() { 173 int fractionDigits = 0; 175 Range range = this.getAxis().getRange(); 176 double dRange = range.getExtent() / this.m_unit.getFactor(); 177 if (dRange < 1) { 178 if (dRange == 0) { 179 fractionDigits = 1; 180 } else { 181 if (dRange == 0) { 182 fractionDigits = 1; 183 } else { 184 while (dRange < 1) { 186 dRange *= 10; 187 fractionDigits++; 188 } 189 } 190 } 191 } else { 192 if (dRange < 10) { 193 fractionDigits = 2; 194 } else if (dRange < 100) { 195 fractionDigits = 1; 196 } else { 197 fractionDigits = 0; 198 } 199 } 200 201 int integerDigits = 0; 203 double max = range.getMax() / (this.m_unit.getFactor()); 204 double min = Math.abs(range.getMin()) / (this.m_unit.getFactor()); 205 if (max == 0 && min == 0) { 206 integerDigits = 1; 207 } else if (max < min) { 208 while (min > 1) { 209 min /= 10; 210 integerDigits++; 211 } 212 } else { 213 while (max > 1) { 214 max /= 10; 215 integerDigits++; 216 } 217 } 218 if (this.m_delegate instanceof LabelFormatterNumber) { 221 222 NumberFormat nf = ((LabelFormatterNumber) this.m_delegate).getNumberFormat(); 223 if (integerDigits > nf.getMaximumIntegerDigits()) { 224 nf.setMaximumIntegerDigits(integerDigits); 225 } 226 if (fractionDigits > nf.getMaximumFractionDigits()) { 227 nf.setMaximumFractionDigits(fractionDigits); 228 } 229 } 230 return 1 + integerDigits + 1 + fractionDigits; 232 233 } 234 235 238 public double getMinimumValueShiftForChange() { 239 return this.m_delegate.getMinimumValueShiftForChange() * this.m_unit.getFactor(); 240 } 241 242 246 public double getNextEvenValue(final double value, final boolean ceiling) { 247 return this.m_delegate.getNextEvenValue(value, ceiling); 248 } 249 250 253 public AUnit getUnit() { 254 return this.m_unit; 255 } 256 257 260 public int hashCode() { 261 return this.m_delegate.hashCode(); 262 } 263 264 267 public void initPaintIteration() { 268 Range domain = this.m_delegate.getAxis().getRange(); 269 this.chooseUnit(domain.getMin(), domain.getMax()); 270 } 271 272 275 public Number parse(final String formatted) throws NumberFormatException { 276 double parsed = this.m_delegate.parse(formatted).doubleValue(); 277 parsed *= this.m_unit.getFactor(); 278 return new Double (parsed); 279 } 280 281 284 public void setAxis(final IAxis axis) { 285 286 this.m_delegate.setAxis(axis); 287 } 288 289 292 public String toString() { 293 return this.m_delegate.toString(); 294 } 295 } 296 | Popular Tags |