Add MappedProperty & reservedSelectedPropertyFor(CheckBox)

This commit is contained in:
yushijinhun 2018-10-03 17:09:12 +08:00
parent b8a84f831a
commit 2f522b744f
No known key found for this signature in database
GPG Key ID: 5BC167F73EA558E4
6 changed files with 109 additions and 11 deletions

View File

@ -46,7 +46,7 @@ import javafx.util.StringConverter;
import org.jackhuang.hmcl.util.*;
import org.jackhuang.hmcl.util.i18n.I18n;
import org.jackhuang.hmcl.util.io.FileUtils;
import org.jackhuang.hmcl.util.javafx.SelectedItemProperties;
import org.jackhuang.hmcl.util.javafx.ExtendedProperties;
import org.jackhuang.hmcl.util.platform.OperatingSystem;
import java.io.File;
@ -352,7 +352,7 @@ 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 SelectedItemProperties#selectedItemPropertyFor(ComboBox)}
* @deprecated Use {@link ExtendedProperties#selectedItemPropertyFor(ComboBox)}
*/
@SuppressWarnings("unchecked")
@Deprecated

View File

@ -47,7 +47,8 @@ import java.util.Optional;
import static org.jackhuang.hmcl.setting.ConfigHolder.config;
import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
import static org.jackhuang.hmcl.util.javafx.SelectedItemProperties.selectedItemPropertyFor;
import static org.jackhuang.hmcl.util.javafx.ExtendedProperties.reservedSelectedPropertyFor;
import static org.jackhuang.hmcl.util.javafx.ExtendedProperties.selectedItemPropertyFor;
public final class SettingsPage extends SettingsView implements DecoratorPage {
private final ReadOnlyStringWrapper title = new ReadOnlyStringWrapper(this, "title", i18n("settings.launcher"));
@ -90,9 +91,7 @@ public final class SettingsPage extends SettingsView implements DecoratorPage {
proxyPane.disableProperty().bind(chkDisableProxy.selectedProperty());
authPane.disableProperty().bind(chkProxyAuthentication.selectedProperty().not());
chkDisableProxy.setSelected(!config().hasProxy());
chkDisableProxy.selectedProperty().addListener(o -> config().setHasProxy(!chkDisableProxy.isSelected()));
config().hasProxyProperty().addListener(o -> chkDisableProxy.setSelected(!config().hasProxy()));
reservedSelectedPropertyFor(chkDisableProxy).bindBidirectional(config().hasProxyProperty());
chkProxyAuthentication.selectedProperty().bindBidirectional(config().hasProxyAuthProperty());
ToggleGroup proxyConfigurationGroup = new ToggleGroup();

View File

@ -25,7 +25,7 @@ import org.jackhuang.hmcl.ui.ListPage;
import org.jackhuang.hmcl.ui.decorator.DecoratorPage;
import org.jackhuang.hmcl.util.javafx.MappedObservableList;
import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
import static org.jackhuang.hmcl.util.javafx.SelectedItemProperties.createSelectedItemPropertyFor;
import static org.jackhuang.hmcl.util.javafx.ExtendedProperties.createSelectedItemPropertyFor;
public class AccountList extends ListPage<AccountListItem> implements DecoratorPage {
private final ReadOnlyStringWrapper title = new ReadOnlyStringWrapper(this, "title", i18n("account.manage"));

View File

@ -26,7 +26,7 @@ import org.jackhuang.hmcl.ui.decorator.DecoratorPage;
import org.jackhuang.hmcl.util.javafx.MappedObservableList;
import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
import static org.jackhuang.hmcl.util.javafx.SelectedItemProperties.createSelectedItemPropertyFor;
import static org.jackhuang.hmcl.util.javafx.ExtendedProperties.createSelectedItemPropertyFor;
public class ProfileList extends ListPage<ProfileListItem> implements DecoratorPage {
private final ReadOnlyStringWrapper title = new ReadOnlyStringWrapper(i18n("profile.manage"));

View File

@ -30,6 +30,7 @@ import javafx.beans.WeakInvalidationListener;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.Property;
import javafx.collections.ObservableList;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.SelectionModel;
import javafx.scene.control.Toggle;
@ -38,9 +39,9 @@ import javafx.scene.control.ToggleGroup;
/**
* @author yushijinhun
*/
public final class SelectedItemProperties {
public final class ExtendedProperties {
private static final String PROP_PREFIX = SelectedItemProperties.class.getName();
private static final String PROP_PREFIX = ExtendedProperties.class.getName();
// ==== ComboBox ====
@SuppressWarnings("unchecked")
@ -123,6 +124,16 @@ public final class SelectedItemProperties {
}
// ====
private SelectedItemProperties() {
// ==== CheckBox ====
@SuppressWarnings("unchecked")
public static ObjectProperty<Boolean> reservedSelectedPropertyFor(CheckBox checkbox) {
return (ObjectProperty<Boolean>) checkbox.getProperties().computeIfAbsent(
PROP_PREFIX + ".checkbox.reservedSelected",
any -> new MappedProperty<>(checkbox, "ext.reservedSelected",
checkbox.selectedProperty(), it -> !it, it -> !it));
}
// ====
private ExtendedProperties() {
}
}

View File

@ -0,0 +1,88 @@
/*
* 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.javafx;
import java.util.function.Function;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.ObjectBinding;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
/**
* @author yushijinhun
*/
public class MappedProperty<T, U> extends SimpleObjectProperty<U> {
private final Property<T> predecessor;
private final Function<U, T> reservedMapper;
private final ObjectBinding<U> binding;
public MappedProperty(Property<T> predecessor, Function<T, U> mapper, Function<U, T> reservedMapper) {
this(null, "", predecessor, mapper, reservedMapper);
}
public MappedProperty(Object bean, String name, Property<T> predecessor, Function<T, U> mapper, Function<U, T> reservedMapper) {
super(bean, name);
this.predecessor = predecessor;
this.reservedMapper = reservedMapper;
binding = new ObjectBinding<U>() {
{
bind(predecessor);
}
@Override
protected U computeValue() {
return mapper.apply(predecessor.getValue());
}
@Override
protected void onInvalidating() {
MappedProperty.this.fireValueChangedEvent();
}
};
}
@Override
public U get() {
return binding.get();
}
@Override
public void set(U value) {
predecessor.setValue(reservedMapper.apply(value));
}
@Override
public void bind(ObservableValue<? extends U> observable) {
predecessor.bind(Bindings.createObjectBinding(() -> reservedMapper.apply(observable.getValue()), observable));
}
@Override
public void unbind() {
predecessor.unbind();
}
@Override
public boolean isBound() {
return predecessor.isBound();
}
}