1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package javax.faces.validator; |
17 | |
|
18 | |
import javax.faces.application.FacesMessage; |
19 | |
import javax.faces.component.StateHolder; |
20 | |
import javax.faces.component.UIComponent; |
21 | |
import javax.faces.context.FacesContext; |
22 | |
import javax.faces.internal.FacesMessageUtil; |
23 | |
import javax.faces.internal.UIComponentUtil; |
24 | |
import javax.faces.internal.UIInputUtil; |
25 | |
|
26 | |
import org.seasar.framework.util.AssertionUtil; |
27 | |
import org.seasar.framework.util.DoubleConversionUtil; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
public class DoubleRangeValidator implements Validator, StateHolder { |
33 | |
|
34 | |
public static final String VALIDATOR_ID = "javax.faces.DoubleRange"; |
35 | |
|
36 | |
public static final String MAXIMUM_MESSAGE_ID = "javax.faces.validator.DoubleRangeValidator.MAXIMUM"; |
37 | |
|
38 | |
public static final String MINIMUM_MESSAGE_ID = "javax.faces.validator.DoubleRangeValidator.MINIMUM"; |
39 | |
|
40 | |
public static final String TYPE_MESSAGE_ID = "javax.faces.validator.DoubleRangeValidator.TYPE"; |
41 | |
|
42 | 85 | private double maximum = Double.MAX_VALUE; |
43 | |
|
44 | 85 | private double minimum = Double.MIN_VALUE; |
45 | |
|
46 | 85 | private boolean transientValue = false; |
47 | |
|
48 | 62 | public DoubleRangeValidator() { |
49 | 62 | } |
50 | |
|
51 | 14 | public DoubleRangeValidator(double maximum) { |
52 | 14 | this.maximum = maximum; |
53 | 14 | } |
54 | |
|
55 | 9 | public DoubleRangeValidator(double maximum, double minimum) { |
56 | 9 | this.maximum = maximum; |
57 | 9 | this.minimum = minimum; |
58 | 9 | } |
59 | |
|
60 | |
public boolean equals(Object obj) { |
61 | 11 | if (!(obj instanceof DoubleRangeValidator)) { |
62 | 1 | return false; |
63 | |
} |
64 | 10 | DoubleRangeValidator v = (DoubleRangeValidator) obj; |
65 | 10 | return maximum == v.maximum && minimum == v.minimum; |
66 | |
} |
67 | |
|
68 | |
public int hashCode() { |
69 | 0 | return (int) maximum * (int) minimum * 17; |
70 | |
} |
71 | |
|
72 | |
public double getMaximum() { |
73 | 26 | return maximum; |
74 | |
} |
75 | |
|
76 | |
public double getMinimum() { |
77 | 23 | return minimum; |
78 | |
} |
79 | |
|
80 | |
public boolean isTransient() { |
81 | 16 | return transientValue; |
82 | |
} |
83 | |
|
84 | |
public void restoreState(FacesContext context, Object state) { |
85 | 16 | Object[] obj = (Object[]) state; |
86 | 16 | maximum = ((Double) obj[0]).doubleValue(); |
87 | 16 | minimum = ((Double) obj[1]).doubleValue(); |
88 | 16 | } |
89 | |
|
90 | |
public Object saveState(FacesContext context) { |
91 | 16 | Object[] obj = new Object[2]; |
92 | 16 | obj[0] = new Double(maximum); |
93 | 16 | obj[1] = new Double(minimum); |
94 | 16 | return obj; |
95 | |
} |
96 | |
|
97 | |
public void setMaximum(double maximum) { |
98 | 24 | this.maximum = maximum; |
99 | 24 | } |
100 | |
|
101 | |
public void setMinimum(double minimum) { |
102 | 31 | this.minimum = minimum; |
103 | 31 | } |
104 | |
|
105 | |
public void setTransient(boolean transientValue) { |
106 | 0 | this.transientValue = transientValue; |
107 | 0 | } |
108 | |
|
109 | |
public void validate(FacesContext context, UIComponent component, |
110 | |
Object value) throws ValidatorException { |
111 | 14 | AssertionUtil.assertNotNull("context", context); |
112 | 13 | AssertionUtil.assertNotNull("component", component); |
113 | 12 | if (UIInputUtil.isEmpty(value)) { |
114 | 2 | return; |
115 | |
} |
116 | |
double doubleValue; |
117 | |
try { |
118 | 10 | doubleValue = DoubleConversionUtil.toDouble(value).doubleValue(); |
119 | 8 | if (maximum != Double.MAX_VALUE && minimum != Double.MIN_VALUE) { |
120 | 4 | if (doubleValue < minimum || doubleValue > maximum) { |
121 | 4 | Object[] args = { new Double(minimum), new Double(maximum), |
122 | |
UIComponentUtil.getLabel(component) }; |
123 | 4 | FacesMessage message = FacesMessageUtil.getMessage(context, |
124 | |
getNotInRangeMessageId(), convertArgs(args)); |
125 | 4 | throw new ValidatorException(message, |
126 | |
getNotInRangeMessageId(), args); |
127 | |
} |
128 | 4 | } else if (minimum != Double.MIN_VALUE) { |
129 | 2 | if (doubleValue < minimum) { |
130 | 2 | Object[] args = { new Double(minimum), |
131 | |
UIComponentUtil.getLabel(component) }; |
132 | 2 | FacesMessage message = FacesMessageUtil.getMessage(context, |
133 | |
getMinimumMessageId(), convertArgs(args)); |
134 | 2 | throw new ValidatorException(message, |
135 | |
getMinimumMessageId(), args); |
136 | |
} |
137 | 2 | } else if (maximum != Double.MAX_VALUE) { |
138 | 2 | if (doubleValue > maximum) { |
139 | 2 | Object[] args = { new Double(maximum), |
140 | |
UIComponentUtil.getLabel(component) }; |
141 | 2 | FacesMessage message = FacesMessageUtil.getMessage(context, |
142 | |
getMaximumMessageId(), convertArgs(args)); |
143 | 2 | throw new ValidatorException(message, |
144 | |
getMaximumMessageId(), args); |
145 | |
} |
146 | |
} |
147 | 2 | } catch (NumberFormatException e) { |
148 | 2 | Object[] args = { UIComponentUtil.getLabel(component) }; |
149 | 2 | FacesMessage message = FacesMessageUtil.getMessage(context, |
150 | |
getTypeMessageId(), args); |
151 | 2 | throw new ValidatorException(message, getTypeMessageId(), args); |
152 | 0 | } |
153 | |
|
154 | 0 | } |
155 | |
|
156 | |
protected String getMaximumMessageId() { |
157 | 4 | return MAXIMUM_MESSAGE_ID; |
158 | |
} |
159 | |
|
160 | |
protected String getMinimumMessageId() { |
161 | 4 | return MINIMUM_MESSAGE_ID; |
162 | |
} |
163 | |
|
164 | |
protected String getNotInRangeMessageId() { |
165 | 8 | return NOT_IN_RANGE_MESSAGE_ID; |
166 | |
} |
167 | |
|
168 | |
protected String getTypeMessageId() { |
169 | 4 | return TYPE_MESSAGE_ID; |
170 | |
} |
171 | |
|
172 | |
protected Object[] convertArgs(Object[] args) { |
173 | 8 | return args; |
174 | |
} |
175 | |
} |