From d71815aa3db9ceb9126513e13475b9fe458ebec6 Mon Sep 17 00:00:00 2001 From: Glavo Date: Fri, 24 Feb 2023 04:14:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B8=85=E7=90=86=E7=BC=96=E8=AF=91=E8=AD=A6?= =?UTF-8?q?=E5=91=8A=20(#2148)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/jackhuang/hmcl/ui/FXUtils.java | 31 +++++++++---------- .../versions/AdvancedVersionSettingPage.java | 1 - 2 files changed, 15 insertions(+), 17 deletions(-) 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 9145b4d54..67a7c7012 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/FXUtils.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/FXUtils.java @@ -625,16 +625,17 @@ 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 ExtendedProperties#selectedItemPropertyFor(ComboBox)} + * @see ExtendedProperties#selectedItemPropertyFor(ComboBox) */ - @SuppressWarnings("unchecked") - @Deprecated - public static void bindEnum(JFXComboBox comboBox, Property> property) { + public static > void bindEnum(JFXComboBox comboBox, Property property) { unbindEnum(comboBox); - @SuppressWarnings("rawtypes") - ChangeListener listener = (a, b, newValue) -> - ((Property) property).setValue(property.getValue().getClass().getEnumConstants()[newValue.intValue()]); - comboBox.getSelectionModel().select(property.getValue().ordinal()); + + T currentValue = property.getValue(); + @SuppressWarnings("unchecked") + T[] enumConstants = (T[]) currentValue.getClass().getEnumConstants(); + ChangeListener listener = (a, b, newValue) -> property.setValue(enumConstants[newValue.intValue()]); + + comboBox.getSelectionModel().select(currentValue.ordinal()); comboBox.getProperties().put("FXUtils.bindEnum.listener", listener); comboBox.getSelectionModel().selectedIndexProperty().addListener(listener); } @@ -645,15 +646,13 @@ public final class FXUtils { * * @param comboBox the combo box being bound with the property which can be inferred by {@code bindEnum}. * @see #bindEnum(JFXComboBox, Property) - * @deprecated Use {@link ExtendedProperties#selectedItemPropertyFor(ComboBox)} + * @see ExtendedProperties#selectedItemPropertyFor(ComboBox) */ - @SuppressWarnings("unchecked") - @Deprecated - public static void unbindEnum(JFXComboBox comboBox) { - @SuppressWarnings("rawtypes") - ChangeListener listener = tryCast(comboBox.getProperties().get("FXUtils.bindEnum.listener"), ChangeListener.class).orElse(null); - if (listener == null) return; - comboBox.getSelectionModel().selectedIndexProperty().removeListener(listener); + public static void unbindEnum(JFXComboBox> comboBox) { + @SuppressWarnings("unchecked") + ChangeListener listener = (ChangeListener) comboBox.getProperties().remove("FXUtils.bindEnum.listener"); + if (listener != null) + comboBox.getSelectionModel().selectedIndexProperty().removeListener(listener); } /** diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/AdvancedVersionSettingPage.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/AdvancedVersionSettingPage.java index 9f9754661..ea60bff1c 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/AdvancedVersionSettingPage.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/AdvancedVersionSettingPage.java @@ -204,7 +204,6 @@ public final class AdvancedVersionSettingPage extends StackPane implements Decor bindProperties(); } - @SuppressWarnings("deprecation") void bindProperties() { nativesDirCustomOption.bindBidirectional(versionSetting.nativesDirProperty()); FXUtils.bindString(txtJVMArgs, versionSetting.javaArgsProperty());