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;
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<ActionEvent> {
private final Cluster cluster;
private final Model model;
@ -27,30 +24,44 @@ public class AddShardHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent event) {
Dialog<String> d = new TextInputDialog();
d.setContentText("Enter the name of the new shard.");
d.showAndWait().ifPresent(s -> {
List<String> choices = Arrays.stream(ShardType.values())
.map(Enum::name)
.collect(Collectors.toList());
Dialog<String> 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<Shard> 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<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) {
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();
});
}
}

View File

@ -7,6 +7,8 @@ public class Cluster implements Comparable<Cluster>, CrystalItem {
private final Set<Shard> shards;
private final Set<Cluster> clusters;
private Cluster parent;
public Cluster(String name, Set<Shard> shards, Set<Cluster> clusters) {
this.name = name;
this.shards = shards;
@ -36,18 +38,22 @@ public class Cluster implements Comparable<Cluster>, 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<Cluster> getClustersOrdered() {
@ -62,6 +68,14 @@ public class Cluster implements Comparable<Cluster>, 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());

View File

@ -17,12 +17,18 @@ public abstract class Shard implements Comparable<Shard>, 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<Shard>, 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());

View File

@ -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;
}
}

View File

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

View File

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