Coverage Report - org.apache.myfaces.shared.renderkit.html.HtmlOutcomeTargetButtonRendererBase
 
Classes in this File Line Coverage Branch Coverage Complexity
HtmlOutcomeTargetButtonRendererBase
0%
0/67
0%
0/32
4.4
 
 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.shared.renderkit.html;
 20  
 
 21  
 import java.io.IOException;
 22  
 import java.util.List;
 23  
 import java.util.Map;
 24  
 
 25  
 import javax.faces.component.UIComponent;
 26  
 import javax.faces.component.UIOutcomeTarget;
 27  
 import javax.faces.component.behavior.ClientBehavior;
 28  
 import javax.faces.component.behavior.ClientBehaviorHolder;
 29  
 import javax.faces.component.html.HtmlOutcomeTargetButton;
 30  
 import javax.faces.context.ExternalContext;
 31  
 import javax.faces.context.FacesContext;
 32  
 import javax.faces.context.ResponseWriter;
 33  
 
 34  
 import org.apache.myfaces.shared.renderkit.ClientBehaviorEvents;
 35  
 import org.apache.myfaces.shared.renderkit.JSFAttr;
 36  
 import org.apache.myfaces.shared.renderkit.RendererUtils;
 37  
 import org.apache.myfaces.shared.renderkit.html.util.ResourceUtils;
 38  
 
 39  
 /**
 40  
  * @since 2.0
 41  
  */
 42  0
 public class HtmlOutcomeTargetButtonRendererBase extends HtmlRenderer
 43  
 {
 44  
 
 45  
     public boolean getRendersChildren()
 46  
     {
 47  0
         return true;
 48  
     }
 49  
 
 50  
     public void encodeBegin(FacesContext facesContext, UIComponent uiComponent)
 51  
             throws IOException
 52  
     {
 53  0
         super.encodeBegin(facesContext, uiComponent); //check for NP
 54  
 
 55  0
         String clientId = uiComponent.getClientId(facesContext);
 56  
 
 57  0
         ResponseWriter writer = facesContext.getResponseWriter();
 58  
 
 59  0
         Map<String, List<ClientBehavior>> behaviors = null;
 60  0
         if (uiComponent instanceof ClientBehaviorHolder)
 61  
         {
 62  0
             behaviors = ((ClientBehaviorHolder) uiComponent).getClientBehaviors();
 63  0
             if (!behaviors.isEmpty())
 64  
             {
 65  0
                 ResourceUtils.renderDefaultJsfJsInlineIfNecessary(facesContext, facesContext.getResponseWriter());
 66  
             }
 67  
         }
 68  
 
 69  0
         writer.startElement(HTML.INPUT_ELEM, uiComponent);
 70  
 
 71  0
         writer.writeAttribute(HTML.ID_ATTR, clientId, JSFAttr.ID_ATTR);
 72  0
         writer.writeAttribute(HTML.NAME_ATTR, clientId, JSFAttr.ID_ATTR);
 73  
 
 74  0
         String image = getImage(uiComponent);
 75  0
         ExternalContext externalContext = facesContext.getExternalContext();
 76  
 
 77  0
         if (image != null)
 78  
         {
 79  
             // type="image"
 80  0
             writer.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_IMAGE, JSFAttr.TYPE_ATTR);
 81  0
             String src = facesContext.getApplication().getViewHandler().getResourceURL(facesContext, image);
 82  0
             writer.writeURIAttribute(HTML.SRC_ATTR, externalContext.encodeResourceURL(src), JSFAttr.IMAGE_ATTR);
 83  0
         }
 84  
         else
 85  
         {
 86  
             // type="button"
 87  0
             writer.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_BUTTON, JSFAttr.TYPE_ATTR);
 88  0
             Object value = RendererUtils.getStringValue(facesContext, uiComponent);
 89  0
             if (value != null)
 90  
             {
 91  0
                 writer.writeAttribute(HTML.VALUE_ATTR, value, JSFAttr.VALUE_ATTR);
 92  
             }
 93  
         }
 94  
 
 95  0
         String outcomeTargetHref = HtmlRendererUtils.getOutcomeTargetHref(
 96  
                     facesContext, (UIOutcomeTarget) uiComponent);
 97  
 
 98  0
         if (HtmlRendererUtils.isDisabled(uiComponent) || outcomeTargetHref == null)
 99  
         {
 100  
             // disable the button - if disabled is true or no fitting NavigationCase was found
 101  0
             HtmlRendererUtils.renderHTMLAttribute(writer, HTML.DISABLED_ATTR, HTML.DISABLED_ATTR, true);
 102  
         }
 103  
         else
 104  
         {
 105  
             // render onClick attribute
 106  0
             String href = facesContext.getExternalContext().encodeResourceURL(outcomeTargetHref);
 107  
 
 108  0
             String commandOnClick = (String) uiComponent.getAttributes().get(HTML.ONCLICK_ATTR);
 109  0
             String navigationJavaScript = "window.location.href = '" + href + "'";
 110  
 
 111  0
             if (behaviors != null && !behaviors.isEmpty())
 112  
             {
 113  0
                 HtmlRendererUtils.renderBehaviorizedAttribute(facesContext, writer, HTML.ONCLICK_ATTR, 
 114  
                         uiComponent, ClientBehaviorEvents.CLICK, null, behaviors, HTML.ONCLICK_ATTR, 
 115  
                         commandOnClick, navigationJavaScript);
 116  
             }
 117  
             else
 118  
             {
 119  0
                 StringBuilder onClick = new StringBuilder();
 120  
     
 121  0
                 if (commandOnClick != null)
 122  
                 {
 123  0
                     onClick.append(commandOnClick);
 124  0
                     onClick.append(';');
 125  
                 }
 126  0
                 onClick.append(navigationJavaScript);
 127  
     
 128  0
                 writer.writeAttribute(HTML.ONCLICK_ATTR, onClick.toString(), null);
 129  
             }
 130  
         }
 131  
 
 132  0
         if (isCommonPropertiesOptimizationEnabled(facesContext))
 133  
         {
 134  0
             long commonPropertiesMarked = CommonPropertyUtils.getCommonPropertiesMarked(uiComponent);
 135  
             
 136  0
             if (behaviors != null && !behaviors.isEmpty() && uiComponent instanceof ClientBehaviorHolder)
 137  
             {
 138  0
                 HtmlRendererUtils.renderBehaviorizedEventHandlersWithoutOnclick(
 139  
                         facesContext, writer, uiComponent, behaviors);
 140  0
                 HtmlRendererUtils.renderBehaviorizedFieldEventHandlersWithoutOnchangeAndOnselect(
 141  
                         facesContext, writer, uiComponent, behaviors);
 142  
             }
 143  
             else
 144  
             {
 145  0
                 CommonPropertyUtils.renderEventPropertiesWithoutOnclick(
 146  
                         writer, commonPropertiesMarked, uiComponent);
 147  0
                 CommonPropertyUtils.renderFocusBlurEventProperties(writer, commonPropertiesMarked, uiComponent);
 148  
             }
 149  
             
 150  0
             CommonPropertyUtils.renderCommonFieldPassthroughPropertiesWithoutDisabledAndEvents(
 151  
                     writer, commonPropertiesMarked, uiComponent);
 152  0
             if ((commonPropertiesMarked & CommonPropertyConstants.ALT_PROP) != 0)
 153  
             {
 154  0
                 HtmlRendererUtils.renderHTMLStringAttribute(writer, uiComponent,
 155  
                         HTML.ALT_ATTR, HTML.ALT_ATTR);
 156  
             }
 157  0
         }
 158  
         else
 159  
         {
 160  0
             if (uiComponent instanceof ClientBehaviorHolder)
 161  
             {
 162  0
                 HtmlRendererUtils.renderBehaviorizedEventHandlersWithoutOnclick(
 163  
                         facesContext, writer, uiComponent, behaviors);
 164  0
                 HtmlRendererUtils.renderBehaviorizedFieldEventHandlersWithoutOnchangeAndOnselect(
 165  
                         facesContext, writer, uiComponent, behaviors);
 166  
             }
 167  
             else
 168  
             {
 169  0
                 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent,
 170  
                         HTML.EVENT_HANDLER_ATTRIBUTES_WITHOUT_ONCLICK);
 171  0
                 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent,
 172  
                         HTML.COMMON_FIELD_EVENT_ATTRIBUTES_WITHOUT_ONSELECT_AND_ONCHANGE);
 173  
     
 174  
             }
 175  0
             HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent,
 176  
                     HTML.COMMON_FIELD_PASSTROUGH_ATTRIBUTES_WITHOUT_DISABLED_AND_EVENTS);
 177  0
             HtmlRendererUtils.renderHTMLStringAttribute(writer, uiComponent,
 178  
                     HTML.ALT_ATTR, HTML.ALT_ATTR);
 179  
         }
 180  
 
 181  0
         writer.flush();
 182  0
     }
 183  
 
 184  
     private String getImage(UIComponent uiComponent)
 185  
     {
 186  0
         if (uiComponent instanceof HtmlOutcomeTargetButton)
 187  
         {
 188  0
             return ((HtmlOutcomeTargetButton) uiComponent).getImage();
 189  
         }
 190  0
         return (String) uiComponent.getAttributes().get(JSFAttr.IMAGE_ATTR);
 191  
     }
 192  
 
 193  
     public void encodeChildren(FacesContext facesContext, UIComponent component)
 194  
             throws IOException
 195  
     {
 196  0
         RendererUtils.renderChildren(facesContext, component);
 197  0
     }
 198  
 
 199  
     public void encodeEnd(FacesContext facesContext, UIComponent component)
 200  
             throws IOException
 201  
     {
 202  0
         super.encodeEnd(facesContext, component); //check for NP
 203  
 
 204  0
         ResponseWriter writer = facesContext.getResponseWriter();
 205  
 
 206  0
         writer.endElement(HTML.INPUT_ELEM);
 207  0
     }
 208  
 }