1 package fr.improve.struts.taglib.layout.field; 2 3 import java.lang.reflect.Array ; 4 import java.util.ArrayList ; 5 import java.util.Collection ; 6 import java.util.List ; 7 8 import javax.servlet.jsp.JspException ; 9 10 import org.apache.struts.taglib.html.BaseHandlerTag; 11 import org.apache.struts.util.ResponseUtils; 12 13 import fr.improve.struts.taglib.layout.event.StartLayoutEvent; 14 import fr.improve.struts.taglib.layout.util.LayoutUtils; 15 import fr.improve.struts.taglib.layout.util.TagUtils; 16 21 public class SelectTag extends AbstractFieldTag implements ChoiceTag { 22 protected org.apache.struts.taglib.html.SelectTag selectTag = 23 new org.apache.struts.taglib.html.SelectTag(); 24 25 protected String multiple; 26 protected String [] match; 27 protected boolean isMatched = false; 28 29 protected boolean isClosed = false; 30 31 private List errors = new ArrayList (); 32 33 37 public static final String OTHER_KEY = "struts.layout.other"; 38 protected String otherKey = "struts.layout.other"; 39 protected boolean need_other = false; 40 protected String otherProperty; 41 protected String otherValue; 42 43 public boolean isMatched(String value) { 44 if ((match == null) || (value == null)) 45 return (false); 46 for (int i = 0; i < match.length; i++) { 47 if (value.equals(match[i])) 48 return (true); 49 } 50 return (false); 51 } 52 53 public void addChoice(StringBuffer out_sb, Choice in_choice) throws JspException { 54 addChoice(out_sb, in_choice.getChoiceValue(), in_choice.getChoiceLabel()); 55 } 56 57 64 public void addChoice(StringBuffer sb, String value, String label) 65 throws JspException { 66 67 if (isClosed) { 68 throw new IllegalStateException ("Cannot add another option after nested text evaluation"); 69 } 70 71 switch (getFieldDisplayMode()) { 72 case MODE_EDIT : 73 sb.append("<option value=\""); 74 sb.append(value); 75 sb.append("\""); 76 if (isMatched(value)) 77 sb.append(" selected"); 78 sb.append(">"); 79 if (filter) { 80 sb.append(ResponseUtils.filter(label)); 81 } else { 82 sb.append(label); 83 } 84 sb.append("</option>\r\n"); 85 break; 86 case MODE_INSPECT : 87 case MODE_INSPECT_ONLY : 88 case MODE_INSPECT_PRESENT : 89 if (isMatched(value)) { 90 if (OTHER_KEY.equals(value)) { 91 sb.append("<span class=\""); 95 sb.append(styleClass); 96 sb.append("\">"); 97 if (otherValue != null) { 98 sb.append(otherValue); 99 } else { 100 sb.append(LayoutUtils.getBeanFromPageContext(pageContext, name, otherProperty)); 101 } 102 sb.append("</span>"); 103 } else { 104 sb.append("<span class=\""); 105 sb.append(styleClass); 106 sb.append("\">"); 107 if (filter) { 108 sb.append(ResponseUtils.filter(label)); 109 } else { 110 sb.append(label); 111 } 112 sb.append("</span>"); 113 } 114 isMatched = true; 115 } 116 117 } 118 119 } 120 121 protected void doAfterValue() throws JspException { 122 if (!isClosed) { 123 isClosed = true; 124 125 if (need_other) { 127 StringBuffer buffer = new StringBuffer (); 128 addChoice( 129 buffer, 130 OTHER_KEY, 131 LayoutUtils.getLabel(pageContext, OTHER_KEY, null)); 132 TagUtils.write(pageContext, buffer.toString()); 133 } 134 135 if (getFieldDisplayMode() == MODE_EDIT) 136 selectTag.doEndTag(); 137 138 if (need_other && getFieldDisplayMode() == MODE_EDIT) { 140 StringBuffer buffer = new StringBuffer (); 141 buffer.append(" <span id=\""); 142 buffer.append(otherProperty); 143 buffer.append("span\""); 144 if (!isMatched(OTHER_KEY)) 145 buffer.append(" style=\"display:none\";"); 146 buffer.append("><span class=\""); 147 buffer.append(styleClass); 148 buffer.append("\">"); 149 buffer.append(LayoutUtils.getLabel(pageContext, otherKey, null)); 150 buffer.append(" : </span><input type=\"text\" name=\""); 151 buffer.append(otherProperty); 152 buffer.append("\" value=\""); 153 if (otherValue != null) 154 buffer.append(otherValue); 155 else 156 buffer.append( 157 LayoutUtils.getBeanFromPageContext(pageContext, name, otherProperty)); 158 buffer.append("\" class=\""); 159 buffer.append(styleClass); 160 buffer.append("\"></span>"); 161 } 162 163 } 164 } 165 171 protected boolean doBeforeValue() throws javax.servlet.jsp.JspException { 172 fieldTag = selectTag; 173 return true; 174 } 175 176 179 protected int doStartEditMode() throws JspException { 180 errors = computeErrors(); 181 if (isLayout()) { 182 beginFieldLayout(); 183 beginFieldError(errors); 184 } 185 int lc_result = doStartEditField(); 186 return lc_result; 187 } 188 189 192 public int doStartInspectField() throws JspException { 193 if (!doBeforeValue()) return SKIP_BODY; 195 196 return EVAL_BODY_INCLUDE; 197 } 198 public int doEndInspectField() throws JspException { 199 doAfterValue(); 200 201 if (!isMatched) { 202 Object lc_value = getFieldValue(); 203 if (lc_value!=null) { 204 StringBuffer buffer = new StringBuffer (); 205 buffer.append("<span class=\""); 206 buffer.append(styleClass); 207 buffer.append("\">"); 208 if (lc_value.getClass().isArray()) { 209 int n = Array.getLength(lc_value); 210 for (int i = 0; i < n; i++) { 211 Object lc_item = Array.get(lc_value, i); 212 if (lc_item!=null) { 213 buffer.append(lc_item.toString()); 214 } 215 } 216 } else { 217 buffer.append(lc_value.toString()); 218 } 219 buffer.append("</span>"); 220 TagUtils.write(pageContext, buffer.toString()); 221 } 222 } 223 return EVAL_PAGE; 224 } 225 226 229 protected int doEndEditMode() throws JspException { 230 int lc_result = doEndEditField(); 231 if (isLayout()) { 232 endFieldError(errors); 233 endFieldLayout(); 234 } 235 return lc_result; 236 } 237 238 public int doEndEditField() throws JspException { 239 doAfterValue(); 240 241 return EVAL_PAGE; 242 } 243 public int doStartEditField() throws JspException { 244 if (!doBeforeValue()) 246 return SKIP_BODY; 247 248 if (fieldTag == null) 250 throw new JspException ( 251 getClass().getName() 252 + " should really set the fieldTag value in doBeforeValue() !"); 253 254 261 copyProperties(fieldTag); 263 264 boolean check = isRequired(); 266 if (check) { 267 fieldTag.setOnchange( 268 "checkValue(this, '" + property + "','TEXT'," + check + ");" + 269 (onchange!=null ? onchange : "") 270 ); 271 } 272 273 fieldTag.doStartTag(); 274 275 return EVAL_BODY_INCLUDE; 276 } 277 280 protected java.lang.Object getFieldValue() throws JspException { 281 if (match != null) 282 return match; 283 284 Object lc_value = super.getFieldValue(); 285 if (lc_value != null) try { 286 if (value!=null) { 287 match = new String [1]; 288 match[0] = value; 289 } else { 290 Object lc_bean = pageContext.findAttribute(name); 291 match = LayoutUtils.getArrayProperty(lc_bean, property); 292 } 293 } catch (Exception e) { 294 TagUtils.saveException(pageContext, e); 295 throw new JspException (e.getMessage()); 296 } 297 return lc_value; 298 } 299 public String getMultiple() { 300 return multiple; 301 } 302 307 public java.lang.String getOtherKey() { 308 return otherKey; 309 } 310 315 public java.lang.String getOtherProperty() { 316 return otherProperty; 317 } 318 323 public java.lang.String getOtherValue() { 324 return otherValue; 325 } 326 public String getSize() { 327 return size; 328 } 329 protected void reset() { 330 super.reset(); 331 match = null; 332 isMatched = false; 333 isClosed = false; 334 need_other = false; 335 errors.clear(); 336 } 337 public void release() { 338 super.release(); 339 340 multiple = null; 341 onchange = null; 342 343 otherKey = "struts.layout.other"; 344 otherValue = null; 345 otherProperty = null; 346 } 347 public void setMultiple(String multiple) { 348 this.multiple = multiple; 349 350 } 351 356 public void setOtherKey(java.lang.String newOtherKey) { 357 otherKey = newOtherKey; 358 need_other = true; 359 } 360 365 public void setOtherProperty(java.lang.String newOtherProperty) { 366 otherProperty = newOtherProperty; 367 need_other = true; 368 } 369 374 public void setOtherValue(java.lang.String newOtherValue) { 375 otherValue = newOtherValue; 376 need_other = true; 377 } 378 public void setSize(String size) { 379 this.size = size; 380 } 381 public void setValue(String value) { 382 this.value = value; 383 } 384 387 protected void printIndexedHiddenValue(Collection lc_collection) throws JspException { 388 if ("true".equalsIgnoreCase(multiple)) { 389 super.printIndexedHiddenValue(lc_collection); 390 } else { 391 if (!lc_collection.isEmpty()) { 392 Object lc_value = lc_collection.iterator().next(); 393 printSimpleHiddenValue(lc_value); 394 } 395 } 396 } 397 398 public String getValue() { 399 return ""; 400 } 401 402 405 public Object processStartLayoutEvent(StartLayoutEvent in_event) throws JspException { 406 if (!isClosed) { 407 doAfterValue(); 408 } 409 return super.processStartLayoutEvent(in_event); 410 } 411 412 protected void copyProperties(BaseHandlerTag in_dest) throws JspException { 413 super.copyProperties(in_dest); 414 selectTag.setProperty(getProperty()); 415 selectTag.setValue(getValue()); 416 selectTag.setName(getName()); 417 selectTag.setMultiple(getMultiple()); 418 selectTag.setSize(getSize()); 419 } 420 421 424 public boolean isFill(Object in_value) throws JspException { 425 if (in_value==null) { 427 return false; 428 } 429 430 if (!in_value.getClass().isArray()) { 432 return in_value.toString().length()!=0; 433 } 434 435 Object [] lc_values = (Object []) in_value; 437 438 if (lc_values.length==0) { 440 return false; 441 } 442 443 if (lc_values.length==1) { 445 Object lc_firstValue = lc_values[0]; 446 return lc_firstValue!=null && lc_firstValue.toString().length()!=0; 447 } 448 449 return true; 451 } 452 } | Popular Tags |