Added better shard creation dialog, added shard-type specific icons back.

This commit is contained in:
Andrew Lalis 2021-06-01 08:44:05 +02:00
parent 33e16e40aa
commit d7f08968b3
7 changed files with 93 additions and 34 deletions

View File

@ -1,21 +1,18 @@
package nl.andrewlalis.crystalkeep.control; package nl.andrewlalis.crystalkeep.control;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.event.EventHandler; import javafx.event.EventHandler;
import javafx.scene.control.ChoiceDialog; import javafx.scene.control.*;
import javafx.scene.control.Dialog; import javafx.scene.layout.GridPane;
import javafx.scene.control.TextInputDialog;
import nl.andrewlalis.crystalkeep.model.Cluster; import nl.andrewlalis.crystalkeep.model.Cluster;
import nl.andrewlalis.crystalkeep.model.Model; import nl.andrewlalis.crystalkeep.model.Model;
import nl.andrewlalis.crystalkeep.model.Shard; import nl.andrewlalis.crystalkeep.model.Shard;
import nl.andrewlalis.crystalkeep.model.ShardType; 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.LoginCredentialsShard;
import nl.andrewlalis.crystalkeep.model.shards.TextShard; 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<ActionEvent> { public class AddShardHandler implements EventHandler<ActionEvent> {
private final Cluster cluster; private final Cluster cluster;
private final Model model; private final Model model;
@ -27,30 +24,44 @@ public class AddShardHandler implements EventHandler<ActionEvent> {
@Override @Override
public void handle(ActionEvent event) { public void handle(ActionEvent event) {
Dialog<String> d = new TextInputDialog(); Dialog<Shard> dialog = new Dialog<>();
d.setContentText("Enter the name of the new shard."); dialog.setTitle("Create Shard");
d.showAndWait().ifPresent(s -> {
List<String> choices = Arrays.stream(ShardType.values()) GridPane gp = new GridPane();
.map(Enum::name) gp.setHgap(5);
.collect(Collectors.toList()); gp.setVgap(5);
Dialog<String> d1 = new ChoiceDialog<>("TEXT", choices); gp.add(new Label("Name"), 0, 0);
d1.setContentText("Choose the type of shard to create."); TextField nameField = new TextField();
d1.showAndWait().ifPresent(typeName -> { gp.add(nameField, 1, 0);
ShardType type = ShardType.valueOf(typeName.toUpperCase()); gp.add(new Label("Type"), 0, 1);
Shard shard; ComboBox<ShardType> 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) { switch (type) {
case TEXT: case TEXT: return new TextShard(name);
shard = new TextShard(s); case LOGIN_CREDENTIALS: return new LoginCredentialsShard(name);
break; case FILE: return new FileShard(name, "", "", new byte[0]);
case LOGIN_CREDENTIALS: default: return null;
shard = new LoginCredentialsShard(s);
break;
default:
throw new IllegalStateException("Invalid shard type selected.");
} }
cluster.addShard(shard); }
model.notifyListeners(); return null;
}); });
dialog.showAndWait().ifPresent(shard -> {
cluster.addShard(shard);
model.notifyListeners();
}); });
} }
} }

View File

