From ca6bea60dda8d4ce089c2681d4cb2bd138a7440e Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Sun, 30 May 2021 11:24:37 +0200 Subject: [PATCH] Improved some things. --- .../ClusterTreeViewItemSelectionListener.java | 16 ++++----- .../crystalkeep/model/ShardType.java | 8 ++++- .../model/serialization/ClusterLoader.java | 24 +++++++++---- .../crystalkeep/model/shards/FileShard.java | 35 +++++++++++++++++++ ...ne.java => LoginCredentialsViewModel.java} | 7 ++-- .../{ShardPane.java => ShardViewModel.java} | 19 ++++++---- ...ShardPane.java => TextShardViewModel.java} | 4 +-- 7 files changed, 88 insertions(+), 25 deletions(-) create mode 100644 src/main/java/nl/andrewlalis/crystalkeep/model/shards/FileShard.java rename src/main/java/nl/andrewlalis/crystalkeep/view/shard_details/{LoginCredentialsPane.java => LoginCredentialsViewModel.java} (87%) rename src/main/java/nl/andrewlalis/crystalkeep/view/shard_details/{ShardPane.java => ShardViewModel.java} (74%) rename src/main/java/nl/andrewlalis/crystalkeep/view/shard_details/{TextShardPane.java => TextShardViewModel.java} (80%) diff --git a/src/main/java/nl/andrewlalis/crystalkeep/control/ClusterTreeViewItemSelectionListener.java b/src/main/java/nl/andrewlalis/crystalkeep/control/ClusterTreeViewItemSelectionListener.java index 505c8b3..fdcc1d0 100644 --- a/src/main/java/nl/andrewlalis/crystalkeep/control/ClusterTreeViewItemSelectionListener.java +++ b/src/main/java/nl/andrewlalis/crystalkeep/control/ClusterTreeViewItemSelectionListener.java @@ -9,18 +9,18 @@ import nl.andrewlalis.crystalkeep.model.Shard; import nl.andrewlalis.crystalkeep.model.shards.LoginCredentialsShard; import nl.andrewlalis.crystalkeep.model.shards.TextShard; import nl.andrewlalis.crystalkeep.view.ShardTreeItem; -import nl.andrewlalis.crystalkeep.view.shard_details.LoginCredentialsPane; -import nl.andrewlalis.crystalkeep.view.shard_details.ShardPane; -import nl.andrewlalis.crystalkeep.view.shard_details.TextShardPane; +import nl.andrewlalis.crystalkeep.view.shard_details.LoginCredentialsViewModel; +import nl.andrewlalis.crystalkeep.view.shard_details.ShardViewModel; +import nl.andrewlalis.crystalkeep.view.shard_details.TextShardViewModel; import java.util.HashMap; import java.util.Map; public class ClusterTreeViewItemSelectionListener implements ChangeListener> { - private static final Map, Class>> shardPanesMap = new HashMap<>(); + private static final Map, Class>> shardPanesMap = new HashMap<>(); static { - shardPanesMap.put(TextShard.class, TextShardPane.class); - shardPanesMap.put(LoginCredentialsShard.class, LoginCredentialsPane.class); + shardPanesMap.put(TextShard.class, TextShardViewModel.class); + shardPanesMap.put(LoginCredentialsShard.class, LoginCredentialsViewModel.class); } private final VBox shardDetailContainer; @@ -36,8 +36,8 @@ public class ClusterTreeViewItemSelectionListener implements ChangeListener promptPassword() { - Dialog d = new TextInputDialog(); - d.setContentText("Enter the password"); - d.setGraphic(null); - d.setHeaderText(null); + Dialog d = new Dialog<>(); d.setTitle("Enter Password"); + d.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL); + + PasswordField pwField = new PasswordField(); + VBox content = new VBox(10); + content.setAlignment(Pos.CENTER); + content.getChildren().addAll(new Label("Enter password"), pwField); + d.getDialogPane().setContent(content); + d.setResultConverter(param -> { + if (param == ButtonType.OK) { + return pwField.getText(); + } + return null; + }); return d.showAndWait(); } diff --git a/src/main/java/nl/andrewlalis/crystalkeep/model/shards/FileShard.java b/src/main/java/nl/andrewlalis/crystalkeep/model/shards/FileShard.java new file mode 100644 index 0000000..ebd8d56 --- /dev/null +++ b/src/main/java/nl/andrewlalis/crystalkeep/model/shards/FileShard.java @@ -0,0 +1,35 @@ +package nl.andrewlalis.crystalkeep.model.shards; + +import nl.andrewlalis.crystalkeep.model.Shard; +import nl.andrewlalis.crystalkeep.model.ShardType; + +import java.time.LocalDateTime; + +public class FileShard extends Shard { + private String fileName; + private String mimeType; + private byte[] contents; + + public FileShard(String name, LocalDateTime createdAt, String fileName, String mimeType, byte[] contents) { + super(name, createdAt, ShardType.FILE); + this.fileName = fileName; + this.mimeType = mimeType; + this.contents = contents; + } + + public FileShard(String name, String fileName, String mimeType, byte[] contents) { + this(name, LocalDateTime.now(), fileName, mimeType, contents); + } + + public String getFileName() { + return fileName; + } + + public String getMimeType() { + return mimeType; + } + + public byte[] getContents() { + return contents; + } +} diff --git a/src/main/java/nl/andrewlalis/crystalkeep/view/shard_details/LoginCredentialsPane.java b/src/main/java/nl/andrewlalis/crystalkeep/view/shard_details/LoginCredentialsViewModel.java similarity index 87% rename from src/main/java/nl/andrewlalis/crystalkeep/view/shard_details/LoginCredentialsPane.java rename to src/main/java/nl/andrewlalis/crystalkeep/view/shard_details/LoginCredentialsViewModel.java index e9f4b8a..989f361 100644 --- a/src/main/java/nl/andrewlalis/crystalkeep/view/shard_details/LoginCredentialsPane.java +++ b/src/main/java/nl/andrewlalis/crystalkeep/view/shard_details/LoginCredentialsViewModel.java @@ -10,8 +10,8 @@ import javafx.scene.layout.GridPane; import javafx.scene.layout.Pane; import nl.andrewlalis.crystalkeep.model.shards.LoginCredentialsShard; -public class LoginCredentialsPane extends ShardPane { - public LoginCredentialsPane(LoginCredentialsShard shard) { +public class LoginCredentialsViewModel extends ShardViewModel { + public LoginCredentialsViewModel(LoginCredentialsShard shard) { super(shard); } @@ -23,6 +23,7 @@ public class LoginCredentialsPane extends ShardPane { gp.setVgap(5); gp.add(new Label("Username"), 0, 0); var usernameField = new TextField(shard.getUsername()); + usernameField.setPrefColumnCount(40); usernameField.textProperty().addListener((observable, oldValue, newValue) -> { shard.setUsername(newValue); }); @@ -30,8 +31,10 @@ public class LoginCredentialsPane extends ShardPane { gp.add(new Label("Password"), 0, 1); var passwordField = new PasswordField(); passwordField.setText(shard.getPassword()); + passwordField.setPrefColumnCount(40); var rawPasswordField = new TextField(shard.getPassword()); rawPasswordField.setVisible(false); + rawPasswordField.setPrefColumnCount(40); passwordField.textProperty().addListener((observable, oldValue, newValue) -> { shard.setPassword(newValue); rawPasswordField.setText(newValue); diff --git a/src/main/java/nl/andrewlalis/crystalkeep/view/shard_details/ShardPane.java b/src/main/java/nl/andrewlalis/crystalkeep/view/shard_details/ShardViewModel.java similarity index 74% rename from src/main/java/nl/andrewlalis/crystalkeep/view/shard_details/ShardPane.java rename to src/main/java/nl/andrewlalis/crystalkeep/view/shard_details/ShardViewModel.java index 93e4a8e..99a85e5 100644 --- a/src/main/java/nl/andrewlalis/crystalkeep/view/shard_details/ShardPane.java +++ b/src/main/java/nl/andrewlalis/crystalkeep/view/shard_details/ShardViewModel.java @@ -12,9 +12,15 @@ import nl.andrewlalis.crystalkeep.model.Shard; import java.time.format.DateTimeFormatter; -public abstract class ShardPane extends VBox { - public ShardPane(T shard) { - this.setSpacing(5); +public abstract class ShardViewModel { + protected final T shard; + + public ShardViewModel(T shard) { + this.shard = shard; + } + + public Node getContentPane() { + VBox pane = new VBox(5); GridPane gp = new GridPane(); gp.setPadding(new Insets(5)); gp.setHgap(5); @@ -29,9 +35,10 @@ public abstract class ShardPane extends VBox { var createdAtField = new TextField(shard.getCreatedAt().format(DateTimeFormatter.ofPattern("dd MMMM yyyy HH:mm:ss"))); createdAtField.setEditable(false); gp.add(createdAtField, 1, 1); - this.getChildren().add(gp); - this.getChildren().add(new Separator(Orientation.HORIZONTAL)); - this.getChildren().add(this.getContent(shard)); + pane.getChildren().add(gp); + pane.getChildren().add(new Separator(Orientation.HORIZONTAL)); + pane.getChildren().add(this.getContent(shard)); + return pane; } protected abstract Node getContent(T shard); diff --git a/src/main/java/nl/andrewlalis/crystalkeep/view/shard_details/TextShardPane.java b/src/main/java/nl/andrewlalis/crystalkeep/view/shard_details/TextShardViewModel.java similarity index 80% rename from src/main/java/nl/andrewlalis/crystalkeep/view/shard_details/TextShardPane.java rename to src/main/java/nl/andrewlalis/crystalkeep/view/shard_details/TextShardViewModel.java index a6ebfcc..16ea447 100644 --- a/src/main/java/nl/andrewlalis/crystalkeep/view/shard_details/TextShardPane.java +++ b/src/main/java/nl/andrewlalis/crystalkeep/view/shard_details/TextShardViewModel.java @@ -4,8 +4,8 @@ import javafx.scene.Node; import javafx.scene.control.TextArea; import nl.andrewlalis.crystalkeep.model.shards.TextShard; -public class TextShardPane extends ShardPane { - public TextShardPane(TextShard shard) { +public class TextShardViewModel extends ShardViewModel { + public TextShardViewModel(TextShard shard) { super(shard); }