Coverage Report - org.apache.myfaces.taglib.core.DelegateActionListener
 
Classes in this File Line Coverage Branch Coverage Complexity
DelegateActionListener
0%
0/43
0%
0/10
2.444
 
 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$)
 48  
  * @version $Revision$ $Date$
 49  
  */
 50  
 public class DelegateActionListener implements ActionListener, StateHolder
 51  
 {
 52  
 
 53  
     private ValueExpression _type;
 54  
     private ValueExpression _binding;
 55  
 
 56  
     public DelegateActionListener()
 57  0
     {
 58  0
     }
 59  
 
 60  
     public DelegateActionListener(ValueExpression type, ValueExpression binding)
 61  
     {
 62  0
         super();
 63  0
         _type = type;
 64  0
         _binding = binding;
 65  0
     }
 66  
 
 67  
     public boolean isTransient()
 68  
     {
 69  0
         return false;
 70  
     }
 71  
 
 72  
     public void restoreState(FacesContext facesContext, Object state)
 73  
     {
 74  0
         Object[] values = (Object[]) state;
 75  0
         _type = (ValueExpression) values[0];
 76  0
         _binding = (ValueExpression) values[1];
 77  0
     }
 78  
 
 79  
     public Object saveState(FacesContext facesContext)
 80  
     {
 81  0
         Object[] values = new Object[2];
 82  0
         values[0] = _type;
 83  0
         values[1] = _binding;
 84  0
         return values;
 85  
     }
 86  
 
 87  
     public void setTransient(boolean arg0)
 88  
     {
 89  
         // Do nothing        
 90  0
     }
 91  
 
 92  
     private ActionListener _getDelegate()
 93  
     {
 94  0
         return _createActionListener();
 95  
     }
 96  
 
 97  
     private ActionListener _createActionListener()
 98  
     {
 99  0
         FacesContext facesContext = FacesContext.getCurrentInstance();
 100  0
         ActionListener listener = null;
 101  
         // type and/or binding must be specified
 102  
         try
 103  
         {
 104  0
             if (null != _binding)
 105  
             {
 106  
                 try
 107  
                 {
 108  0
                     listener = (ActionListener) _binding.getValue(facesContext
 109  
                             .getELContext());
 110  0
                     if (null != listener)
 111  
                     {
 112  0
                         return listener;
 113  
                     }
 114  
                 }
 115  0
                 catch (ELException e)
 116  
                 {
 117  0
                     throw new ELException("Exception while evaluating the binding attribute", e);
 118  0
                 }
 119  
             }
 120  0
             if (null != _type)
 121  
             {
 122  
                 String className;
 123  0
                 if (_type.isLiteralText())
 124  
                 {
 125  0
                     className = _type.getExpressionString();
 126  
                 }
 127  
                 else
 128  
                 {
 129  0
                     className = (String) _type.getValue(facesContext
 130  
                             .getELContext());
 131  
                 }
 132  
 
 133  0
                 listener = (ActionListener) ClassUtils.newInstance(className);
 134  0
                 if (null != _binding)
 135  
                 {
 136  
                     try
 137  
                     {
 138  0
                         _binding
 139  
                                 .setValue(facesContext.getELContext(), listener);
 140  
                     }
 141  0
                     catch (ELException e)
 142  
                     {
 143  0
                         throw new ELException("Exception while evaluating the binding attribute ");
 144  0
                     }
 145  
                 }
 146  0
                 return listener;
 147  
             }
 148  
         }
 149  0
         catch (ClassCastException e)
 150  
         {
 151  0
             throw new FacesException(e);
 152  0
         }
 153  0
         return listener;
 154  
     }
 155  
 
 156  
     public void processAction(ActionEvent event)
 157  
             throws AbortProcessingException
 158  
     {
 159  0
         _getDelegate().processAction(event);
 160  0
     }
 161  
 
 162  
 }