Added "save as" option.

This commit is contained in:
Andrew Lalis 2021-06-02 22:16:10 +02:00
parent e88c51c152
commit a4bc985472
2 changed files with 40 additions and 16 deletions

View File

@ -71,11 +71,11 @@ public class MainViewController implements ModelListener {
var password = this.promptPassword(); var password = this.promptPassword();
Cluster cluster; Cluster cluster;
try { try {
if (password.isEmpty() || password.get().isEmpty()) { if (password.isEmpty() || password.get().length == 0) {
cluster = loader.loadUnencrypted(file.toPath()); cluster = loader.loadUnencrypted(file.toPath());
this.model.setActiveClusterPassword(null); this.model.setActiveClusterPassword(null);
} else { } else {
char[] pw = password.get().toCharArray(); char[] pw = password.get();
cluster = loader.load(file.toPath(), pw); cluster = loader.load(file.toPath(), pw);
this.model.setActiveClusterPassword(pw); this.model.setActiveClusterPassword(pw);
} }
@ -104,7 +104,7 @@ public class MainViewController implements ModelListener {
} }
char[] pw = this.model.getActiveClusterPassword(); char[] pw = this.model.getActiveClusterPassword();
if (pw == null) { if (pw == null) {
pw = this.promptPassword().orElse("").toCharArray(); pw = this.promptPassword().orElse(new char[0]);
} }
try { try {
if (pw.length == 0) { if (pw.length == 0) {
@ -119,6 +119,37 @@ public class MainViewController implements ModelListener {
} }
} }
@FXML
public void saveAs() {
if (model.getActiveCluster() == null) return;
ClusterIO clusterIO = new ClusterIO();
Path path = model.getActiveClusterPath();
FileChooser chooser = new FileChooser();
chooser.setTitle("Save Cluster");
chooser.setSelectedExtensionFilter(new FileChooser.ExtensionFilter("Cluster Files", "cts"));
if (path != null) {
chooser.setInitialDirectory(path.getParent().toFile());
}
File file = chooser.showSaveDialog(this.clusterTreeView.getScene().getWindow());
if (file == null) return;
path = file.toPath();
char[] pw = this.model.getActiveClusterPassword();
if (pw == null) {
pw = this.promptPassword().orElse(new char[0]);
}
try {
if (pw.length == 0) {
clusterIO.saveUnencrypted(model.getActiveCluster(), path);
} else {
clusterIO.save(model.getActiveCluster(), path, pw);
}
} catch (Exception e) {
e.printStackTrace();
var alert = new Alert(Alert.AlertType.ERROR, "Could not save cluster.");
alert.showAndWait();
}
}
@FXML @FXML
public void newCluster() { public void newCluster() {
Cluster c = new Cluster("Root"); Cluster c = new Cluster("Root");
@ -126,8 +157,8 @@ public class MainViewController implements ModelListener {
model.setActiveClusterPath(null); model.setActiveClusterPath(null);
} }
private Optional<String> promptPassword() { private Optional<char[]> promptPassword() {
Dialog<String> d = new Dialog<>(); Dialog<char[]> d = new Dialog<>();
d.setTitle("Enter Password"); d.setTitle("Enter Password");
d.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL); d.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
@ -138,7 +169,7 @@ public class MainViewController implements ModelListener {
d.getDialogPane().setContent(content); d.getDialogPane().setContent(content);
d.setResultConverter(param -> { d.setResultConverter(param -> {
if (param == ButtonType.OK) { if (param == ButtonType.OK) {
return pwField.getText(); return pwField.getText().toCharArray();
} }
return null; return null;
}); });

View File

@ -1,21 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.BorderPane?> <?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TreeView?>
<?import javafx.scene.layout.Pane?>
<VBox xmlns:fx="http://javafx.com/fxml/1" fx:controller="nl.andrewlalis.crystalkeep.control.MainViewController"> <VBox xmlns:fx="http://javafx.com/fxml/1" fx:controller="nl.andrewlalis.crystalkeep.control.MainViewController">
<MenuBar> <MenuBar>
<Menu text="File"> <Menu text="File">
<MenuItem text="New" onAction="#newCluster" /> <MenuItem text="New" onAction="#newCluster" />
<MenuItem text="Load" onAction="#load" /> <MenuItem text="Load" onAction="#load" />
<MenuItem text="Save" onAction="#save" /> <MenuItem text="Save" onAction="#save" />
<MenuItem text="Save As" onAction="#saveAs" />
<MenuItem text="Exit" onAction="#exit" /> <MenuItem text="Exit" onAction="#exit" />
</Menu> </Menu>
</MenuBar> </MenuBar>