This repository has been archived on 2021-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
envoy/client/src/main/java/envoy/client/ui/listcell/ListCellFactory.java

58 lines
1.6 KiB
Java

package envoy.client.ui.listcell;
import java.util.function.Function;
import javafx.scene.Node;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.util.Callback;
/**
* Provides a creation mechanism for generic list cells given a list view and a
* conversion function.
* <p>
* Project: <strong>envoy-client</strong><br>
* File: <strong>ListCellFactory.java</strong><br>
* Created: <strong>13.07.2020</strong><br>
*
* @author Kai S. K. Engelbart
* @param <T> the type of object to display
* @since Envoy Client v0.1-beta
*/
public final class ListCellFactory<T> implements Callback<ListView<T>, ListCell<T>> {
private final class GenericListCell extends ListCell<T> {
private ListView<? extends T> listView;
private GenericListCell(ListView<? extends T> listView) {
this.listView = listView;
getStyleClass().add("listElement");
}
@Override
protected void updateItem(T item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText(null);
setGraphic(null);
} else {
final var control = converter.apply(item);
prefWidthProperty().bind(listView.widthProperty().subtract(40));
setGraphic(control);
}
}
}
private final Function<? super T, ? extends Node> converter;
/**
* @param converter a function converting the type to display into a node
* @since Envoy Client v0.1-beta
*/
public ListCellFactory(Function<? super T, ? extends Node> converter) { this.converter = converter; }
@Override
public ListCell<T> call(ListView<T> listView) { return new GenericListCell(listView); }
}