本文整理汇总了Java中com.vaadin.data.Binder类的典型用法代码示例。如果您正苦于以下问题:Java Binder类的具体用法?Java Binder怎么用?Java Binder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Binder类属于com.vaadin.data包,在下文中一共展示了Binder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testNullRepresentation
import com.vaadin.data.Binder; //导入依赖的package包/类
@Test
public void testNullRepresentation() {
TextField field = new TextField();
// Bind with null representation
Binder<MyEntity> binder = new BeanValidationBinder<>(MyEntity.class);
binder.forField(field).withNullRepresentation("").bind("text");
MyEntity bean = new MyEntity();
assertTrue(validator.validate(bean).isEmpty());
assertTrue(binder.validate().isOk());
binder.setBean(bean);
field.setValue("test");
assertEquals("test", bean.getText());
field.setValue("");
assertNull(bean.getText());
assertTrue(validator.validate(bean).isEmpty());
assertTrue(binder.validate().isOk());
}
示例2: testNullRepresentationSizeMin1
import com.vaadin.data.Binder; //导入依赖的package包/类
@Test
public void testNullRepresentationSizeMin1() {
TextField field = new TextField();
// Bind with null representation
Binder<MyEntitySizeMin1> binder = new BeanValidationBinder<>(MyEntitySizeMin1.class);
binder.forField(field).withNullRepresentation("").bind("text");
MyEntitySizeMin1 bean = new MyEntitySizeMin1();
assertTrue(validator.validate(bean).isEmpty());
assertTrue(binder.validate().isOk());
binder.setBean(bean);
field.setValue("test");
assertEquals("test", bean.getText());
field.setValue("");
assertNull(bean.getText());
assertTrue(validator.validate(bean).isEmpty());
assertTrue(binder.validate().isOk());
}
示例3: testBeanClassLevelValidation
import com.vaadin.data.Binder; //导入依赖的package包/类
@Test
public void testBeanClassLevelValidation() {
TextField field1 = new TextField();
TextField field2 = new TextField();
Label statusLabel = new Label();
Binder<MyEntity> binder = new BeanValidationBinder<>(MyEntity.class);
binder.forField(field1).withNullRepresentation("").bind("s1");
binder.forField(field2).withNullRepresentation("").bind("s2");
binder.setStatusLabel(statusLabel);
MyEntity bean = new MyEntity();
binder.setBean(bean);
assertFalse(validator.validate(bean).isEmpty());
assertTrue(binder.validate().isOk());
// JSR303 bean level validation still missing:
// https://github.com/vaadin/framework/issues/8498
// assertFalse(binder.validate().isOk());
}
示例4: init
import com.vaadin.data.Binder; //导入依赖的package包/类
@Override
protected void init(VaadinRequest vaadinRequest) {
// crate a binder for a form, specifying the data class that will be used in binding
Binder<ImageData> binder = new Binder<>(ImageData.class);
// specify explicit binding in order to add validation and converters
binder.forMemberField(imageSize)
// input should not be null or empty
.withValidator(string -> string != null && !string.isEmpty(), "Input values should not be empty")
// convert String to Integer, throw ValidationException if String is in incorrect format
.withConverter(new StringToIntegerConverter("Input value should be an integer"))
// validate converted integer: it should be positive
.withValidator(integer -> integer > 0, "Input value should be a positive integer");
// tell binder to bind use all fields from the current class, but considering already existing bindings
binder.bindInstanceFields(this);
// crate data object with predefined imageName and imageSize
ImageData imageData = new ImageData("Lorem ipsum", 2);
// fill form with predefined data from data object and
// make binder to automatically update the object from the form, if no validation errors are present
binder.setBean(imageData);
binder.addStatusChangeListener(e -> {
// the real image drawing will not be considered in this article
if (e.hasValidationErrors() || !e.getBinder().isValid()) {
Notification.show("Form contains validation errors, no image will be drawn");
} else {
Notification.show(String.format("I will draw image with \"%s\" text and width %d\n",
imageData.getText(), imageData.getSize()));
}
});
// add a form to layout
setContent(new VerticalLayout(text, imageSize));
}
示例5: init
import com.vaadin.data.Binder; //导入依赖的package包/类
@PostConstruct
private void init() {
addClassName("add-trip");
Binder<Trip> binder = new Binder<>();
binder.bind(from, Trip::getStart, Trip::setStart);
binder.bind(to, Trip::getEnd, Trip::setEnd);
binder.bind(date, Trip::getDate, Trip::setDate);
saveButton.addClickListener(e -> {
Trip trip = new Trip();
if (binder.writeBeanIfValid(trip)) {
repository.save(trip);
TripList.navigateTo(trip);
}
});
TripMap.getCurrent().showTrip(null);
binder.addValueChangeListener(e -> TripMap.getCurrent()
.previewTrip(from.getValue(), to.getValue()));
}
示例6: UserGrid
import com.vaadin.data.Binder; //导入依赖的package包/类
public UserGrid() {
super(User.class);
removeAllColumns();
Binder<User> binder = new BeanValidationBinder<>(User.class);
getEditor().setEnabled(true);
getEditor().setBinder(binder);
getEditor().setCancelCaption(Messages.getInstance().getMessage("button.cancel"));
getEditor().setSaveCaption(Messages.getInstance().getMessage("button.save"));
EnumComboBox<AccessRole> roleEditor = new EnumComboBox<>(AccessRole.class);
binder.forField(roleEditor).bind(User::getRole, User::setRole);
addColumn(User::getUsername).setCaption("Utilisateur").setId("username");
addColumn(User::getRole).setCaption("Role").setId("role").setEditorComponent(roleEditor, User::setRole);
addColumn(User::getName).setCaption("Nom").setId("name");
addColumn("email").setCaption("Courriel").setEditorComponent(new TextField());
setRowHeight(40);
}
示例7: statusChange
import com.vaadin.data.Binder; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public void statusChange(StatusChangeEvent event) {
ET bean = (ET) event.getBinder().getBean();
final Binder<?> binder = event.getBinder();
final boolean valid = binder.isValid();
if (bean == newInstance) {
if (!valid || !isValidBean(bean)) {
return;
}
getAndEnsureValue().add(newInstance);
fireEvent(new ElementAddedEvent<ET>(AbstractElementCollection.this, newInstance));
setPersisted(newInstance, true);
onElementAdded();
}
fireValueChange();
}
示例8: getComponentFor
import com.vaadin.data.Binder; //导入依赖的package包/类
protected final Component getComponentFor(ET pojo, String property) {
ElementEditor editorsstuff = pojoToEditor.get(pojo);
if (editorsstuff == null) {
editorsstuff = createBoundEditor(pojo);
}
Component c = null;
Optional<Binder.Binding<ET, ?>> binding = editorsstuff.beanBinder.getBinding(property);
if (binding.isPresent()) {
c = (Component) binding.get().getField();
} else {
try {
// property that is not a property editor field but custom UI "column"
java.lang.reflect.Field f = editorType.getDeclaredField(property);
f.setAccessible(true);
c = (Component) f.get(editorsstuff.editor);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException ex) {
Logger.getLogger(AbstractElementCollection.class.getName()).log(Level.SEVERE, null, ex);
}
if (c == null) {
c = new Label("");
}
}
return c;
}
示例9: setPopupEditor
import com.vaadin.data.Binder; //导入依赖的package包/类
/**
* Method to set form to allow editing more properties than it would be
* convenient inline.
*
* @param newPopupEditor the popup editor to be used to edit instances
*/
public void setPopupEditor(AbstractForm<ET> newPopupEditor) {
this.popupEditor = newPopupEditor;
if (newPopupEditor != null) {
newPopupEditor.setSavedHandler(new AbstractForm.SavedHandler<ET>() {
private static final long serialVersionUID = 389618696563816566L;
@Override
public void onSave(ET entity) {
Binder<ET> beanBinder = getBinderFor(entity);
beanBinder.writeBeanIfValid(entity);
// TODO refresh binding
popupEditor.getPopup().close();
}
});
}
}
示例10: genNumberField
import com.vaadin.data.Binder; //导入依赖的package包/类
public static <T> TextField genNumberField(Binder<T> binder, String propertyId, Converter converter, String inputPrompt) {
final TextField field = new TextField();
field.setWidth("100%");
field.addStyleName(STYLENAME_GRIDCELLFILTER);
field.addStyleName(ValoTheme.TEXTFIELD_TINY);
field.addValueChangeListener(e -> {
if (binder.isValid()) {
field.setComponentError(null);
}
});
binder.forField(field)
.withNullRepresentation("")
// .withValidator(text -> text != null && text.length() > 0, "invalid")
.withConverter(converter)
.bind(propertyId);
field.setPlaceholder(inputPrompt);
return field;
}
示例11: genDateField
import com.vaadin.data.Binder; //导入依赖的package包/类
public static <T> DateField genDateField(Binder<T> binder, String propertyId, final java.text.SimpleDateFormat dateFormat) {
DateField dateField = new DateField();
binder.bind(dateField, propertyId);
if (dateFormat != null) {
dateField.setDateFormat(dateFormat.toPattern());
}
dateField.setWidth("100%");
dateField.setResolution(DateResolution.DAY);
dateField.addStyleName(STYLENAME_GRIDCELLFILTER);
dateField.addStyleName(ValoTheme.DATEFIELD_TINY);
dateField.addValueChangeListener(e -> {
if (binder.isValid()) {
dateField.setComponentError(null);
}
});
return dateField;
}
示例12: statusChange
import com.vaadin.data.Binder; //导入依赖的package包/类
@Override
public void statusChange(StatusChangeEvent event) {
ET bean = (ET) event.getBinder().getBean();
if (bean == newInstance) {
// God dammit, you can't rely on BeanValidationBinder.isValid !
// Returns true here even if some notnull fields are not set :-(
// Thus, check also with own BeanValidation logi, this should
// also give a bare bones cross field validation support for
// elements
final Binder<?> binder = event.getBinder();
final boolean valid = binder.isValid();
if (!valid || !isValidBean(bean)) {
return;
}
getAndEnsureValue().add(newInstance);
fireEvent(
new ElementAddedEvent<>(AbstractElementCollection.this,
newInstance));
setPersisted(newInstance, true);
onElementAdded();
}
// TODO
fireValueChange();
}
示例13: commit
import com.vaadin.data.Binder; //导入依赖的package包/类
private void commit(final Window win, final Binder<? extends AbstractStep> binder,
final NativeSelect<Step> parentStepSelect) {
AbstractStep step = binder.getBean();
gantt.markStepDirty(step);
if (parentStepSelect.isEnabled() && parentStepSelect.getValue() != null) {
SubStep subStep = addSubStep(parentStepSelect, step);
step = subStep;
}
if (step instanceof Step && !gantt.getSteps().contains(step)) {
gantt.addStep((Step) step);
}
if (ganttListener != null && step instanceof Step) {
ganttListener.stepModified((Step) step);
}
win.close();
}
示例14: testSizeMin1
import com.vaadin.data.Binder; //导入依赖的package包/类
@Test
public void testSizeMin1() {
TextField field = new TextField();
// Bind with null representation
Binder<MyEntitySizeMin1> binder = new BeanValidationBinder<>(MyEntitySizeMin1.class);
binder.forField(field).bind("text");
field.setValue("reset");
MyEntitySizeMin1 bean = new MyEntitySizeMin1();
// Why doesn't this fail when there is no null representation?
// https://github.com/vaadin/framework/issues/9453
binder.setBean(bean);
System.out.println("Field value: " + field.getValue());
assertTrue(validator.validate(bean).isEmpty());
field.setValue("test");
assertEquals("test", bean.getText());
assertTrue(validator.validate(bean).isEmpty());
assertTrue(binder.validate().isOk());
field.setValue("");
assertFalse(binder.validate().isOk());
// Since validation fails the value is still "test" and not "" (also kind of
// hinted at in https://github.com/vaadin/framework/issues/9453)
// See Binder.BinderImpl.writeFieldValue()
// assertEquals("", bean.getText());
}
示例15: testValid
import com.vaadin.data.Binder; //导入依赖的package包/类
@Test
public void testValid() {
TextField text = new TextField();
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Binder<MyEntity> binder = new BeanValidationBinder<>(MyEntity.class);
binder.forField(text).withNullRepresentation("").bind("text");
MyEntity t = new MyEntity();
// valid
t.setText("bla");
binder.setBean(t);
assertTrue(validator.validate(t).isEmpty());
assertTrue(binder.validate().isOk());
text.setValue("");
assertNull(t.getText());
/*
* // invalid t.setText(""); binder.setBean(t);
* assertFalse(validator.validate(t).isEmpty());
* assertFalse(binder.validate().isOk());
*
* t.setText(null); binder.setBean(t);
* assertTrue(validator.validate(t).isEmpty());
* assertTrue(binder.validate().isOk());
*/
}