Added more popups and customizable registry URL.

This commit is contained in:
Andrew Lalis 2022-08-07 15:53:21 +02:00
parent 782f32be8f
commit 38b01f0531
3 changed files with 46 additions and 27 deletions

View File

@ -4,11 +4,9 @@ import javafx.application.Platform;
import javafx.beans.binding.BooleanBinding; import javafx.beans.binding.BooleanBinding;
import javafx.collections.ListChangeListener; import javafx.collections.ListChangeListener;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.Button; import javafx.scene.control.*;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import javafx.stage.Window;
import nl.andrewl.aos2_launcher.model.Profile; import nl.andrewl.aos2_launcher.model.Profile;
import nl.andrewl.aos2_launcher.model.ProfileSet; import nl.andrewl.aos2_launcher.model.ProfileSet;
import nl.andrewl.aos2_launcher.model.ProgressReporter; import nl.andrewl.aos2_launcher.model.ProgressReporter;
@ -32,10 +30,11 @@ public class MainViewController implements ProgressReporter {
@FXML public VBox progressVBox; @FXML public VBox progressVBox;
@FXML public Label progressLabel; @FXML public Label progressLabel;
@FXML public ProgressBar progressBar; @FXML public ProgressBar progressBar;
@FXML public TextField registryUrlField;
private final ProfileSet profileSet = new ProfileSet(); private final ProfileSet profileSet = new ProfileSet();
private final ServersFetcher serversFetcher = new ServersFetcher(); private ServersFetcher serversFetcher;
@FXML @FXML
public void initialize() { public void initialize() {
@ -61,14 +60,24 @@ public class MainViewController implements ProgressReporter {
progressVBox.managedProperty().bind(progressVBox.visibleProperty()); progressVBox.managedProperty().bind(progressVBox.visibleProperty());
progressVBox.setVisible(false); progressVBox.setVisible(false);
refreshServers();
serversFetcher = new ServersFetcher(registryUrlField.textProperty());
Platform.runLater(this::refreshServers);
} }
@FXML @FXML
public void refreshServers() { public void refreshServers() {
serversFetcher.fetchServers() Window owner = this.profilesVBox.getScene().getWindow();
serversFetcher.fetchServers(owner)
.exceptionally(throwable -> { .exceptionally(throwable -> {
throwable.printStackTrace(); 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<>(); return new ArrayList<>();
}) })
.thenAccept(newServers -> Platform.runLater(() -> { .thenAccept(newServers -> Platform.runLater(() -> {

View File

@ -4,6 +4,11 @@ import com.google.gson.Gson;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; 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 nl.andrewl.aos2_launcher.model.Server;
import java.net.URI; import java.net.URI;
@ -16,18 +21,28 @@ import java.util.List;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
class ServersFetcher { class ServersFetcher {
private static final String registryURL = "http://localhost:8080";
private final HttpClient httpClient; private final HttpClient httpClient;
private final Gson gson; private final Gson gson;
private final StringProperty registryUrl;
public ServersFetcher() { public ServersFetcher(StringProperty registryUrlProperty) {
httpClient = HttpClient.newBuilder().build(); httpClient = HttpClient.newBuilder().build();
gson = new Gson(); gson = new Gson();
this.registryUrl = new SimpleStringProperty("http://localhost:8080");
registryUrl.bind(registryUrlProperty);
} }
public CompletableFuture<List<Server>> fetchServers() { public CompletableFuture<List<Server>> fetchServers(Window owner) {
HttpRequest req = HttpRequest.newBuilder(URI.create(registryURL + "/servers")) 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() .GET()
.timeout(Duration.ofSeconds(3)) .timeout(Duration.ofSeconds(3))
.header("Accept", "application/json") .header("Accept", "application/json")

View File

@ -1,10 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?> <?import javafx.geometry.*?>
<?import javafx.scene.control.*?> <?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?> <?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?> <?import javafx.scene.text.*?>
<VBox minHeight="300.0" minWidth="300.0" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="nl.andrewl.aos2_launcher.MainViewController">
<VBox minHeight="300.0" minWidth="300.0" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="nl.andrewl.aos2_launcher.MainViewController">
<TabPane tabClosingPolicy="UNAVAILABLE" VBox.vgrow="ALWAYS"> <TabPane tabClosingPolicy="UNAVAILABLE" VBox.vgrow="ALWAYS">
<Tab text="Profiles"> <Tab text="Profiles">
<VBox> <VBox>
@ -22,6 +23,7 @@
<VBox> <VBox>
<HBox alignment="CENTER" styleClass="button-bar" VBox.vgrow="NEVER"> <HBox alignment="CENTER" styleClass="button-bar" VBox.vgrow="NEVER">
<Button onAction="#refreshServers" text="Refresh" /> <Button onAction="#refreshServers" text="Refresh" />
<TextField fx:id="registryUrlField" prefWidth="300.0" promptText="Registry URL" text="http://localhost:8080" style="-fx-font-size: 10px;" />
</HBox> </HBox>
<ScrollPane fitToWidth="true" VBox.vgrow="ALWAYS"> <ScrollPane fitToWidth="true" VBox.vgrow="ALWAYS">
<VBox fx:id="serversVBox" styleClass="banner-list" /> <VBox fx:id="serversVBox" styleClass="banner-list" />
@ -30,26 +32,19 @@
</Tab> </Tab>
</TabPane> </TabPane>
<HBox alignment="CENTER" styleClass="button-bar" VBox.vgrow="NEVER"> <HBox alignment="CENTER" styleClass="button-bar" VBox.vgrow="NEVER">
<Button <Button fx:id="playButton" mnemonicParsing="false" onAction="#play" text="Play" />
fx:id="playButton"
mnemonicParsing="false"
onAction="#play"
text="Play"
/>
</HBox> </HBox>
<VBox fx:id="progressVBox" VBox.vgrow="NEVER"> <VBox fx:id="progressVBox" VBox.vgrow="NEVER">
<AnchorPane VBox.vgrow="NEVER"> <AnchorPane VBox.vgrow="NEVER">
<padding> <padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0"/> <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding> </padding>
<Label fx:id="progressLabel" text="Work in progress..." AnchorPane.bottomAnchor="0.0" <Label fx:id="progressLabel" text="Work in progress..." AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="0.0">
AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="0.0">
<font> <font>
<Font size="10.0"/> <Font size="10.0" />
</font> </font>
</Label> </Label>
<ProgressBar fx:id="progressBar" prefWidth="200.0" progress="0.0" AnchorPane.bottomAnchor="0.0" <ProgressBar fx:id="progressBar" prefWidth="200.0" progress="0.0" AnchorPane.bottomAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"/>
</AnchorPane> </AnchorPane>
</VBox> </VBox>
</VBox> </VBox>