1 20 package org.enhydra.barracuda.core.forms; 21 22 import java.util.*; 23 import javax.servlet.*; 24 import javax.servlet.http.*; 25 26 import org.enhydra.barracuda.plankton.data.DefaultStateMap; 27 import org.enhydra.barracuda.plankton.data.StateMap; 28 import org.apache.log4j.*; 29 import java.math.BigDecimal ; 30 31 67 public class DefaultFormElement implements FormElement { 68 69 protected static final Logger logger = Logger.getLogger(DefaultFormElement.class.getName()); 71 72 protected String key = null; 74 protected String name = null; 75 protected FormType type = null; 76 protected Object defaultVal = null; 77 protected ParseException pe = null; 78 protected FormValidator validator = null; 79 protected boolean allowMultiples = false; 80 protected Object origVal = null; 81 protected Object val = null; 82 83 86 public DefaultFormElement() { 87 this(null); 88 } 89 90 95 public DefaultFormElement(String ikey) { 96 this(ikey, FormType.STRING); 97 } 98 99 105 public DefaultFormElement(String ikey, FormType itype) { 106 this(ikey, itype, null); 107 } 108 109 117 public DefaultFormElement(String ikey, FormType itype, Object idefaultVal) { 118 this(ikey, itype, idefaultVal, null); 119 } 120 121 130 public DefaultFormElement(String ikey, FormType itype, Object idefaultVal, FormValidator ivalidator) { 131 this(ikey, itype, idefaultVal, ivalidator, false); 132 } 133 134 145 public DefaultFormElement(String ikey, FormType itype, Object idefaultVal, FormValidator ivalidator, boolean iallowMultiples) { 146 this(ikey, ikey, itype, idefaultVal, ivalidator, iallowMultiples); 148 } 149 150 162 public DefaultFormElement(String ikey, String iname, FormType itype, Object idefaultVal, FormValidator ivalidator, boolean iallowMultiples) { 163 setKey(ikey); 164 setName(iname); 165 setType(itype); 166 setDefaultVal(idefaultVal); 167 setValidator(ivalidator); 168 setAllowMultiples(iallowMultiples); 169 } 170 171 176 public void setKey(String ikey) { 177 key = ikey; 178 } 179 180 185 public String getKey() { 186 return key; 187 } 188 189 194 public void setName(String iname) { 195 name = iname; 196 } 197 198 203 public String getName() { 204 return name; 205 } 206 207 212 public void setType(FormType itype) { 213 if (itype==null) itype = FormType.STRING; 214 type = itype; 215 } 216 217 222 public FormType getType() { 223 return type; 224 } 225 226 231 public void setDefaultVal(Object idefaultVal) { 232 defaultVal = idefaultVal; 233 } 234 235 240 public Object getDefaultVal() { 241 return defaultVal; 242 } 243 244 249 public void setAllowMultiples(boolean val) { 250 allowMultiples = val; 251 } 252 253 258 public boolean allowMultiples() { 259 return allowMultiples; 260 } 261 262 267 public void setValidator(FormValidator ivalidator) { 268 validator = ivalidator; 269 } 270 271 276 public FormValidator getValidator() { 277 return validator; 278 } 279 280 285 public void setOrigVal(Object iorigVal) { 286 origVal = iorigVal; 287 } 288 289 294 public Object getOrigVal() { 295 return origVal; 296 } 297 298 303 public void setVal(Object ival) { 304 val = ival; 305 } 306 307 316 public Object getVal() { 317 if (val!=null && val instanceof Object []) { 319 return ((Object []) val)[0]; 320 } 321 return val; 322 } 323 324 331 public Object [] getVals() { 332 Object valCopy = val; 333 if (valCopy==null) { 334 if (type!=null) { 335 return type.getTypeArray(0); 336 } 337 return new Object [] {}; 338 } 339 if (valCopy instanceof ArrayList) { 340 valCopy = ((ArrayList)valCopy).toArray(); 341 } else { 342 valCopy = new Object [] {valCopy}; 343 } 344 Object [] vals = (Object [])valCopy; 345 Object [] typedVals = null; 346 if (type!=null) { 347 typedVals = type.getTypeArray(vals.length); 348 } 349 if (typedVals==null) { 350 typedVals = new Object [vals.length]; 351 } 352 if (logger.isDebugEnabled()) { 353 for(int i = 0; i < vals.length; i++) { 354 logger.debug("Val: " + vals[i]); 355 if (vals[i]!=null) { 356 logger.debug("ValType: " + vals[i].getClass()); 357 } 358 } 359 } 360 System.arraycopy(vals, 0, typedVals, 0, vals.length); 361 return typedVals; 362 } 363 364 367 public String getStringVal() { 368 return (String ) val; 369 } 370 371 375 public String getStringVal(String dflt) { 376 return (val!=null ? (String ) val : dflt); 377 } 378 379 382 public Boolean getBooleanVal() { 383 return (Boolean ) val; 384 } 385 386 390 public Boolean getBooleanVal(Boolean dflt) { 391 return (val!=null ? (Boolean ) val : dflt); 392 } 393 394 397 public Integer getIntegerVal() { 398 return (Integer ) val; 399 } 400 401 405 public Integer getIntegerVal(Integer dflt) { 406 return (val!=null ? (Integer ) val : dflt); 407 } 408 409 412 public Date getDateVal() { 413 return (Date) val; 414 } 415 416 420 public Date getDateVal(Date dflt) { 421 return (val!=null ? (Date) val : dflt); 422 } 423 424 427 public Long getLongVal() { 428 return (Long ) val; 429 } 430 431 435 public Long getLongVal(Long dflt) { 436 return (val!=null ? (Long ) val : dflt); 437 } 438 439 442 public Short getShortVal() { 443 return (Short ) val; 444 } 445 446 450 public Short getShortVal(Short dflt) { 451 return (val!=null ? (Short ) val : dflt); 452 } 453 454 457 public Double getDoubleVal() { 458 return (Double ) val; 459 } 460 461 465 public Double getDoubleVal(Double dflt) { 466 return (val!=null ? (Double ) val : dflt); 467 } 468 469 472 public Float getFloatVal() { 473 return (Float ) val; 474 } 475 476 480 public Float getFloatVal(Float dflt) { 481 return (val!=null ? (Float ) val : dflt); 482 } 483 484 489 public void setParseException(ParseException ipe) { 490 pe = ipe; 491 } 492 493 499 public ParseException getParseException() { 500 return pe; 501 } 502 503 508 public String toString() { 509 return "Key:"+key+" Name:"+name+" Val:"+val+" Type:"+(type!=null ? type.getFormClass().getName() : "null")+" Orig:"+origVal+" Dflt:"+defaultVal+" Mult:"+allowMultiples+" Validator:"+(validator!=null ? "@"+Integer.toHexString(validator.hashCode()) : "null"); 510 } 511 512 } 513 | Popular Tags |