1 package fr.improve.struts.taglib.layout.field; 2 3 import java.util.Iterator ; 4 5 import javax.servlet.jsp.JspException ; 6 import javax.servlet.jsp.tagext.TagSupport ; 7 8 import org.apache.struts.taglib.html.Constants; 9 import org.apache.struts.util.MessageResources; 10 11 import fr.improve.struts.taglib.layout.el.Expression; 12 import fr.improve.struts.taglib.layout.util.LayoutUtils; 13 import fr.improve.struts.taglib.layout.util.TagUtils; 14 15 28 29 public class OptionsTag extends TagSupport implements Choice { 30 31 34 protected static MessageResources messages = 35 MessageResources.getMessageResources(Constants.Package + ".LocalStrings"); 36 37 42 protected String collection = null; 43 44 47 protected String labelName = null; 48 49 52 protected String labelProperty = null; 53 54 57 protected String name=null; 58 59 62 protected String property=null; 63 protected String jspProperty = null; 64 65 68 protected String sourceOf; 69 70 protected String choiceLabel; 71 protected String choiceValue; 72 73 78 public int doEndTag() throws JspException { 79 80 ChoiceTag choiceTag; 81 try { 83 choiceTag = (ChoiceTag) getParent(); 84 } catch (ClassCastException e) { 85 throw new JspException (messages.getMessage("optionsTag.select")); 86 } 87 88 initDynamicValues(); 90 91 StringBuffer sb = new StringBuffer (); 92 if (collection != null) { 94 Iterator collIterator = getIterator(collection, null); 95 while (collIterator.hasNext()) { 96 Object bean = collIterator.next(); 97 Object value = null; 98 Object label = null;; 99 100 value = getValueFromBean(bean, property); 101 if (value == null) 102 value = ""; 103 if (labelProperty != null) 104 label = getLabelFromBean(bean, labelProperty); 105 else 106 label = value; 107 if (label == null) 108 label = ""; 109 choiceLabel = label.toString(); 110 choiceValue = value.toString(); 111 choiceTag.addChoice(sb, this); 112 } 113 if (sourceOf!=null) { 114 initDependentCombo(); 115 } 116 } 117 118 else { 120 121 Iterator valuesIterator = getIterator(name, property); 123 Iterator labelsIterator = null; 124 if ((labelName == null) && (labelProperty == null)) 125 labelsIterator = getIterator(name, property); else 127 labelsIterator = getIterator(labelName, labelProperty); 128 129 while (valuesIterator.hasNext()) { 131 Object obj = valuesIterator.next(); 132 choiceValue = obj==null ? null : obj.toString(); 133 choiceLabel = choiceValue; 134 if (labelsIterator.hasNext()) { 135 obj = labelsIterator.next(); 136 choiceLabel = obj==null ? null : obj.toString(); 137 } 138 choiceTag.addChoice(sb, this); 139 } 140 } 141 142 TagUtils.write(pageContext, sb.toString()); 144 145 reset(); 147 148 return EVAL_PAGE; 149 150 } 151 152 155 protected void initDynamicValues() { 156 jspProperty = property; 157 property = Expression.evaluate(jspProperty, pageContext); 158 } 159 160 163 protected void reset() { 164 property = jspProperty; 165 jspProperty = null; 166 } 167 168 171 protected void initDependentCombo() { 172 SelectTag lc_select = (SelectTag) findAncestorWithClass(this, SelectTag.class); 173 OptionsDependentTag.DependentInfo lc_info = (OptionsDependentTag.DependentInfo) OptionsDependentTag.getDependentInfo(lc_select.getProperty(), pageContext); 174 lc_info.mainCollectionName = collection; 175 lc_info.mainCollectionProperty = null; 176 lc_info.mainCollectionBeanProperty = property; 177 } 178 179 protected Object getLabelFromBean(Object in_bean, String in_labelProperty) throws JspException { 180 return LayoutUtils.getProperty(in_bean, in_labelProperty); 181 } 182 protected Object getValueFromBean(Object in_bean, String in_property) throws JspException { 183 return LayoutUtils.getProperty(in_bean, in_property); 184 } 185 190 191 public int doStartTag() throws JspException { 192 return SKIP_BODY; 193 } 194 public String getCollection() { 195 return (this.collection); 196 } 197 206 protected Iterator getIterator(String name, String property) throws JspException { 207 String lc_beanName = name; 208 if (lc_beanName == null) { 209 lc_beanName = Constants.BEAN_KEY; 210 } 211 return LayoutUtils.getIterator(pageContext, lc_beanName, property); 212 213 } 214 public String getLabelName() { 215 return labelName; 216 } 217 public String getLabelProperty() { 218 return labelProperty; 219 } 220 public String getName() { 221 return name; 222 } 223 public String getProperty() { 224 return property; 225 } 226 229 public void release() { 230 super.release(); 231 collection = null; 232 labelName = null; 233 labelProperty = null; 234 name = null; 235 property = null; 236 sourceOf = null; 237 } 238 public void setCollection(String collection) { 239 this.collection = collection; 240 } 241 public void setLabelName(String labelName) { 242 this.labelName = labelName; 243 } 244 public void setLabelProperty(String labelProperty) { 245 this.labelProperty = labelProperty; 246 } 247 public void setName(String name) { 248 this.name = name; 249 } 250 public void setProperty(String property) { 251 this.property = property; 252 } 253 256 public String getSourceOf() { 257 return sourceOf; 258 } 259 262 public void setSourceOf(String sourceOf) { 263 this.sourceOf = sourceOf; 264 } 265 266 269 public String getChoiceLabel() { 270 return choiceLabel; 271 } 272 275 public String getChoiceTooltip() { 276 return null; 277 } 278 281 public String getChoiceValue() { 282 return choiceValue; 283 } 284 } 285 | Popular Tags |