From 68fdfb36fd6214d44ea6773dc560133d0d9f4616 Mon Sep 17 00:00:00 2001 From: yushijinhun Date: Sun, 16 Sep 2018 00:52:03 +0800 Subject: [PATCH] Add selectedItemPropertyFor(ComboBox) --- .../java/org/jackhuang/hmcl/ui/FXUtils.java | 3 ++ .../org/jackhuang/hmcl/ui/SettingsPage.java | 18 +++----- .../org/jackhuang/hmcl/ui/SettingsView.java | 11 +++-- .../SelectionModelSelectedItemProperty.java | 42 +++++++++++++++++++ 4 files changed, 56 insertions(+), 18 deletions(-) create mode 100644 HMCLCore/src/main/java/org/jackhuang/hmcl/util/SelectionModelSelectedItemProperty.java diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/FXUtils.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/FXUtils.java index 19b96d32e..bc50bff89 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/FXUtils.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/FXUtils.java @@ -325,8 +325,10 @@ public final class FXUtils { * @param comboBox the combo box being bound with {@code property}. * @param property the property being bound with {@code combo box}. * @see #unbindEnum(JFXComboBox) + * @deprecated Use {@link SelectionModelSelectedItemProperty#selectedItemPropertyFor(ComboBox)} */ @SuppressWarnings("unchecked") + @Deprecated public static void bindEnum(JFXComboBox comboBox, Property property) { unbindEnum(comboBox); ChangeListener listener = (a, b, newValue) -> @@ -343,6 +345,7 @@ public final class FXUtils { * @see #bindEnum(JFXComboBox, Property) */ @SuppressWarnings("unchecked") + @Deprecated public static void unbindEnum(JFXComboBox comboBox) { ChangeListener listener = tryCast(comboBox.getProperties().get("FXUtils.bindEnum.listener"), ChangeListener.class).orElse(null); if (listener == null) return; diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/SettingsPage.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/SettingsPage.java index 363774178..ffa339cf5 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/SettingsPage.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/SettingsPage.java @@ -25,9 +25,6 @@ import javafx.beans.WeakInvalidationListener; import javafx.beans.binding.Bindings; import javafx.beans.binding.When; import javafx.beans.property.*; -import javafx.collections.FXCollections; -import javafx.collections.ObservableList; -import javafx.scene.control.Label; import javafx.scene.control.ToggleGroup; import javafx.scene.paint.Color; import javafx.scene.text.Font; @@ -48,6 +45,7 @@ import java.util.Objects; import java.util.Optional; import static org.jackhuang.hmcl.setting.ConfigHolder.config; +import static org.jackhuang.hmcl.util.SelectionModelSelectedItemProperty.selectedItemPropertyFor; import static org.jackhuang.hmcl.util.i18n.I18n.i18n; public final class SettingsPage extends SettingsView implements DecoratorPage { @@ -62,8 +60,7 @@ public final class SettingsPage extends SettingsView implements DecoratorPage { // ==== Download sources ==== cboDownloadSource.getItems().setAll(DownloadProviders.providersById.keySet()); - cboDownloadSource.getSelectionModel().select(config().getDownloadType()); - cboDownloadSource.getSelectionModel().selectedItemProperty().addListener((a, b, newValue) -> config().setDownloadType(newValue)); + selectedItemPropertyFor(cboDownloadSource).bindBidirectional(config().downloadTypeProperty()); // ==== chkEnableGameList.selectedProperty().bindBidirectional(config().enableMainPageGameListProperty()); @@ -87,13 +84,10 @@ public final class SettingsPage extends SettingsView implements DecoratorPage { lblDisplay.setStyle("-fx-font: " + Settings.instance().getFont().getSize() + " \"" + Settings.instance().getFont().getFamily() + "\";"); - ObservableList