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