Coverage Report - org.seasar.teeda.core.render.html.HtmlCommandButtonRenderer
 
Classes in this File Line Coverage Branch Coverage Complexity
HtmlCommandButtonRenderer
100%
49/49
100%
14/14
1.75
 
 1  
 /*
 2  
  * Copyright 2004-2011 the Seasar Foundation and the Others.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 15  
  */
 16  
 package org.seasar.teeda.core.render.html;
 17  
 
 18  
 import java.io.IOException;
 19  
 import java.util.Map;
 20  
 
 21  
 import javax.faces.component.UICommand;
 22  
 import javax.faces.component.UIComponent;
 23  
 import javax.faces.component.html.HtmlCommandButton;
 24  
 import javax.faces.context.FacesContext;
 25  
 import javax.faces.context.ResponseWriter;
 26  
 import javax.faces.event.ActionEvent;
 27  
 import javax.faces.internal.IgnoreAttribute;
 28  
 
 29  
 import org.seasar.framework.util.StringUtil;
 30  
 import org.seasar.teeda.core.JsfConstants;
 31  
 import org.seasar.teeda.core.render.AbstractRenderer;
 32  
 import org.seasar.teeda.core.util.RendererUtil;
 33  
 
 34  
 /**
 35  
  * @author manhole
 36  
  */
 37  42
 public class HtmlCommandButtonRenderer extends AbstractRenderer {
 38  
 
 39  
     public static final String COMPONENT_FAMILY = "javax.faces.Command";
 40  
 
 41  
     public static final String RENDERER_TYPE = "javax.faces.Button";
 42  
 
 43  42
     private final IgnoreAttribute ignoreComponent = new IgnoreAttribute();
 44  
     {
 45  42
         addIgnoreAttributeName(JsfConstants.ID_ATTR);
 46  42
         addIgnoreAttributeName(JsfConstants.NAME_ATTR);
 47  42
         addIgnoreAttributeName(JsfConstants.TYPE_ATTR);
 48  42
         addIgnoreAttributeName(JsfConstants.VALUE_ATTR);
 49  42
         addIgnoreAttributeName(JsfConstants.IMAGE_ATTR);
 50  42
         addIgnoreAttributeName(JsfConstants.ACTION_ATTR);
 51  42
         addIgnoreAttributeName(JsfConstants.IMMEDIATE_ATTR);
 52  42
     }
 53  
 
 54  
     public void encodeEnd(FacesContext context, UIComponent component)
 55  
             throws IOException {
 56  14
         assertNotNull(context, component);
 57  12
         if (!component.isRendered()) {
 58  1
             return;
 59  
         }
 60  11
         encodeHtmlCommandButtonEnd(context, (HtmlCommandButton) component);
 61  11
     }
 62  
 
 63  
     protected void encodeHtmlCommandButtonEnd(FacesContext context,
 64  
             HtmlCommandButton htmlCommandButton) throws IOException {
 65  11
         ResponseWriter writer = context.getResponseWriter();
 66  11
         writer.startElement(JsfConstants.INPUT_ELEM, htmlCommandButton);
 67  11
         final String image = getImage(context, htmlCommandButton);
 68  11
         final boolean isImageType = StringUtil.isNotBlank(image);
 69  
         String type;
 70  11
         if (isImageType) {
 71  2
             type = JsfConstants.IMAGE_VALUE;
 72  
         } else {
 73  9
             type = htmlCommandButton.getType();
 74  
         }
 75  11
         RendererUtil.renderAttribute(writer, JsfConstants.TYPE_ATTR, type);
 76  
 
 77  11
         final String id = getIdForRender(context, htmlCommandButton);
 78  11
         RendererUtil
 79  
                 .renderIdAttributeIfNecessary(writer, htmlCommandButton, id);
 80  11
         RendererUtil.renderAttribute(writer, JsfConstants.NAME_ATTR,
 81  
                 htmlCommandButton.getClientId(context));
 82  
 
 83  11
         if (isImageType) {
 84  2
             RendererUtil.renderAttribute(writer, JsfConstants.SRC_ATTR, image,
 85  
                     JsfConstants.IMAGE_ATTR);
 86  
         } else {
 87  9
             renderValueAttribute(context, htmlCommandButton, writer);
 88  
         }
 89  11
         renderRemainAttributes(htmlCommandButton, writer, ignoreComponent);
 90  11
         writer.endElement(JsfConstants.INPUT_ELEM);
 91  11
     }
 92  
 
 93  
     protected String getImage(FacesContext context,
 94  
             HtmlCommandButton htmlCommandButton) {
 95  11
         return htmlCommandButton.getImage();
 96  
     }
 97  
 
 98  
     protected void renderValueAttribute(FacesContext context,
 99  
             UICommand command, ResponseWriter writer) throws IOException {
 100  9
         RendererUtil.renderAttribute(writer, JsfConstants.VALUE_ATTR, command
 101  
                 .getValue());
 102  9
     }
 103  
 
 104  
     public void decode(FacesContext context, UIComponent component) {
 105  7
         assertNotNull(context, component);
 106  5
         decodeHtmlCommandButton(context, (HtmlCommandButton) component);
 107  5
     }
 108  
 
 109  
     protected void decodeHtmlCommandButton(FacesContext context,
 110  
             HtmlCommandButton htmlCommandButton) {
 111  5
         Map paramMap = context.getExternalContext().getRequestParameterMap();
 112  5
         String clientId = htmlCommandButton.getClientId(context);
 113  5
         if (paramMap.containsKey(clientId) ||
 114  
                 paramMap.containsKey(clientId + ".x") ||
 115  
                 paramMap.containsKey(clientId + ".y")) {
 116  4
             if (JsfConstants.RESET_VALUE.equalsIgnoreCase(htmlCommandButton
 117  1
                     .getType())) {
 118  
             } else {
 119  3
                 enqueueEvent(htmlCommandButton);
 120  
             }
 121  
         }
 122  5
     }
 123  
 
 124  
     protected void enqueueEvent(final HtmlCommandButton htmlCommandButton) {
 125  3
         htmlCommandButton.queueEvent(new ActionEvent(htmlCommandButton));
 126  3
     }
 127  
 
 128  
     protected void addIgnoreAttributeName(final String attr) {
 129  294
         ignoreComponent.addAttributeName(attr);
 130  294
     }
 131  
 }