Create a procedure with an enum parameter, but different enums could be indicated.



  • I have Grid in Vaadin. As a pole filter, I add ComboBox filled enum.

    private enum CITY_LIST {
        Все, Тамбов, Орёл, Краснодар
    }
    private ComboBox getColumnFilterComoBox(final Object columnId, Grid p_grid, Enum en, Object val) {
            ComboBox combobox = new ComboBox();
            combobox.setInvalidAllowed(false);
            combobox.setNullSelectionAllowed(false);
            combobox.setTextInputAllowed(false);
            //Заполняю ComboBox перечислением
            combobox.addItems(CITY_LIST.values());
            combobox.setValue(CITY_LIST.Все);
            combobox.setWidth("100%");
            combobox.addStyleName(ValoTheme.TEXTFIELD_TINY);
            combobox.addValueChangeListener(new Property.ValueChangeListener() {
                    SimpleStringFilter filter = null;
    
                @Override
                public void valueChange(Property.ValueChangeEvent event) {
                    Filterable f = (Filterable) p_grid.getContainerDataSource();
                    // Remove old filter
                    if (filter != null) {
                        f.removeContainerFilter(filter);
                    }
                    //Какое то условие
                    if ((CITY_LIST)combobox.getValue() != CITY_LIST.Все) {
                        filter = new SimpleStringFilter(columnId, combobox.getValue().toString(),
                                true, false);
                        f.addContainerFilter(filter);
    
                        p_grid.cancelEditor();
                    }
                }
            });
        return combobox;
    }
    

    The example indicates the listing of CITY_LIST. How do I change the function to transmit in the input parameters the list to which ComboBox is filled and the value to be chosen. How do we make it pleasant and not lose the compilation verification?
    P.S. There are a few tens, different tables.



  • It can be done:

    private <T extends Enum<T>> ComboBox getColumnFilterComboBox(
            final Object columnId, Grid p_grid, Class<T> enumClass, final T value
        ) {
        ...
        combobox.addItems(enumClass.getEnumConstants());
        combobox.setValue(value);
        ...
        //Какое то условие
        if (combobox.getValue() != value) {
        }
    }
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2