KickJava   Java API By Example, From Geeks To Geeks.

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


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 IntFormat implements Format {
29     static Logger logger = Logger.getLogger("gui");
30
31     protected DecimalFormat JavaDoc format;
32     protected FieldItem field;
33     
34     public IntFormat(FieldItem field) {
35         this.field = field;
36
37         MetaItem type = RttiAC.getFieldType(field);
38         String JavaDoc formatString = null;
39         if (type!=null)
40             formatString = GuiAC.getIntFormat(type);
41         else
42             formatString = GuiAC.getIntFormat(field);
43
44         if (formatString!=null)
45             format = new DecimalFormat JavaDoc(formatString);
46         else
47             logger.error("No format for "+field);
48     }
49     
50     public String JavaDoc format(Object JavaDoc value) {
51         return format((Number JavaDoc)value);
52     }
53
54     public String JavaDoc format(Number JavaDoc value) {
55         if (value!=null)
56             return format(value.longValue());
57         else
58             return "";
59     }
60
61     public String JavaDoc format(long value) {
62         if (format!=null)
63             return format.format(value);
64         else
65             return ""+value;
66     }
67
68     public Object JavaDoc parse(String JavaDoc str, ParsePosition JavaDoc pos) {
69         return parseNumber(str,pos);
70     }
71
72     public Number JavaDoc parseNumber(String JavaDoc str, ParsePosition JavaDoc pos) {
73         String JavaDoc string = str.trim();
74
75         if (Strings.isEmpty(string))
76             return null;
77
78         return format.parse(str,pos);
79     }
80 }
81
82
Popular Tags