1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.custom.updateactionlistener;
20
21 import javax.faces.FacesException;
22 import javax.faces.component.StateHolder;
23 import javax.faces.component.UIComponentBase;
24 import javax.faces.component.ValueHolder;
25 import javax.faces.context.FacesContext;
26 import javax.faces.convert.Converter;
27 import javax.faces.el.ValueBinding;
28 import javax.faces.event.AbortProcessingException;
29 import javax.faces.event.ActionEvent;
30 import javax.faces.event.ActionListener;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 public class UpdateActionListener
67 implements ActionListener, ValueHolder, StateHolder
68 {
69
70
71 private ValueBinding _propertyBinding;
72 private Object _value;
73 private ValueBinding _valueBinding;
74 private Converter _converter;
75
76 public void setPropertyBinding(ValueBinding propertyBinding)
77 {
78 _propertyBinding = propertyBinding;
79 }
80
81 public ValueBinding getPropertyBinding()
82 {
83 return _propertyBinding;
84 }
85
86 public void setValue(Object value)
87 {
88 _value = value;
89 }
90
91 public Object getValue()
92 {
93 if (_value != null) return _value;
94 ValueBinding vb = getValueBinding();
95 if (vb != null)
96 {
97 FacesContext context = FacesContext.getCurrentInstance();
98 return vb.getValue(context);
99 }
100 return null;
101 }
102
103 public Object getLocalValue()
104 {
105 return _value;
106 }
107
108 public ValueBinding getValueBinding()
109 {
110 return _valueBinding;
111 }
112
113 public void setValueBinding(ValueBinding valueBinding)
114 {
115 _valueBinding = valueBinding;
116 }
117
118 public Converter getConverter()
119 {
120 return _converter;
121 }
122
123 public void setConverter(Converter converter)
124 {
125 _converter = converter;
126 }
127
128 public void processAction(ActionEvent actionEvent) throws AbortProcessingException
129 {
130 FacesContext context = FacesContext.getCurrentInstance();
131 ValueBinding updateBinding = getPropertyBinding();
132 Object v = getValue();
133 if (v != null &&
134 v instanceof String)
135 {
136 Class type = updateBinding.getType(context);
137 if (!type.equals(String.class) && ! type.equals(Object.class))
138 {
139 String converterErrorMessage = "No Converter registered with UpdateActionListener and no appropriate standard converter found. Needed to convert String to " + type.getName();
140 Converter converter = getConverter();
141 if (converter == null)
142 {
143 try
144 {
145 converter = context.getApplication().createConverter(type);
146 }
147 catch (Exception e)
148 {
149 throw new FacesException(converterErrorMessage, e);
150 }
151 }
152 if (null == converter)
153 {
154 throw new FacesException(converterErrorMessage);
155 }
156 v = converter.getAsObject(context, context.getViewRoot(), (String)v);
157 }
158 }
159 updateBinding.setValue(context, v);
160 }
161
162
163
164
165
166 public Object saveState(FacesContext context)
167 {
168 Object values[] = new Object[4];
169 values[0] = UIComponentBase.saveAttachedState(context, _propertyBinding);
170 values[1] = _value;
171 values[2] = UIComponentBase.saveAttachedState(context, _valueBinding);
172 values[3] = UIComponentBase.saveAttachedState(context, _converter);
173 return ((Object) (values));
174 }
175
176 public void restoreState(FacesContext context, Object state)
177 {
178 Object values[] = (Object[])state;
179 _propertyBinding = (ValueBinding)UIComponentBase.restoreAttachedState(context, values[0]);
180 _value = values[1];
181 _valueBinding = (ValueBinding)UIComponentBase.restoreAttachedState(context, values[2]);;
182 _converter = (Converter)UIComponentBase.restoreAttachedState(context, values[3]);;
183 }
184
185 public boolean isTransient()
186 {
187 return false;
188 }
189
190 public void setTransient(boolean newTransientValue)
191 {
192 if (newTransientValue == true) throw new IllegalArgumentException();
193 }
194
195 }