Forbid adding profiles with already existent name

This commit is contained in:
huanghongxun 2018-10-05 23:04:56 +08:00
parent 6b3152c4b5
commit 08be1896ee
6 changed files with 25 additions and 3 deletions

View File

@ -24,6 +24,7 @@ import javafx.beans.Observable;
import javafx.beans.property.*;
import org.jackhuang.hmcl.download.DefaultDependencyManager;
import org.jackhuang.hmcl.event.EventBus;
import org.jackhuang.hmcl.event.EventPriority;
import org.jackhuang.hmcl.event.RefreshedVersionsEvent;
import org.jackhuang.hmcl.game.HMCLCacheRepository;
import org.jackhuang.hmcl.game.HMCLGameRepository;
@ -139,7 +140,7 @@ public final class Profile implements Observable {
gameDir.addListener((a, b, newValue) -> repository.changeDirectory(newValue));
this.selectedVersion.addListener(o -> checkSelectedVersion());
listenerHolder.add(EventBus.EVENT_BUS.channel(RefreshedVersionsEvent.class).registerWeak(event -> checkSelectedVersion()));
listenerHolder.add(EventBus.EVENT_BUS.channel(RefreshedVersionsEvent.class).registerWeak(event -> checkSelectedVersion(), EventPriority.HIGHEST));
addPropertyChangedListener(onInvalidating(this::invalidate));
}

View File

@ -20,11 +20,13 @@ package org.jackhuang.hmcl.ui.profile;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXCheckBox;
import com.jfoenix.controls.JFXTextField;
import com.jfoenix.validation.base.ValidatorBase;
import javafx.beans.property.ReadOnlyStringProperty;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.layout.StackPane;
import org.jackhuang.hmcl.setting.Profile;
import org.jackhuang.hmcl.setting.Profiles;
@ -66,6 +68,19 @@ public final class ProfilePage extends StackPane implements DecoratorPage {
FXUtils.loadFXML(this, "/assets/fxml/profile.fxml");
txtProfileName.setText(profileDisplayName);
txtProfileName.getValidators().add(new ValidatorBase() {
{
setMessage(i18n("profile.already_exists"));
}
@Override
protected void eval() {
JFXTextField control = (JFXTextField) this.getSrcControl();
if (Profiles.getProfiles().stream().anyMatch(profile -> profile.getName().equals(control.getText())))
hasErrors.set(true);
else
hasErrors.set(false);
}
});
FXUtils.onChangeAndOperate(txtProfileName.textProperty(), it -> {
btnSave.setDisable(!txtProfileName.validate() || StringUtils.isBlank(getLocation()));
});

View File

@ -20,14 +20,17 @@
<BorderPane> <!-- Name -->
<left>
<VBox>
<VBox alignment="CENTER_LEFT">
<Label text="%profile.name" BorderPane.alignment="CENTER_LEFT"/>
</VBox>
</left>
<right>
<JFXTextField fx:id="txtProfileName" BorderPane.alignment="CENTER_RIGHT">
<BorderPane.margin>
<Insets top="8" bottom="8" />
</BorderPane.margin>
<validators>
<RequiredFieldValidator/>
<RequiredFieldValidator message="%input.not_empty" />
</validators>
</JFXTextField>
</right>

View File

@ -268,6 +268,7 @@ world.name.enter=Enter the world name
world.time=EEE, MMM d, yyyy HH:mm:ss
profile=Game Directories
profile.already_exists=This name already exists, please consider another name.
profile.default=Current directory
profile.home=User home
profile.instance_directory=Game Directory

View File

@ -268,6 +268,7 @@ world.name.enter=輸入世界名稱
world.time=yyyy 年 MM 月 dd 日 HH:mm:ss
profile=遊戲目錄
profile.already_exists=該名稱已存在
profile.default=目前目錄
profile.home=官方啟動器
profile.instance_directory=遊戲路徑

View File

@ -268,6 +268,7 @@ world.name.enter=输入世界名称
world.time=yyyy 年 MM 月 dd 日 HH:mm:ss
profile=游戏目录
profile.already_exists=该名称已存在
profile.default=当前目录
profile.home=官方启动器
profile.instance_directory=游戏路径