1 16 package org.apache.taglibs.input; 17 18 import java.util.Map ; 19 20 import javax.servlet.jsp.JspException ; 21 import javax.servlet.jsp.JspTagException ; 22 import javax.servlet.jsp.JspWriter ; 23 import javax.servlet.jsp.tagext.BodyTagSupport ; 24 25 33 34 public class Option extends BodyTagSupport { 35 36 private String value; 38 private Map attributes; 40 private String attributesText; 42 public void release() { 43 super.release(); 44 value = null; 45 attributes = null; 46 attributesText = null; 47 } 48 49 public int doStartTag() throws JspException { 50 if (getBodyContent() != null) { 53 getBodyContent().clearBody(); 54 } 55 return EVAL_BODY_BUFFERED; 56 } 57 58 public int doEndTag() throws JspException { 59 try { 60 String content = getBodyContent() != null ? getBodyContent() 61 .getString() : null; 62 63 JspWriter out = pageContext.getOut(); 65 66 String value = this.value; 67 if (value == null && content != null) { 68 value = content.trim(); 69 } 70 71 out.print("<option "); 73 if (value != null) { 74 out.print("value=\"" + Util.quote(value) + "\" "); 75 } 76 77 Util.printAttributes(out, attributes); 79 if (attributesText != null) { 80 out.print(attributesText + " "); 81 } 82 83 91 92 if (testAndRemoveChosen(value)) { 93 out.print("selected=\"selected\" "); 94 } 95 96 out.print(">"); 98 if (content != null) { 99 out.print(content); 100 } 101 out.print("</option>"); 102 } catch (Exception ex) { 103 throw new JspTagException (ex.getMessage()); 104 } 105 return EVAL_PAGE; 106 } 107 108 protected boolean testAndRemoveChosen(String value) throws JspException { 109 Select selectTag = (Select) findAncestorWithClass(this, Select.class); 110 if (selectTag != null) { 111 Map chosen = selectTag.getChosen(); 112 if (value != null && chosen != null && chosen.containsKey(value)) { 113 if (!selectTag.isMultiple()) { 114 chosen.remove(value); 115 } 116 return true; 117 } else { 118 return false; 119 } 120 } else { 121 throw new JspTagException ("option tag used outside a select tag"); 122 } 123 } 124 125 public void setValue(String x) { 126 value = x; 127 } 128 129 134 public String getValue() { 135 return value; 136 } 137 138 public void setAttributes(Map x) { 139 attributes = x; 140 } 141 142 147 public Map getAttributes() { 148 return attributes; 149 } 150 151 public void setAttributesText(String x) { 152 attributesText = x; 153 } 154 155 160 public String getAttributesText() { 161 return attributesText; 162 } 163 164 } | Popular Tags |