From c03a0feb7572358c465e815981b059620dc7ad05 Mon Sep 17 00:00:00 2001 From: yushijinhun Date: Mon, 1 Oct 2018 20:59:11 +0800 Subject: [PATCH] Refactor ProfileList --- .../hmcl/ui/profile/ProfileList.java | 33 ++------------ .../hmcl/ui/profile/ProfileListItem.java | 44 ++++++++++--------- .../hmcl/ui/profile/ProfileListItemSkin.java | 2 - 3 files changed, 28 insertions(+), 51 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/profile/ProfileList.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/profile/ProfileList.java index d7a7e551c..a8809cdc6 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/profile/ProfileList.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/profile/ProfileList.java @@ -19,48 +19,23 @@ package org.jackhuang.hmcl.ui.profile; import javafx.beans.property.*; import javafx.collections.FXCollections; -import javafx.collections.ObservableList; -import javafx.scene.control.ToggleGroup; import org.jackhuang.hmcl.setting.Profile; import org.jackhuang.hmcl.ui.Controllers; 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.ui.FXUtils.onInvalidating; import static org.jackhuang.hmcl.util.i18n.I18n.i18n; +import static org.jackhuang.hmcl.util.javafx.SelectedItemProperties.createSelectedItemPropertyFor; public class ProfileList extends ListPage implements DecoratorPage { private final ReadOnlyStringWrapper title = new ReadOnlyStringWrapper(i18n("profile.manage")); - private ObjectProperty selectedProfile = new SimpleObjectProperty() { - { - itemsProperty().addListener(onInvalidating(this::invalidated)); - } - - @Override - protected void invalidated() { - Profile selected = get(); - itemsProperty().forEach(item -> item.selectedProperty().set(item.getProfile() == selected)); - } - }; private final ListProperty profiles = new SimpleListProperty<>(FXCollections.observableArrayList()); - - private ToggleGroup toggleGroup; - private final ObservableList profileItems; + private ObjectProperty selectedProfile; public ProfileList() { - toggleGroup = new ToggleGroup(); - - profileItems = MappedObservableList.create( - profilesProperty(), - profile -> new ProfileListItem(toggleGroup, profile)); - - itemsProperty().bindContent(profileItems); - - toggleGroup.selectedToggleProperty().addListener((o, a, toggle) -> { - if (toggle == null || toggle.getUserData() == null) return; - selectedProfile.set(((ProfileListItem) toggle.getUserData()).getProfile()); - }); + setItems(MappedObservableList.create(profilesProperty(), ProfileListItem::new)); + selectedProfile = createSelectedItemPropertyFor(getItems(), Profile.class); } public ObjectProperty selectedProfileProperty() { diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/profile/ProfileListItem.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/profile/ProfileListItem.java index 846577f6a..838aa3e85 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/profile/ProfileListItem.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/profile/ProfileListItem.java @@ -17,30 +17,26 @@ */ package org.jackhuang.hmcl.ui.profile; -import javafx.beans.property.BooleanProperty; -import javafx.beans.property.SimpleBooleanProperty; import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; -import javafx.scene.control.Control; import javafx.scene.control.Skin; -import javafx.scene.control.ToggleGroup; +import javafx.scene.control.ToggleButton; + import org.jackhuang.hmcl.setting.Profile; import org.jackhuang.hmcl.setting.Profiles; -public class ProfileListItem extends Control { +public class ProfileListItem extends ToggleButton { private final Profile profile; - private final ToggleGroup toggleGroup; private final StringProperty title = new SimpleStringProperty(); private final StringProperty subtitle = new SimpleStringProperty(); - private final BooleanProperty selected = new SimpleBooleanProperty(); - public ProfileListItem(ToggleGroup toggleGroup, Profile profile) { + public ProfileListItem(Profile profile) { this.profile = profile; - this.toggleGroup = toggleGroup; + getStyleClass().clear(); + setUserData(profile); title.set(Profiles.getProfileDisplayName(profile)); subtitle.set(profile.getGameDir().toString()); - selected.set(Profiles.selectedProfileProperty().get() == profile); } @Override @@ -48,27 +44,35 @@ public class ProfileListItem extends Control { return new ProfileListItemSkin(this); } - public ToggleGroup getToggleGroup() { - return toggleGroup; + public void remove() { + Profiles.getProfiles().remove(profile); } public Profile getProfile() { return profile; } + public String getTitle() { + return title.get(); + } + + public void setTitle(String title) { + this.title.set(title); + } + public StringProperty titleProperty() { return title; } + public String getSubtitle() { + return subtitle.get(); + } + + public void setSubtitle(String subtitle) { + this.subtitle.set(subtitle); + } + public StringProperty subtitleProperty() { return subtitle; } - - public BooleanProperty selectedProperty() { - return selected; - } - - public void remove() { - Profiles.getProfiles().remove(profile); - } } diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/profile/ProfileListItemSkin.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/profile/ProfileListItemSkin.java index 841c3adaf..2a8a668a9 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/profile/ProfileListItemSkin.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/profile/ProfileListItemSkin.java @@ -41,9 +41,7 @@ public class ProfileListItemSkin extends SkinBase { JFXRadioButton chkSelected = new JFXRadioButton(); BorderPane.setAlignment(chkSelected, Pos.CENTER); - chkSelected.setUserData(skinnable); chkSelected.selectedProperty().bindBidirectional(skinnable.selectedProperty()); - chkSelected.setToggleGroup(skinnable.getToggleGroup()); root.setLeft(chkSelected); HBox center = new HBox();