AddAccountPane中服务器列表数据视图分离

This commit is contained in:
yushijinhun
2018-06-16 22:58:39 +08:00
parent 76a259c107
commit 7673595a43
2 changed files with 44 additions and 13 deletions

View File

@@ -33,6 +33,7 @@ import javafx.scene.layout.StackPane;
import org.jackhuang.hmcl.Launcher; import org.jackhuang.hmcl.Launcher;
import org.jackhuang.hmcl.auth.*; import org.jackhuang.hmcl.auth.*;
import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorAccount; import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorAccount;
import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorServer;
import org.jackhuang.hmcl.auth.offline.OfflineAccount; import org.jackhuang.hmcl.auth.offline.OfflineAccount;
import org.jackhuang.hmcl.auth.yggdrasil.GameProfile; import org.jackhuang.hmcl.auth.yggdrasil.GameProfile;
import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilAccount; import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilAccount;
@@ -49,7 +50,8 @@ import org.jackhuang.hmcl.ui.construct.Validator;
import org.jackhuang.hmcl.util.Constants; import org.jackhuang.hmcl.util.Constants;
import org.jackhuang.hmcl.util.Logging; import org.jackhuang.hmcl.util.Logging;
import static java.util.stream.Collectors.toList; import static org.jackhuang.hmcl.ui.FXUtils.jfxListCellFactory;
import static org.jackhuang.hmcl.ui.FXUtils.stringConverter;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@@ -64,7 +66,7 @@ public class AddAccountPane extends StackPane {
@FXML private Label lblCreationWarning; @FXML private Label lblCreationWarning;
@FXML private Label lblPassword; @FXML private Label lblPassword;
@FXML private JFXComboBox<String> cboType; @FXML private JFXComboBox<String> cboType;
@FXML private JFXComboBox<TwoLineListItem> cboServers; @FXML private JFXComboBox<AuthlibInjectorServer> cboServers;
@FXML private Label lblInjectorServer; @FXML private Label lblInjectorServer;
@FXML private Hyperlink linkManageInjectorServers; @FXML private Hyperlink linkManageInjectorServers;
@FXML private JFXDialogLayout layout; @FXML private JFXDialogLayout layout;
@@ -82,7 +84,13 @@ public class AddAccountPane extends StackPane {
transitionHandler = new TransitionHandler(acceptPane); transitionHandler = new TransitionHandler(acceptPane);
acceptPane.getChildren().setAll(btnAccept); acceptPane.getChildren().setAll(btnAccept);
loadServers(); cboServers.setCellFactory(jfxListCellFactory(server -> new TwoLineListItem(server.getName(), server.getUrl())));
cboServers.setConverter(stringConverter(AuthlibInjectorServer::getName));
cboServers.setItems(Settings.INSTANCE.SETTINGS.authlibInjectorServers);
// workaround: otherwise the combox will be black
if (!cboServers.getItems().isEmpty())
cboServers.getSelectionModel().select(0);
cboType.getItems().setAll(Launcher.i18n("account.methods.offline"), Launcher.i18n("account.methods.yggdrasil"), Launcher.i18n("account.methods.authlib_injector")); cboType.getItems().setAll(Launcher.i18n("account.methods.offline"), Launcher.i18n("account.methods.yggdrasil"), Launcher.i18n("account.methods.authlib_injector"));
cboType.getSelectionModel().selectedIndexProperty().addListener((a, b, newValue) -> { cboType.getSelectionModel().selectedIndexProperty().addListener((a, b, newValue) -> {
@@ -107,15 +115,6 @@ public class AddAccountPane extends StackPane {
btnAccept.setDisable(!txtUsername.validate() || (cboType.getSelectionModel().getSelectedIndex() != 0 && !txtPassword.validate())); btnAccept.setDisable(!txtUsername.validate() || (cboType.getSelectionModel().getSelectedIndex() != 0 && !txtPassword.validate()));
} }
private void loadServers() {
cboServers.getItems().setAll(
Settings.INSTANCE.SETTINGS.authlibInjectorServers.stream()
.map(server -> new TwoLineListItem(server.getName(), server.getUrl()))
.collect(toList()));
if (!cboServers.getItems().isEmpty())
cboServers.getSelectionModel().select(0);
}
private void showSpinner() { private void showSpinner() {
transitionHandler.setContent(spinnerAccept, ContainerAnimations.FADE.getAnimationProducer()); transitionHandler.setContent(spinnerAccept, ContainerAnimations.FADE.getAnimationProducer());
} }
@@ -129,7 +128,7 @@ public class AddAccountPane extends StackPane {
int type = cboType.getSelectionModel().getSelectedIndex(); int type = cboType.getSelectionModel().getSelectedIndex();
String username = txtUsername.getText(); String username = txtUsername.getText();
String password = txtPassword.getText(); String password = txtPassword.getText();
String apiRoot = Optional.ofNullable(cboServers.getSelectionModel().getSelectedItem()).map(TwoLineListItem::getSubtitle).orElse(null); String apiRoot = Optional.ofNullable(cboServers.getSelectionModel().getSelectedItem()).map(AuthlibInjectorServer::getUrl).orElse(null);
showSpinner(); showSpinner();
lblCreationWarning.setText(""); lblCreationWarning.setText("");
Task.ofResult("create_account", () -> { Task.ofResult("create_account", () -> {

View File

@@ -31,6 +31,7 @@ import javafx.beans.value.WeakChangeListener;
import javafx.event.EventHandler; import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.scene.Node; import javafx.scene.Node;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView; import javafx.scene.control.ListView;
import javafx.scene.control.ScrollBar; import javafx.scene.control.ScrollBar;
import javafx.scene.control.ScrollPane; import javafx.scene.control.ScrollPane;
@@ -40,7 +41,10 @@ import javafx.scene.input.MouseEvent;
import javafx.scene.input.ScrollEvent; import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.Region; import javafx.scene.layout.Region;
import javafx.scene.shape.Rectangle; import javafx.scene.shape.Rectangle;
import javafx.util.Callback;
import javafx.util.Duration; import javafx.util.Duration;
import javafx.util.StringConverter;
import org.jackhuang.hmcl.Launcher; import org.jackhuang.hmcl.Launcher;
import org.jackhuang.hmcl.util.*; import org.jackhuang.hmcl.util.*;
@@ -52,6 +56,7 @@ import java.net.URI;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.logging.Level; import java.util.logging.Level;
@@ -406,6 +411,33 @@ public final class FXUtils {
} }
} }
public static <T> StringConverter<T> stringConverter(Function<T, String> func) {
return new StringConverter<T>() {
@Override
public String toString(T object) {
return object == null ? "" : func.apply(object);
}
@Override
public T fromString(String string) {
throw new UnsupportedOperationException();
}
};
}
public static <T> Callback<ListView<T>, ListCell<T>> jfxListCellFactory(Function<T, Node> graphicBuilder) {
return view -> new JFXListCell<T>() {
@Override
public void updateItem(T item, boolean empty) {
super.updateItem(item, empty);
if (!empty) {
setGraphic(graphicBuilder.apply(item));
}
}
};
}
public static final Interpolator SINE = new Interpolator() { public static final Interpolator SINE = new Interpolator() {
@Override @Override
protected double curve(double t) { protected double curve(double t) {