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.ValueChangeEvent;
28  import javax.faces.event.ValueChangeListener;
29  
30  /**
31   * This class is used in conjunction with ValueChangeListenerTag.
32   * 
33   * When a tag like this is in a jsp page:
34   * 
35   * <f:valueChangeListener binding="#{mybean}"/>
36   * 
37   * or
38   * 
39   * <f:valueChangeListener type="#{'anyid'}" binding="#{mybean}"/>
40   * 
41   * The value of mybean could be already on the context, so this converter avoid creating a new variable and use the
42   * previous one.
43   * 
44   * @author Leonardo Uribe (latest modification by $Author: slessard $)
45   * @version $Revision: 698799 $ $Date: 2008-09-24 21:03:47 -0500 (Wed, 24 Sep 2008) $
46   */
47  public class DelegateValueChangeListener implements ValueChangeListener, StateHolder
48  {
49  
50      private ValueExpression _type;
51      private ValueExpression _binding;
52  
53      public DelegateValueChangeListener()
54      {
55      }
56  
57      public DelegateValueChangeListener(ValueExpression type, ValueExpression binding)
58      {
59          super();
60          _type = type;
61          _binding = binding;
62      }
63  
64      public boolean isTransient()
65      {
66          return false;
67      }
68  
69      public void restoreState(FacesContext facesContext, Object state)
70      {
71          Object[] values = (Object[]) state;
72          _type = (ValueExpression) values[0];
73          _binding = (ValueExpression) values[1];
74      }
75  
76      public Object saveState(FacesContext facesContext)
77      {
78          Object[] values = new Object[2];
79          values[0] = _type;
80          values[1] = _binding;
81          return values;
82      }
83  
84      public void setTransient(boolean arg0)
85      {
86          // Do nothing
87      }
88  
89      private ValueChangeListener _getDelegate()
90      {
91          return _createValueChangeListener();
92      }
93  
94      private ValueChangeListener _createValueChangeListener()
95      {
96          FacesContext facesContext = FacesContext.getCurrentInstance();
97          ValueChangeListener listener = null;
98          // type and/or binding must be specified
99          try
100         {
101             if (null != _binding)
102             {
103                 try
104                 {
105                     listener = (ValueChangeListener)_binding.getValue(facesContext.getELContext());
106                     if (null != listener)
107                     {
108                         return listener;
109                     }
110                 }
111                 catch (ELException e)
112                 {
113                     // throw new JspException("Exception while evaluating the binding attribute of Component "
114                     // + component.getId(), e);
115                 }
116             }
117 
118             if (null != _type)
119             {
120                 // FIXME: The listener never get created, check when this class is really used.
121                 /*String className;
122                 if (_type.isLiteralText())
123                 {
124                     className = _type.getExpressionString();
125                 }
126                 else
127                 {
128                     className = (String) _type.getValue(facesContext.getELContext());
129                 }*/
130 
131                 listener = null;
132                 // listener = (ActionListener) ClassUtils.newInstance(className);
133                 if (null != _binding)
134                 {
135                     try
136                     {
137                         _binding.setValue(facesContext.getELContext(), listener);
138                     }
139                     catch (ELException e)
140                     {
141                         // throw new JspException("Exception while evaluating the binding attribute of Component "
142                         // + component.getId(), e);
143                     }
144                 }
145                 
146                 return listener;
147             }
148         }
149         catch (ClassCastException e)
150         {
151             throw new FacesException(e);
152         }
153         
154         return listener;
155     }
156 
157     public void processValueChange(ValueChangeEvent event) throws AbortProcessingException
158     {
159         _getDelegate().processValueChange(event);
160     }
161 }