Add selectedItemPropertyFor(ComboBox)

This commit is contained in:
yushijinhun
2018-09-16 00:52:03 +08:00
parent 09287f9c72
commit 68fdfb36fd
4 changed files with 56 additions and 18 deletions

View File

@@ -325,8 +325,10 @@ public final class FXUtils {
* @param comboBox the combo box being bound with {@code property}. * @param comboBox the combo box being bound with {@code property}.
* @param property the property being bound with {@code combo box}. * @param property the property being bound with {@code combo box}.
* @see #unbindEnum(JFXComboBox) * @see #unbindEnum(JFXComboBox)
* @deprecated Use {@link SelectionModelSelectedItemProperty#selectedItemPropertyFor(ComboBox)}
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Deprecated
public static void bindEnum(JFXComboBox<?> comboBox, Property<? extends Enum> property) { public static void bindEnum(JFXComboBox<?> comboBox, Property<? extends Enum> property) {
unbindEnum(comboBox); unbindEnum(comboBox);
ChangeListener<Number> listener = (a, b, newValue) -> ChangeListener<Number> listener = (a, b, newValue) ->
@@ -343,6 +345,7 @@ public final class FXUtils {
* @see #bindEnum(JFXComboBox, Property) * @see #bindEnum(JFXComboBox, Property)
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Deprecated
public static void unbindEnum(JFXComboBox<?> comboBox) { public static void unbindEnum(JFXComboBox<?> comboBox) {
ChangeListener listener = tryCast(comboBox.getProperties().get("FXUtils.bindEnum.listener"), ChangeListener.class).orElse(null); ChangeListener listener = tryCast(comboBox.getProperties().get("FXUtils.bindEnum.listener"), ChangeListener.class).orElse(null);
if (listener == null) return; if (listener == null) return;

View File

@@ -25,9 +25,6 @@ import javafx.beans.WeakInvalidationListener;
import javafx.beans.binding.Bindings; import javafx.beans.binding.Bindings;
import javafx.beans.binding.When; import javafx.beans.binding.When;
import javafx.beans.property.*; 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.control.ToggleGroup;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import javafx.scene.text.Font; import javafx.scene.text.Font;
@@ -48,6 +45,7 @@ import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import static org.jackhuang.hmcl.setting.ConfigHolder.config; 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; import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
public final class SettingsPage extends SettingsView implements DecoratorPage { public final class SettingsPage extends SettingsView implements DecoratorPage {
@@ -62,8 +60,7 @@ public final class SettingsPage extends SettingsView implements DecoratorPage {
// ==== Download sources ==== // ==== Download sources ====
cboDownloadSource.getItems().setAll(DownloadProviders.providersById.keySet()); cboDownloadSource.getItems().setAll(DownloadProviders.providersById.keySet());
cboDownloadSource.getSelectionModel().select(config().getDownloadType()); selectedItemPropertyFor(cboDownloadSource).bindBidirectional(config().downloadTypeProperty());
cboDownloadSource.getSelectionModel().selectedItemProperty().addListener((a, b, newValue) -> config().setDownloadType(newValue));
// ==== // ====
chkEnableGameList.selectedProperty().bindBidirectional(config().enableMainPageGameListProperty()); 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() + "\";"); lblDisplay.setStyle("-fx-font: " + Settings.instance().getFont().getSize() + " \"" + Settings.instance().getFont().getFamily() + "\";");
ObservableList<Label> list = FXCollections.observableArrayList(); // ==== Languages ====
for (Locales.SupportedLocale locale : Locales.LOCALES) cboLanguage.getItems().setAll(Locales.LOCALES);
list.add(new Label(locale.getName(config().getLocalization().getResourceBundle()))); selectedItemPropertyFor(cboLanguage).bindBidirectional(config().localizationProperty());
// ====
cboLanguage.setItems(list);
cboLanguage.getSelectionModel().select(Locales.LOCALES.indexOf(config().getLocalization()));
cboLanguage.getSelectionModel().selectedIndexProperty().addListener((a, b, newValue) -> config().setLocalization(Locales.getLocale(newValue.intValue())));
// ==== Proxy ==== // ==== Proxy ====
txtProxyHost.textProperty().bindBidirectional(config().proxyHostProperty()); txtProxyHost.textProperty().bindBidirectional(config().proxyHostProperty());

View File

@@ -27,15 +27,15 @@ import javafx.scene.layout.*;
import javafx.scene.text.Text; import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment; import javafx.scene.text.TextAlignment;
import static org.jackhuang.hmcl.setting.ConfigHolder.config;
import static org.jackhuang.hmcl.ui.FXUtils.stringConverter; import static org.jackhuang.hmcl.ui.FXUtils.stringConverter;
import java.util.function.Function;
import org.jackhuang.hmcl.setting.EnumBackgroundImage; import org.jackhuang.hmcl.setting.EnumBackgroundImage;
import org.jackhuang.hmcl.setting.EnumCommonDirectory; import org.jackhuang.hmcl.setting.EnumCommonDirectory;
import org.jackhuang.hmcl.setting.Theme; import org.jackhuang.hmcl.setting.Theme;
import org.jackhuang.hmcl.ui.construct.*; import org.jackhuang.hmcl.ui.construct.*;
import org.jackhuang.hmcl.util.i18n.I18n; import org.jackhuang.hmcl.util.i18n.I18n;
import org.jackhuang.hmcl.util.i18n.Locales.SupportedLocale;
public abstract class SettingsView extends StackPane { public abstract class SettingsView extends StackPane {
protected final JFXTextField txtProxyHost; protected final JFXTextField txtProxyHost;
@@ -43,7 +43,7 @@ public abstract class SettingsView extends StackPane {
protected final JFXTextField txtProxyUsername; protected final JFXTextField txtProxyUsername;
protected final JFXPasswordField txtProxyPassword; protected final JFXPasswordField txtProxyPassword;
protected final JFXTextField txtFontSize; protected final JFXTextField txtFontSize;
protected final JFXComboBox<Label> cboLanguage; protected final JFXComboBox<SupportedLocale> cboLanguage;
protected final JFXComboBox<String> cboDownloadSource; protected final JFXComboBox<String> cboDownloadSource;
protected final FontComboBox cboFont; protected final FontComboBox cboFont;
protected final MultiFileItem<EnumCommonDirectory> fileCommonLocation; protected final MultiFileItem<EnumCommonDirectory> fileCommonLocation;
@@ -167,9 +167,7 @@ public abstract class SettingsView extends StackPane {
{ {
cboDownloadSource = new JFXComboBox<>(); cboDownloadSource = new JFXComboBox<>();
Function<String, String> nameConverter = key -> I18n.i18n("download.provider." + key); cboDownloadSource.setConverter(stringConverter(key -> I18n.i18n("download.provider." + key)));
cboDownloadSource.setCellFactory(FXUtils.jfxListCellFactory(nameConverter.andThen(Label::new)));
cboDownloadSource.setConverter(stringConverter(nameConverter));
downloadSourcePane.setRight(cboDownloadSource); downloadSourcePane.setRight(cboDownloadSource);
} }
settingsPane.getContent().add(downloadSourcePane); settingsPane.getContent().add(downloadSourcePane);
@@ -183,6 +181,7 @@ public abstract class SettingsView extends StackPane {
languagePane.setLeft(left); languagePane.setLeft(left);
cboLanguage = new JFXComboBox<>(); cboLanguage = new JFXComboBox<>();
cboLanguage.setConverter(stringConverter(locale -> locale.getName(config().getLocalization().getResourceBundle())));
FXUtils.setLimitWidth(cboLanguage, 400); FXUtils.setLimitWidth(cboLanguage, 400);
languagePane.setRight(cboLanguage); languagePane.setRight(cboLanguage);

View File

@@ -0,0 +1,42 @@
/*
* Hello Minecraft! Launcher.
* Copyright (C) 2018 huangyuhui <huanghongxun2008@126.com>
*
* 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 javafx.beans.property.SimpleObjectProperty;
import javafx.scene.control.ComboBox;
import javafx.scene.control.SelectionModel;
public class SelectionModelSelectedItemProperty<T> extends SimpleObjectProperty<T> {
public static <T> SelectionModelSelectedItemProperty<T> selectedItemPropertyFor(ComboBox<T> comboBox) {
return new SelectionModelSelectedItemProperty<>(comboBox.getSelectionModel());
}
private SelectionModel<T> model;
public SelectionModelSelectedItemProperty(SelectionModel<T> model) {
this.model = model;
model.selectedItemProperty().addListener((observable, oldValue, newValue) -> set(newValue));
set(model.getSelectedItem());
}
@Override
protected void invalidated() {
model.select(get());
}
}