Load vault list cells from FXML

This commit is contained in:
Sebastian Stenzel
2019-07-23 13:13:36 +02:00
parent c2ed9958c7
commit edfdca6e5f
6 changed files with 115 additions and 3 deletions

View File

@@ -30,7 +30,7 @@ import javafx.scene.layout.CornerRadii;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
class DraggableListCell<T> extends ListCell<T> {
public class DraggableListCell<T> extends ListCell<T> {
private static final double DROP_LINE_WIDTH = 4.0;
private static final Paint DROP_LINE_COLOR = Color.gray(0.0, 0.6);

View File

@@ -52,4 +52,9 @@ public abstract class MainWindowModule {
@FxControllerKey(VaultDetailController.class)
abstract FxController bindVaultDetailController(VaultDetailController controller);
@Binds
@IntoMap
@FxControllerKey(VaultListCellController.class)
abstract FxController bindVaultListCellController(VaultListCellController controller);
}

View File

@@ -0,0 +1,31 @@
package org.cryptomator.ui.mainwindow;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import org.cryptomator.ui.common.FxController;
import org.cryptomator.ui.model.Vault;
import javax.inject.Inject;
// unscoped because each cell needs its own controller
public class VaultListCellController implements FxController {
private final ObjectProperty<Vault> vault = new SimpleObjectProperty<>();
@Inject
VaultListCellController() {}
/* Getter/Setter */
public ObjectProperty<Vault> vaultProperty() {
return vault;
}
public Vault getVault() {
return vault.get();
}
public void setVault(Vault value) {
vault.set(value);
}
}

View File

@@ -0,0 +1,61 @@
package org.cryptomator.ui.mainwindow;
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 org.cryptomator.ui.common.FXMLLoaderFactory;
import org.cryptomator.ui.controls.DraggableListCell;
import org.cryptomator.ui.model.Vault;
import javax.inject.Inject;
import java.io.IOException;
import java.io.UncheckedIOException;
@MainWindowScoped
public class VaultListCellFactory implements Callback<ListView<Vault>, ListCell<Vault>> {
private final FXMLLoaderFactory fxmlLoaders;
@Inject
VaultListCellFactory(@MainWindow FXMLLoaderFactory fxmlLoaders) {
this.fxmlLoaders = fxmlLoaders;
}
@Override
public ListCell<Vault> call(ListView<Vault> param) {
try {
FXMLLoader fxmlLoader = fxmlLoaders.load("/fxml/vault_list_cell.fxml");
return new Cell(fxmlLoader.getRoot(), fxmlLoader.getController());
} catch (IOException e) {
throw new UncheckedIOException("Failed to load /fxml/vault_list_cell.fxml.", e);
}
}
private static class Cell extends DraggableListCell<Vault> {
private final Parent node;
private final VaultListCellController controller;
public Cell(Parent node, VaultListCellController controller) {
this.node = node;
this.controller = controller;
}
@Override
protected void updateItem(Vault item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
setGraphic(node);
controller.setVault(item);
}
}
}
}

View File

@@ -16,20 +16,23 @@ public class VaultListController implements FxController {
private final ObservableList<Vault> vaults;
private final ObjectProperty<Vault> selectedVault;
private final VaultListCellFactory cellFactory;
private final AddVaultWizardComponent.Builder addVaultWizard;
public ListView vaultList;
public ListView<Vault> vaultList;
public AnchorPane onboardingOverlay;
@Inject
VaultListController(ObservableList<Vault> vaults, ObjectProperty<Vault> selectedVault, AddVaultWizardComponent.Builder addVaultWizard) {
VaultListController(ObservableList<Vault> vaults, ObjectProperty<Vault> selectedVault, VaultListCellFactory cellFactory, AddVaultWizardComponent.Builder addVaultWizard) {
this.vaults = vaults;
this.selectedVault = selectedVault;
this.cellFactory = cellFactory;
this.addVaultWizard = addVaultWizard;
}
public void initialize() {
onboardingOverlay.visibleProperty().bind(Bindings.isEmpty(vaults));
vaultList.setItems(vaults);
vaultList.setCellFactory(cellFactory);
selectedVault.bind(vaultList.getSelectionModel().selectedItemProperty());
}

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>
<VBox xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="org.cryptomator.ui.mainwindow.VaultListCellController"
prefHeight="50.0" prefWidth="200.0">
<Label text="${controller.vault}"/>
</VBox>