Coverage Report - org.seasar.teeda.core.render.html.HtmlSelectManyListboxRenderer
 
Classes in this File Line Coverage Branch Coverage Complexity
HtmlSelectManyListboxRenderer
99%
84/85
100%
26/26
2.083
 
 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.Iterator;
 20  
 
 21  
 import javax.faces.component.UIComponent;
 22  
 import javax.faces.component.UIOutput;
 23  
 import javax.faces.component.html.HtmlSelectManyListbox;
 24  
 import javax.faces.context.FacesContext;
 25  
 import javax.faces.context.ResponseWriter;
 26  
 import javax.faces.convert.ConverterException;
 27  
 import javax.faces.internal.IgnoreAttribute;
 28  
 import javax.faces.internal.SelectItemsIterator;
 29  
 import javax.faces.internal.UIComponentUtil;
 30  
 import javax.faces.model.SelectItem;
 31  
 import javax.faces.model.SelectItemGroup;
 32  
 
 33  
 import org.seasar.framework.util.ArrayIterator;
 34  
 import org.seasar.framework.util.ArrayUtil;
 35  
 import org.seasar.teeda.core.JsfConstants;
 36  
 import org.seasar.teeda.core.render.AbstractInputRenderer;
 37  
 import org.seasar.teeda.core.util.RendererUtil;
 38  
 import org.seasar.teeda.core.util.ValueHolderUtil;
 39  
 
 40  
 /**
 41  
  * @author manhole
 42  
  * @author shot
 43  
  */
 44  175
 public class HtmlSelectManyListboxRenderer extends AbstractInputRenderer {
 45  
 
 46  
     public static final String COMPONENT_FAMILY = "javax.faces.SelectMany";
 47  
 
 48  
     public static final String RENDERER_TYPE = "javax.faces.Listbox";
 49  
 
 50  175
     private final IgnoreAttribute ignoreComponent = new IgnoreAttribute();
 51  
     {
 52  175
         ignoreComponent.addAttributeName(JsfConstants.ID_ATTR);
 53  175
         ignoreComponent.addAttributeName(JsfConstants.VALUE_ATTR);
 54  175
         ignoreComponent.addAttributeName(JsfConstants.SIZE_ATTR);
 55  175
         ignoreComponent.addAttributeName("selectedValues");
 56  175
         ignoreComponent.addAttributeName(JsfConstants.DISABLED_CLASS_ATTR);
 57  175
         ignoreComponent.addAttributeName(JsfConstants.ENABLED_CLASS_ATTR);
 58  175
         ignoreComponent.addAttributeName(JsfConstants.STYLE_CLASS_ATTR);
 59  175
         ignoreComponent.addAttributeName(JsfConstants.MULTIPLE_ATTR);
 60  175
         ignoreComponent.addAttributeName(JsfConstants.NAME_ATTR);
 61  175
     }
 62  
 
 63  
     public void encodeEnd(FacesContext context, UIComponent component)
 64  
             throws IOException {
 65  74
         assertNotNull(context, component);
 66  66
         if (!component.isRendered()) {
 67  4
             return;
 68  
         }
 69  62
         encodeHtmlSelectListboxEnd(context, component);
 70  62
     }
 71  
 
 72  
     protected void encodeHtmlSelectListboxEnd(FacesContext context,
 73  
             UIComponent component) throws IOException {
 74  
 
 75  62
         Iterator it = new SelectItemsIterator(component);
 76  62
         if (!it.hasNext()) {
 77  4
             return;
 78  
         }
 79  
 
 80  58
         ResponseWriter writer = context.getResponseWriter();
 81  58
         writer.startElement(JsfConstants.SELECT_ELEM, component);
 82  
 
 83  58
         RendererUtil.renderIdAttributeIfNecessary(writer, component,
 84  
                 getIdForRender(context, component));
 85  58
         RendererUtil.renderAttribute(writer, JsfConstants.NAME_ATTR, component
 86  
                 .getClientId(context));
 87  58
         renderMultiple(context, component, writer);
 88  58
         renderSize(context, component, writer);
 89  58
         renderStyleClass(context, component, writer);
 90  58
         renderRemainAttributes(component, writer, ignoreComponent);
 91  58
         final String[] selectedValues = getValuesForRender(context, component);
 92  58
         renderSelectItems(context, component, writer, it, selectedValues);
 93  
 
 94  58
         writer.endElement(JsfConstants.SELECT_ELEM);
 95  58
     }
 96  
 
 97  
     protected String[] getValuesForRender(FacesContext context,
 98  
             UIComponent component) {
 99  26
         return ValueHolderUtil.getValuesForRender(context, component);
 100  
     }
 101  
 
 102  
     protected void renderStyleClass(FacesContext context,
 103  
             UIComponent component, ResponseWriter writer) throws IOException {
 104  14
         HtmlSelectManyListbox htmlSelectManyListbox = (HtmlSelectManyListbox) component;
 105  14
         final String styleClass = htmlSelectManyListbox.getStyleClass();
 106  14
         RendererUtil.renderAttribute(writer, JsfConstants.STYLE_CLASS_ATTR,
 107  
                 styleClass);
 108  14
     }
 109  
 
 110  
     protected void renderSize(FacesContext context, UIComponent component,
 111  
             ResponseWriter writer) throws IOException {
 112  31
         final int size = getSize(component);
 113  31
         RendererUtil.renderAttribute(writer, JsfConstants.SIZE_ATTR,
 114  
                 new Integer(size));
 115  31
     }
 116  
 
 117  
     private int getSize(UIComponent component) {
 118  31
         int size = UIComponentUtil.getPrimitiveIntAttribute(component,
 119  
                 JsfConstants.SIZE_ATTR);
 120  31
         if (0 < size) {
 121  4
             return size;
 122  
         }
 123  27
         size = 0;
 124  27
         for (Iterator it = new SelectItemsIterator(component); it.hasNext();) {
 125  43
             SelectItem item = (SelectItem) it.next();
 126  43
             if (item instanceof SelectItemGroup) {
 127  2
                 SelectItemGroup itemGroup = (SelectItemGroup) item;
 128  2
                 size += itemGroup.getSelectItems().length;
 129  
             }
 130  43
             size++;
 131  
         }
 132  27
         return size;
 133  
     }
 134  
 
 135  
     protected void renderMultiple(FacesContext context, UIComponent component,
 136  
             ResponseWriter writer) throws IOException {
 137  26
         RendererUtil.renderAttribute(writer, JsfConstants.MULTIPLE_ATTR,
 138  
                 JsfConstants.MULTIPLE_VALUE);
 139  26
     }
 140  
 
 141  
     protected void renderSelectItems(FacesContext context,
 142  
             UIComponent component, ResponseWriter writer, Iterator it,
 143  
             String[] selectedValues) throws IOException {
 144  
 
 145  159
         while (it.hasNext()) {
 146  97
             final SelectItem selectItem = (SelectItem) it.next();
 147  
 
 148  97
             if (selectItem instanceof SelectItemGroup) {
 149  4
                 SelectItemGroup selectItemGroup = (SelectItemGroup) selectItem;
 150  4
                 SelectItem[] selectItems = selectItemGroup.getSelectItems();
 151  4
                 Iterator selectItemsIt = new ArrayIterator(selectItems);
 152  4
                 writer.startElement(JsfConstants.OPTGROUP_ELEM, component);
 153  4
                 RendererUtil.renderAttribute(writer, JsfConstants.LABEL_ATTR,
 154  
                         selectItemGroup.getLabel());
 155  
                 // TODO case: optgroup is disabled
 156  4
                 renderSelectItems(context, component, writer, selectItemsIt,
 157  
                         selectedValues);
 158  4
                 writer.endElement(JsfConstants.OPTGROUP_ELEM);
 159  
             } else {
 160  93
                 writer.startElement(JsfConstants.OPTION_ELEM, component);
 161  93
                 final Object value = selectItem.getValue();
 162  93
                 RendererUtil.renderAttribute(writer, JsfConstants.VALUE_ATTR,
 163  
                         value);
 164  
 
 165  93
                 final boolean disabled = UIComponentUtil.isDisabled(component)
 166  
                         || selectItem.isDisabled();
 167  93
                 final String labelClass = getLabelStyleClass(component,
 168  
                         disabled);
 169  93
                 if (labelClass != null) {
 170  12
                     RendererUtil.renderAttribute(writer,
 171  
                             JsfConstants.CLASS_ATTR, labelClass);
 172  
                 }
 173  93
                 if (value != null
 174  
                         && isSelected(selectedValues, value.toString())) {
 175  13
                     renderSelectedAttribute(writer);
 176  
                 }
 177  93
                 if (selectItem.isDisabled()) {
 178  20
                     renderDisabledAttribute(writer);
 179  
                 }
 180  93
                 writer.writeText(selectItem.getLabel(), null);
 181  93
                 writer.endElement(JsfConstants.OPTION_ELEM);
 182  
             }
 183  
         }
 184  62
     }
 185  
 
 186  
     private boolean isSelected(final String[] selectedValues, final String value) {
 187  92
         return ArrayUtil.contains(selectedValues, value);
 188  
     }
 189  
 
 190  
     public void decode(FacesContext context, UIComponent component) {
 191  8
         assertNotNull(context, component);
 192  4
         getDecoder().decodeMany(context, component);
 193  4
     }
 194  
 
 195  
     public Object getConvertedValue(FacesContext context,
 196  
             UIComponent component, Object submittedValue)
 197  
             throws ConverterException {
 198  4
         assertNotNull(context, component);
 199  0
         return RendererUtil.getConvertedUIOutputValues(context,
 200  
                 (UIOutput) component, submittedValue);
 201  
     }
 202  
 
 203  
     public void addIgnoreAttributeName(final String name) {
 204  127
         ignoreComponent.addAttributeName(name);
 205  127
     }
 206  
 
 207  
 }