@ -7,6 +7,8 @@ public class Cluster implements Comparable<Cluster>, CrystalItem {
private final Set<Shard> shards; private final Set<Shard> shards;
private final Set<Cluster> clusters; private final Set<Cluster> clusters;
private Cluster parent;
public Cluster(String name, Set<Shard> shards, Set<Cluster> clusters) { public Cluster(String name, Set<Shard> shards, Set<Cluster> clusters) {
this.name = name; this.name = name;
this.shards = shards; this.shards = shards;
@ -36,18 +38,22 @@ public class Cluster implements Comparable<Cluster>, CrystalItem {
public void addShard(Shard shard) { public void addShard(Shard shard) {
this.shards.add(shard); this.shards.add(shard);
shard.setParent(this);
} }
public void removeShard(Shard shard) { public void removeShard(Shard shard) {
this.shards.remove(shard); this.shards.remove(shard);
shard.setParent(null);
} }
public void addCluster(Cluster cluster) { public void addCluster(Cluster cluster) {
this.clusters.add(cluster); this.clusters.add(cluster);
cluster.setParent(this);
} }
public void removeCluster(Cluster cluster) { public void removeCluster(Cluster cluster) {
this.clusters.remove(cluster); this.clusters.remove(cluster);
cluster.setParent(null);
} }
public List<Cluster> getClustersOrdered() { public List<Cluster> getClustersOrdered() {
@ -62,6 +68,14 @@ public class Cluster implements Comparable<Cluster>, CrystalItem {
return shards; return shards;
} }
public Cluster getParent() {
return parent;
}
public void setParent(Cluster parent) {
this.parent = parent;
}
@Override @Override
public int compareTo(Cluster o) { public int compareTo(Cluster o) {
return this.getName().compareTo(o.getName()); return this.getName().compareTo(o.getName());

View File

@ -17,12 +17,18 @@ public abstract class Shard implements Comparable<Shard>, CrystalItem {
private final LocalDateTime createdAt; private final LocalDateTime createdAt;
private final ShardType type; private final ShardType type;
private Cluster parent;
public Shard(String name, LocalDateTime createdAt, ShardType type) { public Shard(String name, LocalDateTime createdAt, ShardType type) {
this.name = name; this.name = name;
this.createdAt = createdAt; this.createdAt = createdAt;
this.type = type; this.type = type;
} }
public Shard(String name, ShardType type) {
this(name, LocalDateTime.now(), type);
}
@Override @Override
public String getName() { public String getName() {
return name; return name;
@ -40,6 +46,14 @@ public abstract class Shard implements Comparable<Shard>, CrystalItem {
return type; return type;
} }
public Cluster getParent() {
return parent;
}
public void setParent(Cluster parent) {
this.parent = parent;
}
@Override @Override
public int compareTo(Shard o) { public int compareTo(Shard o) {
int r = this.getName().compareTo(o.getName()); int r = this.getName().compareTo(o.getName());

View File

@ -16,32 +16,43 @@ public enum ShardType {
/** /**
* Represents a {@link TextShard} * Represents a {@link TextShard}
*/ */
TEXT(1), TEXT(1, "Text"),
/** /**
* Represents a {@link LoginCredentialsShard} * Represents a {@link LoginCredentialsShard}
*/ */
LOGIN_CREDENTIALS(2), LOGIN_CREDENTIALS(2, "Login Credentials"),
/** /**
* Represents a {@link FileShard} * Represents a {@link FileShard}
*/ */
FILE(3); FILE(3, "File");
private final int value; private final int value;
private final String name;
ShardType(int value) { ShardType(int value, String name) {
this.value = value; this.value = value;
this.name = name;
} }
public int getValue() { public int getValue() {
return value; return value;
} }
public String getName() {
return name;
}
public static ShardType valueOf(int value) { public static ShardType valueOf(int value) {
for (var type : values()) { for (var type : values()) {
if (type.getValue() == value) return type; if (type.getValue() == value) return type;
} }
throw new IllegalArgumentException("Invalid value."); throw new IllegalArgumentException("Invalid value.");
} }
@Override
public String toString() {
return this.name;
}
} }

View File

@ -23,7 +23,6 @@ public class CrystalItemTreeCell extends TreeCell<CrystalItem> {
this.model = model; this.model = model;
} }
@Override @Override
protected void updateItem(CrystalItem item, boolean empty) { protected void updateItem(CrystalItem item, boolean empty) {
super.updateItem(item, empty); super.updateItem(item, empty);

View File

@ -74,4 +74,9 @@ public class LoginCredentialsViewModel extends ShardViewModel<LoginCredentialsSh
gp.add(passwordActionsPane, 1, 2); gp.add(passwordActionsPane, 1, 2);
return gp; return gp;
} }
@Override
public String getIconPath() {
return "/nl/andrewlalis/crystalkeep/ui/images/login_credentials_shard_node_icon.png";
}
} }

View File

@ -18,4 +18,9 @@ public class TextShardViewModel extends ShardViewModel<TextShard> {
}); });
return textArea; return textArea;
} }
@Override
public String getIconPath() {
return "/nl/andrewlalis/crystalkeep/ui/images/text_shard_node_icon.png";
}
} }