diff --git a/src/main/java/nl/andrewlalis/crystalkeep/control/AddClusterHandler.java b/src/main/java/nl/andrewlalis/crystalkeep/control/AddClusterHandler.java index f481c81..025cc23 100644 --- a/src/main/java/nl/andrewlalis/crystalkeep/control/AddClusterHandler.java +++ b/src/main/java/nl/andrewlalis/crystalkeep/control/AddClusterHandler.java @@ -23,9 +23,6 @@ public class AddClusterHandler implements EventHandler { d.setHeaderText(null); d.setTitle("Add Cluster"); d.setGraphic(null); - d.showAndWait().ifPresent(s -> { - cluster.addCluster(new Cluster(s.trim())); - model.notifyListeners(); - }); + d.showAndWait().ifPresent(s -> cluster.addCluster(new Cluster(s.trim()))); } } diff --git a/src/main/java/nl/andrewlalis/crystalkeep/control/AddShardHandler.java b/src/main/java/nl/andrewlalis/crystalkeep/control/AddShardHandler.java index cbe8bb5..528a24c 100644 --- a/src/main/java/nl/andrewlalis/crystalkeep/control/AddShardHandler.java +++ b/src/main/java/nl/andrewlalis/crystalkeep/control/AddShardHandler.java @@ -59,9 +59,6 @@ public class AddShardHandler implements EventHandler { } return null; }); - dialog.showAndWait().ifPresent(shard -> { - cluster.addShard(shard); - model.notifyListeners(); - }); + dialog.showAndWait().ifPresent(cluster::addShard); } } diff --git a/src/main/java/nl/andrewlalis/crystalkeep/control/DeleteItemHandler.java b/src/main/java/nl/andrewlalis/crystalkeep/control/DeleteItemHandler.java index 942dba9..cad13b7 100644 --- a/src/main/java/nl/andrewlalis/crystalkeep/control/DeleteItemHandler.java +++ b/src/main/java/nl/andrewlalis/crystalkeep/control/DeleteItemHandler.java @@ -7,7 +7,6 @@ import javafx.scene.control.ButtonType; import javafx.scene.control.TreeItem; import nl.andrewlalis.crystalkeep.model.Cluster; import nl.andrewlalis.crystalkeep.model.CrystalItem; -import nl.andrewlalis.crystalkeep.model.Model; import nl.andrewlalis.crystalkeep.model.Shard; import nl.andrewlalis.crystalkeep.view.ClusterTreeItem; @@ -15,11 +14,9 @@ import java.util.Optional; public class DeleteItemHandler implements EventHandler { private final TreeItem treeItem; - private final Model model; - public DeleteItemHandler(TreeItem treeItem, Model model) { + public DeleteItemHandler(TreeItem treeItem) { this.treeItem = treeItem; - this.model = model; } @Override @@ -27,14 +24,15 @@ public class DeleteItemHandler implements EventHandler { if (this.treeItem.getParent() != null && this.treeItem.getParent() instanceof ClusterTreeItem) { Optional result = new Alert(Alert.AlertType.CONFIRMATION, "Are you sure you want to delete this item?").showAndWait(); 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(); if (item instanceof Shard) { - cluster.removeShard((Shard) item); + parentCluster.removeShard((Shard) item); } else if (item instanceof Cluster) { - cluster.removeCluster((Cluster) item); + var cluster = (Cluster) item; + parentCluster.removeCluster(cluster); } - this.model.notifyListeners(); } } } diff --git a/src/main/java/nl/andrewlalis/crystalkeep/control/MainViewController.java b/src/main/java/nl/andrewlalis/crystalkeep/control/MainViewController.java index 8395b13..1cce2b7 100644 --- a/src/main/java/nl/andrewlalis/crystalkeep/control/MainViewController.java +++ b/src/main/java/nl/andrewlalis/crystalkeep/control/MainViewController.java @@ -43,7 +43,7 @@ public class MainViewController implements ModelListener { } private TreeItem createNode(Cluster cluster) { - ClusterTreeItem node = new ClusterTreeItem(cluster); + ClusterTreeItem node = new ClusterTreeItem(cluster, this.clusterTreeView); node.setExpanded(true); for (Cluster child : cluster.getClustersOrdered()) { node.getChildren().add(this.createNode(child)); diff --git a/src/main/java/nl/andrewlalis/crystalkeep/model/Cluster.java b/src/main/java/nl/andrewlalis/crystalkeep/model/Cluster.java index 1c7ba3c..dc2001d 100644 --- a/src/main/java/nl/andrewlalis/crystalkeep/model/Cluster.java +++ b/src/main/java/nl/andrewlalis/crystalkeep/model/Cluster.java @@ -8,6 +8,7 @@ public class Cluster implements Comparable, CrystalItem { private final Set clusters; private Cluster parent; + private final Set listeners = new HashSet<>(); public Cluster(String name, Set shards, Set clusters) { this.name = name; @@ -39,21 +40,25 @@ public class Cluster implements Comparable, CrystalItem { public void addShard(Shard shard) { this.shards.add(shard); shard.setParent(this); + this.listeners.forEach(l -> l.shardAdded(shard)); } public void removeShard(Shard shard) { this.shards.remove(shard); shard.setParent(null); + this.listeners.forEach(l -> l.shardRemoved(shard)); } public void addCluster(Cluster cluster) { this.clusters.add(cluster); cluster.setParent(this); + this.listeners.forEach(l -> l.clusterAdded(cluster)); } public void removeCluster(Cluster cluster) { this.clusters.remove(cluster); cluster.setParent(null); + this.listeners.forEach(l -> l.clusterRemoved(cluster)); } public List getClustersOrdered() { @@ -76,6 +81,14 @@ public class Cluster implements Comparable, CrystalItem { this.parent = parent; } + public void addListener(ClusterListener listener) { + this.listeners.add(listener); + } + + public void removeListener(ClusterListener listener) { + this.listeners.remove(listener); + } + @Override public int compareTo(Cluster o) { return this.getName().compareTo(o.getName()); diff --git a/src/main/java/nl/andrewlalis/crystalkeep/model/ClusterListener.java b/src/main/java/nl/andrewlalis/crystalkeep/model/ClusterListener.java new file mode 100644 index 0000000..29a935a --- /dev/null +++ b/src/main/java/nl/andrewlalis/crystalkeep/model/ClusterListener.java @@ -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) {} +} diff --git a/src/main/java/nl/andrewlalis/crystalkeep/view/ClusterTreeItem.java b/src/main/java/nl/andrewlalis/crystalkeep/view/ClusterTreeItem.java index f17606e..6b83982 100644 --- a/src/main/java/nl/andrewlalis/crystalkeep/view/ClusterTreeItem.java +++ b/src/main/java/nl/andrewlalis/crystalkeep/view/ClusterTreeItem.java @@ -1,18 +1,59 @@ package nl.andrewlalis.crystalkeep.view; import javafx.scene.control.TreeItem; +import javafx.scene.control.TreeView; import nl.andrewlalis.crystalkeep.model.Cluster; +import nl.andrewlalis.crystalkeep.model.ClusterListener; import nl.andrewlalis.crystalkeep.model.CrystalItem; +import nl.andrewlalis.crystalkeep.model.Shard; -public class ClusterTreeItem extends TreeItem { +public class ClusterTreeItem extends TreeItem implements ClusterListener { private final Cluster cluster; + private final TreeView treeView; - public ClusterTreeItem(Cluster cluster) { + public ClusterTreeItem(Cluster cluster, TreeView treeView) { super(cluster); this.cluster = cluster; + this.treeView = treeView; + this.cluster.addListener(this); } public Cluster getCluster() { 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; + } + } + } } diff --git a/src/main/java/nl/andrewlalis/crystalkeep/view/CrystalItemTreeCell.java b/src/main/java/nl/andrewlalis/crystalkeep/view/CrystalItemTreeCell.java index c9f3a97..8bafa6e 100644 --- a/src/main/java/nl/andrewlalis/crystalkeep/view/CrystalItemTreeCell.java +++ b/src/main/java/nl/andrewlalis/crystalkeep/view/CrystalItemTreeCell.java @@ -40,7 +40,7 @@ public class CrystalItemTreeCell extends TreeCell { if (this.getTreeItem().getParent() != null && this.getTreeItem().getParent() instanceof ClusterTreeItem) { var deleteItem = new MenuItem("Delete"); - deleteItem.setOnAction(new DeleteItemHandler(this.getTreeItem(), this.model)); + deleteItem.setOnAction(new DeleteItemHandler(this.getTreeItem())); menu.getItems().add(deleteItem); } this.setText(item.getName());