Coverage Report - org.apache.myfaces.view.facelets.tag.jsf.core.ResetValuesActionListenerHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
ResetValuesActionListenerHandler
0%
0/54
0%
0/34
4.333
ResetValuesActionListenerHandler$LiteralResetValuesActionListener
0%
0/25
0%
0/10
4.333
ResetValuesActionListenerHandler$ResetValuesActionListener
0%
0/36
0%
0/16
4.333
 
 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  
 import java.util.ArrayList;
 24  
 import java.util.Arrays;
 25  
 import java.util.Collection;
 26  
 import java.util.Collections;
 27  
 import java.util.List;
 28  
 import javax.el.ELException;
 29  
 import javax.el.ValueExpression;
 30  
 import javax.faces.FacesException;
 31  
 import javax.faces.component.ActionSource;
 32  
 import javax.faces.component.UIComponent;
 33  
 import javax.faces.component.UIViewRoot;
 34  
 import javax.faces.context.FacesContext;
 35  
 import javax.faces.event.AbortProcessingException;
 36  
 import javax.faces.event.ActionEvent;
 37  
 import javax.faces.event.ActionListener;
 38  
 import javax.faces.view.ActionSource2AttachedObjectHandler;
 39  
 import javax.faces.view.AttachedObjectHandler;
 40  
 import javax.faces.view.Location;
 41  
 import javax.faces.view.facelets.ComponentHandler;
 42  
 import javax.faces.view.facelets.FaceletContext;
 43  
 import javax.faces.view.facelets.FaceletException;
 44  
 import javax.faces.view.facelets.TagAttribute;
 45  
 import javax.faces.view.facelets.TagConfig;
 46  
 import javax.faces.view.facelets.TagException;
 47  
 import javax.faces.view.facelets.TagHandler;
 48  
 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletAttribute;
 49  
 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletTag;
 50  
 import org.apache.myfaces.shared.renderkit.JSFAttr;
 51  
 import org.apache.myfaces.view.facelets.FaceletCompositionContext;
 52  
 import org.apache.myfaces.view.facelets.el.CompositeComponentELUtils;
 53  
 
 54  
 /**
 55  
  *
 56  
  * @author Leonardo Uribe
 57  
  */
 58  
 @JSFFaceletTag(
 59  
             name = "f:resetValues",
 60  
             bodyContent = "empty")
 61  0
 public class ResetValuesActionListenerHandler extends TagHandler
 62  
     implements ActionSource2AttachedObjectHandler
 63  
 {
 64  
     /**
 65  
      * 
 66  
      */
 67  
     @JSFFaceletAttribute(name = "render", className = "javax.el.ValueExpression",
 68  
                          deferredValueType = "java.lang.Object")
 69  
     private final TagAttribute _render;
 70  
     
 71  
     private final Collection<String> _clientIds;
 72  
 
 73  
     public ResetValuesActionListenerHandler(TagConfig config)
 74  
     {
 75  0
         super(config);
 76  0
         _render = getRequiredAttribute("render");
 77  0
         if (_render.isLiteral())
 78  
         {
 79  0
             String value = _render.getValue();
 80  0
             if (value == null)
 81  
             {
 82  0
                 _clientIds = Collections.emptyList();
 83  
             }
 84  
             else
 85  
             {
 86  0
                 String[] arrValue = value.split(" ");
 87  0
                 _clientIds = Arrays.asList(arrValue);
 88  
             }
 89  0
         }
 90  
         else
 91  
         {
 92  0
             _clientIds = null;
 93  
         }
 94  0
     }
 95  
 
 96  
     /*
 97  
      * (non-Javadoc)
 98  
      * 
 99  
      * @see javax.faces.view.facelets.FaceletHandler#apply(javax.faces.view.facelets.FaceletContext, javax.faces.component.UIComponent)
 100  
      */
 101  
     public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException,
 102  
             ELException
 103  
     {
 104  
         //Apply only if we are creating a new component
 105  0
         if (!ComponentHandler.isNew(parent))
 106  
         {
 107  0
             return;
 108  
         }
 109  0
         if (parent instanceof ActionSource)
 110  
         {
 111  0
             applyAttachedObject(ctx.getFacesContext(), parent, false);
 112  
         }
 113  0
         else if (UIComponent.isCompositeComponent(parent))
 114  
         {
 115  0
             if (getAttribute(JSFAttr.FOR_ATTR) == null)
 116  
             {
 117  0
                 throw new TagException(tag, "is nested inside a composite component"
 118  
                         + " but does not have a for attribute.");
 119  
             }
 120  0
             FaceletCompositionContext mctx = FaceletCompositionContext.getCurrentInstance(ctx);
 121  0
             mctx.addAttachedObjectHandler(parent, this);
 122  0
         }
 123  
         else
 124  
         {
 125  0
             throw new TagException(this.tag,
 126  
                     "Parent is not composite component or of type ActionSource, type is: " + parent);
 127  
         }
 128  0
     }
 129  
 
 130  
     public void applyAttachedObject(FacesContext context, UIComponent parent)
 131  
     {
 132  0
         applyAttachedObject(context, parent, true);
 133  0
     }
 134  
     
 135  
     public void applyAttachedObject(FacesContext context, UIComponent parent, boolean checkForParentCC)
 136  
     {
 137  0
         UIComponent topParentComponent = null;
 138  
         // Retrieve the current FaceletContext from FacesContext object
 139  0
         FaceletContext faceletContext = (FaceletContext) context.getAttributes().get(
 140  
                 FaceletContext.FACELET_CONTEXT_KEY);
 141  
         
 142  0
         if (checkForParentCC)
 143  
         {
 144  0
             FaceletCompositionContext mctx = FaceletCompositionContext.getCurrentInstance(faceletContext);
 145  0
             UIComponent parentComponent = parent;
 146  0
             while (parentComponent != null)
 147  
             {
 148  0
                 if (UIComponent.isCompositeComponent(parentComponent))
 149  
                 {
 150  0
                     List<AttachedObjectHandler> handlerList = mctx.getAttachedObjectHandlers(parentComponent);
 151  0
                     if (handlerList != null && handlerList.contains(this))
 152  
                     {
 153  
                         //Found, but we need to go right to the top for it. 
 154  0
                         topParentComponent = parentComponent;
 155  
                     }
 156  
                 }
 157  0
                 parentComponent = parentComponent.getParent();
 158  
             }
 159  
         }
 160  
 
 161  0
         ActionSource as = (ActionSource) parent;
 162  0
         ActionListener listener = null;
 163  0
         if (_render.isLiteral())
 164  
         {
 165  0
             listener = new LiteralResetValuesActionListener(_clientIds,
 166  
                 topParentComponent != null ? 
 167  
                 (Location) topParentComponent.getAttributes().get(
 168  
                     CompositeComponentELUtils.LOCATION_KEY) : null);
 169  
         }
 170  
         else
 171  
         {
 172  0
             listener = new ResetValuesActionListener(_render
 173  
                 .getValueExpression(faceletContext, Object.class) ,
 174  
                 topParentComponent != null ? 
 175  
                 (Location) topParentComponent.getAttributes().get(
 176  
                     CompositeComponentELUtils.LOCATION_KEY) : null);
 177  
         }
 178  0
         as.addActionListener(listener);
 179  0
     }
 180  
 
 181  
     /**
 182  
      * TODO: Document me!
 183  
      */
 184  
     @JSFFaceletAttribute
 185  
     public String getFor()
 186  
     {
 187  0
         TagAttribute forAttribute = getAttribute("for");
 188  
         
 189  0
         if (forAttribute == null)
 190  
         {
 191  0
             return null;
 192  
         }
 193  
         else
 194  
         {
 195  0
             return forAttribute.getValue();
 196  
         }
 197  
     }
 198  
     
 199  
     private final static class ResetValuesActionListener implements ActionListener, Serializable
 200  
     {
 201  
         private static final long serialVersionUID = -9200000013153262119L;
 202  
 
 203  
         private ValueExpression renderExpression;
 204  
         
 205  
         private Location topCompositeComponentReference;
 206  
         
 207  
         private ResetValuesActionListener()
 208  0
         {
 209  0
         }
 210  
         
 211  
         public ResetValuesActionListener(ValueExpression renderExpression, Location location)
 212  0
         {
 213  0
             this.renderExpression = renderExpression;
 214  0
             this.topCompositeComponentReference = location;
 215  0
         }
 216  
 
 217  
         public void processAction(ActionEvent event) throws AbortProcessingException
 218  
         {
 219  0
             FacesContext faces = FacesContext.getCurrentInstance();
 220  0
             if (faces == null)
 221  
             {
 222  0
                 return;
 223  
             }
 224  0
             UIViewRoot root = faces.getViewRoot();
 225  0
             if (root == null)
 226  
             {
 227  0
                 return;
 228  
             }
 229  0
             Object value = renderExpression.getValue(faces.getELContext());
 230  0
             Collection<String> clientIds = null;
 231  0
             if (value == null)
 232  
             {
 233  0
                 value = Collections.emptyList();
 234  
             }
 235  0
             if (value instanceof Collection)
 236  
             {
 237  0
                 clientIds = (Collection) value;
 238  
             }
 239  0
             else if (value instanceof String)
 240  
             {
 241  0
                 String[] arrValue = ((String)value).split(" ");
 242  0
                 clientIds = Arrays.asList(arrValue);
 243  0
             }
 244  
             else
 245  
             {
 246  0
                 throw new IllegalArgumentException("Type " + value.getClass()
 247  
                         + " not supported for attribute render");
 248  
             }
 249  
             
 250  
             // Calculate the final clientIds
 251  0
             UIComponent contextComponent = null;
 252  0
             if (topCompositeComponentReference != null)
 253  
             {
 254  0
                 contextComponent = CompositeComponentELUtils.getCompositeComponentBasedOnLocation(
 255  
                     faces, event.getComponent(), topCompositeComponentReference);
 256  0
                 if (contextComponent == null)
 257  
                 {
 258  0
                     contextComponent = event.getComponent();
 259  
                 }
 260  
                 else
 261  
                 {
 262  0
                     contextComponent = contextComponent.getParent();
 263  
                 }
 264  
             }
 265  
             else
 266  
             {
 267  0
                 contextComponent = event.getComponent();
 268  
             }
 269  0
             List<String> list = new ArrayList<String>();
 270  0
             for (String id : clientIds)
 271  
             {
 272  0
                 list.add(getComponentId(faces, contextComponent, id));
 273  0
             }
 274  
             
 275  
             // Call resetValues
 276  0
             root.resetValues(faces, list);
 277  0
         }
 278  
     }
 279  
     
 280  
     private final static class LiteralResetValuesActionListener implements ActionListener, Serializable
 281  
     {
 282  
         private static final long serialVersionUID = -9200000013153262119L;
 283  
 
 284  
         private Collection<String> clientIds;
 285  
         
 286  
         private Location topCompositeComponentReference;
 287  
         
 288  
         private LiteralResetValuesActionListener()
 289  0
         {
 290  0
         }
 291  
         
 292  
         public LiteralResetValuesActionListener(Collection<String> clientIds, Location location)
 293  0
         {
 294  0
             this.clientIds = clientIds;
 295  0
             this.topCompositeComponentReference = location;
 296  0
         }
 297  
 
 298  
         public void processAction(ActionEvent event) throws AbortProcessingException
 299  
         {
 300  0
             FacesContext faces = FacesContext.getCurrentInstance();
 301  0
             if (faces == null)
 302  
             {
 303  0
                 return;
 304  
             }
 305  0
             UIViewRoot root = faces.getViewRoot();
 306  0
             if (root == null)
 307  
             {
 308  0
                 return;
 309  
             }
 310  
             
 311  
             // Calculate the final clientIds
 312  0
             UIComponent contextComponent = null;
 313  0
             if (topCompositeComponentReference != null)
 314  
             {
 315  0
                 contextComponent = CompositeComponentELUtils.getCompositeComponentBasedOnLocation(
 316  
                     faces, event.getComponent(), topCompositeComponentReference);
 317  0
                 if (contextComponent == null)
 318  
                 {
 319  0
                     contextComponent = event.getComponent();
 320  
                 }
 321  
                 else
 322  
                 {
 323  0
                     contextComponent = contextComponent.getParent();
 324  
                 }
 325  
             }
 326  
             else
 327  
             {
 328  0
                 contextComponent = event.getComponent();
 329  
             }
 330  0
             List<String> list = new ArrayList<String>();
 331  0
             for (String id : clientIds)
 332  
             {
 333  0
                 list.add(getComponentId(faces, contextComponent, id));
 334  0
             }
 335  
             
 336  0
             root.resetValues(faces, list);
 337  0
         }
 338  
     }
 339  
     
 340  
     private static final String getComponentId(FacesContext facesContext, 
 341  
         UIComponent contextComponent, String id)
 342  
     {
 343  0
         UIComponent target = contextComponent.findComponent(id);
 344  0
         if (target == null)
 345  
         {
 346  0
             target = contextComponent.findComponent(
 347  
                 facesContext.getNamingContainerSeparatorChar() + id);
 348  
         }
 349  0
         if (target != null)
 350  
         {
 351  0
             return target.getClientId(facesContext);
 352  
         }
 353  0
         return id;
 354  
     }
 355  
 }