From 929ce398eb7e7f1483af628eadfcff962539089d Mon Sep 17 00:00:00 2001 From: yushijinhun Date: Sun, 8 Jul 2018 18:33:37 +0800 Subject: [PATCH] =?UTF-8?q?AddAccountPane=20=E4=BD=BF=E7=94=A8=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E7=BB=91=E5=AE=9A=20&=20=E7=99=BB=E5=BD=95=E6=97=B6?= =?UTF-8?q?=E8=AE=BE=E7=BD=AEdisable=E4=B8=BAtrue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/jackhuang/hmcl/ui/AddAccountPane.java | 49 ++++++++----------- .../resources/assets/fxml/account-add.fxml | 4 +- 2 files changed, 23 insertions(+), 30 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/AddAccountPane.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/AddAccountPane.java index cc8fbb5b6..c620eee7e 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/AddAccountPane.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/AddAccountPane.java @@ -21,9 +21,9 @@ import com.jfoenix.concurrency.JFXUtilities; import com.jfoenix.controls.*; import javafx.beans.binding.Bindings; +import javafx.beans.property.ReadOnlyIntegerProperty; import javafx.fxml.FXML; import javafx.geometry.Pos; -import javafx.scene.Node; import javafx.scene.control.Hyperlink; import javafx.scene.control.Label; import javafx.scene.image.Image; @@ -85,22 +85,22 @@ public class AddAccountPane extends StackPane { selectDefaultServer(); cboType.getItems().setAll(i18n("account.methods.offline"), i18n("account.methods.yggdrasil"), i18n("account.methods.authlib_injector")); - cboType.getSelectionModel().selectedIndexProperty().addListener((a, b, newValue) -> { - txtPassword.setVisible(newValue.intValue() != 0); - lblPassword.setVisible(newValue.intValue() != 0); - cboServers.setVisible(newValue.intValue() == 2); - linkManageInjectorServers.setVisible(newValue.intValue() == 2); - lblInjectorServer.setVisible(newValue.intValue() == 2); - validateAcceptButton(); - }); cboType.getSelectionModel().select(0); - txtPassword.setOnAction(e -> onCreationAccept()); - txtUsername.setOnAction(e -> onCreationAccept()); + ReadOnlyIntegerProperty loginTypeIdProperty = cboType.getSelectionModel().selectedIndexProperty(); + + txtPassword.visibleProperty().bind(loginTypeIdProperty.isNotEqualTo(0)); + lblPassword.visibleProperty().bind(txtPassword.visibleProperty()); + + cboServers.visibleProperty().bind(loginTypeIdProperty.isEqualTo(2)); + lblInjectorServer.visibleProperty().bind(cboServers.visibleProperty()); + linkManageInjectorServers.visibleProperty().bind(cboServers.visibleProperty()); + txtUsername.getValidators().add(new Validator(i18n("input.email"), str -> !txtPassword.isVisible() || str.contains("@"))); - txtUsername.textProperty().addListener(it -> validateAcceptButton()); - txtPassword.textProperty().addListener(it -> validateAcceptButton()); + btnAccept.disableProperty().bind(Bindings.createBooleanBinding( + () -> !txtUsername.validate() || (loginTypeIdProperty.get() != 0 && !txtPassword.validate()), + txtUsername.textProperty(), txtPassword.textProperty(), loginTypeIdProperty)); } /** @@ -112,12 +112,11 @@ public class AddAccountPane extends StackPane { } } - private void validateAcceptButton() { - btnAccept.setDisable(!txtUsername.validate() || (cboType.getSelectionModel().getSelectedIndex() != 0 && !txtPassword.validate())); - } - @FXML private void onCreationAccept() { + if (btnAccept.isDisabled()) + return; + String username = txtUsername.getText(); String password = txtPassword.getText(); Object addtionalData; @@ -149,6 +148,7 @@ public class AddAccountPane extends StackPane { acceptPane.showSpinner(); lblCreationWarning.setText(""); + setDisable(true); Task.ofResult("create_account", () -> factory.create(new Selector(), username, password, addtionalData, Settings.INSTANCE.getProxy())) .finalized(Schedulers.javafx(), variables -> { @@ -161,6 +161,7 @@ public class AddAccountPane extends StackPane { } else { lblCreationWarning.setText(accountException(exception)); } + setDisable(false); acceptPane.hideSpinner(); }).start(); } @@ -176,14 +177,6 @@ public class AddAccountPane extends StackPane { Controllers.navigate(Controllers.getServersPage()); } - private void showSelector(Node node) { - getChildren().setAll(node); - } - - private void closeSelector() { - getChildren().setAll(layout); - } - private class Selector extends BorderPane implements CharacterSelector { private final AdvancedListBox listBox = new AdvancedListBox(); private final JFXButton cancel = new JFXButton(); @@ -240,7 +233,7 @@ public class AddAccountPane extends StackPane { listBox.add(accountItem); } - JFXUtilities.runInFX(() -> showSelector(this)); + JFXUtilities.runInFX(() -> Controllers.dialog(this)); try { latch.await(); @@ -248,11 +241,11 @@ public class AddAccountPane extends StackPane { if (selectedProfile == null) throw new NoSelectedCharacterException(account); - JFXUtilities.runInFX(AddAccountPane.this::closeSelector); - return selectedProfile; } catch (InterruptedException ignore) { throw new NoSelectedCharacterException(account); + } finally { + JFXUtilities.runInFX(() -> Selector.this.fireEvent(new DialogCloseEvent())); } } } diff --git a/HMCL/src/main/resources/assets/fxml/account-add.fxml b/HMCL/src/main/resources/assets/fxml/account-add.fxml index 2567bd210..bf0e6313e 100644 --- a/HMCL/src/main/resources/assets/fxml/account-add.fxml +++ b/HMCL/src/main/resources/assets/fxml/account-add.fxml @@ -37,7 +37,7 @@