1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
package javax.faces.convert; |
21 | |
|
22 | |
import javax.faces.component.StateHolder; |
23 | |
import javax.faces.component.UIComponent; |
24 | |
import javax.faces.context.FacesContext; |
25 | |
|
26 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFConverter; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
@JSFConverter |
34 | |
public class EnumConverter implements Converter, StateHolder { |
35 | |
|
36 | |
public static final String CONVERTER_ID = "javax.faces.Enum"; |
37 | |
public static final String ENUM_ID = "javax.faces.converter.EnumConverter.ENUM"; |
38 | |
public static final String ENUM_NO_CLASS_ID = "javax.faces.converter.EnumConverter.ENUM_NO_CLASS"; |
39 | |
|
40 | |
private Class targetClass; |
41 | |
|
42 | 0 | private boolean isTransient = false; |
43 | |
|
44 | |
|
45 | 0 | public EnumConverter() { |
46 | 0 | } |
47 | |
|
48 | 0 | public EnumConverter(Class targetClass) { |
49 | 0 | if (!targetClass.isEnum()) throw new IllegalArgumentException("targetClass for EnumConverter must be an Enum"); |
50 | 0 | this.targetClass = targetClass; |
51 | 0 | } |
52 | |
|
53 | |
public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) throws ConverterException { |
54 | 0 | if (facesContext == null) throw new NullPointerException("facesContext can not be null"); |
55 | 0 | if (uiComponent == null) throw new NullPointerException("uiComponent can not be null"); |
56 | 0 | if (value == null) return ""; |
57 | 0 | checkTargetClass(facesContext, uiComponent, value); |
58 | |
|
59 | 0 | for (Object enumConstant : targetClass.getEnumConstants()) { |
60 | 0 | if (enumConstant == value) return enumConstant.toString(); |
61 | |
} |
62 | |
|
63 | 0 | return value.toString(); |
64 | |
} |
65 | |
|
66 | |
public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) throws ConverterException { |
67 | 0 | if (facesContext == null) throw new NullPointerException("facesContext"); |
68 | 0 | if (uiComponent == null) throw new NullPointerException("uiComponent"); |
69 | 0 | if (value == null) return null; |
70 | 0 | value = value.trim(); |
71 | 0 | if (value.length() == 0) return null; |
72 | 0 | checkTargetClass(facesContext, uiComponent, value); |
73 | |
|
74 | |
|
75 | |
|
76 | |
try { |
77 | 0 | return Enum.valueOf(targetClass, value); |
78 | 0 | } catch (IllegalArgumentException e) { |
79 | 0 | Object[] params = new Object[]{value, |
80 | |
firstConstantOfEnum(), |
81 | |
_MessageUtils.getLabel(facesContext, uiComponent)}; |
82 | |
|
83 | 0 | throw new ConverterException(_MessageUtils.getErrorMessage(facesContext, |
84 | |
ENUM_ID, |
85 | |
params)); |
86 | |
} |
87 | |
} |
88 | |
|
89 | |
private void checkTargetClass(FacesContext facesContext, UIComponent uiComponent, Object value) { |
90 | 0 | if (targetClass == null) { |
91 | 0 | Object[] params = new Object[]{value, _MessageUtils.getLabel(facesContext, uiComponent)}; |
92 | 0 | throw new ConverterException(_MessageUtils.getErrorMessage(facesContext, |
93 | |
ENUM_NO_CLASS_ID, |
94 | |
params)); |
95 | |
} |
96 | 0 | } |
97 | |
|
98 | |
|
99 | |
private String firstConstantOfEnum() { |
100 | 0 | Object[] enumConstants= targetClass.getEnumConstants(); |
101 | |
|
102 | 0 | if (enumConstants.length != 0 ) return enumConstants[0].toString(); |
103 | |
|
104 | 0 | return ""; |
105 | |
} |
106 | |
|
107 | |
public void restoreState(FacesContext context, Object state) { |
108 | 0 | targetClass = (Class)state; |
109 | 0 | } |
110 | |
|
111 | |
public Object saveState(FacesContext context) { |
112 | 0 | return targetClass; |
113 | |
} |
114 | |
|
115 | |
public void setTransient(boolean newTransientValue) { |
116 | 0 | isTransient = newTransientValue; |
117 | 0 | } |
118 | |
|
119 | |
public boolean isTransient() { |
120 | 0 | return isTransient; |
121 | |
} |
122 | |
|
123 | |
} |