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  
30  import org.apache.myfaces.shared.util.ClassUtils;
31  
32  
33  /**
34   * This class is used in conjunction with ActionListenerTag. 
35   * 
36   * When a tag like this is in a jsp page:
37   * 
38   * <f:actionListener binding="#{mybean}"/>
39   *  
40   *  or
41   *  
42   * <f:actionListener 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: 1151650 $ $Date: 2011-07-27 17:14:17 -0500 (Wed, 27 Jul 2011) $
49   */
50  public class DelegateActionListener implements ActionListener, StateHolder
51  {
52  
53      private ValueExpression _type;
54      private ValueExpression _binding;
55  
56      public DelegateActionListener()
57      {
58      }
59  
60      public DelegateActionListener(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 ActionListener _getDelegate()
93      {
94          return _createActionListener();
95      }
96  
97      private ActionListener _createActionListener()
98      {
99          FacesContext facesContext = FacesContext.getCurrentInstance();
100         ActionListener listener = null;
101         // type and/or binding must be specified
102         try
103         {
104             if (null != _binding)
105             {
106                 try
107                 {
108                     listener = (ActionListener) _binding.getValue(facesContext
109                             .getELContext());
110                     if (null != listener)
111                     {
112                         return listener;
113                     }
114                 }
115                 catch (ELException e)
116                 {
117                     throw new ELException("Exception while evaluating the binding attribute", e);
118                 }
119             }
120             if (null != _type)
121             {
122                 String className;
123                 if (_type.isLiteralText())
124                 {
125                     className = _type.getExpressionString();
126                 }
127                 else
128                 {
129                     className = (String) _type.getValue(facesContext
130                             .getELContext());
131                 }
132 
133                 listener = (ActionListener) ClassUtils.newInstance(className);
134                 if (null != _binding)
135                 {
136                     try
137                     {
138                         _binding
139                                 .setValue(facesContext.getELContext(), listener);
140                     }
141                     catch (ELException e)
142                     {
143                         throw new ELException("Exception while evaluating the binding attribute ");
144                     }
145                 }
146                 return listener;
147             }
148         }
149         catch (ClassCastException e)
150         {
151             throw new FacesException(e);
152         }
153         return listener;
154     }
155 
156     public void processAction(ActionEvent event)
157             throws AbortProcessingException
158     {
159         _getDelegate().processAction(event);
160     }
161 
162 }