diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/Controllers.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/Controllers.java index 7edfd58db..bc4f297f5 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/Controllers.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/Controllers.java @@ -31,6 +31,7 @@ import org.jackhuang.hmcl.ui.construct.InputDialogPane; import org.jackhuang.hmcl.ui.construct.MessageBox; import org.jackhuang.hmcl.ui.construct.MessageDialogPane; import org.jackhuang.hmcl.ui.construct.TaskExecutorDialogPane; +import org.jackhuang.hmcl.util.FutureCallback; import org.jackhuang.hmcl.util.JavaVersion; import java.util.function.Consumer; @@ -140,8 +141,10 @@ public final class Controllers { dialog(new MessageDialogPane(text, title, onAccept, onCancel)); } - public static void inputDialog(String text, Consumer onResult) { - dialog(new InputDialogPane(text, onResult)); + public static InputDialogPane inputDialog(String text, FutureCallback onResult) { + InputDialogPane pane = new InputDialogPane(text, onResult); + dialog(pane); + return pane; } public static Region taskDialog(TaskExecutor executor, String title, String subtitle) { diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/VersionPage.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/VersionPage.java index ade98dc17..73f351997 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/VersionPage.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/VersionPage.java @@ -195,12 +195,15 @@ public final class VersionPage extends StackPane implements DecoratorPage { } public static void renameVersion(Profile profile, String version) { - Controllers.inputDialog(i18n("version.manage.rename.message"), res -> { + Controllers.inputDialog(i18n("version.manage.rename.message"), (res, resolve, reject) -> { if (profile.getRepository().renameVersion(version, res)) { profile.getRepository().refreshVersionsAsync().start(); Controllers.navigate(null); + resolve.run(); + } else { + reject.accept(i18n("version.manage.rename.fail")); } - }); + }).setInitialText(version); } public static void exportVersion(Profile profile, String version) { diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/InputDialogPane.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/InputDialogPane.java index 8e7dfe365..b7699e98f 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/InputDialogPane.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/InputDialogPane.java @@ -19,12 +19,12 @@ package org.jackhuang.hmcl.ui.construct; import com.jfoenix.controls.JFXButton; import com.jfoenix.controls.JFXTextField; +import javafx.beans.binding.Bindings; import javafx.fxml.FXML; import javafx.scene.control.Label; import javafx.scene.layout.StackPane; import org.jackhuang.hmcl.ui.FXUtils; - -import java.util.function.Consumer; +import org.jackhuang.hmcl.util.FutureCallback; public class InputDialogPane extends StackPane { @@ -36,14 +36,28 @@ public class InputDialogPane extends StackPane { private JFXTextField textField; @FXML private Label content; + @FXML + private Label lblCreationWarning; - public InputDialogPane(String text, Consumer onResult) { + public InputDialogPane(String text, FutureCallback onResult) { FXUtils.loadFXML(this, "/assets/fxml/input-dialog.fxml"); content.setText(text); cancelButton.setOnMouseClicked(e -> fireEvent(new DialogCloseEvent())); acceptButton.setOnMouseClicked(e -> { - onResult.accept(textField.getText()); - fireEvent(new DialogCloseEvent()); + onResult.call(textField.getText(), () -> { + fireEvent(new DialogCloseEvent()); + }, msg -> { + lblCreationWarning.setText(msg); + }); }); + + acceptButton.disableProperty().bind(Bindings.createBooleanBinding( + () -> !textField.validate(), + textField.textProperty() + )); + } + + public void setInitialText(String text) { + textField.setText(text); } } diff --git a/HMCL/src/main/resources/assets/fxml/input-dialog.fxml b/HMCL/src/main/resources/assets/fxml/input-dialog.fxml index fd7cf7aaa..3cd8215bc 100644 --- a/HMCL/src/main/resources/assets/fxml/input-dialog.fxml +++ b/HMCL/src/main/resources/assets/fxml/input-dialog.fxml @@ -5,6 +5,7 @@ + @@ -16,8 +17,11 @@ + diff --git a/HMCL/src/main/resources/assets/lang/I18N.properties b/HMCL/src/main/resources/assets/lang/I18N.properties index 290cf8f86..4ccdd815f 100644 --- a/HMCL/src/main/resources/assets/lang/I18N.properties +++ b/HMCL/src/main/resources/assets/lang/I18N.properties @@ -325,11 +325,12 @@ version.launch_script.failed=Unable to make launch script. version.launch_script.save=Save the launch script version.launch_script.success=Finished script creation, %s. version.manage.redownload_assets_index=Redownload Assets Index -version.manage.remove=Delete this version +version.manage.remove=Delete this game version.manage.remove.confirm=Sure to remove version %s? version.manage.remove_libraries=Delete library files -version.manage.rename=Rename this version +version.manage.rename=Rename this game version.manage.rename.message=Please enter the new name +version.manage.rename.fail=Failed to rename this game. version.settings=Settings version.update=Update diff --git a/HMCL/src/main/resources/assets/lang/I18N_zh.properties b/HMCL/src/main/resources/assets/lang/I18N_zh.properties index 403fd3fb3..2d480d78f 100644 --- a/HMCL/src/main/resources/assets/lang/I18N_zh.properties +++ b/HMCL/src/main/resources/assets/lang/I18N_zh.properties @@ -330,6 +330,7 @@ version.manage.remove.confirm=真的要刪除版本 %s 嗎? version.manage.remove_libraries=刪除所有庫文件 version.manage.rename=重命名該版本 version.manage.rename.message=請輸入要改成的名字 +version.manage.rename.fail=重命名版本失敗,可能文件被佔用或者名字有特殊字符 version.settings=遊戲設置 version.update=更新 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 86e033a04..fe5751a81 100644 --- a/HMCL/src/main/resources/assets/lang/I18N_zh_CN.properties +++ b/HMCL/src/main/resources/assets/lang/I18N_zh_CN.properties @@ -330,6 +330,7 @@ version.manage.remove.confirm=真的要删除版本 %s 吗? version.manage.remove_libraries=删除所有库文件 version.manage.rename=重命名该版本 version.manage.rename.message=请输入要改成的名字 +version.manage.rename.fail=重命名版本失败,可能文件被占用或者名字有特殊字符 version.settings=游戏设置 version.update=更新 diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/FutureCallback.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/FutureCallback.java new file mode 100644 index 000000000..e26950f72 --- /dev/null +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/FutureCallback.java @@ -0,0 +1,25 @@ +/* + * Hello Minecraft! Launcher. + * Copyright (C) 2017 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.util; + +import java.util.function.Consumer; + +@FunctionalInterface +public interface FutureCallback { + void call(T obj, Runnable resolve, Consumer reject); +}