类型bindingbuilder< livre,stock>中的方法withconverter(converter< stock,newtarget>)不适用于参数

vx6bjr1n  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(259)

类型bindingbuilder<livre,stock>中的方法withconverter(converter<stock,newtarget>)不适用于参数(livrorm.myconverter)。由于在vaadin crm教程应用程序中实现vaading myconverter类,这个错误一直困扰着我。其思想是接受stock::getquantitestock提供的整数值,并输出一个字符串。我很清楚它为什么会出现,以及如何避免它。我欢迎您提供任何建议,并添加我的完整表单类供您检查:

package com.vaadin.tutorial.crm.UI.views.list;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.ComponentEvent;
import com.vaadin.flow.component.ComponentEventListener;
import com.vaadin.flow.component.HasValue;
import com.vaadin.flow.component.Key;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.combobox.ComboBox;
import com.vaadin.flow.component.formlayout.FormLayout;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.textfield.EmailField;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.data.binder.BeanValidationBinder;
import com.vaadin.flow.data.binder.Binder;
import com.vaadin.flow.data.binder.Result;
import com.vaadin.flow.data.binder.ValidationException;
import com.vaadin.flow.data.binder.ValueContext;
import com.vaadin.flow.shared.Registration;
import com.vaadin.tutorial.crm.backend.entity.Livre;
import com.vaadin.tutorial.crm.backend.entity.Livre.Categorie;
import com.vaadin.tutorial.crm.backend.entity.Stock;
import com.vaadin.flow.data.converter.Converter;
import com.vaadin.flow.data.converter.StringToIntegerConverter;

import java.util.List;

public class LivreForm extends FormLayout{
    TextField titreLivre = new TextField("titreLivre");
    TextField description = new TextField("description");
    TextField auteur = new TextField("auteur");
    TextField refeni = new TextField("refeni");
    TextField isbn = new TextField("isbn");
    ComboBox<Livre.Categorie> categorie = new ComboBox<>("Categorie");
    ComboBox<Livre.Status> status = new ComboBox<>("Status");
    ComboBox<Stock> stock = new ComboBox<>("Stock");
    ComboBox<Livre.Campus> campus = new ComboBox<>("Campus");

    Button save = new Button("Save"); 
    Button delete = new Button("Delete");
    Button close = new Button("Cancel");
    Binder<Livre> binder = new BeanValidationBinder<>(Livre.class);
    private Livre livre;

    public LivreForm(List<Stock> stocks) {
        addClassName("livre-form");
        binder.forField(stock)
          .withConverter(new MyConverter())
          .bind(Stock::getQuantiteStock, Stock::setQuantiteStock);
        binder.bindInstanceFields(this);
        stock.setItems(stocks);
        status.setItems(Livre.Status.values());
        categorie.setItems(Livre.Categorie.values());
        add(titreLivre,
            description,
            auteur,
            refeni,
            isbn,
            categorie,
            status,
            campus,
            stock,
            createButtonsLayout()); 
      }

    public void setLivre(Livre livre) {
        this.livre = livre; 
        binder.readBean(livre); 
    }

    private Component createButtonsLayout() {
        save.addThemeVariants(ButtonVariant.LUMO_PRIMARY); 
        delete.addThemeVariants(ButtonVariant.LUMO_ERROR);
        close.addThemeVariants(ButtonVariant.LUMO_TERTIARY);

        save.addClickShortcut(Key.ENTER); 
        close.addClickShortcut(Key.ESCAPE);

        save.addClickListener(event -> validateAndSave()); 
        delete.addClickListener(event -> fireEvent(new DeleteEvent(this, livre))); 
        close.addClickListener(event -> fireEvent(new CloseEvent(this))); 

        binder.addStatusChangeListener(e -> save.setEnabled(binder.isValid()));

        return new HorizontalLayout(save, delete, close); 
      }

    private void validateAndSave() {
          try {
            binder.writeBean(livre); 
            fireEvent(new SaveEvent(this, livre)); 
          } catch (ValidationException e) {
            e.printStackTrace();
          }
        }

    public static abstract class LivreFormEvent extends ComponentEvent<LivreForm> {

        /**
         * 
         */
        private static final long serialVersionUID = -7236023661050023675L;
        private Livre livre;

          protected LivreFormEvent(LivreForm source,Livre livre) { 
            super(source, false);
            this.livre = livre;
          }

          public Livre getLivre() {
            return livre;
          }
        }

        public static class SaveEvent extends LivreFormEvent {
          SaveEvent(LivreForm source, Livre livre) {
            super(source, livre);
          }
        }

        public static class DeleteEvent extends LivreFormEvent {
          DeleteEvent(LivreForm source, Livre livre) {
            super(source, livre);
          }

        }

        public static class CloseEvent extends LivreFormEvent {
          CloseEvent(LivreForm source) {
            super(source, null);
          }
        }

        public <T extends ComponentEvent<?>> Registration addListener(Class<T> eventType,
            ComponentEventListener<T> listener) { 
          return getEventBus().addListener(eventType, listener);
        }

        class MyConverter implements Converter<String, Integer> {
              @Override
              public Result<Integer> convertToModel(String fieldValue, ValueContext context) {
                // Produces a converted value or an error
                try {
                  // ok is a static helper method that creates a Result
                  return Result.ok(Integer.valueOf(fieldValue));
                } catch (NumberFormatException e) {
                  // error is a static helper method that creates a Result
                  return Result.error("Please enter a number");
                }
              }

              @Override
              public String convertToPresentation(Integer integer, ValueContext context) {
                // Converting to the field type should always succeed,
                // so there is no support for returning an error Result.
                return String.valueOf(integer);
              }
            }

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题