diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/AddAuthlibInjectorServerDialog.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/AddAuthlibInjectorServerDialog.java new file mode 100644 index 000000000..b037168b4 --- /dev/null +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/AddAuthlibInjectorServerDialog.java @@ -0,0 +1,131 @@ +/* + * Hello Minecraft! Launcher. + * Copyright (C) 2018 huangyuhui + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see {http://www.gnu.org/licenses/}. + */ +package org.jackhuang.hmcl.ui; + +import static org.jackhuang.hmcl.ui.FXUtils.loadFXML; +import static org.jackhuang.hmcl.util.i18n.I18n.i18n; + +import java.io.IOException; + +import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorServer; +import org.jackhuang.hmcl.setting.Settings; +import org.jackhuang.hmcl.task.Schedulers; +import org.jackhuang.hmcl.task.Task; +import org.jackhuang.hmcl.ui.animation.ContainerAnimations; +import org.jackhuang.hmcl.ui.animation.TransitionHandler; +import org.jackhuang.hmcl.ui.construct.SpinnerPane; +import org.jackhuang.hmcl.util.NetworkUtils; + +import com.jfoenix.controls.JFXButton; +import com.jfoenix.controls.JFXDialog; +import com.jfoenix.controls.JFXDialogLayout; +import com.jfoenix.controls.JFXTextField; + +import javafx.beans.binding.Bindings; +import javafx.fxml.FXML; +import javafx.scene.control.Label; +import javafx.scene.layout.StackPane; + +public class AddAuthlibInjectorServerDialog extends JFXDialog { + + @FXML private StackPane addServerContainer; + @FXML private Label lblServerUrl; + @FXML private Label lblServerName; + @FXML private Label lblCreationWarning; + @FXML private Label lblServerWarning; + @FXML private JFXTextField txtServerUrl; + @FXML private JFXDialogLayout addServerPane; + @FXML private JFXDialogLayout confirmServerPane; + @FXML private SpinnerPane nextPane; + @FXML private JFXButton btnAddNext; + + private TransitionHandler transitionHandler; + + private AuthlibInjectorServer serverBeingAdded; + + public AddAuthlibInjectorServerDialog() { + loadFXML(this, "/assets/fxml/authlib-injector-server-add.fxml"); + transitionHandler = new TransitionHandler(addServerContainer); + transitionHandler.setContent(addServerPane, ContainerAnimations.NONE.getAnimationProducer()); + + btnAddNext.disableProperty().bind( + Bindings.createBooleanBinding(txtServerUrl::validate, txtServerUrl.textProperty()).not()); + nextPane.hideSpinner(); + } + + private String fixInputUrl(String url) { + if (!url.endsWith("/")) { + url += "/"; + } + return url; + } + + private String resolveFetchExceptionMessage(Throwable exception) { + if (exception instanceof IOException) { + return i18n("account.failed.connect_injector_server"); + } else { + return exception.getClass() + ": " + exception.getLocalizedMessage(); + } + } + + @FXML + private void onAddCancel() { + this.close(); + } + + @FXML + private void onAddNext() { + String url = fixInputUrl(txtServerUrl.getText()); + + nextPane.showSpinner(); + addServerPane.setDisable(true); + + Task.of(() -> { + serverBeingAdded = AuthlibInjectorServer.fetchServerInfo(url); + }).finalized(Schedulers.javafx(), (variables, isDependentsSucceeded) -> { + addServerPane.setDisable(false); + nextPane.hideSpinner(); + + if (isDependentsSucceeded) { + lblServerName.setText(serverBeingAdded.getName()); + lblServerUrl.setText(serverBeingAdded.getUrl()); + + lblServerWarning.setVisible("http".equals(NetworkUtils.toURL(serverBeingAdded.getUrl()).getProtocol())); + + transitionHandler.setContent(confirmServerPane, ContainerAnimations.SWIPE_LEFT.getAnimationProducer()); + } else { + lblCreationWarning.setText(resolveFetchExceptionMessage(variables.get("lastException"))); + } + }).start(); + + } + + @FXML + private void onAddPrev() { + transitionHandler.setContent(addServerPane, ContainerAnimations.SWIPE_RIGHT.getAnimationProducer()); + } + + @FXML + private void onAddFinish() { + if (!Settings.INSTANCE.SETTINGS.authlibInjectorServers.contains(serverBeingAdded)) { + Settings.INSTANCE.SETTINGS.authlibInjectorServers.add(serverBeingAdded); + } + this.close(); + } + +} diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/AuthlibInjectorServersPage.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/AuthlibInjectorServersPage.java index 49fd0961a..4bf95687a 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/AuthlibInjectorServersPage.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/AuthlibInjectorServersPage.java @@ -17,137 +17,57 @@ */ package org.jackhuang.hmcl.ui; -import com.jfoenix.controls.*; +import static java.util.stream.Collectors.toList; +import static org.jackhuang.hmcl.ui.FXUtils.loadFXML; +import static org.jackhuang.hmcl.ui.FXUtils.smoothScrolling; +import static org.jackhuang.hmcl.util.i18n.I18n.i18n; + +import org.jackhuang.hmcl.setting.Settings; +import org.jackhuang.hmcl.ui.wizard.DecoratorPage; + +import javafx.beans.InvalidationListener; +import javafx.beans.WeakInvalidationListener; import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; import javafx.fxml.FXML; -import javafx.scene.control.Label; import javafx.scene.control.ScrollPane; import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; -import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorServer; -import org.jackhuang.hmcl.setting.Settings; -import org.jackhuang.hmcl.task.Schedulers; -import org.jackhuang.hmcl.task.Task; -import org.jackhuang.hmcl.ui.animation.ContainerAnimations; -import org.jackhuang.hmcl.ui.animation.TransitionHandler; -import org.jackhuang.hmcl.ui.construct.SpinnerPane; -import org.jackhuang.hmcl.ui.wizard.DecoratorPage; -import org.jackhuang.hmcl.util.NetworkUtils; -import static java.util.stream.Collectors.toList; -import static org.jackhuang.hmcl.util.i18n.I18n.i18n; - -import java.io.IOException; - public class AuthlibInjectorServersPage extends StackPane implements DecoratorPage { private final StringProperty title = new SimpleStringProperty(this, "title", i18n("account.injector.server")); @FXML private ScrollPane scrollPane; - @FXML private StackPane addServerContainer; - @FXML private Label lblServerUrl; - @FXML private Label lblServerName; - @FXML private Label lblCreationWarning; - @FXML private Label lblServerWarning; @FXML private VBox listPane; - @FXML private JFXTextField txtServerUrl; - @FXML private JFXDialogLayout addServerPane; - @FXML private JFXDialogLayout confirmServerPane; - @FXML private JFXDialog dialog; @FXML private StackPane contentPane; - @FXML private SpinnerPane nextPane; - @FXML private JFXButton btnAddNext; - private final TransitionHandler transitionHandler; + private InvalidationListener serversListener; - private AuthlibInjectorServer serverBeingAdded; + public AuthlibInjectorServersPage() { + loadFXML(this, "/assets/fxml/authlib-injector-servers.fxml"); + smoothScrolling(scrollPane); - { - FXUtils.loadFXML(this, "/assets/fxml/authlib-injector-servers.fxml"); - FXUtils.smoothScrolling(scrollPane); - transitionHandler = new TransitionHandler(addServerContainer); - - getChildren().remove(dialog); - dialog.setDialogContainer(this); - - txtServerUrl.textProperty().addListener((a, b, newValue) -> - btnAddNext.setDisable(!txtServerUrl.validate())); - - reload(); + serversListener = observable -> updateServersList(); + Settings.SETTINGS.authlibInjectorServers.addListener(new WeakInvalidationListener(serversListener)); + updateServersList(); } - private void removeServer(AuthlibInjectorServerItem item) { - Settings.SETTINGS.authlibInjectorServers.remove(item.getServer()); - reload(); - } - - private void reload() { + private void updateServersList() { listPane.getChildren().setAll( Settings.SETTINGS.authlibInjectorServers.stream() - .map(server -> new AuthlibInjectorServerItem(server, this::removeServer)) + .map(server -> new AuthlibInjectorServerItem(server, + item -> Settings.SETTINGS.authlibInjectorServers.remove(item.getServer()))) .collect(toList())); - if (Settings.SETTINGS.authlibInjectorServers.isEmpty()) { - onAdd(); - } } + @FXML private void onAdd() { - transitionHandler.setContent(addServerPane, ContainerAnimations.NONE.getAnimationProducer()); - txtServerUrl.setText(""); - txtServerUrl.resetValidation(); - lblCreationWarning.setText(""); - addServerPane.setDisable(false); - nextPane.hideSpinner(); + AddAuthlibInjectorServerDialog dialog = new AddAuthlibInjectorServerDialog(); + dialog.setDialogContainer(this); dialog.show(); } - @FXML - private void onAddCancel() { - dialog.close(); - } - - @FXML - private void onAddNext() { - String url = fixInputUrl(txtServerUrl.getText()); - - nextPane.showSpinner(); - addServerPane.setDisable(true); - - Task.of(() -> { - serverBeingAdded = AuthlibInjectorServer.fetchServerInfo(url); - }).finalized(Schedulers.javafx(), (variables, isDependentsSucceeded) -> { - nextPane.hideSpinner(); - addServerPane.setDisable(false); - - if (isDependentsSucceeded) { - lblServerName.setText(serverBeingAdded.getName()); - lblServerUrl.setText(serverBeingAdded.getUrl()); - - lblServerWarning.setVisible("http".equals(NetworkUtils.toURL(serverBeingAdded.getUrl()).getProtocol())); - - transitionHandler.setContent(confirmServerPane, ContainerAnimations.SWIPE_LEFT.getAnimationProducer()); - } else { - lblCreationWarning.setText(resolveFetchExceptionMessage(variables.get("lastException"))); - } - }).start(); - - } - - @FXML - private void onAddPrev() { - transitionHandler.setContent(addServerPane, ContainerAnimations.SWIPE_RIGHT.getAnimationProducer()); - } - - @FXML - private void onAddFinish() { - if (!Settings.INSTANCE.SETTINGS.authlibInjectorServers.contains(serverBeingAdded)) { - Settings.INSTANCE.SETTINGS.authlibInjectorServers.add(serverBeingAdded); - } - reload(); - dialog.close(); - } - public String getTitle() { return title.get(); } @@ -160,19 +80,4 @@ public class AuthlibInjectorServersPage extends StackPane implements DecoratorPa public void setTitle(String title) { this.title.set(title); } - - private String fixInputUrl(String url) { - if (!url.endsWith("/")) { - url += "/"; - } - return url; - } - - private String resolveFetchExceptionMessage(Throwable exception) { - if (exception instanceof IOException) { - return i18n("account.failed.connect_injector_server"); - } else { - return exception.getClass() + ": " + exception.getLocalizedMessage(); - } - } } diff --git a/HMCL/src/main/resources/assets/fxml/authlib-injector-server-add.fxml b/HMCL/src/main/resources/assets/fxml/authlib-injector-server-add.fxml new file mode 100644 index 000000000..97cac357d --- /dev/null +++ b/HMCL/src/main/resources/assets/fxml/authlib-injector-server-add.fxml @@ -0,0 +1,66 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/HMCL/src/main/resources/assets/fxml/authlib-injector-servers.fxml b/HMCL/src/main/resources/assets/fxml/authlib-injector-servers.fxml index 5d055e20f..43fb262d1 100644 --- a/HMCL/src/main/resources/assets/fxml/authlib-injector-servers.fxml +++ b/HMCL/src/main/resources/assets/fxml/authlib-injector-servers.fxml @@ -1,11 +1,9 @@ - - - - + + @@ -23,61 +21,4 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -