1 17 18 package org.objectweb.jac.aspects.gui; 19 20 import java.text.DecimalFormat ; 21 import java.text.ParsePosition ; 22 import org.apache.log4j.Logger; 23 import org.objectweb.jac.core.rtti.FieldItem; 24 import org.objectweb.jac.core.rtti.MetaItem; 25 import org.objectweb.jac.core.rtti.RttiAC; 26 import org.objectweb.jac.util.Strings; 27 28 public class FloatFormat implements NumberFormat { 29 static Logger logger = Logger.getLogger("gui"); 30 31 protected DecimalFormat format; 32 protected FieldItem field; 33 34 public FloatFormat(FieldItem field) { 35 this.field = field; 36 37 MetaItem type = field!=null?RttiAC.getFieldType(field):null; 38 String formatString = null; 39 if (type!=null) 40 formatString = GuiAC.getFloatFormat(type); 41 else if (field!=null) 42 formatString = GuiAC.getFloatFormat(field); 43 else 44 formatString = GuiAC.getFloatFormat(); 45 46 if (formatString!=null) 47 format = new DecimalFormat (formatString); 48 else 49 logger.error("No format for "+field); 50 } 51 52 public String format(Object value) { 53 return format((Number )value); 54 } 55 56 public String format(Number value) { 57 if (value!=null) 58 return format(value.doubleValue()); 59 else 60 return ""; 61 } 62 63 public String format(double value) { 64 if (format!=null) 65 return format.format(value); 66 else 67 return ""+value; 68 } 69 70 public Object parse(String str, ParsePosition pos) { 71 return parseNumber(str,pos); 72 } 73 74 public Number parseNumber(String str, ParsePosition pos) { 75 String string = str.trim(); 76 77 if (Strings.isEmpty(string)) 78 return null; 79 80 return format.parse(str,pos); 81 } 82 83 public Float parseFloat(String str, ParsePosition pos) { 84 Number n = parseNumber(str,pos); 85 if (n!=null) { 86 return new Float (n.floatValue()); 87 } else { 88 return null; 89 } 90 } 91 92 public Double parseDouble(String str, ParsePosition pos) { 93 Number n = parseNumber(str,pos); 94 if (n!=null) { 95 return new Double (n.doubleValue()); 96 } else { 97 return null; 98 } 99 } 100 } 101 102 | Popular Tags |