1 package org.seasar.cubby.tags; 2 3 import java.io.IOException; 4 import java.util.Map; 5 6 import javax.servlet.jsp.JspException; 7 import javax.servlet.jsp.JspWriter; 8 import javax.servlet.jsp.PageContext; 9 10 import org.seasar.cubby.util.CubbyFunctions; 11 import org.seasar.cubby.util.CubbyHelperFunctions; 12 13 public class SelectTag extends DynamicAttributesTagSupport { 14 15 private Object items; 16 private String labelProperty; 17 private String valueProperty; 18 private boolean emptyOption = true; 19 private String emptyOptionLabel; 20 21 public Object getItems() { 22 return items; 23 } 24 25 public void setItems(final Object items) { 26 this.items = items; 27 } 28 29 public String getLabelProperty() { 30 return labelProperty; 31 } 32 33 public void setLabelProperty(final String labelProperty) { 34 this.labelProperty = labelProperty; 35 } 36 37 public String getValueProperty() { 38 return valueProperty; 39 } 40 41 public void setValueProperty(final String valueProperty) { 42 this.valueProperty = valueProperty; 43 } 44 45 public boolean isEmptyOption() { 46 return emptyOption; 47 } 48 49 public void setEmptyOption(final boolean emptyOption) { 50 this.emptyOption = emptyOption; 51 } 52 53 public String getEmptyOptionLabel() { 54 return emptyOptionLabel; 55 } 56 57 public void setEmptyOptionLabel(final String emptyOptionLabel) { 58 this.emptyOptionLabel = emptyOptionLabel; 59 } 60 61 @SuppressWarnings("unchecked") 62 @Override 63 public void doTag() throws JspException, IOException { 64 final Object form = getJspContext().getAttribute("__form", PageContext.REQUEST_SCOPE); 65 final Object value = CubbyHelperFunctions.formValue2(getDynamicAttribute(), form, getJspContext(), "value"); 66 getJspContext().setAttribute("value", value, PageContext.PAGE_SCOPE); 67 final Map fieldErros = (Map) getJspContext().getAttribute("fieldErrors", PageContext.REQUEST_SCOPE); 68 if (fieldErros.get(getDynamicAttribute().get("name")) != null) { 69 CubbyHelperFunctions.addClassName(getDynamicAttribute(), "fieldError"); 70 } 71 final JspWriter out = getJspContext().getOut(); 72 out.write("<select "); 73 out.write(CubbyHelperFunctions.toAttr(getDynamicAttribute())); 74 out.write(">\n"); 75 if (emptyOption) { 76 out.write("<option value=\"\">"); 77 out.write(CubbyFunctions.out(emptyOptionLabel)); 78 out.write("</option>\n"); 79 } 80 for (Object item : CubbyHelperFunctions.toArray(items)) { 81 out.write("<option value=\""); 82 String itemValue = toString(CubbyHelperFunctions.property(item, valueProperty)); 83 String labelValue = labelProperty == null ? itemValue : toString(CubbyHelperFunctions.property(item, labelProperty)); 84 out.write(CubbyFunctions.out(itemValue)); 85 out.write("\" "); 86 out.write(CubbyHelperFunctions.selected(itemValue, value)); 87 out.write(">"); 88 out.write(CubbyFunctions.out(labelValue)); 89 out.write("</option>\n"); 90 } 91 out.write("</select>\n"); 92 } 93 }