From 487816ab74e0e1f205dc76a4734194a410725a65 Mon Sep 17 00:00:00 2001 From: yushijinhun Date: Thu, 19 Jul 2018 21:15:11 +0800 Subject: [PATCH] Use AccountFactory in cboType --- .../org/jackhuang/hmcl/ui/AddAccountPane.java | 90 ++++++++++--------- .../resources/assets/lang/I18N.properties | 1 - .../resources/assets/lang/I18N_zh.properties | 1 - .../assets/lang/I18N_zh_CN.properties | 1 - 4 files changed, 47 insertions(+), 46 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 f10c269fb..7a9bc7ff9 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/AddAccountPane.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/AddAccountPane.java @@ -21,7 +21,7 @@ import com.jfoenix.concurrency.JFXUtilities; import com.jfoenix.controls.*; import javafx.beans.binding.Bindings; -import javafx.beans.property.ReadOnlyIntegerProperty; +import javafx.beans.property.ReadOnlyObjectProperty; import javafx.fxml.FXML; import javafx.geometry.Pos; import javafx.scene.control.Hyperlink; @@ -51,24 +51,28 @@ import org.jackhuang.hmcl.ui.construct.SpinnerPane; import org.jackhuang.hmcl.ui.construct.Validator; import org.jackhuang.hmcl.util.Constants; import org.jackhuang.hmcl.util.Logging; + +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.CountDownLatch; +import java.util.logging.Level; + import static org.jackhuang.hmcl.setting.ConfigHolder.CONFIG; import static org.jackhuang.hmcl.ui.FXUtils.jfxListCellFactory; import static org.jackhuang.hmcl.ui.FXUtils.onInvalidating; import static org.jackhuang.hmcl.ui.FXUtils.stringConverter; +import static org.jackhuang.hmcl.util.Lang.mapOf; +import static org.jackhuang.hmcl.util.Pair.pair; import static org.jackhuang.hmcl.util.i18n.I18n.i18n; -import java.util.List; -import java.util.Optional; -import java.util.concurrent.CountDownLatch; -import java.util.logging.Level; - public class AddAccountPane extends StackPane { @FXML private JFXTextField txtUsername; @FXML private JFXPasswordField txtPassword; @FXML private Label lblCreationWarning; @FXML private Label lblPassword; - @FXML private JFXComboBox cboType; + @FXML private JFXComboBox> cboType; @FXML private JFXComboBox cboServers; @FXML private Label lblInjectorServer; @FXML private Hyperlink linkManageInjectorServers; @@ -85,23 +89,35 @@ public class AddAccountPane extends StackPane { cboServers.getItems().addListener(onInvalidating(this::selectDefaultServer)); selectDefaultServer(); - cboType.getItems().setAll(i18n("account.methods.offline"), i18n("account.methods.yggdrasil"), i18n("account.methods.authlib_injector")); + Map, String> type2name = mapOf( + pair(Accounts.FACTORY_OFFLINE, i18n("account.methods.offline")), + pair(Accounts.FACTORY_YGGDRASIL, i18n("account.methods.yggdrasil")), + pair(Accounts.FACTORY_AUTHLIB_INJECTOR, i18n("account.methods.authlib_injector"))); + cboType.getItems().setAll(type2name.keySet()); + cboType.setConverter(stringConverter(type2name::get)); cboType.getSelectionModel().select(0); - ReadOnlyIntegerProperty loginTypeIdProperty = cboType.getSelectionModel().selectedIndexProperty(); + ReadOnlyObjectProperty> loginType = cboType.getSelectionModel().selectedItemProperty(); - txtPassword.visibleProperty().bind(loginTypeIdProperty.isNotEqualTo(0)); + txtPassword.visibleProperty().bind(loginType.isNotEqualTo(Accounts.FACTORY_OFFLINE)); lblPassword.visibleProperty().bind(txtPassword.visibleProperty()); - cboServers.visibleProperty().bind(loginTypeIdProperty.isEqualTo(2)); + cboServers.visibleProperty().bind(loginType.isEqualTo(Accounts.FACTORY_AUTHLIB_INJECTOR)); lblInjectorServer.visibleProperty().bind(cboServers.visibleProperty()); linkManageInjectorServers.visibleProperty().bind(cboServers.visibleProperty()); txtUsername.getValidators().add(new Validator(i18n("input.email"), str -> !txtPassword.isVisible() || str.contains("@"))); btnAccept.disableProperty().bind(Bindings.createBooleanBinding( - () -> !txtUsername.validate() || (loginTypeIdProperty.get() != 0 && !txtPassword.validate()), - txtUsername.textProperty(), txtPassword.textProperty(), loginTypeIdProperty)); + () -> !( // consider the opposite situation: input is valid + txtUsername.validate() && + // invisible means the field is not needed, neither should it be validated + (!txtPassword.isVisible() || txtPassword.validate()) && + (!cboServers.isVisible() || cboServers.getSelectionModel().getSelectedItem() != null) + ), + txtUsername.textProperty(), + txtPassword.textProperty(), txtPassword.visibleProperty(), + cboServers.getSelectionModel().selectedItemProperty(), cboServers.visibleProperty())); } /** @@ -113,45 +129,33 @@ public class AddAccountPane extends StackPane { } } + /** + * Gets the additional data that needs to be passed into {@link AccountFactory#create(CharacterSelector, String, String, Object)}. + */ + private Object getAuthAdditionalData() { + AccountFactory factory = cboType.getSelectionModel().getSelectedItem(); + if (factory == Accounts.FACTORY_AUTHLIB_INJECTOR) { + // throw an exception if none is selected + return Optional.ofNullable(cboServers.getSelectionModel().getSelectedItem()).get(); + } + return null; + } + @FXML private void onCreationAccept() { if (btnAccept.isDisabled()) return; - String username = txtUsername.getText(); - String password = txtPassword.getText(); - Object addtionalData; - - int type = cboType.getSelectionModel().getSelectedIndex(); - AccountFactory factory; - switch (type) { - case 0: - factory = Accounts.ACCOUNT_FACTORY.get(Accounts.OFFLINE_ACCOUNT_KEY); - addtionalData = null; - break; - case 1: - factory = Accounts.ACCOUNT_FACTORY.get(Accounts.YGGDRASIL_ACCOUNT_KEY); - addtionalData = null; - break; - case 2: - factory = Accounts.ACCOUNT_FACTORY.get(Accounts.AUTHLIB_INJECTOR_ACCOUNT_KEY); - Optional server = Optional.ofNullable(cboServers.getSelectionModel().getSelectedItem()); - if (server.isPresent()) { - addtionalData = server.get(); - } else { - lblCreationWarning.setText(i18n("account.failed.no_selected_server")); - return; - } - break; - default: - throw new Error(); - } - acceptPane.showSpinner(); lblCreationWarning.setText(""); setDisable(true); - Task.ofResult("create_account", () -> factory.create(new Selector(), username, password, addtionalData)) + String username = txtUsername.getText(); + String password = txtPassword.getText(); + AccountFactory factory = cboType.getSelectionModel().getSelectedItem(); + Object additionalData = getAuthAdditionalData(); + + Task.ofResult("create_account", () -> factory.create(new Selector(), username, password, additionalData)) .finalized(Schedulers.javafx(), variables -> { Settings.INSTANCE.addAccount(variables.get("create_account")); acceptPane.hideSpinner(); diff --git a/HMCL/src/main/resources/assets/lang/I18N.properties b/HMCL/src/main/resources/assets/lang/I18N.properties index ca86a7bfd..9a69307a5 100644 --- a/HMCL/src/main/resources/assets/lang/I18N.properties +++ b/HMCL/src/main/resources/assets/lang/I18N.properties @@ -39,7 +39,6 @@ account.failed.invalid_credentials=Incorrect password, or you are forbidden to l account.failed.invalid_password=Invalid password account.failed.invalid_token=Please log out and re-input your password to login. account.failed.no_character=No character in this account. -account.failed.no_selected_server=No authentication server is selected. account.failed.connect_injector_server=Cannot connect to the authentication server. Check your network and ensure the URL is correct. account.injector.add=Add an authentication server account.injector.manage=Manage authentication servers diff --git a/HMCL/src/main/resources/assets/lang/I18N_zh.properties b/HMCL/src/main/resources/assets/lang/I18N_zh.properties index 706a9c5aa..7f8c2e010 100644 --- a/HMCL/src/main/resources/assets/lang/I18N_zh.properties +++ b/HMCL/src/main/resources/assets/lang/I18N_zh.properties @@ -39,7 +39,6 @@ account.failed.invalid_credentials=您的用戶名或密碼錯誤,或者登錄 account.failed.invalid_password=無效的密碼 account.failed.invalid_token=請嘗試登出並重新輸入密碼登錄 account.failed.no_character=該帳號沒有角色 -account.failed.no_selected_server=未選擇認證服務器 account.failed.connect_injector_server=無法連接認證服務器,可能是網絡故障或 URL 輸入錯誤 account.injector.add=添加認證服務器 account.injector.manage=管理認證服務器 diff --git a/HMCL/src/main/resources/assets/lang/I18N_zh_CN.properties b/HMCL/src/main/resources/assets/lang/I18N_zh_CN.properties index f183968d6..33957fee0 100644 --- a/HMCL/src/main/resources/assets/lang/I18N_zh_CN.properties +++ b/HMCL/src/main/resources/assets/lang/I18N_zh_CN.properties @@ -39,7 +39,6 @@ account.failed.invalid_credentials=您的用户名或密码错误,或者登录 account.failed.invalid_password=无效的密码 account.failed.invalid_token=请尝试登出并重新输入密码登录 account.failed.no_character=该帐号没有角色 -account.failed.no_selected_server=未选择认证服务器 account.failed.connect_injector_server=无法连接认证服务器,可能是网络故障或 URL 输入错误 account.injector.add=添加认证服务器 account.injector.manage=管理认证服务器