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   * <code>
38   * &lt;f:actionListener binding="#{mybean}"/&gt;
39   *  
40   *  or
41   *  
42   * &lt;f:actionListener type="#{'anyid'}" binding="#{mybean}"/&gt;
43   * </code>
44   * 
45   * The value of mybean could be already on the context, so this
46   * converter avoid creating a new variable and use the previous one.
47   * 
48   * @author Leonardo Uribe (latest modification by $Author$)
49   * @version $Revision$ $Date$
50   */
51  public class DelegateActionListener implements ActionListener, StateHolder
52  {
53  
54      private ValueExpression _type;
55      private ValueExpression _binding;
56  
57      public DelegateActionListener()
58      {
59      }
60  
61      public DelegateActionListener(ValueExpression type, ValueExpression binding)
62      {
63          super();
64          _type = type;
65          _binding = binding;
66      }
67  
68      public boolean isTransient()
69      {
70          return false;
71      }
72  
73      public void restoreState(FacesContext facesContext, Object state)
74      {
75          Object[] values = (Object[]) state;
76          _type = (ValueExpression) values[0];
77          _binding = (ValueExpression) values[1];
78      }
79  
80      public Object saveState(FacesContext facesContext)
81      {
82          Object[] values = new Object[2];
83          values[0] = _type;
84          values[1] = _binding;
85          return values;
86      }
87  
88      public void setTransient(boolean arg0)
89      {
90          // Do nothing        
91      }
92  
93      private ActionListener _getDelegate()
94      {
95          return _createActionListener();
96      }
97  
98      private ActionListener _createActionListener()
99      {
100         FacesContext facesContext = FacesContext.getCurrentInstance();
101         ActionListener listener = null;
102         // type and/or binding must be specified
103         try
104         {
105             if (null != _binding)
106             {
107                 try
108                 {
109                     listener = (ActionListener) _binding.getValue(facesContext
110                             .getELContext());
111                     if (null != listener)
112                     {
113                         return listener;
114                     }
115                 }
116                 catch (ELException e)
117                 {
118                     throw new ELException("Exception while evaluating the binding attribute", 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 
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 ELException("Exception while evaluating the binding attribute ");
145                     }
146                 }
147                 return listener;
148             }
149         }
150         catch (ClassCastException e)
151         {
152             throw new FacesException(e);
153         }
154         return listener;
155     }
156 
157     public void processAction(ActionEvent event)
158             throws AbortProcessingException
159     {
160         _getDelegate().processAction(event);
161     }
162 
163 }