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