1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package javax.faces.convert; |
20 | |
|
21 | |
import javax.faces.component.UIComponent; |
22 | |
import javax.faces.context.FacesContext; |
23 | |
|
24 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFConverter; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
@JSFConverter |
30 | |
public class FloatConverter |
31 | |
implements Converter |
32 | |
{ |
33 | |
|
34 | |
public static final String CONVERTER_ID = "javax.faces.Float"; |
35 | |
public static final String STRING_ID = "javax.faces.converter.STRING"; |
36 | |
public static final String FLOAT_ID = "javax.faces.converter.FloatConverter.FLOAT"; |
37 | |
|
38 | |
|
39 | |
public FloatConverter() |
40 | 2 | { |
41 | 2 | } |
42 | |
|
43 | |
|
44 | |
public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) |
45 | |
{ |
46 | 4 | if (facesContext == null) |
47 | |
{ |
48 | 0 | throw new NullPointerException("facesContext"); |
49 | |
} |
50 | 4 | if (uiComponent == null) |
51 | |
{ |
52 | 0 | throw new NullPointerException("uiComponent"); |
53 | |
} |
54 | |
|
55 | 4 | if (value != null) |
56 | |
{ |
57 | 4 | value = value.trim(); |
58 | 4 | if (value.length() > 0) |
59 | |
{ |
60 | |
try |
61 | |
{ |
62 | 4 | return Float.valueOf(value); |
63 | |
} |
64 | 0 | catch (NumberFormatException e) |
65 | |
{ |
66 | 0 | throw new ConverterException(_MessageUtils.getErrorMessage(facesContext, |
67 | |
FLOAT_ID, |
68 | |
new Object[]{value,"50000",_MessageUtils.getLabel(facesContext, uiComponent)}), e); |
69 | |
} |
70 | |
} |
71 | |
} |
72 | 0 | return null; |
73 | |
} |
74 | |
|
75 | |
public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) |
76 | |
{ |
77 | 0 | if (facesContext == null) |
78 | |
{ |
79 | 0 | throw new NullPointerException("facesContext"); |
80 | |
} |
81 | 0 | if (uiComponent == null) |
82 | |
{ |
83 | 0 | throw new NullPointerException("uiComponent"); |
84 | |
} |
85 | |
|
86 | 0 | if (value == null) |
87 | |
{ |
88 | 0 | return ""; |
89 | |
} |
90 | 0 | if (value instanceof String) |
91 | |
{ |
92 | 0 | return (String)value; |
93 | |
} |
94 | |
try |
95 | |
{ |
96 | 0 | return Float.toString(((Number)value).floatValue()); |
97 | |
} |
98 | 0 | catch (Exception e) |
99 | |
{ |
100 | 0 | throw new ConverterException(_MessageUtils.getErrorMessage(facesContext, STRING_ID, |
101 | |
new Object[]{value,_MessageUtils.getLabel(facesContext, uiComponent)}),e); |
102 | |
} |
103 | |
} |
104 | |
} |