use same listCell factory pattern as in vault list

This commit is contained in:
Armin Schrenk
2025-02-14 11:54:25 +01:00
parent a4f79fb98a
commit 7af7c920e3
6 changed files with 130 additions and 15 deletions

View File

@@ -0,0 +1,39 @@
package org.cryptomator.ui.eventview;
import org.cryptomator.common.ObservableUtil;
import org.cryptomator.event.Event;
import org.cryptomator.ui.common.FxController;
import org.cryptomator.ui.controls.FontAwesome5IconView;
import javax.inject.Inject;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
import javafx.scene.layout.HBox;
public class EventListCellController implements FxController {
private final ObjectProperty<Event> event;
private final ObservableValue<String> description;
public FontAwesome5IconView eventIcon;
public HBox eventListCell;
@Inject
public EventListCellController() {
this.event = new SimpleObjectProperty<>(null);
this.description = ObservableUtil.mapWithDefault(event, e -> e.getClass().getName(),"");
}
public void setEvent(Event item) {
event.set(item);
}
public ObservableValue<String> descriptionProperty() {
return description;
}
public String getDescription() {
return description.getValue();
}
}

View File

@@ -0,0 +1,63 @@
package org.cryptomator.ui.eventview;
import org.cryptomator.event.Event;
import org.cryptomator.ui.common.FxmlLoaderFactory;
import org.cryptomator.ui.mainwindow.VaultListCellController;
import javax.inject.Inject;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.util.Callback;
import java.io.IOException;
import java.io.UncheckedIOException;
public class EventListCellFactory implements Callback<ListView<Event>, ListCell<Event>> {
private static final String FXML_PATH = "/fxml/eventview_cell.fxml";
private final FxmlLoaderFactory fxmlLoaders;
@Inject
EventListCellFactory(@EventViewWindow FxmlLoaderFactory fxmlLoaders) {
this.fxmlLoaders = fxmlLoaders;
}
@Override
public ListCell<Event> call(ListView<Event> eventListView) {
try {
FXMLLoader fxmlLoader = fxmlLoaders.load(FXML_PATH);
return new Cell(fxmlLoader.getRoot(), fxmlLoader.getController());
} catch (IOException e) {
throw new UncheckedIOException("Failed to load %s.".formatted(FXML_PATH), e);
}
}
private static class Cell extends ListCell<Event> {
private final Parent root;
private final EventListCellController controller;
public Cell(Parent root, EventListCellController controller) {
this.root = root;
this.controller = controller;
}
@Override
protected void updateItem(Event item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
setGraphic(root);
controller.setEvent(item);
}
}
}
}

View File

@@ -15,18 +15,21 @@ public class EventViewController implements FxController {
private final SortedList<Event> reversedEventList;
private final ObservableList<Event> eventList;
private final EventListCellFactory cellFactory;
@FXML
ListView<Event> eventListView;
@Inject
public EventViewController(ObservableList<Event> eventList) {
public EventViewController(ObservableList<Event> eventList, EventListCellFactory cellFactory) {
reversedEventList = new SortedList<>(eventList, Comparator.reverseOrder());
this.eventList = eventList;
this.cellFactory = cellFactory;
}
@FXML
public void initialize() {
eventListView.setCellFactory(cellFactory);
eventListView.setItems(reversedEventList);
}

View File

@@ -57,6 +57,6 @@ abstract class EventViewModule {
@Binds
@IntoMap
@FxControllerKey(UpdateEventViewController.class)
abstract FxController bindUpdateEventViewController(UpdateEventViewController controller);
@FxControllerKey(EventListCellController.class)
abstract FxController bindEventListCellController(EventListCellController controller);
}