| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 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.UIComponent; |
| 22 | |
import javax.faces.component.html.HtmlSelectBooleanCheckbox; |
| 23 | |
import javax.faces.context.FacesContext; |
| 24 | |
import javax.faces.context.ResponseWriter; |
| 25 | |
import javax.faces.internal.IgnoreAttribute; |
| 26 | |
|
| 27 | |
import org.seasar.teeda.core.JsfConstants; |
| 28 | |
import org.seasar.teeda.core.render.AbstractRenderer; |
| 29 | |
import org.seasar.teeda.core.util.RendererUtil; |
| 30 | |
import org.seasar.teeda.core.util.ValueHolderUtil; |
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | 38 | public class HtmlSelectBooleanCheckboxRenderer extends AbstractRenderer { |
| 36 | |
|
| 37 | |
public static final String COMPONENT_FAMILY = "javax.faces.SelectBoolean"; |
| 38 | |
|
| 39 | |
public static final String RENDERER_TYPE = "javax.faces.Checkbox"; |
| 40 | |
|
| 41 | 38 | private final IgnoreAttribute ignoreComponent = new IgnoreAttribute(); |
| 42 | |
{ |
| 43 | 38 | ignoreComponent.addAttributeName(JsfConstants.ID_ATTR); |
| 44 | 38 | ignoreComponent.addAttributeName(JsfConstants.VALUE_ATTR); |
| 45 | 38 | ignoreComponent.addAttributeName(JsfConstants.SELECTED_ATTR); |
| 46 | 38 | ignoreComponent.addAttributeName(JsfConstants.NAME_ATTR); |
| 47 | 38 | ignoreComponent.addAttributeName(JsfConstants.TYPE_ATTR); |
| 48 | 38 | ignoreComponent.addAttributeName(JsfConstants.CHECKED_ATTR); |
| 49 | 38 | } |
| 50 | |
|
| 51 | |
public void encodeEnd(FacesContext context, UIComponent component) |
| 52 | |
throws IOException { |
| 53 | 10 | assertNotNull(context, component); |
| 54 | 8 | if (!component.isRendered()) { |
| 55 | 1 | return; |
| 56 | |
} |
| 57 | 7 | encodeHtmlSelectBooleanCheckboxEnd(context, |
| 58 | |
(HtmlSelectBooleanCheckbox) component); |
| 59 | 7 | } |
| 60 | |
|
| 61 | |
protected void encodeHtmlSelectBooleanCheckboxEnd(FacesContext context, |
| 62 | |
HtmlSelectBooleanCheckbox htmlSelectBooleanCheckbox) |
| 63 | |
throws IOException { |
| 64 | 7 | ResponseWriter writer = context.getResponseWriter(); |
| 65 | 7 | writer.startElement(JsfConstants.INPUT_ELEM, htmlSelectBooleanCheckbox); |
| 66 | 7 | RendererUtil.renderAttribute(writer, JsfConstants.TYPE_ATTR, |
| 67 | |
JsfConstants.CHECKBOX_VALUE); |
| 68 | 7 | RendererUtil.renderIdAttributeIfNecessary(writer, |
| 69 | |
htmlSelectBooleanCheckbox, getIdForRender(context, |
| 70 | |
htmlSelectBooleanCheckbox)); |
| 71 | 7 | RendererUtil.renderAttribute(writer, JsfConstants.NAME_ATTR, |
| 72 | |
htmlSelectBooleanCheckbox.getClientId(context)); |
| 73 | |
|
| 74 | 7 | RendererUtil.renderAttribute(writer, JsfConstants.VALUE_ATTR, |
| 75 | |
Boolean.TRUE); |
| 76 | 7 | String value = ValueHolderUtil.getValueForRender(context, |
| 77 | |
htmlSelectBooleanCheckbox); |
| 78 | 7 | if (isChecked(value)) { |
| 79 | 2 | renderCheckedAttribute(writer); |
| 80 | |
} |
| 81 | 7 | renderRemainAttributes(htmlSelectBooleanCheckbox, writer, |
| 82 | |
ignoreComponent); |
| 83 | 7 | writer.endElement(JsfConstants.INPUT_ELEM); |
| 84 | 7 | } |
| 85 | |
|
| 86 | |
private boolean isChecked(String value) { |
| 87 | 9 | return "true".equalsIgnoreCase(value); |
| 88 | |
} |
| 89 | |
|
| 90 | |
public void decode(FacesContext context, UIComponent component) { |
| 91 | 7 | assertNotNull(context, component); |
| 92 | 5 | decodeHtmlSelectBooleanCheckbox(context, |
| 93 | |
(HtmlSelectBooleanCheckbox) component); |
| 94 | 5 | } |
| 95 | |
|
| 96 | |
protected void decodeHtmlSelectBooleanCheckbox(FacesContext context, |
| 97 | |
HtmlSelectBooleanCheckbox htmlSelectBooleanCheckbox) { |
| 98 | 5 | if (isChecked(context, htmlSelectBooleanCheckbox)) { |
| 99 | 3 | htmlSelectBooleanCheckbox |
| 100 | |
.setSubmittedValue(Boolean.TRUE.toString()); |
| 101 | |
} else { |
| 102 | 2 | htmlSelectBooleanCheckbox.setSubmittedValue(Boolean.FALSE |
| 103 | |
.toString()); |
| 104 | |
} |
| 105 | 5 | } |
| 106 | |
|
| 107 | |
private boolean isChecked(FacesContext context, UIComponent component) { |
| 108 | 5 | Map paramMap = context.getExternalContext().getRequestParameterMap(); |
| 109 | 5 | String clientId = component.getClientId(context); |
| 110 | 5 | if (paramMap.containsKey(clientId)) { |
| 111 | 4 | String value = (String) paramMap.get(clientId); |
| 112 | 4 | return "on".equalsIgnoreCase(value) |
| 113 | |
|| "yes".equalsIgnoreCase(value) || isChecked(value); |
| 114 | |
} |
| 115 | 1 | return false; |
| 116 | |
} |
| 117 | |
|
| 118 | |
public void addIgnoreAttributeName(final String name) { |
| 119 | 26 | ignoreComponent.addAttributeName(name); |
| 120 | 26 | } |
| 121 | |
|
| 122 | |
} |