Coverage Report - org.apache.myfaces.view.facelets.tag.jsf.core.ValueChangeListenerHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
ValueChangeListenerHandler
0%
0/35
0%
0/14
4.667
ValueChangeListenerHandler$LazyValueChangeListener
0%
0/20
0%
0/12
4.667
 
 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.view.facelets.tag.jsf.core;
 20  
 
 21  
 import java.io.IOException;
 22  
 import java.io.Serializable;
 23  
 
 24  
 import javax.el.ELException;
 25  
 import javax.el.ValueExpression;
 26  
 import javax.faces.FacesException;
 27  
 import javax.faces.component.EditableValueHolder;
 28  
 import javax.faces.component.UIComponent;
 29  
 import javax.faces.context.FacesContext;
 30  
 import javax.faces.event.AbortProcessingException;
 31  
 import javax.faces.event.ValueChangeEvent;
 32  
 import javax.faces.event.ValueChangeListener;
 33  
 import javax.faces.view.EditableValueHolderAttachedObjectHandler;
 34  
 import javax.faces.view.facelets.ComponentHandler;
 35  
 import javax.faces.view.facelets.FaceletContext;
 36  
 import javax.faces.view.facelets.FaceletException;
 37  
 import javax.faces.view.facelets.TagAttribute;
 38  
 import javax.faces.view.facelets.TagAttributeException;
 39  
 import javax.faces.view.facelets.TagConfig;
 40  
 import javax.faces.view.facelets.TagException;
 41  
 import javax.faces.view.facelets.TagHandler;
 42  
 
 43  
 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletAttribute;
 44  
 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletTag;
 45  
 import org.apache.myfaces.view.facelets.FaceletCompositionContext;
 46  
 import org.apache.myfaces.view.facelets.util.ReflectionUtil;
 47  
 
 48  
 /**
 49  
  * Register an ValueChangeListener instance on the UIComponent associated with the closest parent UIComponent custom
 50  
  * action.<p/> See <a target="_new"
 51  
  * href="http://java.sun.com/j2ee/javaserverfaces/1.1_01/docs/tlddocs/f/valueChangeListener.html">tag documentation</a>.
 52  
  * 
 53  
  * @author Jacob Hookom
 54  
  * @version $Id$
 55  
  */
 56  
 @JSFFaceletTag(
 57  
         name = "f:valueChangeListener",
 58  
         bodyContent = "empty", 
 59  
         tagClass="org.apache.myfaces.taglib.core.ValueChangeListenerTag")
 60  
 public final class ValueChangeListenerHandler extends TagHandler
 61  
     implements EditableValueHolderAttachedObjectHandler
 62  
 {
 63  
 
 64  
     private static class LazyValueChangeListener implements ValueChangeListener, Serializable
 65  
     {
 66  
 
 67  
         private static final long serialVersionUID = 7613811124326963180L;
 68  
 
 69  
         private final String type;
 70  
 
 71  
         private final ValueExpression binding;
 72  
 
 73  
         public LazyValueChangeListener(String type, ValueExpression binding)
 74  0
         {
 75  0
             this.type = type;
 76  0
             this.binding = binding;
 77  0
         }
 78  
 
 79  
         public void processValueChange(ValueChangeEvent event) throws AbortProcessingException
 80  
         {
 81  0
             ValueChangeListener instance = null;
 82  0
             FacesContext faces = FacesContext.getCurrentInstance();
 83  0
             if (faces == null)
 84  
             {
 85  0
                 return;
 86  
             }
 87  0
             if (this.binding != null)
 88  
             {
 89  0
                 instance = (ValueChangeListener) binding.getValue(faces.getELContext());
 90  
             }
 91  0
             if (instance == null && this.type != null)
 92  
             {
 93  
                 try
 94  
                 {
 95  0
                     instance = (ValueChangeListener) ReflectionUtil.forName(this.type).newInstance();
 96  
                 }
 97  0
                 catch (Exception e)
 98  
                 {
 99  0
                     throw new AbortProcessingException("Couldn't Lazily instantiate ValueChangeListener", e);
 100  0
                 }
 101  0
                 if (this.binding != null)
 102  
                 {
 103  0
                     binding.setValue(faces.getELContext(), instance);
 104  
                 }
 105  
             }
 106  0
             if (instance != null)
 107  
             {
 108  0
                 instance.processValueChange(event);
 109  
             }
 110  0
         }
 111  
     }
 112  
 
 113  
     private final TagAttribute binding;
 114  
 
 115  
     private final String listenerType;
 116  
 
 117  
     public ValueChangeListenerHandler(TagConfig config)
 118  
     {
 119  0
         super(config);
 120  0
         this.binding = this.getAttribute("binding");
 121  0
         TagAttribute type = this.getAttribute("type");
 122  0
         if (type != null)
 123  
         {
 124  0
             if (!type.isLiteral())
 125  
             {
 126  0
                 throw new TagAttributeException(type, "Must be a literal class name of type ValueChangeListener");
 127  
             }
 128  
             else
 129  
             {
 130  
                 // test it out
 131  
                 try
 132  
                 {
 133  0
                     ReflectionUtil.forName(type.getValue());
 134  
                 }
 135  0
                 catch (ClassNotFoundException e)
 136  
                 {
 137  0
                     throw new TagAttributeException(type, "Couldn't qualify ValueChangeListener", e);
 138  0
                 }
 139  
             }
 140  0
             this.listenerType = type.getValue();
 141  
         }
 142  
         else
 143  
         {
 144  0
             this.listenerType = null;
 145  
         }
 146  0
     }
 147  
 
 148  
     /**
 149  
      * See taglib documentation.
 150  
      * 
 151  
      * @see javax.faces.view.facelets.FaceletHandler#apply(javax.faces.view.facelets.FaceletContext, javax.faces.component.UIComponent)
 152  
      */
 153  
     public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException,
 154  
             ELException
 155  
     {
 156  0
         if (!ComponentHandler.isNew(parent))
 157  
         {
 158  0
             return;
 159  
         }
 160  0
         if (parent instanceof EditableValueHolder)
 161  
         {
 162  0
             applyAttachedObject(ctx.getFacesContext(), parent);
 163  
         }
 164  0
         else if (UIComponent.isCompositeComponent(parent))
 165  
         {
 166  0
             FaceletCompositionContext mctx = FaceletCompositionContext.getCurrentInstance(ctx);
 167  0
             mctx.addAttachedObjectHandler(parent, this);
 168  0
         }
 169  
         else
 170  
         {
 171  0
             throw new TagException(this.tag,
 172  
                     "Parent not composite component or an instance of EditableValueHolder: " + parent);
 173  
         }
 174  0
     }
 175  
 
 176  
     public void applyAttachedObject(FacesContext context, UIComponent parent)
 177  
     {
 178  
         // Retrieve the current FaceletContext from FacesContext object
 179  0
         FaceletContext faceletContext = (FaceletContext) context.getAttributes().get(
 180  
                 FaceletContext.FACELET_CONTEXT_KEY);
 181  
 
 182  0
         EditableValueHolder evh = (EditableValueHolder) parent;
 183  0
         ValueExpression b = null;
 184  0
         if (this.binding != null)
 185  
         {
 186  0
             b = this.binding.getValueExpression(faceletContext, ValueChangeListener.class);
 187  
         }
 188  0
         ValueChangeListener listener = new LazyValueChangeListener(this.listenerType, b);
 189  0
         evh.addValueChangeListener(listener);
 190  0
     }
 191  
 
 192  
     /**
 193  
      * TODO: Document me!
 194  
      */
 195  
     @JSFFaceletAttribute
 196  
     public String getFor()
 197  
     {
 198  0
         TagAttribute forAttribute = getAttribute("for");
 199  
         
 200  0
         if (forAttribute == null)
 201  
         {
 202  0
             return null;
 203  
         }
 204  
         else
 205  
         {
 206  0
             return forAttribute.getValue();
 207  
         }
 208  
     }
 209  
 
 210  
 }