KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc format(double value) {
33         String JavaDoc strValue = null;
34         Class JavaDoc type = field.getType();
35         double editedValue = value;
36         if (type == float.class || type == Float JavaDoc.class ||
37             type == double.class || type == Double JavaDoc.class)
38             editedValue *= 100;
39         return format.format(editedValue)+"%";
40     }
41
42     public Number JavaDoc parseNumber(String JavaDoc str, ParsePosition JavaDoc pos) {
43         String JavaDoc string = ((String JavaDoc)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 JavaDoc type = field.getType();
52
53         Number JavaDoc number = format.parse(string,pos);
54
55         if (type == int.class || type == Integer JavaDoc.class) {
56             return new Integer JavaDoc(number.intValue());
57         } else if (type == short.class || type == Short JavaDoc.class) {
58             return new Short JavaDoc(number.shortValue());
59         } else if (type == long.class || type == Long JavaDoc.class) {
60             return new Long JavaDoc(number.longValue());
61         } else if (type == float.class || type == Float JavaDoc.class) {
62             return new Float JavaDoc(number.floatValue()/100);
63         } else if (type == double.class || type == Double JavaDoc.class) {
64             return new Double JavaDoc(number.doubleValue()/100);
65         } else {
66             throw new RuntimeException JavaDoc("PercentEditor: Unhandled type "+type.getName());
67         }
68     }
69 }
70
71
Popular Tags