1 17 18 package org.objectweb.jac.aspects.gui; 19 20 import java.text.DecimalFormat ; 21 import java.text.ParsePosition ; 22 import org.objectweb.jac.core.rtti.FieldItem; 23 import org.objectweb.jac.core.rtti.MetaItem; 24 import org.objectweb.jac.core.rtti.RttiAC; 25 26 public class PercentFormat extends FloatFormat { 27 28 public PercentFormat(FieldItem field) { 29 super(field); 30 } 31 32 public String format(double value) { 33 String strValue = null; 34 Class type = field.getType(); 35 double editedValue = value; 36 if (type == float.class || type == Float .class || 37 type == double.class || type == Double .class) 38 editedValue *= 100; 39 return format.format(editedValue)+"%"; 40 } 41 42 public Number parseNumber(String str, ParsePosition pos) { 43 String string = ((String )str).trim(); 44 45 if (string==null || string.length()==0) 46 return null; 47 if (string.endsWith("%")) { 48 string = string.substring(0,string.length()-1).trim(); 49 } 50 51 Class type = field.getType(); 52 53 Number number = format.parse(string,pos); 54 55 if (type == int.class || type == Integer .class) { 56 return new Integer (number.intValue()); 57 } else if (type == short.class || type == Short .class) { 58 return new Short (number.shortValue()); 59 } else if (type == long.class || type == Long .class) { 60 return new Long (number.longValue()); 61 } else if (type == float.class || type == Float .class) { 62 return new Float (number.floatValue()/100); 63 } else if (type == double.class || type == Double .class) { 64 return new Double (number.doubleValue()/100); 65 } else { 66 throw new RuntimeException ("PercentEditor: Unhandled type "+type.getName()); 67 } 68 } 69 } 70 71 | Popular Tags |