Cleaned up the structure of adding/removing crystal items from the tree.
This commit is contained in:
parent
e65f5d828c
commit
3c6c165bc6
|
@ -23,9 +23,6 @@ public class AddClusterHandler implements EventHandler<ActionEvent> {
|
|||
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())));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -59,9 +59,6 @@ public class AddShardHandler implements EventHandler<ActionEvent> {
|
|||
}
|
||||
return null;
|
||||
});
|
||||
dialog.showAndWait().ifPresent(shard -> {
|
||||
cluster.addShard(shard);
|
||||
model.notifyListeners();
|
||||
});
|
||||
dialog.showAndWait().ifPresent(cluster::addShard);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<ActionEvent> {
|
||||
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.model = model;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -27,14 +24,15 @@ public class DeleteItemHandler implements EventHandler<ActionEvent> {
|
|||
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();
|
||||
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);
|
||||
}
|
||||
this.model.notifyListeners();
|
||||
var cluster = (Cluster) item;
|
||||
parentCluster.removeCluster(cluster);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -43,7 +43,7 @@ public class MainViewController implements ModelListener {
|
|||
}
|
||||
|
||||
private TreeItem<CrystalItem> 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));
|
||||
|
|
|
@ -8,6 +8,7 @@ public class Cluster implements Comparable<Cluster>, CrystalItem {
|
|||
private final Set<Cluster> clusters;
|
||||
|
||||
private Cluster parent;
|
||||
private final Set<ClusterListener> listeners = new HashSet<>();
|
||||
|
||||
public Cluster(String name, Set<Shard> shards, Set<Cluster> clusters) {
|
||||
this.name = name;
|
||||
|
@ -39,21 +40,25 @@ public class Cluster implements Comparable<Cluster>, 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<Cluster> getClustersOrdered() {
|
||||
|
@ -76,6 +81,14 @@ public class Cluster implements Comparable<Cluster>, 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());
|
||||
|
|
|
@ -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) {}
|
||||
}
|
|
@ -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<CrystalItem> {
|
||||
public class ClusterTreeItem extends TreeItem<CrystalItem> implements ClusterListener {
|
||||
private final Cluster cluster;
|
||||
private final TreeView<CrystalItem> treeView;
|
||||
|
||||
public ClusterTreeItem(Cluster cluster) {
|
||||
public ClusterTreeItem(Cluster cluster, TreeView<CrystalItem> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,7 +40,7 @@ public class CrystalItemTreeCell extends TreeCell<CrystalItem> {
|
|||
|
||||
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());
|
||||
|
|
Loading…
Reference in New Issue