Skip to content

Commit

Permalink
Move references.
Browse files Browse the repository at this point in the history
  • Loading branch information
burningtnt committed May 6, 2024
1 parent dd05c9d commit e1db5e8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import org.jackhuang.hmcl.ui.construct.MessageDialogPane;
import org.jackhuang.hmcl.ui.construct.PageCloseEvent;
import org.jackhuang.hmcl.ui.decorator.DecoratorPage;
import org.jackhuang.hmcl.util.AllSelectPolicy;
import org.jackhuang.hmcl.util.javafx.SelectionBinding;
import org.jackhuang.hmcl.util.Pair;
import org.jackhuang.hmcl.util.TaskCancellationAction;
import org.jackhuang.hmcl.util.io.CSVTable;
Expand All @@ -67,7 +67,7 @@ public class ModUpdatesPage extends BorderPane implements DecoratorPage {

private final ModManager modManager;
private final ObservableList<ModUpdateObject> objects;
private final AllSelectPolicy allEnabled; // Keep a reference.
private final SelectionBinding allEnabled; // Keep a reference.

@SuppressWarnings("unchecked")
public ModUpdatesPage(ModManager modManager, List<LocalModFile.ModUpdate> updates) {
Expand Down Expand Up @@ -101,7 +101,7 @@ public ModUpdatesPage(ModManager modManager, List<LocalModFile.ModUpdate> update

objects = FXCollections.observableList(updates.stream().map(ModUpdateObject::new).collect(Collectors.toList()));

allEnabled = new AllSelectPolicy(allEnabledBox.selectedProperty(), objects.stream().map(o -> o.enabled).collect(Collectors.toList()));
allEnabled = new SelectionBinding(allEnabledBox.selectedProperty(), objects.stream().map(o -> o.enabled).collect(Collectors.toList()));

TableView<ModUpdateObject> table = new TableView<>(objects);
table.setEditable(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.jackhuang.hmcl.util;
package org.jackhuang.hmcl.util.javafx;

import javafx.beans.property.BooleanProperty;
import org.jackhuang.hmcl.ui.FXUtils;
import javafx.beans.value.ObservableValue;

import java.util.List;
import java.util.function.Consumer;

public final class AllSelectPolicy {
public final class SelectionBinding {
private final BooleanProperty allSelected;

private final List<BooleanProperty> children;
Expand All @@ -15,7 +15,7 @@ public final class AllSelectPolicy {

private boolean updating = false;

public AllSelectPolicy(BooleanProperty allSelected, List<BooleanProperty> children) {
public SelectionBinding(BooleanProperty allSelected, List<BooleanProperty> children) {
this.allSelected = allSelected;
this.children = children;
int itemCount = children.size();
Expand All @@ -25,7 +25,7 @@ public AllSelectPolicy(BooleanProperty allSelected, List<BooleanProperty> childr
childSelectedCount++;
}

FXUtils.onChange(child, wrap(value -> {
onChange(child, wrap(value -> {
if (value) {
childSelectedCount++;
} else {
Expand All @@ -38,7 +38,7 @@ public AllSelectPolicy(BooleanProperty allSelected, List<BooleanProperty> childr

allSelected.set(childSelectedCount == itemCount);

FXUtils.onChange(allSelected, wrap(value -> {
onChange(allSelected, wrap(value -> {
for (BooleanProperty child : children) {
child.setValue(value);
}
Expand All @@ -50,6 +50,10 @@ public AllSelectPolicy(BooleanProperty allSelected, List<BooleanProperty> childr
}));
}

private static <T> void onChange(ObservableValue<T> value, Consumer<T> consumer) {
value.addListener((a, b, c) -> consumer.accept(c));
}

private <T> Consumer<T> wrap(Consumer<T> c) {
return value -> {
if (!updating) {
Expand Down

0 comments on commit e1db5e8

Please sign in to comment.