alejandro-du / crudui

Automatically generate CRUD-like Vaadin views for any Java Bean
https://vaadin.com/directory#!addon/crud-ui-add-on
Apache License 2.0
86 stars 54 forks source link

Cannot block the Delete button #83

Closed DanielMartensson closed 3 years ago

DanielMartensson commented 3 years ago

Hi!

When doing:

crudFormFactory.setDisabledProperties(CrudOperation.DELETE, columnProperties);

This will not block the delete button.

Markering_021

By the way! Your library is awesome!

Full code:

@Route("pwm")
@CssImport("./styles/shared-styles.css")
@CssImport(value = "./styles/vaadin-text-field-styles.css", themeFor = "vaadin-text-field")
/**
 * This class modifies the user interface for pwm handling
 * 
 * @author dell
 *
 */
public class PWMFrequencyView extends AppLayout {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private static final int MIN_VALUE_INTEGER_FIELD = 31;

    private static final int MAX_VALUE_INTEGER_FIELD = 2000000;

    @Autowired
    private PWMService pwmService;

    @Autowired
    private Serial serial;

    @PostConstruct
    public void init() {
        Top top = new Top();
        top.setTopAppLayout(this);

        // Selector and integer text fields
        Select<SerialPort> serialDevice = new Select<SerialPort>(serial.getSerialPorts()); // Will show port description
        serialDevice.setLabel("Serial devices");
        IntegerField Frequency_P0_P1_P2 = new IntegerField("Frequency P0 P1 P2");
        IntegerField Frequency_P3_P7_P8 = new IntegerField("Frequency P3 P7 P8");
        IntegerField Frequency_P6_P5 = new IntegerField("Frequency P6 P5");
        IntegerField Frequency_P4 = new IntegerField("Frequency P4");

        // Get values
        int Frequency_P0_P1_P2_Value = MIN_VALUE_INTEGER_FIELD;
        int Frequency_P3_P7_P8_Value = MIN_VALUE_INTEGER_FIELD;
        int Frequency_P6_P5_Value = MIN_VALUE_INTEGER_FIELD;
        int Frequency_P4_Value = MIN_VALUE_INTEGER_FIELD;
        List<PWM> list = pwmService.findAll(); // We only want the first row
        if(list.size() > 0) {
            Frequency_P0_P1_P2_Value = list.get(0).getFrequencyP0P1P2();
            Frequency_P3_P7_P8_Value = list.get(0).getFrequencyP3P7P8();
            Frequency_P6_P5_Value = list.get(0).getFrequencyP6P5();
            Frequency_P4_Value = list.get(0).getFrequencyP4();
        }

        // Set max and min value and also current value
        setMinMaxValueIntegerField(Frequency_P0_P1_P2, Frequency_P0_P1_P2_Value);
        setMinMaxValueIntegerField(Frequency_P3_P7_P8, Frequency_P3_P7_P8_Value);
        setMinMaxValueIntegerField(Frequency_P6_P5, Frequency_P6_P5_Value);
        setMinMaxValueIntegerField(Frequency_P4, Frequency_P4_Value);

        // Grid layout
        GridCrud<PWM> pwmCrud = new GridCrud<>(PWM.class);
        CrudFormFactory<PWM> crudFormFactory = new DefaultCrudFormFactory<PWM>(PWM.class);
        pwmCrud.setCrudFormFactory(crudFormFactory);
        String[] columnNames = new String[] {"portName", "frequencyP0P1P2", "frequencyP3P7P8", "frequencyP6P5", "frequencyP4"};
        String[] columnProperties = new String[] {"id", "portName", "frequencyP0P1P2", "frequencyP3P7P8", "frequencyP6P5", "frequencyP4"};
        pwmCrud.getGrid().setColumns(columnNames);
        pwmCrud.getGrid().setColumnReorderingAllowed(true);
        crudFormFactory.setUseBeanValidation(true);
        crudFormFactory.setDisabledProperties(CrudOperation.ADD, columnProperties);
        crudFormFactory.setDisabledProperties(CrudOperation.DELETE, columnProperties);
        crudFormFactory.setDisabledProperties(CrudOperation.UPDATE, columnProperties);

        // Select settings
        Button selectSetting = new Button("Select settings");
        selectSetting.addClickListener(e -> {
            if(serialDevice.getValue() == null)
                return;
            pwmService.deleteAll(); // Only one setting allowed
            int P0_P1_P2_Value = Frequency_P0_P1_P2.getValue(); 
            int P3_P7_P8_Value = Frequency_P3_P7_P8.getValue();
            int P6_P5_Value = Frequency_P6_P5.getValue();
            int P4_Value = Frequency_P4.getValue();
            pwmService.save(new PWM(0, serialDevice.getValue().getPortDescription(), P0_P1_P2_Value, P3_P7_P8_Value, P6_P5_Value, P4_Value));
            int[] frequency = new int[4];
            frequency[0] = 2000000/P0_P1_P2_Value;
            frequency[1] = 2000000/P3_P7_P8_Value;
            frequency[2] = 2000000/P6_P5_Value;
            frequency[3] = 2000000/P4_Value;
            //serial.trancieve(frequency);
            pwmCrud.refreshGrid();
        });
        FormLayout settings = new FormLayout(serialDevice, Frequency_P0_P1_P2, Frequency_P3_P7_P8, Frequency_P6_P5, Frequency_P4, selectSetting);

        // Listener
        pwmCrud.setCrudListener(new CrudListener<PWM>() {

            /**
             * 
             */
            private static final long serialVersionUID = 1L;

            @Override
            public Collection<PWM> findAll() {
                return pwmService.findAll();
            }

            @Override
            public PWM add(PWM pwm) {
                return null;
            }

            @Override
            public PWM update(PWM pwm) {
                return null;
            }

            @Override
            public void delete(PWM pwm) {}

        });

        setContent(new VerticalLayout(settings, pwmCrud));
    }

    private void setMinMaxValueIntegerField(IntegerField integerField, int value) {
        // Min MIN_VALUE_INTEGER_FIELD Hz and max MAX_VALUE_INTEGER_FIELD Hz as TIM frequency in this STM32 device
        integerField.setMin(MIN_VALUE_INTEGER_FIELD);
        integerField.setHasControls(true);
        integerField.setMax(MAX_VALUE_INTEGER_FIELD);
        integerField.setValue(value);
    }

    public PWMFrequencyView() {

    }
}
DanielMartensson commented 3 years ago

By the way! Do you think there is a better way to display this code? Perhaps remove the listener because I don't use it except for the findAll() function?

alejandro-du commented 3 years ago

Thanks for the feedback! This method is for disabling input components in forms. The dialog in your screenshot is simply a confirmation dialog. To disable the delete operation use:

crud.setDeleteOperationVisible(false);

By the way! Do you think there is a better way to display this code? Perhaps remove the listener because I don't use it except for the findAll() function?

You can use:

crud.setFindAllOperation(() -> pwmService.findAll());