KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > FloatFormat


1 /*
2   Copyright (C) 2003 Laurent Martelli <laurent@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.aspects.gui;
19
20 import java.text.DecimalFormat JavaDoc;
21 import java.text.ParsePosition JavaDoc;
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 JavaDoc 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 JavaDoc 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 JavaDoc(formatString);
48         else
49             logger.error("No format for "+field);
50     }
51     
52     public String JavaDoc format(Object JavaDoc value) {
53         return format((Number JavaDoc)value);
54     }
55
56     public String JavaDoc format(Number JavaDoc value) {
57         if (value!=null)
58             return format(value.doubleValue());
59         else
60             return "";
61     }
62
63     public String JavaDoc format(double value) {
64         if (format!=null)
65             return format.format(value);
66         else
67             return ""+value;
68     }
69
70     public Object JavaDoc parse(String JavaDoc str, ParsePosition JavaDoc pos) {
71         return parseNumber(str,pos);
72     }
73
74     public Number JavaDoc parseNumber(String JavaDoc str, ParsePosition JavaDoc pos) {
75         String JavaDoc string = str.trim();
76
77         if (Strings.isEmpty(string))
78             return null;
79
80         return format.parse(str,pos);
81     }
82
83     public Float JavaDoc parseFloat(String JavaDoc str, ParsePosition JavaDoc pos) {
84         Number JavaDoc n = parseNumber(str,pos);
85         if (n!=null) {
86             return new Float JavaDoc(n.floatValue());
87         } else {
88             return null;
89         }
90     }
91
92     public Double JavaDoc parseDouble(String JavaDoc str, ParsePosition JavaDoc pos) {
93         Number JavaDoc n = parseNumber(str,pos);
94         if (n!=null) {
95             return new Double JavaDoc(n.doubleValue());
96         } else {
97             return null;
98         }
99     }
100 }
101
102
Popular Tags