used builder pattern

This commit is contained in:
Jan-Peter Klein 2024-10-22 17:07:42 +02:00
parent 8e25dcd396
commit 7fcbb57ab1
No known key found for this signature in database
GPG Key ID: 90EDA3A7C822FD0E
5 changed files with 119 additions and 66 deletions

View File

@ -1,49 +1,93 @@
package org.cryptomator.ui.controls;
import org.cryptomator.ui.common.FxmlFile;
import org.cryptomator.ui.quit.QuitComponent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.function.Consumer;
public class CustomDialogBuilder {
public CustomDialogBuilder() {
private static final Logger LOG = LoggerFactory.getLogger(CustomDialogBuilder.class);
private String title;
private String message;
private String description;
private FontAwesome5Icon icon;
private String okButtonText = "OK";
private String cancelButtonText = "Cancel";
private Consumer<Stage> okAction;
private Consumer<Stage> cancelAction;
public CustomDialogBuilder setTitle(String title) {
this.title = title;
return this;
}
public void showDialog(ResourceBundle resourceBundle, Stage owner, FontAwesome5Icon icon, String title, String message, String description, Runnable okAction, String okText) {
public CustomDialogBuilder setMessage(String message) {
this.message = message;
return this;
}
public CustomDialogBuilder setDescription(String description) {
this.description = description;
return this;
}
public CustomDialogBuilder setIcon(FontAwesome5Icon icon) {
this.icon = icon;
return this;
}
public CustomDialogBuilder setOkButtonText(String okButtonText) {
this.okButtonText = okButtonText;
return this;
}
public CustomDialogBuilder setCancelButtonText(String cancelButtonText) {
this.cancelButtonText = cancelButtonText;
return this;
}
public CustomDialogBuilder setOkAction(Consumer<Stage> okAction) {
this.okAction = okAction;
return this;
}
public CustomDialogBuilder setCancelAction(Consumer<Stage> cancelAction) {
this.cancelAction = cancelAction;
return this;
}
public void buildAndShow(Stage owner) {
try {
FXMLLoader loader = new FXMLLoader(getResource(FxmlFile.CUSTOM_DIALOG), resourceBundle);
FXMLLoader loader = new FXMLLoader(getClass().getResource(FxmlFile.CUSTOM_DIALOG.getRessourcePathString()));
Pane pane = loader.load();
CustomDialogController controller = loader.getController();
controller.setIcon(icon);
controller.setMessage(message);
controller.setDescription(description);
controller.setOkButtonText(okText);
controller.setOkAction(okAction);
controller.setIcon(icon);
controller.setOkButtonText(okButtonText);
controller.setCancelButtonText(cancelButtonText);
Stage dialogStage = new Stage();
dialogStage.setTitle(title);
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.initOwner(owner);
dialogStage.setScene(new Scene(pane));
dialogStage.setResizable(false);
controller.setDialogStage(dialogStage);
controller.setOkAction(() -> okAction.accept(dialogStage));
controller.setCancelAction(() -> cancelAction.accept(dialogStage));
dialogStage.showAndWait();
} catch (Exception e) {
e.printStackTrace(); // Handle loading errors
LOG.error("Failed to build and show dialog stage.", e);
}
}
private URL getResource(FxmlFile fxmlFile) {
return getClass().getResource(fxmlFile.getRessourcePathString());
}
}
}

View File

@ -1,48 +1,51 @@
package org.cryptomator.ui.controls;
import org.cryptomator.ui.common.FxController;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class CustomDialogController implements FxController {
public class CustomDialogController {
@FXML
private Label message;
private Label messageLabel;
@FXML
private Label description;
private Label descriptionLabel;
@FXML
private FontAwesome5IconView icon;
private FontAwesome5IconView iconView;
@FXML
private Button okButton;
@FXML
private Button cancelButton;
private Stage dialogStage;
private Runnable okAction;
public void setDialogStage(Stage stage) {
this.dialogStage = stage;
}
public void setIcon(FontAwesome5Icon glyph) {
icon.setGlyph(glyph);
}
private Runnable cancelAction;
public void setMessage(String message) {
this.message.setText(message);
messageLabel.setText(message);
}
public void setDescription(String desc) {
this.description.setText(desc);
public void setDescription(String description) {
descriptionLabel.setText(description);
}
public void setIcon(FontAwesome5Icon icon) {
iconView.setGlyph(icon);
}
public void setOkButtonText(String text) {
okButton.setText(text);
}
public void setCancelButtonText(String text) {
cancelButton.setText(text);
}
public void setOkAction(Runnable action) {
this.okAction = action;
}
public void setOkButtonText(String text) {
okButton.setText(text);
public void setCancelAction(Runnable action) {
this.cancelAction = action;
}
@FXML
@ -50,12 +53,12 @@ public class CustomDialogController implements FxController {
if (okAction != null) {
okAction.run();
}
dialogStage.close();
}
@FXML
private void handleCancel() {
dialogStage.close();
if (cancelAction != null) {
cancelAction.run();
}
}
}
}

View File

@ -13,7 +13,6 @@ import org.cryptomator.ui.controls.CustomDialogBuilder;
import org.cryptomator.ui.controls.FontAwesome5Icon;
import org.cryptomator.ui.fxapp.FxApplicationWindows;
import org.cryptomator.ui.preferences.SelectedPreferencesTab;
import org.cryptomator.ui.removevault.RemoveVaultComponent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -68,7 +67,6 @@ public class VaultListController implements FxController {
private final VaultListCellFactory cellFactory;
private final AddVaultWizardComponent.Builder addVaultWizard;
private final BooleanBinding emptyVaultList;
private final RemoveVaultComponent.Builder removeVaultDialogue;
private final VaultListManager vaultListManager;
private final BooleanProperty draggingVaultOver = new SimpleBooleanProperty();
private final ResourceBundle resourceBundle;
@ -88,7 +86,6 @@ public class VaultListController implements FxController {
VaultListCellFactory cellFactory, //
VaultService vaultService, //
AddVaultWizardComponent.Builder addVaultWizard, //
RemoveVaultComponent.Builder removeVaultDialogue, //
VaultListManager vaultListManager, //
ResourceBundle resourceBundle, //
FxApplicationWindows appWindows, //
@ -99,7 +96,6 @@ public class VaultListController implements FxController {
this.cellFactory = cellFactory;
this.vaultService = vaultService;
this.addVaultWizard = addVaultWizard;
this.removeVaultDialogue = removeVaultDialogue;
this.vaultListManager = vaultListManager;
this.resourceBundle = resourceBundle;
this.appWindows = appWindows;
@ -210,16 +206,20 @@ public class VaultListController implements FxController {
private void pressedShortcutToRemoveVault() {
final var vault = selectedVault.get();
if (vault != null && EnumSet.of(LOCKED, MISSING, ERROR, NEEDS_MIGRATION).contains(vault.getState())) {
new CustomDialogBuilder().showDialog(resourceBundle, mainWindow, //
FontAwesome5Icon.CROWN, //
String.format(resourceBundle.getString("removeVault.title"), vault.getDisplayName()), //
resourceBundle.getString("removeVault.message"), //
resourceBundle.getString("removeVault.description"), //
() -> {
new CustomDialogBuilder() //
.setTitle(String.format(resourceBundle.getString("removeVault.title"), vault.getDisplayName())) //
.setMessage(resourceBundle.getString("removeVault.message")) //
.setDescription(resourceBundle.getString("removeVault.description")) //
.setIcon(FontAwesome5Icon.QUESTION) //
.setOkButtonText(resourceBundle.getString("removeVault.confirmBtn")) //
.setCancelButtonText(resourceBundle.getString("generic.button.cancel")) //
.setOkAction(v -> {
vaults.remove(vault);
LOG.debug("Removing vault {}.", vault.getDisplayName());
}, //
resourceBundle.getString("removeVault.confirmBtn"));
v.close();
}) //
.setCancelAction(Stage::close) //
.buildAndShow(mainWindow);
}
}

View File

@ -86,13 +86,19 @@ public class SupporterCertificateController implements FxController {
@FXML
void didClickRemoveCert() {
new CustomDialogBuilder().showDialog(resourceBundle, window, //
FontAwesome5Icon.QUESTION, //
resourceBundle.getString("removeCert.title"), //
resourceBundle.getString("removeCert.message"), //
resourceBundle.getString("removeCert.description"), //
() -> settings.licenseKey.set(null), //
resourceBundle.getString("removeCert.confirmBtn"));
new CustomDialogBuilder()
.setTitle(resourceBundle.getString("removeCert.title"))
.setMessage(resourceBundle.getString("removeCert.message"))
.setDescription(resourceBundle.getString("removeCert.description"))
.setIcon(FontAwesome5Icon.BUG)
.setOkButtonText(resourceBundle.getString("removeCert.confirmBtn"))
.setCancelButtonText(resourceBundle.getString("generic.button.cancel"))
.setOkAction(v -> {
settings.licenseKey.set(null);
v.close();
})
.setCancelAction(Stage::close)
.buildAndShow(window);
}
public LicenseHolder getLicenseHolder() {

View File

@ -28,21 +28,21 @@
<Insets topRightBottomLeft="6"/>
</padding>
<Circle styleClass="glyph-icon-primary" radius="24"/>
<FontAwesome5IconView fx:id="icon" styleClass="glyph-icon-white" glyph="QUESTION" glyphSize="24"/>
<FontAwesome5IconView fx:id="iconView" styleClass="glyph-icon-white" glyph="QUESTION" glyphSize="24"/>
</StackPane>
</Group>
<VBox HBox.hgrow="ALWAYS">
<Label fx:id="message" styleClass="label-large" wrapText="true">
<Label fx:id="messageLabel" styleClass="label-large" wrapText="true">
<padding>
<Insets bottom="6" top="6"/>
</padding>
</Label>
<Label fx:id="description" wrapText="true"/>
<Label fx:id="descriptionLabel" wrapText="true"/>
<Region VBox.vgrow="ALWAYS" minHeight="18"/>
<ButtonBar buttonMinWidth="120" buttonOrder="+CI">
<buttons>
<Button text="%generic.button.cancel" ButtonBar.buttonData="CANCEL_CLOSE" cancelButton="true" onAction="#handleCancel"/>
<Button fx:id="cancelButton" text="CANCEL" ButtonBar.buttonData="CANCEL_CLOSE" cancelButton="true" onAction="#handleCancel"/>
<Button fx:id="okButton" text="OK" ButtonBar.buttonData="FINISH" defaultButton="true" onAction="#handleOk"/>
</buttons>
</ButtonBar>