Coverage Report - org.seasar.cubby.tags.SelectTag
 
Classes in this File Line Coverage Branch Coverage Complexity
SelectTag
88%
43/49
62%
10/16
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.contains;
 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  
  * @since 1.0.0
 49  
  */
 50  86
 public class SelectTag extends DynamicAttributesTagSupport {
 51  
 
 52  1
         private static final Logger logger = Logger.getLogger(SelectTag.class);
 53  
 
 54  
         /** name属性。 */
 55  
         private String name;
 56  
 
 57  
         /** option要素リスト。 */
 58  
         private Object items;
 59  
 
 60  
         /** optionのラベルのプロパティ名。 */
 61  
         private String labelProperty;
 62  
 
 63  
         /** optionの値のプロパティ名。 */
 64  
         private String valueProperty;
 65  
 
 66  
         /**
 67  
          * 空のoption要素を出力するかどうか。
 68  
          */
 69  13
         private Boolean emptyOption = Boolean.TRUE;
 70  
 
 71  
         /**
 72  
          * 空のoption要素を出力した場合のラベル文字列
 73  
          */
 74  
         private String emptyOptionLabel;
 75  
 
 76  
         /**
 77  
          * option要素リストをセットします。
 78  
          * 
 79  
          * @param items
 80  
          *            option要素リスト
 81  
          */
 82  
         public void setItems(final Object items) {
 83  13
                 this.items = items;
 84  13
         }
 85  
 
 86  
         /**
 87  
          * optionのラベルのプロパティ名をセットします。
 88  
          * 
 89  
          * @param labelProperty
 90  
          *            optionのラベルのプロパティ名
 91  
          */
 92  
         public void setLabelProperty(final String labelProperty) {
 93  6
                 this.labelProperty = labelProperty;
 94  6
         }
 95  
 
 96  
         /**
 97  
          * optionのラベルのプロパティ名をセットします。
 98  
          * 
 99  
          * @param valueProperty
 100  
          *            optionのラベルのプロパティ名
 101  
          */
 102  
         public void setValueProperty(final String valueProperty) {
 103  7
                 this.valueProperty = valueProperty;
 104  7
         }
 105  
 
 106  
         /**
 107  
          * 空のoption要素を出力するかどうかをセットします。
 108  
          * 
 109  
          * @param emptyOption
 110  
          *            空のoption要素を出力するかどうか
 111  
          */
 112  
         public void setEmptyOption(final Boolean emptyOption) {
 113  2
                 this.emptyOption = emptyOption;
 114  2
         }
 115  
 
 116  
         /**
 117  
          * 空のoption要素を出力した場合のラベル文字列をセットします。
 118  
          * 
 119  
          * @param emptyOptionLabel
 120  
          *            空のoption要素を出力した場合のラベル文字列
 121  
          */
 122  
         public void setEmptyOptionLabel(final String emptyOptionLabel) {
 123  3
                 this.emptyOptionLabel = emptyOptionLabel;
 124  3
         }
 125  
 
 126  
         /**
 127  
          * name属性を設定します。
 128  
          * 
 129  
          * @param name
 130  
          *            name属性
 131  
          */
 132  
         public void setName(final String name) {
 133  13
                 this.name = name;
 134  13
         }
 135  
 
 136  
         /**
 137  
          * {@inheritDoc}
 138  
          */
 139  
         @Override
 140  
         public void doTag() throws JspException, IOException {
 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.getDynamicAttribute();
 145  13
                 final Map<String, String[]> outputValues = outputValues(context);
 146  
 
 147  13
                 if (!errors.getFields().get(this.name).isEmpty()) {
 148  0
                         addClassName(dyn, "fieldError");
 149  
                 }
 150  
 
 151  13
                 final Object[] value = multipleFormValues(context, outputValues,
 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(MessageFormatter.getMessage(
 183  
                                                 "ECUB1001", new Object[] { "items", 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>\n");
 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,
 202  
                                 final Object value) throws IOException {
 203  36
                         out.write("<option value=\"");
 204  36
                         final String itemValue = DynamicAttributesTagSupport
 205  
                                         .toString(itemAdaptor.getItemValue(item));
 206  36
                         final String labelValue = DynamicAttributesTagSupport
 207  
                                         .toString(itemAdaptor.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=\"true\"";
 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(MessageFormatter.getMessage(
 255  
                                                 "ECUB1002", new Object[] { "items", "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  
                 private Object property(final Object bean, final String propertyName) {
 280  30
                         final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(bean
 281  
                                         .getClass());
 282  30
                         final PropertyDesc propertyDesc = beanDesc
 283  
                                         .getPropertyDesc(propertyName);
 284  30
                         return propertyDesc.getValue(bean);
 285  
                 }
 286  
 
 287  
         }
 288  
 
 289  13
         private class EntryItemAdaptor implements ItemAdaptor {
 290  
 
 291  7
                 EntryItemAdaptor() {
 292  7
                         if (valueProperty != null) {
 293  2
                                 logger.log("WCUB1001", new Object[] { "items",
 294  
                                                 Map.class.getSimpleName(), "valueProperty",
 295  
                                                 valueProperty,
 296  
                                                 Entry.class.getSimpleName() + "#getKey()" });
 297  
                         }
 298  7
                         if (labelProperty != null) {
 299  2
                                 logger.log("WCUB1002", new Object[] { "items",
 300  
                                                 Map.class.getSimpleName(), "labelProperty",
 301  
                                                 labelProperty,
 302  
                                                 Entry.class.getSimpleName() + "#getValue()" });
 303  
                         }
 304  7
                 }
 305  
 
 306  
                 /**
 307  
                  * {@inheritDoc}
 308  
                  */
 309  
                 public Object getItemValue(final Object item) {
 310  21
                         return ((Entry<?, ?>) item).getKey();
 311  
                 }
 312  
 
 313  
                 /**
 314  
                  * {@inheritDoc}
 315  
                  */
 316  
                 public Object getLabelValue(final Object item) {
 317  21
                         return ((Entry<?, ?>) item).getValue();
 318  
                 }
 319  
 
 320  
         }
 321  
 
 322  
 }