| 1 package fr.improve.struts.taglib.layout.field; 2 3 import java.lang.reflect.InvocationTargetException ; 4 import java.util.Iterator ; 5 6 import javax.servlet.jsp.JspException ; 7 import javax.servlet.jsp.tagext.TagSupport ; 8 9 import org.apache.commons.beanutils.PropertyUtils; 10 import org.apache.struts.taglib.html.Constants; 11 import org.apache.struts.util.MessageResources; 12 13 import fr.improve.struts.taglib.layout.util.LayoutUtils; 14 import fr.improve.struts.taglib.layout.util.TagUtils; 15 16 33 34 public class OptionsCollectionTag extends TagSupport implements Choice { 35 36 38 41 protected static MessageResources messages = 42 MessageResources.getMessageResources(Constants.Package + ".LocalStrings"); 43 44 47 protected String label = "label"; 48 49 50 53 protected String choiceLabel; 54 55 58 protected String choiceValue; 59 60 61 public String getLabel() { 62 return label; 63 } 64 65 public void setLabel(String label) { 66 this.label = label; 67 } 68 69 72 protected String name = Constants.BEAN_KEY; 73 74 public String getName() { 75 return name; 76 } 77 78 public void setName(String name) { 79 this.name = name; 80 } 81 82 85 protected String property = null; 86 87 public String getProperty() { 88 return property; 89 } 90 91 public void setProperty(String property) { 92 this.property = property; 93 } 94 95 98 protected String value = "value"; 99 100 public String getValue() { 101 return value; 102 } 103 104 public void setValue(String value) { 105 this.value = value; 106 } 107 108 110 115 public int doStartTag() throws JspException { 116 117 118 ChoiceTag choiceTag; 119 choiceTag = (ChoiceTag) findAncestorWithClass(this, ChoiceTag.class); 121 if (choiceTag==null) { 122 throw new JspException (messages.getMessage("optionsTag.select")); 123 } 124 125 Object collection = TagUtils.lookup(pageContext, name, property, null); 127 128 if (collection == null) { 129 JspException e = 130 new JspException (messages.getMessage("optionsCollectionTag.collection")); 131 TagUtils.saveException(pageContext, e); 132 throw e; 133 } 134 135 Iterator iter = LayoutUtils.getIterator(collection); 137 138 StringBuffer sb = new StringBuffer (); 139 140 while (iter.hasNext()) { 142 143 Object bean = iter.next(); 144 Object beanLabel = null; 145 Object beanValue = null; 146 147 try { 149 beanLabel = PropertyUtils.getProperty(bean, label); 150 if (beanLabel == null) { 151 beanLabel = ""; 152 } 153 } catch (IllegalAccessException e) { 154 JspException jspe = 155 new JspException (messages.getMessage("getter.access", label, bean)); 156 TagUtils.saveException(pageContext, jspe); 157 throw jspe; 158 } catch (InvocationTargetException e) { 159 Throwable t = e.getTargetException(); 160 JspException jspe = 161 new JspException (messages.getMessage("getter.result", label, t.toString())); 162 TagUtils.saveException(pageContext, jspe); 163 throw jspe; 164 } catch (NoSuchMethodException e) { 165 JspException jspe = 166 new JspException (messages.getMessage("getter.method", label, bean)); 167 TagUtils.saveException(pageContext, jspe); 168 throw jspe; 169 } 170 171 try { 173 beanValue = PropertyUtils.getProperty(bean, value); 174 if (beanValue == null) { 175 beanValue = ""; 176 } 177 } catch (IllegalAccessException e) { 178 JspException jspe = 179 new JspException (messages.getMessage("getter.access", value, bean)); 180 TagUtils.saveException(pageContext, jspe); 181 throw jspe; 182 } catch (InvocationTargetException e) { 183 Throwable t = e.getTargetException(); 184 JspException jspe = 185 new JspException (messages.getMessage("getter.result", value, t.toString())); 186 TagUtils.saveException(pageContext, jspe); 187 throw jspe; 188 } catch (NoSuchMethodException e) { 189 JspException jspe = 190 new JspException (messages.getMessage("getter.method", value, bean)); 191 TagUtils.saveException(pageContext, jspe); 192 throw jspe; 193 } 194 195 choiceLabel = beanLabel.toString(); 196 choiceValue = beanValue.toString(); 197 198 choiceTag.addChoice(sb, this); 200 } 201 202 TagUtils.write(pageContext, sb.toString()); 204 205 return SKIP_BODY; 206 } 207 208 211 public void release() { 212 super.release(); 213 label = "label"; 214 name = Constants.BEAN_KEY; 215 property = null; 216 value = "value"; 217 } 218 219 222 public String getChoiceLabel() { 223 return choiceLabel; 224 } 225 228 public String getChoiceTooltip() { 229 return null; 230 } 231 234 public String getChoiceValue() { 235 return choiceValue; 236 } 237 } 238 | Popular Tags |