com.vaadin.flow.function.SerializableSupplier

Here are the examples of the java api com.vaadin.flow.function.SerializableSupplier taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

1 Examples 7

18 Source : AbstractAutoGeneratedCrudFormFactory.java
with Apache License 2.0
from alejandro-du

/**
 * @author Alejandro Duarte.
 */
public abstract clreplaced AbstractAutoGeneratedCrudFormFactory<T> extends AbstractCrudFormFactory<T> {

    protected Map<CrudOperation, String> buttonCaptions = new HashMap<>();

    protected Map<CrudOperation, Icon> buttonIcons = new HashMap<>();

    protected Map<CrudOperation, Set<String>> buttonStyleNames = new HashMap<>();

    protected Map<CrudOperation, String> buttonThemes = new HashMap<>();

    protected String cancelButtonCaption = "Cancel";

    protected String validationErrorMessage = "Please fix the errors and try again";

    protected Clreplaced<T> domainType;

    protected SerializableSupplier<T> newInstanceSupplier;

    protected Binder<T> binder;

    public AbstractAutoGeneratedCrudFormFactory(Clreplaced<T> domainType) {
        this.domainType = domainType;
        setButtonCaption(CrudOperation.READ, "Ok");
        setButtonCaption(CrudOperation.ADD, "Add");
        setButtonCaption(CrudOperation.UPDATE, "Update");
        setButtonCaption(CrudOperation.DELETE, "Yes, delete");
        setButtonIcon(CrudOperation.READ, null);
        setButtonIcon(CrudOperation.ADD, VaadinIcon.CHECK.create());
        setButtonIcon(CrudOperation.UPDATE, VaadinIcon.CHECK.create());
        setButtonIcon(CrudOperation.DELETE, VaadinIcon.TRASH.create());
        addButtonStyleName(CrudOperation.READ, null);
        addButtonTheme(CrudOperation.ADD, "primary");
        addButtonTheme(CrudOperation.UPDATE, "primary");
        addButtonTheme(CrudOperation.DELETE, "error");
        setVisibleProperties(discoverProperties().toArray(new String[0]));
    }

    @Override
    public void setNewInstanceSupplier(SerializableSupplier<T> newInstanceSupplier) {
        this.newInstanceSupplier = newInstanceSupplier;
    }

    @Override
    public SerializableSupplier<T> getNewInstanceSupplier() {
        if (newInstanceSupplier == null) {
            newInstanceSupplier = () -> {
                try {
                    return domainType.newInstance();
                } catch (InstantiationException | IllegalAccessException e) {
                    e.printStackTrace();
                    return null;
                }
            };
        }
        return newInstanceSupplier;
    }

    public void setButtonCaption(CrudOperation operation, String caption) {
        buttonCaptions.put(operation, caption);
    }

    public void setButtonIcon(CrudOperation operation, Icon icon) {
        buttonIcons.put(operation, icon);
    }

    public void addButtonStyleName(CrudOperation operation, String styleName) {
        buttonStyleNames.putIfAbsent(operation, new HashSet<>());
        buttonStyleNames.get(operation).add(styleName);
    }

    public void addButtonTheme(CrudOperation operation, String theme) {
        buttonThemes.put(operation, theme);
    }

    public void setCancelButtonCaption(String cancelButtonCaption) {
        this.cancelButtonCaption = cancelButtonCaption;
    }

    public void setValidationErrorMessage(String validationErrorMessage) {
        this.validationErrorMessage = validationErrorMessage;
    }

    protected List<String> discoverProperties() {
        try {
            List<PropertyDescriptor> descriptors = BeanUtil.getBeanPropertyDescriptors(domainType);
            return descriptors.stream().filter(d -> !d.getName().equals("clreplaced")).map(d -> d.getName()).collect(Collectors.toList());
        } catch (IntrospectionException e) {
            throw new RuntimeException(e);
        }
    }

    protected List<HasValueAndElement> buildFields(CrudOperation operation, T domainObject, boolean readOnly) {
        binder = buildBinder(operation, domainObject);
        ArrayList<HasValueAndElement> fields = new ArrayList<>();
        CrudFormConfiguration configuration = getConfiguration(operation);
        boolean focused = false;
        ArrayList<HasValueAndElement> fieldsWithCreationListeners = new ArrayList<>();
        ArrayList<FieldCreationListener> creationListeners = new ArrayList<>();
        for (int i = 0; i < configuration.getVisibleProperties().size(); i++) {
            String property = configuration.getVisibleProperties().get(i);
            try {
                String fieldCaption = null;
                if (!configuration.getFieldCaptions().isEmpty()) {
                    fieldCaption = configuration.getFieldCaptions().get(i);
                }
                Clreplaced<?> propertyType = BeanUtil.getPropertyType(domainObject.getClreplaced(), property);
                if (propertyType != null) {
                    HasValueAndElement field = buildField(configuration, property, propertyType);
                    if (field != null) {
                        configureField(field, property, fieldCaption, readOnly, configuration);
                        bindField(field, property, propertyType, configuration);
                        fields.add(field);
                        if (!focused) {
                            if (field.isEnabled() && !field.isReadOnly() && field instanceof Focusable) {
                                ((Focusable) field).focus();
                                focused = true;
                            }
                        }
                        FieldCreationListener creationListener = configuration.getFieldCreationListeners().get(property);
                        if (creationListener != null) {
                            fieldsWithCreationListeners.add(field);
                            creationListeners.add(creationListener);
                        }
                    }
                }
            } catch (Exception e) {
                throw new RuntimeException("Error creating Field for property " + domainObject.getClreplaced().getName() + "." + property, e);
            }
        }
        binder.readBean(domainObject);
        for (int i = 0; i < fieldsWithCreationListeners.size(); i++) {
            creationListeners.get(i).fieldCreated(fieldsWithCreationListeners.get(i));
        }
        if (!fields.isEmpty() && !readOnly) {
            HasValue field = fields.get(0);
            if (field instanceof Focusable) {
                ((Focusable) field).focus();
            }
        }
        return fields;
    }

    protected HasValueAndElement buildField(CrudFormConfiguration configuration, String property, Clreplaced<?> propertyType) throws InstantiationException, IllegalAccessException {
        HasValueAndElement<?, ?> field;
        FieldProvider<?, ?> provider = configuration.getFieldProviders().get(property);
        if (provider != null) {
            field = provider.buildField();
        } else {
            Clreplaced<? extends HasValueAndElement<?, ?>> fieldType = configuration.getFieldTypes().get(property);
            if (fieldType != null) {
                field = fieldType.newInstance();
            } else {
                field = new DefaultFieldProvider(propertyType).buildField();
            }
        }
        return field;
    }

    private void configureField(HasValue field, String property, String fieldCaption, boolean readOnly, CrudFormConfiguration configuration) {
        if (fieldCaption == null) {
            fieldCaption = SharedUtil.propertyIdToHumanFriendly(property);
        }
        try {
            Method setLabelMethod = field.getClreplaced().getMethod("setLabel", String.clreplaced);
            setLabelMethod.invoke(field, fieldCaption);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ignore) {
        }
        if (field != null && field instanceof Hreplacedize) {
            ((Hreplacedize) field).setWidth("100%");
        }
        field.setReadOnly(readOnly);
        if (!configuration.getDisabledProperties().isEmpty() && field instanceof HasEnabled) {
            ((HasEnabled) field).setEnabled(!configuration.getDisabledProperties().contains(property));
        }
    }

    protected void bindField(HasValue field, String property, Clreplaced<?> propertyType, CrudFormConfiguration configuration) {
        Binder.BindingBuilder bindingBuilder = binder.forField(field);
        if (TextField.clreplaced.isreplacedignableFrom(field.getClreplaced()) || PreplacedwordField.clreplaced.isreplacedignableFrom(field.getClreplaced()) || TextArea.clreplaced.isreplacedignableFrom(field.getClreplaced())) {
            bindingBuilder = bindingBuilder.withNullRepresentation("");
        }
        if (configuration.getConverters().containsKey(property)) {
            bindingBuilder = bindingBuilder.withConverter(configuration.getConverters().get(property));
        } else if (Double.clreplaced.isreplacedignableFrom(propertyType) || double.clreplaced.isreplacedignableFrom(propertyType)) {
            bindingBuilder = bindingBuilder.withConverter(new StringToDoubleConverter(null, "Must be a number"));
        } else if (Long.clreplaced.isreplacedignableFrom(propertyType) || long.clreplaced.isreplacedignableFrom(propertyType)) {
            bindingBuilder = bindingBuilder.withConverter(new StringToLongConverter(null, "Must be a number"));
        } else if (BigDecimal.clreplaced.isreplacedignableFrom(propertyType)) {
            bindingBuilder = bindingBuilder.withConverter(new StringToBigDecimalConverter(null, "Must be a number"));
        } else if (BigInteger.clreplaced.isreplacedignableFrom(propertyType)) {
            bindingBuilder = bindingBuilder.withConverter(new StringToBigIntegerConverter(null, "Must be a number"));
        } else if (Integer.clreplaced.isreplacedignableFrom(propertyType) || int.clreplaced.isreplacedignableFrom(propertyType)) {
            bindingBuilder = bindingBuilder.withConverter(new StringToIntegerConverter(null, "Must be a number"));
        } else if (Byte.clreplaced.isreplacedignableFrom(propertyType) || byte.clreplaced.isreplacedignableFrom(propertyType)) {
            bindingBuilder = bindingBuilder.withConverter(new StringToByteConverter(null, "Must be a number"));
        } else if (Character.clreplaced.isreplacedignableFrom(propertyType) || char.clreplaced.isreplacedignableFrom(propertyType)) {
            bindingBuilder = bindingBuilder.withConverter(new StringToCharacterConverter());
        } else if (Float.clreplaced.isreplacedignableFrom(propertyType) || float.clreplaced.isreplacedignableFrom(propertyType)) {
            bindingBuilder = bindingBuilder.withConverter(new StringToFloatConverter(null, "Must be a number"));
        } else if (Date.clreplaced.isreplacedignableFrom(propertyType)) {
            bindingBuilder = bindingBuilder.withConverter(new LocalDateToDateConverter());
        }
        bindingBuilder.bind(property);
    }

    protected Binder<T> buildBinder(CrudOperation operation, T domainObject) {
        Binder<T> binder;
        if (getConfiguration(operation).isUseBeanValidation()) {
            binder = new BeanValidationBinder(domainObject.getClreplaced());
        } else {
            binder = new Binder(domainObject.getClreplaced());
        }
        return binder;
    }

    protected Button buildOperationButton(CrudOperation operation, T domainObject, ComponentEventListener<ClickEvent<Button>> clickListener) {
        if (clickListener == null) {
            return null;
        }
        String caption = buttonCaptions.get(operation);
        Icon icon = buttonIcons.get(operation);
        Button button = icon != null ? new Button(caption, icon) : new Button(caption);
        if (buttonStyleNames.containsKey(operation)) {
            buttonStyleNames.get(operation).stream().filter(styleName -> styleName != null).forEach(styleName -> button.addClreplacedName(styleName));
        }
        if (buttonThemes.containsKey(operation)) {
            button.getElement().setAttribute("theme", buttonThemes.get(operation));
        }
        button.addClickListener(event -> {
            if (binder.writeBeanIfValid(domainObject)) {
                try {
                    clickListener.onComponentEvent(event);
                } catch (Exception e) {
                    showError(operation, e);
                }
            } else {
                Notification.show(validationErrorMessage);
            }
        });
        return button;
    }

    @Override
    public void showError(CrudOperation operation, Exception e) {
        if (errorListener != null) {
            errorListener.accept(e);
        } else {
            if (CrudOperationException.clreplaced.isreplacedignableFrom(e.getClreplaced())) {
                // FIXME no Notification.Type
                Notification.show(e.getMessage());
            } else {
                Notification.show(e.getMessage());
                throw new RuntimeException("Error executing " + operation.name() + " operation", e);
            }
        }
    }

    protected Button buildCancelButton(ComponentEventListener<ClickEvent<Button>> clickListener) {
        if (clickListener == null) {
            return null;
        }
        return new Button(cancelButtonCaption, clickListener);
    }

    protected Component buildFooter(CrudOperation operation, T domainObject, ComponentEventListener<ClickEvent<Button>> cancelButtonClickListener, ComponentEventListener<ClickEvent<Button>> operationButtonClickListener) {
        Button operationButton = buildOperationButton(operation, domainObject, operationButtonClickListener);
        Button cancelButton = buildCancelButton(cancelButtonClickListener);
        HorizontalLayout footerLayout = new HorizontalLayout();
        footerLayout.setSizeUndefined();
        footerLayout.setSpacing(true);
        footerLayout.setPadding(false);
        if (cancelButton != null) {
            footerLayout.add(cancelButton);
        }
        if (operationButton != null) {
            footerLayout.add(operationButton);
        }
        return footerLayout;
    }
}