diff --git a/src/main/java/com/erikmafo/btviewer/ui/projectexplorer/ProjectExplorerController.java b/src/main/java/com/erikmafo/btviewer/ui/projectexplorer/ProjectExplorerController.java index e7fce53..1015906 100644 --- a/src/main/java/com/erikmafo/btviewer/ui/projectexplorer/ProjectExplorerController.java +++ b/src/main/java/com/erikmafo/btviewer/ui/projectexplorer/ProjectExplorerController.java @@ -9,9 +9,7 @@ import com.google.inject.Provider; import javafx.beans.property.ReadOnlyObjectProperty; import javafx.beans.property.SimpleObjectProperty; -import javafx.event.ActionEvent; import javafx.fxml.FXML; -import javafx.scene.control.Button; import javafx.scene.control.ContentDisplay; import javafx.scene.control.ContextMenu; import javafx.scene.control.MenuItem; @@ -26,9 +24,6 @@ public class ProjectExplorerController { - @FXML - private Button addInstanceButton; - @FXML private TreeView treeView; @@ -93,7 +88,6 @@ protected void updateItem(@Nullable TreeItemData item, boolean empty) { } }); treeView.setVisible(true); - addInstanceButton.setOnAction(this::handleAddInstanceAction); } @NotNull @@ -110,34 +104,41 @@ public ReadOnlyObjectProperty selectedTableProperty() { public ContextMenu createContextMenu(@NotNull TreeItemData item){ ContextMenu menu = null; if (item.isProject()) { - var addInstance = new MenuItem("Add instance"); - addInstance.setGraphic(FontAwesomeUtil.create(FontAwesome.Glyph.PLUS)); - addInstance.setOnAction(actionEvent -> - AddInstanceDialog - .displayAndAwaitResult(item.getProjectId()) - .whenComplete(this::handleAddInstanceResult)); - var removeProject = new MenuItem("Remove"); - removeProject.setGraphic(FontAwesomeUtil.create(FontAwesome.Glyph.REMOVE)); - removeProject.setOnAction(actionEvent -> { - removeProjectService.setProjectId(item.getProjectId()); - removeProjectService.setOnSucceeded(event -> ((RootTreeItem)treeView.getRoot()).removeProject((item.getProjectId()))); - removeProjectService.setOnFailed(event -> AlertUtil.displayError("Unable to remove project", event)); - removeProjectService.restart(); - }); - - menu = new ContextMenu(addInstance, removeProject); + menu = new ContextMenu(getAddInstanceMenuItem(item), getRemoveProjectMenuItem(item)); } else if (item.isInstance()) { var refreshTables = new MenuItem("Refresh tables"); refreshTables.setGraphic(FontAwesomeUtil.create(FontAwesome.Glyph.REFRESH)); refreshTables.setOnAction(e -> ((InstanceTreeItem)item.getTreeItem()).loadChildren()); menu = new ContextMenu(refreshTables); + } else if (item.isRoot()) { + menu = new ContextMenu(getAddInstanceMenuItem(item)); } return menu; } - private void handleAddInstanceAction(ActionEvent ignore) { - AddInstanceDialog.displayAndAwaitResult().whenComplete(this::handleAddInstanceResult); + @NotNull + private MenuItem getRemoveProjectMenuItem(@NotNull TreeItemData item) { + var removeProject = new MenuItem("Remove"); + removeProject.setGraphic(FontAwesomeUtil.create(FontAwesome.Glyph.REMOVE)); + removeProject.setOnAction(actionEvent -> { + removeProjectService.setProjectId(item.getProjectId()); + removeProjectService.setOnSucceeded(event -> ((RootTreeItem)treeView.getRoot()).removeProject((item.getProjectId()))); + removeProjectService.setOnFailed(event -> AlertUtil.displayError("Unable to remove project", event)); + removeProjectService.restart(); + }); + return removeProject; + } + + @NotNull + private MenuItem getAddInstanceMenuItem(@NotNull TreeItemData item) { + var addInstance = new MenuItem("Add instance"); + addInstance.setGraphic(FontAwesomeUtil.create(FontAwesome.Glyph.PLUS)); + addInstance.setOnAction(actionEvent -> + AddInstanceDialog + .displayAndAwaitResult(item.getProjectId()) + .whenComplete(this::handleAddInstanceResult)); + return addInstance; } private void handleAddInstanceResult(@Nullable BigtableInstance instance, Throwable throwable) {