@Override public void readBean(BEAN bean) { if (binder.getBean() == null) { try { Constructor<BEAN> ctor = clz.getConstructor(); binder.setBean(ctor.newInstance()); } catch (InvocationTargetException | IllegalArgumentException | IllegalAccessException | InstantiationException | NoSuchMethodException | SecurityException e) { throw new UnsupportedOperationException("Not supported"); } } BEAN targetBean = binder.getBean(); binder.removeBean(); binder.getBindings().stream().forEach(e -> { @SuppressWarnings("unchecked") Setter<BEAN, Object> setter = (Setter<BEAN, Object>) e.setter; if (setter != null) { setter.accept(targetBean, e.getter.apply(bean)); } }); binder.setBean(targetBean); }
@Override public void writeBean(BEAN bean) throws ValidationException { if (!binder.isValid()) { BasicBinderValidationStatus<BEAN> vs = binder.getValidationStatus(); throw new ValidationException(vs.getFieldValidationErrors(), vs.getBeanValidationErrors()); } BEAN sourceBean = binder.getBean(); if (sourceBean == null) { return; } binder.getBindings().stream().forEach(e -> { @SuppressWarnings("unchecked") Setter<BEAN, Object> setter = (Setter<BEAN, Object>) e.setter; if (setter != null) { setter.accept(bean, e.getter.apply(sourceBean)); } }); // Trigger StatusChange (required by Grid editor). binder.removeBean(); binder.setBean(sourceBean); }
@SuppressWarnings({ "unchecked", "rawtypes" }) public <PRESENTATION, MODEL> EasyBinding<BEAN, PRESENTATION, MODEL> bind(HasValue<PRESENTATION> field, String propertyName, Converter<PRESENTATION, ?> converter, boolean readOnly) { Objects.requireNonNull(converter); Objects.requireNonNull(propertyName, "Property name cannot be null"); // checkUnbound(); PropertyDefinition<BEAN, ?> definition = propertySet.getProperty(propertyName) .orElseThrow(() -> new IllegalArgumentException( "Could not resolve property name " + propertyName + " from " + propertySet)); ValueProvider<BEAN, ?> getter = definition.getGetter(); Setter<BEAN, ?> setter = readOnly ? null : definition.getSetter().orElse(null); EasyBinding<BEAN, PRESENTATION, MODEL> binding = bind(field, (ValueProvider) getter, (Setter) setter, propertyName, (Converter) converter); boundProperties.put(propertyName, binding); Optional<Field> modelField = getDeclaredFieldByName(definition.getPropertyHolderType(), definition.getName()); if (Arrays.asList(modelField.get().getAnnotations()).stream().anyMatch(requiredConfigurator)) { field.setRequiredIndicatorVisible(true); } return binding; }
public EasyBinding(BasicBinder<BEAN> binder, HasValue<FIELDVALUE> field, ValueProvider<BEAN, TARGET> getter, Setter<BEAN, TARGET> setter, String property, Converter<FIELDVALUE, TARGET> converterValidatorChain) { this.field = field; this.getter = getter; this.setter = setter; this.property = property; this.converterValidatorChain = converterValidatorChain; registration = field.addValueChangeListener(e -> { if (binder.getBean() != null) { if (binder.fieldToBean(this)) { binder.fireValueChangeEvent(e); } } }); if (setter == null) { field.setReadOnly(true); } }
public <FIELDVALUE, TARGET> EasyBinding<BEAN, FIELDVALUE, TARGET> bind(HasValue<FIELDVALUE> field, ValueProvider<BEAN, TARGET> getter, Setter<BEAN, TARGET> setter, String property, Converter<FIELDVALUE, TARGET> converter) { Objects.requireNonNull(field); Objects.requireNonNull(getter); Objects.requireNonNull(converter); // Register as binding EasyBinding<BEAN, FIELDVALUE, TARGET> binding = new EasyBinding<BEAN, FIELDVALUE, TARGET>(this, field, getter, setter, property, converter); // TODO: remove from binding /* * binding.registration = field.addValueChangeListener(e -> { if (getBean() != * null) { if(fieldToBean(binding)) { fireValueChangeEvent(e); } } }); */ bindings.add(binding); // Add property to validation error map if (property != null) { propertyToBindingMap.put(property, binding); } if (getBean() != null) { if (fieldToBean(binding)) { // TODO: should this be fired? // fireValueChangeEvent(e); } } else { fireStatusChangeEvent(); } return binding; }
@Override public Optional<Setter<PropertyBox, V>> getSetter() { if (property.isReadOnly()) { return Optional.empty(); } return Optional.of((pb, value) -> { pb.setValue(property, value); }); }
@Override public <FIELDVALUE> Binding<BEAN, FIELDVALUE> bind(HasValue<FIELDVALUE> field, ValueProvider<BEAN, FIELDVALUE> getter, Setter<BEAN, FIELDVALUE> setter) { throw new UnsupportedOperationException("Not supported"); }
public <FIELDVALUE, TARGET> EasyBinding<BEAN, FIELDVALUE, FIELDVALUE> bind(HasValue<FIELDVALUE> field, ValueProvider<BEAN, FIELDVALUE> getter, Setter<BEAN, FIELDVALUE> setter, String property) { return bind(field, getter, setter, property, Converter.identity()); }