KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > updateactionlistener > UpdateActionListener


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.custom.updateactionlistener;
17
18 import javax.faces.FacesException;
19 import javax.faces.component.StateHolder;
20 import javax.faces.component.UIComponentBase;
21 import javax.faces.component.ValueHolder;
22 import javax.faces.context.FacesContext;
23 import javax.faces.convert.Converter;
24 import javax.faces.el.ValueBinding;
25 import javax.faces.event.AbortProcessingException;
26 import javax.faces.event.ActionEvent;
27 import javax.faces.event.ActionListener;
28
29 /**
30  * @author Manfred Geiler (latest modification by $Author: matze $)
31  * @version $Revision: 1.4 $ $Date: 2004/10/13 11:50:59 $
32  * $Log: UpdateActionListener.java,v $
33  * Revision 1.4 2004/10/13 11:50:59 matze
34  * renamed packages to org.apache
35  *
36  * Revision 1.3 2004/07/01 21:53:11 mwessendorf
37  * ASF switch
38  *
39  * Revision 1.2 2004/05/05 17:36:32 o_rossmueller
40  * fix #948629: no converter is needed to convert a String value to Object
41  *
42  * Revision 1.1 2004/03/31 12:15:28 manolito
43  * custom component refactoring
44  *
45  */

46 public class UpdateActionListener
47         implements ActionListener, ValueHolder, StateHolder
48 {
49     //private static final Log log = LogFactory.getLog(UpdateActionListener.class);
50

51     private ValueBinding _propertyBinding;
52     private Object JavaDoc _value;
53     private ValueBinding _valueBinding;
54     private Converter _converter;
55
56     public void setPropertyBinding(ValueBinding propertyBinding)
57     {
58         _propertyBinding = propertyBinding;
59     }
60
61     public ValueBinding getPropertyBinding()
62     {
63         return _propertyBinding;
64     }
65
66     public void setValue(Object JavaDoc value)
67     {
68         _value = value;
69     }
70
71     public Object JavaDoc getValue()
72     {
73         if (_value != null) return _value;
74         ValueBinding vb = getValueBinding();
75         if (vb != null)
76         {
77             FacesContext context = FacesContext.getCurrentInstance();
78             return vb.getValue(context);
79         }
80         return null;
81     }
82
83     public Object JavaDoc getLocalValue()
84     {
85         return _value;
86     }
87
88     public ValueBinding getValueBinding()
89     {
90         return _valueBinding;
91     }
92
93     public void setValueBinding(ValueBinding valueBinding)
94     {
95         _valueBinding = valueBinding;
96     }
97
98     public Converter getConverter()
99     {
100         return _converter;
101     }
102
103     public void setConverter(Converter converter)
104     {
105         _converter = converter;
106     }
107
108     public void processAction(ActionEvent actionEvent) throws AbortProcessingException
109     {
110         FacesContext context = FacesContext.getCurrentInstance();
111         ValueBinding updateBinding = getPropertyBinding();
112         Object JavaDoc v = getValue();
113         if (v != null &&
114             v instanceof String JavaDoc)
115         {
116             Class JavaDoc type = updateBinding.getType(context);
117             if (!type.equals(String JavaDoc.class) && ! type.equals(Object JavaDoc.class))
118             {
119                 Converter converter = getConverter();
120                 if (converter == null)
121                 {
122                     try
123                     {
124                         converter = context.getApplication().createConverter(type);
125                     }
126                     catch (Exception JavaDoc e)
127                     {
128                         throw new FacesException("No Converter registered with UpdateActionListener and no appropriate standard converter found. Needed to convert String to " + type.getName(), e);
129                     }
130                 }
131                 v = converter.getAsObject(context, context.getViewRoot(), (String JavaDoc)v);
132             }
133         }
134         updateBinding.setValue(context, v);
135     }
136
137
138
139     // StateHolder methods
140

141     public Object JavaDoc saveState(FacesContext context)
142     {
143         Object JavaDoc values[] = new Object JavaDoc[4];
144         values[0] = UIComponentBase.saveAttachedState(context, _propertyBinding);
145         values[1] = _value;
146         values[2] = UIComponentBase.saveAttachedState(context, _valueBinding);
147         values[3] = UIComponentBase.saveAttachedState(context, _converter);
148         return ((Object JavaDoc) (values));
149     }
150
151     public void restoreState(FacesContext context, Object JavaDoc state)
152     {
153         Object JavaDoc values[] = (Object JavaDoc[])state;
154         _propertyBinding = (ValueBinding)UIComponentBase.restoreAttachedState(context, values[0]);
155         _value = values[1];
156         _valueBinding = (ValueBinding)UIComponentBase.restoreAttachedState(context, values[2]);;
157         _converter = (Converter)UIComponentBase.restoreAttachedState(context, values[3]);;
158     }
159
160     public boolean isTransient()
161     {
162         return false;
163     }
164
165     public void setTransient(boolean newTransientValue)
166     {
167         if (newTransientValue == true) throw new IllegalArgumentException JavaDoc();
168     }
169
170 }
171
Popular Tags