1 |
|
package org.apache.tapestry.form.translator; |
2 |
|
|
3 |
|
import org.apache.hivemind.ApplicationRuntimeException; |
4 |
|
import org.apache.tapestry.form.IFormComponent; |
5 |
|
import org.apache.tapestry.form.ValidationMessages; |
6 |
|
import org.apache.tapestry.valid.ValidationConstraint; |
7 |
|
import org.apache.tapestry.valid.ValidationStrings; |
8 |
|
import org.apache.tapestry.valid.ValidatorException; |
9 |
|
|
10 |
|
import java.math.BigDecimal; |
11 |
|
import java.util.Locale; |
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
public class BigDecimalTranslator extends AbstractTranslator { |
17 |
|
|
18 |
|
public BigDecimalTranslator() |
19 |
5 |
{ |
20 |
5 |
} |
21 |
|
|
22 |
|
|
23 |
|
public BigDecimalTranslator(String initializer) |
24 |
|
{ |
25 |
0 |
super(initializer); |
26 |
0 |
} |
27 |
|
|
28 |
|
protected String formatObject(IFormComponent field, Locale locale, Object object) |
29 |
|
{ |
30 |
2 |
if (!BigDecimal.class.isInstance(object)) |
31 |
0 |
throw new ApplicationRuntimeException("BigDecimalTranslator translates values of type BigDecimal, not: " + object.getClass()); |
32 |
|
|
33 |
1 |
return object.toString(); |
34 |
|
} |
35 |
|
|
36 |
|
protected Object parseText(IFormComponent field, ValidationMessages messages, String text) |
37 |
|
throws ValidatorException |
38 |
|
{ |
39 |
|
try { |
40 |
|
|
41 |
2 |
return new BigDecimal(text); |
42 |
|
} |
43 |
1 |
catch (NumberFormatException e) { |
44 |
1 |
throw new ValidatorException(buildMessage(messages, field, ValidationStrings.INVALID_NUMBER), ValidationConstraint.NUMBER_FORMAT); |
45 |
|
} |
46 |
|
} |
47 |
|
} |