Example usage for javafx.scene.control TreeTableColumn setCellFactory

List of usage examples for javafx.scene.control TreeTableColumn setCellFactory

Introduction

In this page you can find the example usage for javafx.scene.control TreeTableColumn setCellFactory.

Prototype

public final void setCellFactory(Callback<TreeTableColumn<S, T>, TreeTableCell<S, T>> value) 

Source Link

Usage

From source file:qupath.lib.gui.tma.TMASummaryViewer.java

private void refreshTableData() {

    //      int nn = 0;
    //      double nPositive = 0;
    //      for (TMAEntry entry : entriesBase) {
    //         if (entry.isMissing())
    //            continue;
    //         nPositive += entry.getMeasurementAsDouble("Num Positive");
    //         nn++;
    //      }//from  w  w w  .j  av a2  s. co  m
    //      System.err.println(nPositive + " positive cells across " + nn + " tissue samples");

    Collection<? extends TMAEntry> entries = groupByIDProperty.get() ? createSummaryEntries(entriesBase)
            : entriesBase;

    // Ensure that we don't try to modify a filtered list
    List<TreeTableColumn<TMAEntry, ?>> columns = new ArrayList<>();

    // Add an empty column.
    // Its purpose is to provide the space needed for the little expansion arrows, to avoid 
    // these stealing space from the first interesting column.
    // Note: there's nothing to prevent the user reordering it along with other columns... 
    // but hopefully it looks 'right' enough where it is that few would try to do that
    TreeTableColumn<TMAEntry, String> columnEmpty = new TreeTableColumn<>("  ");
    columnEmpty
            .setCellValueFactory(new Callback<CellDataFeatures<TMAEntry, String>, ObservableValue<String>>() {
                @Override
                public ObservableValue<String> call(CellDataFeatures<TMAEntry, String> p) {
                    return Bindings.createStringBinding(() -> "");
                }
            });
    columnEmpty.setSortable(false);
    columnEmpty.setResizable(false);
    columns.add(columnEmpty);

    // Check if we have any images or overlays
    boolean hasImages = entries.stream().anyMatch(e -> e.hasImage());
    boolean hasOverlay = entries.stream().anyMatch(e -> e.hasOverlay());

    // Add columns to show images, if we have them
    if (hasImages || hasOverlay) {
        TreeTableColumn<TMAEntry, TMAEntry> columnImage = hasImages ? new TreeTableColumn<>("Thumbnail") : null;
        TreeTableColumn<TMAEntry, TMAEntry> columnOverlay = hasOverlay ? new TreeTableColumn<>("Overlay")
                : null;

        if (hasImages) {
            columnImage.setCellValueFactory(
                    new Callback<CellDataFeatures<TMAEntry, TMAEntry>, ObservableValue<TMAEntry>>() {
                        @Override
                        public ObservableValue<TMAEntry> call(CellDataFeatures<TMAEntry, TMAEntry> p) {
                            return p.getValue().valueProperty();
                        }
                    });
            columnImage.setCellFactory(c -> new ImageTableCell(imageCache, false));
            columnImage.maxWidthProperty().bind(maxSmallWidth);
            columnImage.widthProperty().addListener((v, o, n) -> {
                if (n.doubleValue() == columnImage.getPrefWidth())
                    return;
                if (hasOverlay)
                    columnOverlay.setPrefWidth(n.doubleValue());
                table.refresh();
            });
            columns.add(columnImage);
        }

        if (hasOverlay) {
            columnOverlay.setCellValueFactory(
                    new Callback<CellDataFeatures<TMAEntry, TMAEntry>, ObservableValue<TMAEntry>>() {
                        @Override
                        public ObservableValue<TMAEntry> call(CellDataFeatures<TMAEntry, TMAEntry> p) {
                            return p.getValue().valueProperty();
                        }
                    });
            columnOverlay.setCellFactory(c -> new ImageTableCell(imageCache, true));
            columnOverlay.maxWidthProperty().bind(maxSmallWidth);
            columnOverlay.widthProperty().addListener((v, o, n) -> {
                if (n.doubleValue() == columnOverlay.getPrefWidth())
                    return;
                columnImage.setPrefWidth(n.doubleValue());
                if (hasImages)
                    table.refresh();
            });
            columns.add(columnOverlay);
        }
    }

    // Update image availability
    if (hasImages) {
        if (hasOverlay)
            imageAvailability.set(ImageAvailability.BOTH);
        else
            imageAvailability.set(ImageAvailability.IMAGE_ONLY);
    } else if (hasOverlay) {
        imageAvailability.set(ImageAvailability.OVERLAY_ONLY);
    } else
        imageAvailability.set(ImageAvailability.NONE);

    for (String name : model.getAllNames()) {
        if (model.getMeasurementNames().contains(name)) {
            TreeTableColumn<TMAEntry, Number> column = new TreeTableColumn<>(name);
            column.setCellValueFactory(
                    new Callback<CellDataFeatures<TMAEntry, Number>, ObservableValue<Number>>() {
                        @Override
                        public ObservableValue<Number> call(CellDataFeatures<TMAEntry, Number> p) {
                            double value = p.getValue() == null ? Double.NaN
                                    : model.getNumericValue(p.getValue().getValue(), name);
                            return new SimpleDoubleProperty(value);
                        }
                    });
            column.setCellFactory(c -> new NumericTableCell<>());
            columns.add(column);
        } else {
            TreeTableColumn<TMAEntry, Object> column = new TreeTableColumn<>(name);
            column.setCellValueFactory(
                    new Callback<CellDataFeatures<TMAEntry, Object>, ObservableValue<Object>>() {
                        @Override
                        public ObservableValue<Object> call(CellDataFeatures<TMAEntry, Object> p) {
                            return new SimpleObjectProperty<>(p.getValue() == null ? null
                                    : model.getStringValue(p.getValue().getValue(), name));
                        }
                    });
            column.setCellFactory(c -> new BasicTableCell<>());
            columns.add(column);
        }
    }

    // Set the column visibility depending upon whether they were hidden previously
    columns.stream().forEach(c -> c.setVisible(!lastHiddenColumns.contains(c.getText())));

    // Set columns for table
    table.getColumns().setAll(columns);

    // Set new root for table
    TreeItem<TMAEntry> root = new RootTreeItem(entries, combinedPredicate);
    table.setShowRoot(false);
    table.setRoot(root);

    model.refreshList();
}