1 21 package au.id.jericho.lib.html; 22 23 import java.util.*; 24 25 102 public final class FormField { 103 private final String name; 104 private int userValueCount=0; 105 private boolean allowsMultipleValues=false; 106 private LinkedHashSet predefinedValues=null; private final LinkedHashSet formControls=new LinkedHashSet(); 108 private transient FormControl firstFormControl=null; int columnIndex; 111 112 FormField(final String name) { 113 this.name=name; 114 } 115 116 129 public String getName() { 130 return name; 131 } 132 133 142 public Collection getFormControls() { 143 return formControls; 144 } 145 146 156 public FormControl getFormControl(final String predefinedValue) { 157 if (predefinedValue==null) { 158 for (final Iterator i=formControls.iterator(); i.hasNext();) { 159 final FormControl formControl=(FormControl)i.next(); 160 if (!formControl.getFormControlType().hasPredefinedValue()) return formControl; 161 if (formControl.getFormControlType().getElementName()!=Tag.SELECT && formControl.getPredefinedValue()==null) return formControl; 162 } 163 } else { 164 for (final Iterator i=formControls.iterator(); i.hasNext();) { 165 final FormControl formControl=(FormControl)i.next(); 166 if (formControl.getFormControlType().getElementName()==Tag.SELECT) { 167 if (formControl.getPredefinedValues().contains(predefinedValue)) return formControl; 168 } else { 169 if (predefinedValue.equals(formControl.getPredefinedValue())) return formControl; 170 } 171 } 172 } 173 return null; 174 } 175 176 182 public FormControl getFormControl() { 183 return (FormControl)formControls.iterator().next(); 184 } 185 186 200 public boolean allowsMultipleValues() { 201 return allowsMultipleValues; 202 } 203 204 225 public int getUserValueCount() { 226 return userValueCount; 227 } 228 229 239 public Collection getPredefinedValues() { 240 return predefinedValues!=null ? predefinedValues : Collections.EMPTY_SET; 241 } 242 243 253 public Collection getValues() { 254 final HashSet values=new HashSet(); 255 for (final Iterator i=formControls.iterator(); i.hasNext();) 256 ((FormControl)i.next()).addValuesTo(values); 257 return values; 258 } 259 260 264 public void clearValues() { 265 for (final Iterator i=formControls.iterator(); i.hasNext();) 266 ((FormControl)i.next()).clearValues(); 267 } 268 269 280 public void setValues(final Collection values) { 281 clearValues(); 282 addValues(values); 283 } 284 285 301 public boolean setValue(final CharSequence value) { 302 clearValues(); 303 return value!=null ? addValue(value) : true; 304 } 305 306 322 public boolean addValue(final CharSequence value) { 323 if (value==null) throw new IllegalArgumentException ("value argument must not be null"); 324 if (formControls.size()==1) return getFirstFormControl().addValue(value); 325 List userValueControls=null; 326 for (final Iterator i=formControls.iterator(); i.hasNext();) { 327 final FormControl formControl=(FormControl)i.next(); 328 if (!formControl.getFormControlType().hasPredefinedValue()) { 329 if (userValueControls==null) userValueControls=new LinkedList(); 333 userValueControls.add(formControl); 334 continue; 335 } 336 if (formControl.addValue(value)) return true; } 338 if (userValueControls==null) return false; 339 for (final Iterator i=userValueControls.iterator(); i.hasNext();) { 340 final FormControl formControl=(FormControl)i.next(); 341 if (formControl.addValue(value)) return true; 342 } 343 return false; 344 } 345 346 350 public String getDebugInfo() { 351 final StringBuffer sb=new StringBuffer (); 352 sb.append("Field: ").append(name).append(", UserValueCount=").append(userValueCount).append(", AllowsMultipleValues=").append(allowsMultipleValues); 353 if (predefinedValues!=null) { 354 for (final Iterator i=predefinedValues.iterator(); i.hasNext();) { 355 sb.append("\nPredefinedValue: "); 356 sb.append(i.next()); 357 } 358 } 359 for (final Iterator i=formControls.iterator(); i.hasNext();) { 360 sb.append("\nFormControl: "); 361 sb.append(((FormControl)i.next()).getDebugInfo()); 362 } 363 sb.append("\n\n"); 364 return sb.toString(); 365 } 366 367 374 public String toString() { 375 return getDebugInfo(); 376 } 377 378 void addValues(final Collection values) { 379 if (values!=null) for (final Iterator i=values.iterator(); i.hasNext();) addValue((CharSequence )i.next()); 380 } 381 382 void addValues(final CharSequence [] values) { 383 if (values!=null) for (int i=0; i<values.length; i++) addValue(values[i]); 384 } 385 386 void addFormControl(final FormControl formControl, final String predefinedValue) { 387 if (predefinedValue==null) { 389 userValueCount++; 390 } else { 391 if (predefinedValues==null) predefinedValues=new LinkedHashSet(); 392 predefinedValues.add(predefinedValue); 393 } 394 formControls.add(formControl); 395 allowsMultipleValues=calculateAllowsMultipleValues(formControl); 396 } 397 398 private boolean calculateAllowsMultipleValues(final FormControl newFormControl) { 399 if (allowsMultipleValues || userValueCount>1) return true; 402 if (userValueCount==1) return predefinedValues!=null; 403 if (predefinedValues.size()==1) return false; 405 final FormControlType newFormControlType=newFormControl.getFormControlType(); 406 if (formControls.size()==1) return newFormControlType==FormControlType.SELECT_MULTIPLE; 407 final FormControlType firstFormControlType=getFirstFormControl().getFormControlType(); 412 if (newFormControlType==FormControlType.RADIO && firstFormControlType==FormControlType.RADIO) return false; 413 if (newFormControlType.isSubmit() && firstFormControlType.isSubmit()) return false; 414 return true; 415 } 416 417 FormControl getFirstFormControl() { 418 if (firstFormControl==null) firstFormControl=(FormControl)formControls.iterator().next(); 422 return firstFormControl; 423 } 424 425 426 void merge(final FormField formField) { 427 if (formField.userValueCount>userValueCount) userValueCount=formField.userValueCount; 428 allowsMultipleValues=allowsMultipleValues || formField.allowsMultipleValues; 429 if (predefinedValues==null) { 430 predefinedValues=formField.predefinedValues; 431 } else if (formField.predefinedValues!=null) { 432 for (final Iterator i=formField.predefinedValues.iterator(); i.hasNext();) 433 predefinedValues.add(i.next()); 434 } 435 for (final Iterator i=formField.getFormControls().iterator(); i.hasNext();) 436 formControls.add(i.next()); 437 } 438 } 439 440 | Popular Tags |