2020-07-13 22:08:08 +02:00
|
|
|
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;
|
|
|
|
|
2020-07-16 19:07:27 +02:00
|
|
|
private GenericListCell(ListView<? extends T> listView) {
|
|
|
|
this.listView = listView;
|
|
|
|
getStyleClass().add("listElement");
|
|
|
|
}
|
2020-07-13 22:08:08 +02:00
|
|
|
|
|
|
|
@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); }
|
|
|
|
}
|