Coverage Report - org.seasar.cubby.tags.SelectTag
 
Classes in this File Line Coverage Branch Coverage Complexity
SelectTag
86%
43/50
64%
9/14
0
SelectTag$BeanItemAdaptor
100%
12/12
100%
4/4
0
SelectTag$EntryItemAdaptor
100%
8/8
100%
4/4
0
SelectTag$ItemAdaptor
N/A
N/A
0
SelectTag$OptionWriter
94%
17/18
67%
4/6
0
 
 1  
 /*
 2  
  * Copyright 2004-2008 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.cubby.tags;
 17  
 
 18  
 import static org.seasar.cubby.tags.TagUtils.addClassName;
 19  
 import static org.seasar.cubby.tags.TagUtils.errors;
 20  
 import static org.seasar.cubby.tags.TagUtils.isChecked;
 21  
 import static org.seasar.cubby.tags.TagUtils.multipleFormValues;
 22  
 import static org.seasar.cubby.tags.TagUtils.outputValues;
 23  
 import static org.seasar.cubby.tags.TagUtils.toAttr;
 24  
 
 25  
 import java.io.IOException;
 26  
 import java.util.Collection;
 27  
 import java.util.Map;
 28  
 import java.util.Map.Entry;
 29  
 
 30  
 import javax.servlet.jsp.JspContext;
 31  
 import javax.servlet.jsp.JspException;
 32  
 import javax.servlet.jsp.JspTagException;
 33  
 import javax.servlet.jsp.JspWriter;
 34  
 
 35  
 import org.seasar.cubby.action.ActionErrors;
 36  
 import org.seasar.cubby.util.CubbyFunctions;
 37  
 import org.seasar.framework.beans.BeanDesc;
 38  
 import org.seasar.framework.beans.PropertyDesc;
 39  
 import org.seasar.framework.beans.factory.BeanDescFactory;
 40  
 import org.seasar.framework.log.Logger;
 41  
 import org.seasar.framework.message.MessageFormatter;
 42  
 
 43  
 /**
 44  
  * selectを出力するタグ
 45  
  * 
 46  
  * @author agata
 47  
  * @author baba
 48  
  */
 49  86
 public class SelectTag extends DynamicAttributesTagSupport {
 50  
 
 51  1
         private static final Logger logger = Logger.getLogger(SelectTag.class);
 52  
 
 53  
         private String name;
 54  
 
 55  
         /**
 56  
          * option要素リスト
 57  
          */
 58  
         private Object items;
 59  
 
 60  
         /**
 61  
          * optionのラベルのプロパティ名
 62  
          */
 63  
         private String labelProperty;
 64  
 
 65  
         /**
 66  
          * optionの値のプロパティ名
 67  
          */
 68  
         private String valueProperty;
 69  
 
 70  
         /**
 71  
          * 空のoption要素を出力するかどうか。
 72  
          */
 73  13
         private Boolean emptyOption = Boolean.TRUE;
 74  
 
 75  
         /**
 76  
          * 空のoption要素を出力した場合のラベル文字列
 77  
          */
 78  
         private String emptyOptionLabel;
 79  
 
 80  
         public Object getItems() {
 81  0
                 return items;
 82  
         }
 83  
 
 84  
         /**
 85  
          * option要素リストをセットします。
 86  
          * 
 87  
          * @param items
 88  
          *            option要素リスト
 89  
          */
 90  
         public void setItems(final Object items) {
 91  13
                 this.items = items;
 92  13
         }
 93  
 
 94  
         /**
 95  
          * optionのラベルのプロパティ名をセットします。
 96  
          * 
 97  
          * @param labelProperty
 98  
          *            optionのラベルのプロパティ名
 99  
          */
 100  
         public void setLabelProperty(final String labelProperty) {
 101  6
                 this.labelProperty = labelProperty;
 102  6
         }
 103  
 
 104  
         /**
 105  
          * optionのラベルのプロパティ名をセットします。
 106  
          * 
 107  
          * @param valueProperty
 108  
          *            optionのラベルのプロパティ名
 109  
          */
 110  
         public void setValueProperty(final String valueProperty) {
 111  7
                 this.valueProperty = valueProperty;
 112  7
         }
 113  
 
 114  
         /**
 115  
          * 空のoption要素を出力するかどうかをセットします。
 116  
          * 
 117  
          * @param emptyOption
 118  
          *            空のoption要素を出力するかどうか
 119  
          */
 120  
         public void setEmptyOption(final Boolean emptyOption) {
 121  2
                 this.emptyOption = emptyOption;
 122  2
         }
 123  
 
 124  
         /**
 125  
          * 空のoption要素を出力した場合のラベル文字列をセットします。
 126  
          * 
 127  
          * @param emptyOptionLabel
 128  
          *            空のoption要素を出力した場合のラベル文字列
 129  
          */
 130  
         public void setEmptyOptionLabel(final String emptyOptionLabel) {
 131  3
                 this.emptyOptionLabel = emptyOptionLabel;
 132  3
         }
 133  
 
 134  
         public void setName(final String name) {
 135  13
                 this.name = name;
 136  13
         }
 137  
 
 138  
         /**
 139  
          * タグの処理
 140  
          */
 141  
         @Override
 142  
         public void doTag() throws JspException, IOException {
 143  13
                 final JspContext context = this.getJspContext();
 144  13
                 final JspWriter out = context.getOut();
 145  13
                 final ActionErrors errors = errors(context);
 146  13
                 final Map<String, Object> dyn = this.getDynamicAttribute();
 147  13
                 final Map<String, String[]> outputValues = outputValues(context);
 148  
 
 149  13
                 if (!errors.getFields().get(this.name).isEmpty()) {
 150  0
                         addClassName(dyn, "fieldError");
 151  
                 }
 152  
 
 153  13
                 final Object[] value = multipleFormValues(context, outputValues,
 154  
                                 this.name);
 155  
 
 156  13
                 out.write("<select name=\"");
 157  13
                 out.write(this.name);
 158  13
                 out.write("\" ");
 159  13
                 out.write(toAttr(dyn));
 160  13
                 out.write(">\n");
 161  
 
 162  13
                 if (emptyOption) {
 163  11
                         out.write("<option value=\"\">");
 164  11
                         out.write(CubbyFunctions.out(emptyOptionLabel));
 165  11
                         out.write("</option>\n");
 166  
                 }
 167  
 
 168  13
                 if (items.getClass().isArray()) {
 169  0
                         final OptionWriter optionWriter = new OptionWriter(
 170  
                                         new BeanItemAdaptor());
 171  0
                         for (final Object item : (Object[]) items) {
 172  0
                                 optionWriter.write(out, item, value);
 173  
                         }
 174  0
                 } else {
 175  
                         final OptionWriter optionWriter;
 176  
                         final Collection<?> collection;
 177  13
                         if (items instanceof Collection) {
 178  6
                                 optionWriter = new OptionWriter(new BeanItemAdaptor());
 179  5
                                 collection = (Collection<?>) items;
 180  7
                         } else if (items instanceof Map) {
 181  7
                                 optionWriter = new OptionWriter(new EntryItemAdaptor());
 182  7
                                 collection = ((Map<?, ?>) items).entrySet();
 183  
                         } else {
 184  0
                                 throw new JspTagException(MessageFormatter.getMessage(
 185  
                                                 "ECUB1001", new Object[] { "items", items.getClass() }));
 186  
                         }
 187  12
                         for (final Object item : collection) {
 188  36
                                 optionWriter.write(out, item, value);
 189  
                         }
 190  
                 }
 191  
 
 192  12
                 out.write("</select>\n");
 193  12
         }
 194  
 
 195  
         static class OptionWriter {
 196  
 
 197  
                 private final ItemAdaptor itemAdaptor;
 198  
 
 199  12
                 OptionWriter(final ItemAdaptor itemAdaptor) {
 200  12
                         this.itemAdaptor = itemAdaptor;
 201  12
                 }
 202  
 
 203  
                 public void write(final JspWriter out, final Object item,
 204  
                                 final Object value) throws IOException {
 205  36
                         out.write("<option value=\"");
 206  36
                         final String itemValue = DynamicAttributesTagSupport
 207  
                                         .toString(itemAdaptor.getItemValue(item));
 208  36
                         final String labelValue = DynamicAttributesTagSupport
 209  
                                         .toString(itemAdaptor.getLabelValue(item));
 210  36
                         out.write(CubbyFunctions.out(itemValue));
 211  36
                         out.write("\" ");
 212  36
                         out.write(selected(itemValue, value));
 213  36
                         out.write(">");
 214  36
                         out.write(CubbyFunctions.out(labelValue));
 215  36
                         out.write("</option>\n");
 216  36
                 }
 217  
 
 218  
                 private String selected(final String value, final Object values) {
 219  36
                         if (value == null || values == null) {
 220  0
                                 return "";
 221  
                         }
 222  36
                         if (isChecked(value, values)) {
 223  14
                                 return "selected=\"true\"";
 224  
                         } else {
 225  22
                                 return "";
 226  
                         }
 227  
                 }
 228  
         }
 229  
 
 230  
         interface ItemAdaptor {
 231  
 
 232  
                 Object getItemValue(Object item);
 233  
 
 234  
                 Object getLabelValue(Object item);
 235  
 
 236  
         }
 237  
 
 238  
         class BeanItemAdaptor implements ItemAdaptor {
 239  
 
 240  6
                 public BeanItemAdaptor() throws JspTagException {
 241  6
                         if (valueProperty == null) {
 242  1
                                 throw new JspTagException(MessageFormatter.getMessage(
 243  
                                                 "ECUB1002", new Object[] { "items", "valueProperty" }));
 244  
                         }
 245  5
                 }
 246  
 
 247  
                 public Object getItemValue(final Object item) {
 248  18
                         return property(item, valueProperty);
 249  
                 }
 250  
 
 251  
                 public Object getLabelValue(final Object item) {
 252  
                         final Object labelValue;
 253  15
                         if (labelProperty == null) {
 254  3
                                 labelValue = getItemValue(item);
 255  
                         } else {
 256  12
                                 labelValue = property(item, labelProperty);
 257  
                         }
 258  15
                         return labelValue;
 259  
                 }
 260  
 
 261  
                 private Object property(final Object bean, final String propertyName) {
 262  30
                         final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(bean
 263  
                                         .getClass());
 264  30
                         final PropertyDesc propertyDesc = beanDesc
 265  
                                         .getPropertyDesc(propertyName);
 266  30
                         return propertyDesc.getValue(bean);
 267  
                 }
 268  
 
 269  
         }
 270  
 
 271  13
         class EntryItemAdaptor implements ItemAdaptor {
 272  
 
 273  7
                 public EntryItemAdaptor() {
 274  7
                         if (valueProperty != null) {
 275  2
                                 logger.log("WCUB1001", new Object[] { "items",
 276  
                                                 Map.class.getSimpleName(), "valueProperty",
 277  
                                                 valueProperty,
 278  
                                                 Entry.class.getSimpleName() + "#getKey()" });
 279  
                         }
 280  7
                         if (labelProperty != null) {
 281  2
                                 logger.log("WCUB1002", new Object[] { "items",
 282  
                                                 Map.class.getSimpleName(), "labelProperty",
 283  
                                                 labelProperty,
 284  
                                                 Entry.class.getSimpleName() + "#getValue()" });
 285  
                         }
 286  7
                 }
 287  
 
 288  
                 public Object getItemValue(final Object item) {
 289  21
                         return ((Entry<?, ?>) item).getKey();
 290  
                 }
 291  
 
 292  
                 public Object getLabelValue(final Object item) {
 293  21
                         return ((Entry<?, ?>) item).getValue();
 294  
                 }
 295  
 
 296  
         }
 297  
 
 298  
 }