From 38b01f0531e23b818439ccb373c71ce90c103ba7 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Sun, 7 Aug 2022 15:53:21 +0200 Subject: [PATCH] Added more popups and customizable registry URL. --- .../aos2_launcher/MainViewController.java | 23 +++++++++++------ .../andrewl/aos2_launcher/ServersFetcher.java | 25 +++++++++++++++---- launcher/src/main/resources/main_view.fxml | 25 ++++++++----------- 3 files changed, 46 insertions(+), 27 deletions(-) diff --git a/launcher/src/main/java/nl/andrewl/aos2_launcher/MainViewController.java b/launcher/src/main/java/nl/andrewl/aos2_launcher/MainViewController.java index d7ba0e4..5bc6545 100644 --- a/launcher/src/main/java/nl/andrewl/aos2_launcher/MainViewController.java +++ b/launcher/src/main/java/nl/andrewl/aos2_launcher/MainViewController.java @@ -4,11 +4,9 @@ import javafx.application.Platform; import javafx.beans.binding.BooleanBinding; import javafx.collections.ListChangeListener; import javafx.fxml.FXML; -import javafx.scene.control.Button; -import javafx.scene.control.Label; -import javafx.scene.control.ProgressBar; -import javafx.scene.control.ProgressIndicator; +import javafx.scene.control.*; import javafx.scene.layout.VBox; +import javafx.stage.Window; import nl.andrewl.aos2_launcher.model.Profile; import nl.andrewl.aos2_launcher.model.ProfileSet; import nl.andrewl.aos2_launcher.model.ProgressReporter; @@ -32,10 +30,11 @@ public class MainViewController implements ProgressReporter { @FXML public VBox progressVBox; @FXML public Label progressLabel; @FXML public ProgressBar progressBar; + @FXML public TextField registryUrlField; private final ProfileSet profileSet = new ProfileSet(); - private final ServersFetcher serversFetcher = new ServersFetcher(); + private ServersFetcher serversFetcher; @FXML public void initialize() { @@ -61,14 +60,24 @@ public class MainViewController implements ProgressReporter { progressVBox.managedProperty().bind(progressVBox.visibleProperty()); progressVBox.setVisible(false); - refreshServers(); + + serversFetcher = new ServersFetcher(registryUrlField.textProperty()); + Platform.runLater(this::refreshServers); } @FXML public void refreshServers() { - serversFetcher.fetchServers() + Window owner = this.profilesVBox.getScene().getWindow(); + serversFetcher.fetchServers(owner) .exceptionally(throwable -> { throwable.printStackTrace(); + Platform.runLater(() -> { + Alert alert = new Alert(Alert.AlertType.ERROR); + alert.setHeaderText("Couldn't fetch servers."); + alert.setContentText("An error occurred, and the list of servers couldn't be fetched: " + throwable.getMessage() + ". Are you sure that you have the correct registry URL? Check the \"Servers\" tab."); + alert.initOwner(owner); + alert.show(); + }); return new ArrayList<>(); }) .thenAccept(newServers -> Platform.runLater(() -> { diff --git a/launcher/src/main/java/nl/andrewl/aos2_launcher/ServersFetcher.java b/launcher/src/main/java/nl/andrewl/aos2_launcher/ServersFetcher.java index b32e641..4917af1 100644 --- a/launcher/src/main/java/nl/andrewl/aos2_launcher/ServersFetcher.java +++ b/launcher/src/main/java/nl/andrewl/aos2_launcher/ServersFetcher.java @@ -4,6 +4,11 @@ import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import javafx.application.Platform; +import javafx.beans.property.SimpleStringProperty; +import javafx.beans.property.StringProperty; +import javafx.scene.control.Alert; +import javafx.stage.Window; import nl.andrewl.aos2_launcher.model.Server; import java.net.URI; @@ -16,18 +21,28 @@ import java.util.List; import java.util.concurrent.CompletableFuture; class ServersFetcher { - private static final String registryURL = "http://localhost:8080"; - private final HttpClient httpClient; private final Gson gson; + private final StringProperty registryUrl; - public ServersFetcher() { + public ServersFetcher(StringProperty registryUrlProperty) { httpClient = HttpClient.newBuilder().build(); gson = new Gson(); + this.registryUrl = new SimpleStringProperty("http://localhost:8080"); + registryUrl.bind(registryUrlProperty); } - public CompletableFuture> fetchServers() { - HttpRequest req = HttpRequest.newBuilder(URI.create(registryURL + "/servers")) + public CompletableFuture> fetchServers(Window owner) { + if (registryUrl.get() == null || registryUrl.get().isBlank()) { + Platform.runLater(() -> { + Alert alert = new Alert(Alert.AlertType.WARNING); + alert.setContentText("Invalid or missing registry URL. Can't fetch the list of servers."); + alert.initOwner(owner); + alert.show(); + }); + return CompletableFuture.completedFuture(new ArrayList<>()); + } + HttpRequest req = HttpRequest.newBuilder(URI.create(registryUrl.get() + "/servers")) .GET() .timeout(Duration.ofSeconds(3)) .header("Accept", "application/json") diff --git a/launcher/src/main/resources/main_view.fxml b/launcher/src/main/resources/main_view.fxml index 8281c94..4e30590 100644 --- a/launcher/src/main/resources/main_view.fxml +++ b/launcher/src/main/resources/main_view.fxml @@ -1,10 +1,11 @@ - + - - + + + @@ -22,6 +23,7 @@