From d7f08968b3fc6cc72febada215607dd36b4aba29 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Tue, 1 Jun 2021 08:44:05 +0200 Subject: [PATCH] Added better shard creation dialog, added shard-type specific icons back. --- .../crystalkeep/control/AddShardHandler.java | 69 +++++++++++-------- .../crystalkeep/model/Cluster.java | 14 ++++ .../andrewlalis/crystalkeep/model/Shard.java | 14 ++++ .../crystalkeep/model/ShardType.java | 19 +++-- .../crystalkeep/view/CrystalItemTreeCell.java | 1 - .../LoginCredentialsViewModel.java | 5 ++ .../shard_details/TextShardViewModel.java | 5 ++ 7 files changed, 93 insertions(+), 34 deletions(-) diff --git a/src/main/java/nl/andrewlalis/crystalkeep/control/AddShardHandler.java b/src/main/java/nl/andrewlalis/crystalkeep/control/AddShardHandler.java index a1f0164..cbe8bb5 100644 --- a/src/main/java/nl/andrewlalis/crystalkeep/control/AddShardHandler.java +++ b/src/main/java/nl/andrewlalis/crystalkeep/control/AddShardHandler.java @@ -1,21 +1,18 @@ package nl.andrewlalis.crystalkeep.control; +import javafx.collections.FXCollections; import javafx.event.ActionEvent; import javafx.event.EventHandler; -import javafx.scene.control.ChoiceDialog; -import javafx.scene.control.Dialog; -import javafx.scene.control.TextInputDialog; +import javafx.scene.control.*; +import javafx.scene.layout.GridPane; import nl.andrewlalis.crystalkeep.model.Cluster; import nl.andrewlalis.crystalkeep.model.Model; import nl.andrewlalis.crystalkeep.model.Shard; import nl.andrewlalis.crystalkeep.model.ShardType; +import nl.andrewlalis.crystalkeep.model.shards.FileShard; import nl.andrewlalis.crystalkeep.model.shards.LoginCredentialsShard; import nl.andrewlalis.crystalkeep.model.shards.TextShard; -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; - public class AddShardHandler implements EventHandler { private final Cluster cluster; private final Model model; @@ -27,30 +24,44 @@ public class AddShardHandler implements EventHandler { @Override public void handle(ActionEvent event) { - Dialog d = new TextInputDialog(); - d.setContentText("Enter the name of the new shard."); - d.showAndWait().ifPresent(s -> { - List choices = Arrays.stream(ShardType.values()) - .map(Enum::name) - .collect(Collectors.toList()); - Dialog d1 = new ChoiceDialog<>("TEXT", choices); - d1.setContentText("Choose the type of shard to create."); - d1.showAndWait().ifPresent(typeName -> { - ShardType type = ShardType.valueOf(typeName.toUpperCase()); - Shard shard; + Dialog dialog = new Dialog<>(); + dialog.setTitle("Create Shard"); + + GridPane gp = new GridPane(); + gp.setHgap(5); + gp.setVgap(5); + gp.add(new Label("Name"), 0, 0); + TextField nameField = new TextField(); + gp.add(nameField, 1, 0); + gp.add(new Label("Type"), 0, 1); + ComboBox typeBox = new ComboBox<>(FXCollections.observableArrayList(ShardType.values())); + gp.add(typeBox, 1, 1); + dialog.getDialogPane().setContent(gp); + + dialog.getDialogPane().getButtonTypes().add(ButtonType.OK); + dialog.getDialogPane().getButtonTypes().add(ButtonType.CANCEL); + dialog.setResultConverter(result -> { + if (result == ButtonType.OK) { + ShardType type = typeBox.getValue(); + if (type == null) { + return null; + }; + String name = nameField.getText().trim(); + if (name.isBlank()) { + return null; + }; switch (type) { - case TEXT: - shard = new TextShard(s); - break; - case LOGIN_CREDENTIALS: - shard = new LoginCredentialsShard(s); - break; - default: - throw new IllegalStateException("Invalid shard type selected."); + case TEXT: return new TextShard(name); + case LOGIN_CREDENTIALS: return new LoginCredentialsShard(name); + case FILE: return new FileShard(name, "", "", new byte[0]); + default: return null; } - cluster.addShard(shard); - model.notifyListeners(); - }); + } + return null; + }); + dialog.showAndWait().ifPresent(shard -> { + cluster.addShard(shard); + model.notifyListeners(); }); } } diff --git a/src/main/java/nl/andrewlalis/crystalkeep/model/Cluster.java b/src/main/java/nl/andrewlalis/crystalkeep/model/Cluster.java index b994b57..1c7ba3c 100644 --- a/src/main/java/nl/andrewlalis/crystalkeep/model/Cluster.java +++ b/src/main/java/nl/andrewlalis/crystalkeep/model/Cluster.java @@ -7,6 +7,8 @@ public class Cluster implements Comparable, CrystalItem { private final Set shards; private final Set clusters; + private Cluster parent; + public Cluster(String name, Set shards, Set clusters) { this.name = name; this.shards = shards; @@ -36,18 +38,22 @@ public class Cluster implements Comparable, CrystalItem { public void addShard(Shard shard) { this.shards.add(shard); + shard.setParent(this); } public void removeShard(Shard shard) { this.shards.remove(shard); + shard.setParent(null); } public void addCluster(Cluster cluster) { this.clusters.add(cluster); + cluster.setParent(this); } public void removeCluster(Cluster cluster) { this.clusters.remove(cluster); + cluster.setParent(null); } public List getClustersOrdered() { @@ -62,6 +68,14 @@ public class Cluster implements Comparable, CrystalItem { return shards; } + public Cluster getParent() { + return parent; + } + + public void setParent(Cluster parent) { + this.parent = parent; + } + @Override public int compareTo(Cluster o) { return this.getName().compareTo(o.getName()); diff --git a/src/main/java/nl/andrewlalis/crystalkeep/model/Shard.java b/src/main/java/nl/andrewlalis/crystalkeep/model/Shard.java index 3149a22..0309a21 100644 --- a/src/main/java/nl/andrewlalis/crystalkeep/model/Shard.java +++ b/src/main/java/nl/andrewlalis/crystalkeep/model/Shard.java @@ -17,12 +17,18 @@ public abstract class Shard implements Comparable, CrystalItem { private final LocalDateTime createdAt; private final ShardType type; + private Cluster parent; + public Shard(String name, LocalDateTime createdAt, ShardType type) { this.name = name; this.createdAt = createdAt; this.type = type; } + public Shard(String name, ShardType type) { + this(name, LocalDateTime.now(), type); + } + @Override public String getName() { return name; @@ -40,6 +46,14 @@ public abstract class Shard implements Comparable, CrystalItem { return type; } + public Cluster getParent() { + return parent; + } + + public void setParent(Cluster parent) { + this.parent = parent; + } + @Override public int compareTo(Shard o) { int r = this.getName().compareTo(o.getName()); diff --git a/src/main/java/nl/andrewlalis/crystalkeep/model/ShardType.java b/src/main/java/nl/andrewlalis/crystalkeep/model/ShardType.java index e99b6bb..98072a6 100644 --- a/src/main/java/nl/andrewlalis/crystalkeep/model/ShardType.java +++ b/src/main/java/nl/andrewlalis/crystalkeep/model/ShardType.java @@ -16,32 +16,43 @@ public enum ShardType { /** * Represents a {@link TextShard} */ - TEXT(1), + TEXT(1, "Text"), /** * Represents a {@link LoginCredentialsShard} */ - LOGIN_CREDENTIALS(2), + LOGIN_CREDENTIALS(2, "Login Credentials"), /** * Represents a {@link FileShard} */ - FILE(3); + FILE(3, "File"); private final int value; + private final String name; - ShardType(int value) { + ShardType(int value, String name) { this.value = value; + this.name = name; } public int getValue() { return value; } + public String getName() { + return name; + } + public static ShardType valueOf(int value) { for (var type : values()) { if (type.getValue() == value) return type; } throw new IllegalArgumentException("Invalid value."); } + + @Override + public String toString() { + return this.name; + } } diff --git a/src/main/java/nl/andrewlalis/crystalkeep/view/CrystalItemTreeCell.java b/src/main/java/nl/andrewlalis/crystalkeep/view/CrystalItemTreeCell.java index 32bad78..c9f3a97 100644 --- a/src/main/java/nl/andrewlalis/crystalkeep/view/CrystalItemTreeCell.java +++ b/src/main/java/nl/andrewlalis/crystalkeep/view/CrystalItemTreeCell.java @@ -23,7 +23,6 @@ public class CrystalItemTreeCell extends TreeCell { this.model = model; } - @Override protected void updateItem(CrystalItem item, boolean empty) { super.updateItem(item, empty); diff --git a/src/main/java/nl/andrewlalis/crystalkeep/view/shard_details/LoginCredentialsViewModel.java b/src/main/java/nl/andrewlalis/crystalkeep/view/shard_details/LoginCredentialsViewModel.java index c943b8a..b23cfb1 100644 --- a/src/main/java/nl/andrewlalis/crystalkeep/view/shard_details/LoginCredentialsViewModel.java +++ b/src/main/java/nl/andrewlalis/crystalkeep/view/shard_details/LoginCredentialsViewModel.java @@ -74,4 +74,9 @@ public class LoginCredentialsViewModel extends ShardViewModel { }); return textArea; } + + @Override + public String getIconPath() { + return "/nl/andrewlalis/crystalkeep/ui/images/text_shard_node_icon.png"; + } }