+ * Project: envoy-client
+ * File: AbstractListCell.java
+ * Created: 18.07.2020
+ *
+ * @author Kai S. K. Engelbart
+ * @param the type of element displayed by the list cell
+ * @param the type of node as which the list element will be displayed
+ * @since Envoy Client v0.1-beta
+ */
+public abstract class AbstractListCell extends ListCell {
+
+ protected ListView extends T> listView;
+
+ /**
+ * @param listView the list view inside of which the cell will be displayed
+ * @since Envoy Client v0.1-beta
+ */
+ public AbstractListCell(ListView extends T> listView) {
+ this.listView = listView;
+ setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
+ getStyleClass().add("listElement");
+ }
+
+ @Override
+ protected final void updateItem(T item, boolean empty) {
+ super.updateItem(item, empty);
+ setGraphic(empty || item == null ? null : renderItem(item));
+ }
+
+ /**
+ * Converts a list item to a node. This can have side effects on the list cell.
+ *
+ * @param item the item to render
+ * @return a node representing the item
+ * @since Envoy Client v0.1-beta
+ */
+ protected abstract U renderItem(T item);
+}
diff --git a/client/src/main/java/envoy/client/ui/listcell/GenericListCell.java b/client/src/main/java/envoy/client/ui/listcell/GenericListCell.java
new file mode 100644
index 0000000..ad2b364
--- /dev/null
+++ b/client/src/main/java/envoy/client/ui/listcell/GenericListCell.java
@@ -0,0 +1,36 @@
+package envoy.client.ui.listcell;
+
+import java.util.function.Function;
+
+import javafx.scene.Node;
+import javafx.scene.control.ListView;
+
+/**
+ * A generic list cell rendering an item using a provided render function.
+ *
+ * Project: envoy-client
+ * File: GenericListCell.java
+ * Created: 18.07.2020
+ *
+ * @author Kai S. K. Engelbart
+ * @param the type of element displayed by the list cell
+ * @param the type of node as which the list element will be displayed
+ * @since Envoy Client v0.2-beta
+ */
+public final class GenericListCell extends AbstractListCell {
+
+ private Function super T, U> renderer;
+
+ /**
+ * @param listView the list view inside of which the cell will be displayed
+ * @param renderer a function converting a list item to a node
+ * @since Envoy Client v0.1-beta
+ */
+ public GenericListCell(ListView extends T> listView, Function super T, U> renderer) {
+ super(listView);
+ this.renderer = renderer;
+ }
+
+ @Override
+ protected U renderItem(T item) { return renderer.apply(item); }
+}
diff --git a/client/src/main/java/envoy/client/ui/listcell/ListCellFactory.java b/client/src/main/java/envoy/client/ui/listcell/ListCellFactory.java
index db7483f..018a0ba 100644
--- a/client/src/main/java/envoy/client/ui/listcell/ListCellFactory.java
+++ b/client/src/main/java/envoy/client/ui/listcell/ListCellFactory.java
@@ -17,41 +17,19 @@ import javafx.util.Callback;
*
* @author Kai S. K. Engelbart
* @param the type of object to display
+ * @param the type of node displayed
* @since Envoy Client v0.1-beta
*/
-public final class ListCellFactory implements Callback, ListCell> {
+public final class ListCellFactory implements Callback, ListCell> {
- private final class GenericListCell extends ListCell {
-
- 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;
+ private final Function super T, U> renderer;
/**
- * @param converter a function converting the type to display into a node
+ * @param renderer 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; }
+ public ListCellFactory(Function super T, U> renderer) { this.renderer = renderer; }
@Override
- public ListCell call(ListView listView) { return new GenericListCell(listView); }
+ public ListCell call(ListView listView) { return new GenericListCell<>(listView, renderer); }
}
diff --git a/client/src/main/java/envoy/client/ui/listcell/MessageListCell.java b/client/src/main/java/envoy/client/ui/listcell/MessageListCell.java
new file mode 100644
index 0000000..71c2ae2
--- /dev/null
+++ b/client/src/main/java/envoy/client/ui/listcell/MessageListCell.java
@@ -0,0 +1,31 @@
+package envoy.client.ui.listcell;
+
+import javafx.scene.control.ListView;
+
+import envoy.data.Message;
+
+/**
+ * A list cell containing messages represented as message controls.
+ *