Cleaned up the structure of adding/removing crystal items from the tree.

This commit is contained in:
Andrew Lalis 2021-06-01 09:11:24 +02:00
parent e65f5d828c
commit 3c6c165bc6
8 changed files with 74 additions and 20 deletions

View File

@ -23,9 +23,6 @@ public class AddClusterHandler implements EventHandler<ActionEvent> {
d.setHeaderText(null); d.setHeaderText(null);
d.setTitle("Add Cluster"); d.setTitle("Add Cluster");
d.setGraphic(null); d.setGraphic(null);
d.showAndWait().ifPresent(s -> { d.showAndWait().ifPresent(s -> cluster.addCluster(new Cluster(s.trim())));
cluster.addCluster(new Cluster(s.trim()));
model.notifyListeners();
});
} }
} }

View File

@ -59,9 +59,6 @@ public class AddShardHandler implements EventHandler<ActionEvent> {
} }
return null; return null;
}); });
dialog.showAndWait().ifPresent(shard -> { dialog.showAndWait().ifPresent(cluster::addShard);
cluster.addShard(shard);
model.notifyListeners();
});
} }
} }

View File

@ -7,7 +7,6 @@ import javafx.scene.control.ButtonType;
import javafx.scene.control.TreeItem; import javafx.scene.control.TreeItem;
import nl.andrewlalis.crystalkeep.model.Cluster; import nl.andrewlalis.crystalkeep.model.Cluster;
import nl.andrewlalis.crystalkeep.model.CrystalItem; import nl.andrewlalis.crystalkeep.model.CrystalItem;
import nl.andrewlalis.crystalkeep.model.Model;
import nl.andrewlalis.crystalkeep.model.Shard; import nl.andrewlalis.crystalkeep.model.Shard;
import nl.andrewlalis.crystalkeep.view.ClusterTreeItem; import nl.andrewlalis.crystalkeep.view.ClusterTreeItem;
@ -15,11 +14,9 @@ import java.util.Optional;
public class DeleteItemHandler implements EventHandler<ActionEvent> { public class DeleteItemHandler implements EventHandler<ActionEvent> {
private final TreeItem<CrystalItem> treeItem; private final TreeItem<CrystalItem> treeItem;
private final Model model;
public DeleteItemHandler(TreeItem<CrystalItem> treeItem, Model model) { public DeleteItemHandler(TreeItem<CrystalItem> treeItem) {
this.treeItem = treeItem; this.treeItem = treeItem;
this.model = model;
} }
@Override @Override
@ -27,14 +24,15 @@ public class DeleteItemHandler implements EventHandler<ActionEvent> {
if (this.treeItem.getParent() != null && this.treeItem.getParent() instanceof ClusterTreeItem) { if (this.treeItem.getParent() != null && this.treeItem.getParent() instanceof ClusterTreeItem) {
Optional<ButtonType> result = new Alert(Alert.AlertType.CONFIRMATION, "Are you sure you want to delete this item?").showAndWait(); Optional<ButtonType> result = new Alert(Alert.AlertType.CONFIRMATION, "Are you sure you want to delete this item?").showAndWait();
if (result.isPresent() && result.get() == ButtonType.OK) { if (result.isPresent() && result.get() == ButtonType.OK) {
var cluster = ((ClusterTreeItem) this.treeItem.getParent()).getCluster(); ClusterTreeItem parentTreeItem = (ClusterTreeItem) this.treeItem.getParent();
var parentCluster = parentTreeItem.getCluster();
var item = this.treeItem.getValue(); var item = this.treeItem.getValue();
if (item instanceof Shard) { if (item instanceof Shard) {
cluster.removeShard((Shard) item); parentCluster.removeShard((Shard) item);
} else if (item instanceof Cluster) { } else if (item instanceof Cluster) {
cluster.removeCluster((Cluster) item); var cluster = (Cluster) item;
parentCluster.removeCluster(cluster);
} }
this.model.notifyListeners();
} }
} }
} }

View File

@ -43,7 +43,7 @@ public class MainViewController implements ModelListener {
} }
private TreeItem<CrystalItem> createNode(Cluster cluster) { private TreeItem<CrystalItem> createNode(Cluster cluster) {
ClusterTreeItem node = new ClusterTreeItem(cluster); ClusterTreeItem node = new ClusterTreeItem(cluster, this.clusterTreeView);
node.setExpanded(true); node.setExpanded(true);
for (Cluster child : cluster.getClustersOrdered()) { for (Cluster child : cluster.getClustersOrdered()) {
node.getChildren().add(this.createNode(child)); node.getChildren().add(this.createNode(child));

View File

@ -8,6 +8,7 @@ public class Cluster implements Comparable<Cluster>, CrystalItem {
private final Set<Cluster> clusters; private final Set<Cluster> clusters;
private Cluster parent; private Cluster parent;
private final Set<ClusterListener> listeners = new HashSet<>();
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;
@ -39,21 +40,25 @@ 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); shard.setParent(this);
this.listeners.forEach(l -> l.shardAdded(shard));
} }
public void removeShard(Shard shard) { public void removeShard(Shard shard) {
this.shards.remove(shard); this.shards.remove(shard);
shard.setParent(null); shard.setParent(null);
this.listeners.forEach(l -> l.shardRemoved(shard));
} }
public void addCluster(Cluster cluster) { public void addCluster(Cluster cluster) {
this.clusters.add(cluster); this.clusters.add(cluster);
cluster.setParent(this); cluster.setParent(this);
this.listeners.forEach(l -> l.clusterAdded(cluster));
} }
public void removeCluster(Cluster cluster) { public void removeCluster(Cluster cluster) {
this.clusters.remove(cluster); this.clusters.remove(cluster);
cluster.setParent(null); cluster.setParent(null);
this.listeners.forEach(l -> l.clusterRemoved(cluster));
} }
public List<Cluster> getClustersOrdered() { public List<Cluster> getClustersOrdered() {
@ -76,6 +81,14 @@ public class Cluster implements Comparable<Cluster>, CrystalItem {
this.parent = parent; this.parent = parent;
} }
public void addListener(ClusterListener listener) {
this.listeners.add(listener);
}
public void removeListener(ClusterListener listener) {
this.listeners.remove(listener);
}
@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

@ -0,0 +1,8 @@
package nl.andrewlalis.crystalkeep.model;
public interface ClusterListener {
default void clusterAdded(Cluster cluster) {}
default void clusterRemoved(Cluster cluster) {}
default void shardAdded(Shard shard) {}
default void shardRemoved(Shard shard) {}
}

View File

@ -1,18 +1,59 @@
package nl.andrewlalis.crystalkeep.view; package nl.andrewlalis.crystalkeep.view;
import javafx.scene.control.TreeItem; import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import nl.andrewlalis.crystalkeep.model.Cluster; import nl.andrewlalis.crystalkeep.model.Cluster;
import nl.andrewlalis.crystalkeep.model.ClusterListener;
import nl.andrewlalis.crystalkeep.model.CrystalItem; import nl.andrewlalis.crystalkeep.model.CrystalItem;
import nl.andrewlalis.crystalkeep.model.Shard;
public class ClusterTreeItem extends TreeItem<CrystalItem> { public class ClusterTreeItem extends TreeItem<CrystalItem> implements ClusterListener {
private final Cluster cluster; private final Cluster cluster;
private final TreeView<CrystalItem> treeView;
public ClusterTreeItem(Cluster cluster) { public ClusterTreeItem(Cluster cluster, TreeView<CrystalItem> treeView) {
super(cluster); super(cluster);
this.cluster = cluster; this.cluster = cluster;
this.treeView = treeView;
this.cluster.addListener(this);
} }
public Cluster getCluster() { public Cluster getCluster() {
return cluster; return cluster;
} }
@Override
public void clusterAdded(Cluster cluster) {
var item = new ClusterTreeItem(cluster, treeView);
this.getChildren().add(item);
this.treeView.getSelectionModel().select(item);
}
@Override
public void clusterRemoved(Cluster cluster) {
for (var child : this.getChildren()) {
if (child.getValue().equals(cluster)) {
this.getChildren().remove(child);
if (child instanceof ClusterListener) cluster.removeListener((ClusterListener) child);
return;
}
}
}
@Override
public void shardAdded(Shard shard) {
var item = new ShardTreeItem(shard);
this.getChildren().add(item);
this.treeView.getSelectionModel().select(item);
}
@Override
public void shardRemoved(Shard shard) {
for (var child : this.getChildren()) {
if (child.getValue().equals(shard)) {
this.getChildren().remove(child);
return;
}
}
}
} }

View File

@ -40,7 +40,7 @@ public class CrystalItemTreeCell extends TreeCell<CrystalItem> {
if (this.getTreeItem().getParent() != null && this.getTreeItem().getParent() instanceof ClusterTreeItem) { if (this.getTreeItem().getParent() != null && this.getTreeItem().getParent() instanceof ClusterTreeItem) {
var deleteItem = new MenuItem("Delete"); var deleteItem = new MenuItem("Delete");
deleteItem.setOnAction(new DeleteItemHandler(this.getTreeItem(), this.model)); deleteItem.setOnAction(new DeleteItemHandler(this.getTreeItem()));
menu.getItems().add(deleteItem); menu.getItems().add(deleteItem);
} }
this.setText(item.getName()); this.setText(item.getName());