dimanche 13 octobre 2019

JavaFX generifying CellValueFactory for columns

I have been working on a project that has got many Tabs, Tables and of course Columns. I'm trying to generify the process, mostly because of scaling (duplicating code when adding new Tables, Columns..) and because of efficiency.

I'd like to shrink this code:

        column_productReference.setCellValueFactory(new PropertyValueFactory<>("productReference"));
        column_productName.setCellValueFactory(new PropertyValueFactory<>("productName"));
        column_productName.setSortType(TableColumn.SortType.ASCENDING);
        column_productPrice.setCellValueFactory(new PropertyValueFactory<>("productPriceWODDV"));
        column_priceWithDDV.setCellValueFactory(new PropertyValueFactory<>("productPrice"));
        column_productSKU.setCellValueFactory(new PropertyValueFactory<>("productSKU"));
        column_productBarcode.setCellValueFactory(new PropertyValueFactory<>("productBarcode"));
        column_VATValue.setCellValueFactory(new PropertyValueFactory<>("productVATValue"));

I want to make a method that will automatically set CellValueFactory for columns in the desired table based on the class and constructor parameters I'd provide (code below). It could also get a class parsed to the table. I'm using "desiredConstructor" because my class has multiple fields, some of which are reused, therefore I used another constructor instead of the whole new class. I'd like to get some feedback on how to do it properly (I am new to generics and reflection).

private static <T> void setColumnValueFactory(TableView table, Class objectClass, Constructor desiredConstructor) {
        try {
            table.getColumns().forEach((tableColumn) -> {
                TableColumn column = (TableColumn) tableColumn;
                for (Constructor constructor : objectClass.getDeclaredConstructors()) {
                    if (constructor.equals(desiredConstructor)) {
                        for (Parameter parameter : constructor.getParameters()) {
                            column.setCellValueFactory(new PropertyValueFactory<>(parameter.getName()));
                        }
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Example of the constructor I'm trying to generify:

public products(String productName, String productBarcode, Integer productReference, Integer productSKU, BigDecimal productPriceWODDV, BigDecimal productVATValue) {
        this.productName = new SimpleStringProperty(StringUtils.normalizeSpace(productName));
        this.productBarcode = new SimpleStringProperty(StringUtils.normalizeSpace(productBarcode));
        this.productReference = new SimpleIntegerProperty(productReference);
        this.productSKU = new SimpleIntegerProperty(productSKU);
        this.productPriceWODDV = new SimpleDoubleProperty(productPriceWODDV.setScale(2, RoundingMode.CEILING).doubleValue());
        this.productPrice = new SimpleDoubleProperty(productPriceWODDV.multiply(new BigDecimal(1.22)).setScale(2, RoundingMode.CEILING).doubleValue());
        this.productVATValue = new SimpleDoubleProperty(productVATValue.doubleValue());
    }




Aucun commentaire:

Enregistrer un commentaire