View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.taglib.core;
20  
21  import javax.el.ELException;
22  import javax.el.ValueExpression;
23  import javax.faces.FacesException;
24  import javax.faces.component.StateHolder;
25  import javax.faces.context.FacesContext;
26  import javax.faces.event.AbortProcessingException;
27  import javax.faces.event.ActionEvent;
28  import javax.faces.event.ActionListener;
29  import javax.faces.event.ValueChangeEvent;
30  import javax.faces.event.ValueChangeListener;
31  
32  
33  /**
34   * This class is used in conjunction with ValueChangeListenerTag. 
35   * 
36   * When a tag like this is in a jsp page:
37   * 
38   * <f:valueChangeListener binding="#{mybean}"/>
39   *  
40   *  or
41   *  
42   * <f:valueChangeListener type="#{'anyid'}" binding="#{mybean}"/>
43   * 
44   * The value of mybean could be already on the context, so this
45   * converter avoid creating a new variable and use the previous one.
46   * 
47   * @author Leonardo Uribe (latest modification by $Author: lu4242 $)
48   * @version $Revision: 600199 $ $Date: 2007-12-01 16:28:15 -0500 (Sat, 01 Dec 2007) $
49   */
50  public class DelegateValueChangeListener implements ValueChangeListener, StateHolder
51  {
52  
53      private ValueExpression _type;
54      private ValueExpression _binding;
55  
56      public DelegateValueChangeListener()
57      {
58      }
59  
60      public DelegateValueChangeListener(ValueExpression type, ValueExpression binding)
61      {
62          super();
63          _type = type;
64          _binding = binding;
65      }
66  
67      public boolean isTransient()
68      {
69          return false;
70      }
71  
72      public void restoreState(FacesContext facesContext, Object state)
73      {
74          Object[] values = (Object[]) state;
75          _type = (ValueExpression) values[0];
76          _binding = (ValueExpression) values[1];
77      }
78  
79      public Object saveState(FacesContext facesContext)
80      {
81          Object[] values = new Object[2];
82          values[0] = _type;
83          values[1] = _binding;
84          return values;
85      }
86  
87      public void setTransient(boolean arg0)
88      {
89          // Do nothing        
90      }
91  
92      private ValueChangeListener _getDelegate()
93      {
94          return _createValueChangeListener();
95      }
96  
97      private ValueChangeListener _createValueChangeListener()
98      {
99          FacesContext facesContext = FacesContext.getCurrentInstance();
100         ValueChangeListener listener = null;
101         // type and/or binding must be specified
102         try
103         {
104             if (null != _binding)
105             {
106                 try
107                 {
108                     listener = (ValueChangeListener) _binding.getValue(facesContext
109                             .getELContext());
110                     if (null != listener)
111                     {
112                         return listener;
113                     }
114                 }
115                 catch (ELException e)
116                 {
117                     //throw new JspException("Exception while evaluating the binding attribute of Component "
118                     //        + component.getId(), e);
119                 }
120             }
121             if (null != _type)
122             {
123                 String className;
124                 if (_type.isLiteralText())
125                 {
126                     className = _type.getExpressionString();
127                 }
128                 else
129                 {
130                     className = (String) _type.getValue(facesContext
131                             .getELContext());
132                 }
133                 listener = null;
134                 //listener = (ActionListener) ClassUtils.newInstance(className);
135                 if (null != _binding)
136                 {
137                     try
138                     {
139                         _binding
140                                 .setValue(facesContext.getELContext(), listener);
141                     }
142                     catch (ELException e)
143                     {
144                         //throw new JspException("Exception while evaluating the binding attribute of Component "
145                         //        + component.getId(), e);
146                     }
147                 }
148                 return listener;
149             }
150         }
151         catch (ClassCastException e)
152         {
153             throw new FacesException(e);
154         }
155         return listener;
156     }
157 
158     public void processValueChange(ValueChangeEvent event)
159             throws AbortProcessingException
160     {
161         _getDelegate().processValueChange(event);
162     }
163 
164 }