Add AutomatedToggleGroup & Refactor AccountList
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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 javafx.collections.ListChangeListener;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.scene.control.Toggle;
|
||||
import javafx.scene.control.ToggleGroup;
|
||||
|
||||
/**
|
||||
* @author yushijinhun
|
||||
*/
|
||||
public class AutomatedToggleGroup extends ToggleGroup {
|
||||
|
||||
private final ObservableList<? extends Toggle> toggles;
|
||||
private final ListChangeListener<Toggle> listListener;
|
||||
|
||||
public AutomatedToggleGroup(ObservableList<? extends Toggle> toggles) {
|
||||
this.toggles = toggles;
|
||||
|
||||
listListener = change -> {
|
||||
while (change.next()) {
|
||||
change.getRemoved().forEach(it -> it.setToggleGroup(null));
|
||||
change.getAddedSubList().forEach(it -> it.setToggleGroup(this));
|
||||
}
|
||||
};
|
||||
toggles.addListener(listListener);
|
||||
|
||||
toggles.forEach(it -> it.setToggleGroup(this));
|
||||
}
|
||||
|
||||
public void disconnect() {
|
||||
toggles.removeListener(listListener);
|
||||
toggles.forEach(it -> it.setToggleGroup(null));
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,7 @@ import javafx.beans.InvalidationListener;
|
||||
import javafx.beans.WeakInvalidationListener;
|
||||
import javafx.beans.property.ObjectProperty;
|
||||
import javafx.beans.property.Property;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.scene.control.ComboBox;
|
||||
import javafx.scene.control.SelectionModel;
|
||||
import javafx.scene.control.Toggle;
|
||||
@@ -41,6 +42,7 @@ public final class SelectedItemProperties {
|
||||
|
||||
private static final String PROP_PREFIX = SelectedItemProperties.class.getName();
|
||||
|
||||
// ==== ComboBox ====
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> ObjectProperty<T> selectedItemPropertyFor(ComboBox<T> comboBox) {
|
||||
return (ObjectProperty<T>) comboBox.getProperties().computeIfAbsent(
|
||||
@@ -54,7 +56,9 @@ public final class SelectedItemProperties {
|
||||
.flatMap(SelectionModel::selectedItemProperty),
|
||||
modelProperty.getValue()::select);
|
||||
}
|
||||
// ====
|
||||
|
||||
// ==== Toggle ====
|
||||
@SuppressWarnings("unchecked")
|
||||
public static ObjectProperty<Toggle> selectedTogglePropertyFor(ToggleGroup toggleGroup) {
|
||||
return (ObjectProperty<Toggle>) toggleGroup.getProperties().computeIfAbsent(
|
||||
@@ -68,6 +72,10 @@ public final class SelectedItemProperties {
|
||||
toggleGroup::selectToggle);
|
||||
}
|
||||
|
||||
public static <T> ObjectProperty<T> createSelectedItemPropertyFor(ObservableList<? extends Toggle> items, Class<T> userdataType) {
|
||||
return selectedItemPropertyFor(new AutomatedToggleGroup(items), userdataType);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> ObjectProperty<T> selectedItemPropertyFor(ToggleGroup toggleGroup, Class<T> userdataType) {
|
||||
return (ObjectProperty<T>) toggleGroup.getProperties().computeIfAbsent(
|
||||
@@ -113,6 +121,7 @@ public final class SelectedItemProperties {
|
||||
|
||||
return property;
|
||||
}
|
||||
// ====
|
||||
|
||||
private SelectedItemProperties() {
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user