Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 156209 Details for
Bug 279166
[DataBinding] JSR 303: Bean Validation
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
A JFace validator using JSR-303 to validate a single property
JSR303Validator.java (text/plain), 5.48 KB, created by
Marcus Ilgner
on 2010-01-15 03:59:52 EST
(
hide
)
Description:
A JFace validator using JSR-303 to validate a single property
Filename:
MIME Type:
Creator:
Marcus Ilgner
Created:
2010-01-15 03:59:52 EST
Size:
5.48 KB
patch
obsolete
>package com.marcusilgner.research.validation; > >import java.util.Iterator; >import java.util.Set; > >import javax.validation.ConstraintViolation; >import javax.validation.Validation; >import javax.validation.ValidationException; >import javax.validation.Validator; >import javax.validation.ValidatorFactory; > >import org.eclipse.core.databinding.validation.IValidator; >import org.eclipse.core.databinding.validation.ValidationStatus; >import org.eclipse.core.runtime.IStatus; >import org.eclipse.jface.fieldassist.ControlDecoration; >import org.eclipse.jface.fieldassist.FieldDecoration; >import org.eclipse.jface.fieldassist.FieldDecorationRegistry; >import org.eclipse.swt.SWT; >import org.eclipse.swt.widgets.Control; > >/** > * An Eclipse Databinding Validator implementation which uses the JSR303 > * validation API to check a single property for validity. Use as > * beforeSetValidator, after the value has been converted to the targets > * property class > * > * Licensed under EPL v1.0 (http://www.eclipse.org/legal/epl-v10.html) > * > * To use, initialize JSR303 first and call setValidatorFactory > * > * If given a control, this validator will also paint a field decoration > * > * @author Marcus Ilgner <mail@marcusilgner.com> > * > * @param <T> > * The class of the bean being bound. Not actively used as > * {@link javax.validation.Validator#validateValue(Class, String, Object, Class...)} > * works somewhat differently than > * {@link javax.validation.Validator#validateProperty(Object, String, Class...)} > * Should the API be frozen like this, the template class will > * probably removed > */ >public class JSR303Validator<T extends Object> implements IValidator { > > // the bean to be validated > private T bean; > > // the name of the bean property to check > private String propertyName; > > // a validator to run validation against > private Validator validator; > > // will show a field decoration if validation constraints are violated > private ControlDecoration controlDecoration; > > // a shared validator factory to be used by all instances of this class > private static ValidatorFactory validatorFactory; > > /** > * initializes the validator with a non-default > * javax.validation.ValidatorFactory > * > * @param _factory > */ > public static void setValidatorFactory(ValidatorFactory _factory) { > validatorFactory = _factory; > } > > /** > * simple constructor which doesn't use field decorations JSR303Validator(T, > * String, Control) is recommended > * > * @param _bean > * the bean to validate > * @param _propertyName > * name of the property to be validated > */ > public JSR303Validator(T _bean, String _propertyName) { > bean = _bean; > propertyName = _propertyName; > > controlDecoration = null; > initValidator(); > } > > /** > * constructor which constructs a field decoration on the specified control > * > * @param _bean > * the bean to validate > * @param _propertyName > * name of the property to be validated > * @param _control > * control to receive field decoration on constraint violation > */ > public JSR303Validator(T _bean, String _propertyName, Control _control) { > bean = _bean; > propertyName = _propertyName; > initValidator(); > createFieldDecoration(_control); > } > > /** > * Creates a field decoration on the given control > * > * @param _control > * control to be decorated > */ > private void createFieldDecoration(Control _control) { > controlDecoration = new ControlDecoration(_control, SWT.LEFT > | SWT.BOTTOM); > FieldDecoration fieldDecoration = FieldDecorationRegistry.getDefault() > .getFieldDecoration(FieldDecorationRegistry.DEC_ERROR); > controlDecoration.setImage(fieldDecoration.getImage()); > } > > /** > * Creates a validator. If no javax.validation.ValidatorFactory was set it > * will try to create a factory from defaults > */ > private void initValidator() { > if (validatorFactory == null) { > // JSR303 hasn't been initialized. Give it a shot. > try { > validatorFactory = Validation.buildDefaultValidatorFactory(); > } catch (Exception e) { > throw new ValidationException( > "ValidationFactory not set and unable to initialize validation from default values"); > } > } > validator = validatorFactory.getValidator(); > > } > > /** > * runs the actual validation and shows the decoration if required > */ > @Override > public IStatus validate(Object _value) { > Set<?> validationResult = validator.validateValue(bean.getClass(), > propertyName, _value, javax.validation.groups.Default.class); > if (validationResult.size() > 0) { > String message = assembleErrorMessage(validationResult); > if (controlDecoration != null) { > controlDecoration.setDescriptionText(message); > controlDecoration.show(); > } > return ValidationStatus.error(message); > } > if (controlDecoration != null) { > controlDecoration.hide(); > } > return ValidationStatus.ok(); > } > > /** > * Concatenates the messages from the validationResult into a single > * multi-line string > * > * @param validationResult > * a result from calling a JSR303 validation method > * @return > */ > @SuppressWarnings("unchecked") > private String assembleErrorMessage(Set<?> validationResult) { > String message = ""; > Iterator<ConstraintViolation> iterator = (Iterator<ConstraintViolation>) validationResult > .iterator(); > while (iterator.hasNext()) { > ConstraintViolation violation = iterator.next(); > message += violation.getMessage(); > if (iterator.hasNext()) { > message += System.getProperty("line.separator"); > } > } > return message; > } > >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 279166
: 156209 |
160643
|
206651