1 15 package org.apache.tapestry.valid; 16 17 import java.util.ArrayList ; 18 import java.util.Collections ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 import org.apache.tapestry.IMarkupWriter; 25 import org.apache.tapestry.IRender; 26 import org.apache.tapestry.IRequestCycle; 27 import org.apache.tapestry.Tapestry; 28 import org.apache.tapestry.form.IFormComponent; 29 30 37 38 public class ValidationDelegate implements IValidationDelegate 39 { 40 private static final long serialVersionUID = 6215074338439140780L; 41 42 private transient IFormComponent _currentComponent; 43 44 47 48 private final List _trackings = new ArrayList (); 49 50 53 54 private final Map _trackingMap = new HashMap (); 55 56 public void clear() 57 { 58 _currentComponent = null; 59 _trackings.clear(); 60 _trackingMap.clear(); 61 } 62 63 68 69 public void writeLabelPrefix(IFormComponent component, IMarkupWriter writer, IRequestCycle cycle) 70 { 71 if (isInError(component)) 72 { 73 writer.begin("font"); 74 writer.attribute("color", "red"); 75 } 76 } 77 78 83 84 public void writeLabelSuffix(IFormComponent component, IMarkupWriter writer, IRequestCycle cycle) 85 { 86 if (isInError(component)) 87 { 88 writer.end(); 89 } 90 } 91 92 105 106 protected FieldTracking getComponentTracking() 107 { 108 return (FieldTracking) _trackingMap.get(_currentComponent.getName()); 109 } 110 111 public void setFormComponent(IFormComponent component) 112 { 113 _currentComponent = component; 114 } 115 116 public boolean isInError() 117 { 118 IFieldTracking tracking = getComponentTracking(); 119 120 return tracking != null && tracking.isInError(); 121 } 122 123 public String getFieldInputValue() 124 { 125 IFieldTracking tracking = getComponentTracking(); 126 127 return tracking == null ? null : tracking.getInput(); 128 } 129 130 133 134 public List getFieldTracking() 135 { 136 if (Tapestry.size(_trackings) == 0) 137 return null; 138 139 return Collections.unmodifiableList(_trackings); 140 } 141 142 143 public IFieldTracking getCurrentFieldTracking() 144 { 145 return findCurrentTracking(); 146 } 147 148 public void reset() 149 { 150 IFieldTracking tracking = getComponentTracking(); 151 152 if (tracking != null) 153 { 154 _trackings.remove(tracking); 155 _trackingMap.remove(tracking.getFieldName()); 156 } 157 } 158 159 164 165 public void record(ValidatorException ex) 166 { 167 IRender errorRenderer = ex.getErrorRenderer(); 168 169 if (errorRenderer == null) 170 record(ex.getMessage(), ex.getConstraint()); 171 else 172 record(errorRenderer, ex.getConstraint()); 173 } 174 175 179 180 public void record(String message, ValidationConstraint constraint) 181 { 182 record(new RenderString(message), constraint); 183 } 184 185 197 198 public void record(IRender errorRenderer, ValidationConstraint constraint) 199 { 200 FieldTracking tracking = findCurrentTracking(); 201 202 205 tracking.setErrorRenderer(errorRenderer); 206 tracking.setConstraint(constraint); 207 } 208 209 public void recordFieldInputValue(String input) 210 { 211 FieldTracking tracking = findCurrentTracking(); 212 213 tracking.setInput(input); 214 } 215 216 223 224 protected FieldTracking findCurrentTracking() 225 { 226 FieldTracking result = null; 227 228 if (_currentComponent == null) 229 { 230 result = new FieldTracking(); 231 232 235 _trackings.add(result); 236 } 237 else 238 { 239 result = getComponentTracking(); 240 241 if (result == null) 242 { 243 String fieldName = _currentComponent.getName(); 244 245 result = new FieldTracking(fieldName, _currentComponent); 246 247 _trackings.add(result); 248 _trackingMap.put(fieldName, result); 249 } 250 } 251 252 return result; 253 } 254 255 258 259 public void writePrefix(IMarkupWriter writer, IRequestCycle cycle, IFormComponent component, 260 IValidator validator) 261 { 262 } 263 264 267 268 public void writeAttributes(IMarkupWriter writer, IRequestCycle cycle, 269 IFormComponent component, IValidator validator) 270 { 271 } 272 273 277 278 public void writeSuffix(IMarkupWriter writer, IRequestCycle cycle, IFormComponent component, 279 IValidator validator) 280 { 281 if (isInError()) 282 { 283 writer.printRaw(" "); 284 writer.begin("font"); 285 writer.attribute("color", "red"); 286 writer.print("**"); 287 writer.end(); 288 } 289 } 290 291 public boolean getHasErrors() 292 { 293 return getFirstError() != null; 294 } 295 296 301 302 public IRender getFirstError() 303 { 304 if (Tapestry.size(_trackings) == 0) 305 return null; 306 307 Iterator i = _trackings.iterator(); 308 309 while (i.hasNext()) 310 { 311 IFieldTracking tracking = (IFieldTracking) i.next(); 312 313 if (tracking.isInError()) 314 return tracking.getErrorRenderer(); 315 } 316 317 return null; 318 } 319 320 325 326 protected boolean isInError(IFormComponent component) 327 { 328 330 String fieldName = component.getName(); 331 332 IFieldTracking tracking = (IFieldTracking) _trackingMap.get(fieldName); 333 334 return tracking != null && tracking.isInError(); 335 } 336 337 345 346 public List getAssociatedTrackings() 347 { 348 int count = Tapestry.size(_trackings); 349 350 if (count == 0) 351 return null; 352 353 List result = new ArrayList (count); 354 355 for (int i = 0; i < count; i++) 356 { 357 IFieldTracking tracking = (IFieldTracking) _trackings.get(i); 358 359 if (tracking.getFieldName() == null) 360 continue; 361 362 result.add(tracking); 363 } 364 365 return result; 366 } 367 368 376 377 public List getUnassociatedTrackings() 378 { 379 int count = Tapestry.size(_trackings); 380 381 if (count == 0) 382 return null; 383 384 List result = new ArrayList (count); 385 386 for (int i = 0; i < count; i++) 387 { 388 IFieldTracking tracking = (IFieldTracking) _trackings.get(i); 389 390 if (tracking.getFieldName() != null) 391 continue; 392 393 result.add(tracking); 394 } 395 396 return result; 397 } 398 399 public List getErrorRenderers() 400 { 401 List result = new ArrayList (); 402 403 Iterator i = _trackings.iterator(); 404 while (i.hasNext()) 405 { 406 IFieldTracking tracking = (IFieldTracking) i.next(); 407 408 IRender errorRenderer = tracking.getErrorRenderer(); 409 410 if (errorRenderer != null) 411 result.add(errorRenderer); 412 } 413 414 return result; 415 } 416 } | Popular Tags |