org.panteleyev.fx.TreeTableColumnBuilder Maven / Gradle / Ivy
The newest version!
/*
Copyright © 2024 Petr Panteleyev
SPDX-License-Identifier: BSD-2-Clause
*/
package org.panteleyev.fx;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TreeTableCell;
import javafx.scene.control.TreeTableColumn;
import javafx.util.Callback;
import java.util.Comparator;
import java.util.function.Consumer;
/**
* This class implements builder that creates instances of {@link TreeTableColumn}.
*
* @param The type of the {@link TreeTableColumn} generic type (i.e. S == TreeTableView<S>)
* @param The type of the content in all cells in this {@link TreeTableColumn}.
*/
public class TreeTableColumnBuilder {
private final String text;
private Callback, TreeTableCell> cellFactory;
private Callback propertyCallback;
private ObservableValue widthBinding;
private Comparator comparator;
/**
* Creates tree table column.
*
* @param text column text
* @param builder tree table column builder
* @param the type of the {@link TreeTableColumn} generic type
* @param the type of the item contained within the cell.
* @return tree table column
*/
public static TreeTableColumn treeTableColumn(String text, Consumer> builder) {
var columnBuilder = new TreeTableColumnBuilder(text);
builder.accept(columnBuilder);
return columnBuilder.build();
}
/**
* Creates tree table column.
*
* @param text column text
* @param builder tree table column builder
* @param the type of the {@link TreeTableColumn} generic type, cell contains the same type
* @return tree table column
*/
public static TreeTableColumn treeTableObjectColumn(String text, Consumer> builder) {
return treeTableColumn(text, builder.andThen(b -> b.withPropertyCallback(p -> p)));
}
private TreeTableColumnBuilder(String text) {
this.text = text;
}
/**
* Sets comparator for the tree table column. Such column is also set as sortable.
*
* @param comparator column comparator
* @return this
*/
public TreeTableColumnBuilder withComparator(Comparator comparator) {
this.comparator = comparator;
return this;
}
/**
* Sets cell factory for the tree table column. Refer to the {@link TreeTableColumn#setCellFactory(Callback)} for details.
*
* @param cellFactory cell factory
* @return this
*/
public TreeTableColumnBuilder withCellFactory(Callback, TreeTableCell> cellFactory) {
this.cellFactory = cellFactory;
return this;
}
/**
* Sets property callback for the tree table column. This callback converts object of type {@code } to the value
* of type {@code } that is shown in the tree table cell.
*
* @param propertyCallback property callback
* @return this
*/
public TreeTableColumnBuilder withPropertyCallback(Callback propertyCallback) {
this.propertyCallback = propertyCallback;
return this;
}
/**
*
* Sets binding for {@link TreeTableColumn#widthProperty()}. This can be used to set column width as percentage of
* tree table view width.
*
* Example:
* {@snippet :
* var w = tableView.widthProperty();
* var column = tableColumn("Text", b -> b.withWidthBinding(w.multiply(0.15)));
*}
*
* @param widthBinding binding for widthProperty()
* @return this
*/
public TreeTableColumnBuilder withWidthBinding(ObservableValue widthBinding) {
this.widthBinding = widthBinding;
return this;
}
private TreeTableColumn build() {
var column = new TreeTableColumn(text);
if (comparator != null) {
column.setComparator(comparator);
column.setSortable(true);
} else {
column.setSortable(false);
}
if (cellFactory != null) {
column.setCellFactory(cellFactory);
}
if (propertyCallback != null) {
column.setCellValueFactory(p -> new ReadOnlyObjectWrapper<>(propertyCallback.call(p.getValue().getValue())));
}
if (widthBinding != null) {
column.prefWidthProperty().bind(widthBinding);
}
return column;
}
}