From b3d84c4f9920e6a65cc6f6911b32f5d54737a12d Mon Sep 17 00:00:00 2001 From: huangyuhui Date: Fri, 31 Aug 2018 22:21:15 +0800 Subject: [PATCH] Refactor --- .../jackhuang/hmcl/ui/AdvancedListItem2.java | 29 ++++++---- .../hmcl/ui/AdvancedListItemSkin.java | 22 +++---- .../jackhuang/hmcl/ui/LeftPaneController.java | 3 +- .../ui/account/AccountAdvancedListItem.java | 57 +++++++------------ .../ui/versions/GameAdvancedListItem.java | 23 +------- 5 files changed, 55 insertions(+), 79 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/AdvancedListItem2.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/AdvancedListItem2.java index 1933ba7b3..449a5fd76 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/AdvancedListItem2.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/AdvancedListItem2.java @@ -17,27 +17,36 @@ */ package org.jackhuang.hmcl.ui; -import javafx.beans.property.ObjectProperty; -import javafx.beans.property.ObjectPropertyBase; -import javafx.beans.property.StringProperty; +import javafx.beans.property.*; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Rectangle2D; import javafx.scene.control.Control; import javafx.scene.control.Skin; import javafx.scene.image.Image; +import org.jackhuang.hmcl.game.AccountHelper; -public abstract class AdvancedListItem2 extends Control { +public class AdvancedListItem2 extends Control { + private final ObjectProperty image = new SimpleObjectProperty<>(); + private final ObjectProperty viewport = new SimpleObjectProperty<>(); + private final StringProperty title = new SimpleStringProperty(); + private final StringProperty subtitle = new SimpleStringProperty(); - public abstract ObjectProperty imageProperty(); - - public ObjectProperty viewportProperty() { - return null; + public ObjectProperty imageProperty() { + return image; } - public abstract StringProperty titleProperty(); + public ObjectProperty viewportProperty() { + return viewport; + } - public abstract StringProperty subtitleProperty(); + public StringProperty titleProperty() { + return title; + } + + public StringProperty subtitleProperty() { + return subtitle; + } public final ObjectProperty> onActionProperty() { return onAction; diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/AdvancedListItemSkin.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/AdvancedListItemSkin.java index 3aa303374..58747a4c6 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/AdvancedListItemSkin.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/AdvancedListItemSkin.java @@ -54,8 +54,7 @@ public class AdvancedListItemSkin extends SkinBase { FXUtils.limitSize(imageView, 32, 32); imageView.setPreserveRatio(true); imageView.imageProperty().bind(skinnable.imageProperty()); - Optional.ofNullable(skinnable.viewportProperty()) - .ifPresent(imageView.viewportProperty()::bind); + imageView.viewportProperty().bind(skinnable.viewportProperty()); imageViewContainer.getChildren().setAll(imageView); VBox vbox = new VBox(); @@ -69,14 +68,17 @@ public class AdvancedListItemSkin extends SkinBase { title.setTextAlignment(TextAlignment.JUSTIFY); vbox.getChildren().add(title); - if (skinnable.subtitleProperty() != null) { - Label subtitle = new Label(); - subtitle.textProperty().bind(skinnable.subtitleProperty()); - subtitle.setMaxWidth(90); - subtitle.setStyle("-fx-font-size: 10;"); - subtitle.setTextAlignment(TextAlignment.JUSTIFY); - vbox.getChildren().add(subtitle); - } + Label subtitle = new Label(); + subtitle.textProperty().bind(skinnable.subtitleProperty()); + subtitle.setMaxWidth(90); + subtitle.setStyle("-fx-font-size: 10;"); + subtitle.setTextAlignment(TextAlignment.JUSTIFY); + vbox.getChildren().add(subtitle); + + FXUtils.onChangeAndOperate(skinnable.subtitleProperty(), subtitleString -> { + if (subtitleString == null) vbox.getChildren().setAll(title); + else vbox.getChildren().setAll(title, subtitle); + }); left.getChildren().setAll(imageViewContainer, vbox); root.setLeft(left); diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/LeftPaneController.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/LeftPaneController.java index 9b2d143b2..cb45f358c 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/LeftPaneController.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/LeftPaneController.java @@ -58,8 +58,9 @@ public final class LeftPaneController { public LeftPaneController(AdvancedListBox leftPane) { this.leftPane = leftPane; - AdvancedListItem2 accountListItem = new AccountAdvancedListItem(); + AccountAdvancedListItem accountListItem = new AccountAdvancedListItem(); accountListItem.setOnAction(e -> Controllers.navigate(Controllers.getAccountListPage())); + accountListItem.accountProperty().bind(Accounts.selectedAccountProperty()); AdvancedListItem2 gameListItem = new GameAdvancedListItem(); gameListItem.setOnAction(e -> Controllers.navigate(Controllers.getGameListPage())); diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/AccountAdvancedListItem.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/AccountAdvancedListItem.java index 17dd802a3..4aaf5c32c 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/AccountAdvancedListItem.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/AccountAdvancedListItem.java @@ -32,60 +32,41 @@ import org.jackhuang.hmcl.task.Schedulers; import org.jackhuang.hmcl.ui.AdvancedListItem2; import org.jackhuang.hmcl.ui.FXUtils; +import static org.jackhuang.hmcl.ui.FXUtils.onInvalidating; import static org.jackhuang.hmcl.util.i18n.I18n.i18n; public class AccountAdvancedListItem extends AdvancedListItem2 { - private final ObjectProperty image = new SimpleObjectProperty<>(); - private final ObjectProperty viewport = new SimpleObjectProperty<>(AccountHelper.getViewport(4)); - private final StringProperty title = new SimpleStringProperty(); - private final StringProperty subtitle = new SimpleStringProperty(); + private ObjectProperty account = new SimpleObjectProperty() { - public AccountAdvancedListItem() { - - FXUtils.onChangeAndOperate(Accounts.selectedAccountProperty(), account -> { + @Override + protected void invalidated() { + Account account = get(); if (account == null) { - title.set(i18n("account.missing")); - subtitle.set(i18n("account.missing.add")); - image.set(new Image("/assets/img/craft_table.png")); + titleProperty().set(i18n("account.missing")); + subtitleProperty().set(i18n("account.missing.add")); + imageProperty().set(new Image("/assets/img/craft_table.png")); } else { - title.set(account.getCharacter()); - subtitle.set(accountSubtitle(account)); + titleProperty().set(account.getCharacter()); + subtitleProperty().set(accountSubtitle(account)); - this.image.set(AccountHelper.getDefaultSkin(account.getUUID(), 4)); + imageProperty().set(AccountHelper.getDefaultSkin(account.getUUID(), 4)); if (account instanceof YggdrasilAccount) { AccountHelper.loadSkinAsync((YggdrasilAccount) account).subscribe(Schedulers.javafx(), () -> { Image image = AccountHelper.getSkin((YggdrasilAccount) account, 4); - this.image.set(image); + imageProperty().set(image); }); } } - }); + } + }; + + public AccountAdvancedListItem() { + viewportProperty().set(AccountHelper.getViewport(4)); } - @Override - protected void layoutChildren() { - super.layoutChildren(); - } - - @Override - public ObjectProperty imageProperty() { - return image; - } - - @Override - public ObjectProperty viewportProperty() { - return viewport; - } - - @Override - public StringProperty titleProperty() { - return title; - } - - @Override - public StringProperty subtitleProperty() { - return subtitle; + public ObjectProperty accountProperty() { + return account; } private static String accountSubtitle(Account account) { diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/GameAdvancedListItem.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/GameAdvancedListItem.java index ddeb7bf1f..db0961124 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/GameAdvancedListItem.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/GameAdvancedListItem.java @@ -35,8 +35,6 @@ import org.jackhuang.hmcl.ui.WeakListenerHelper; import java.io.File; public class GameAdvancedListItem extends AdvancedListItem2 { - private final ObjectProperty image = new SimpleObjectProperty<>(); - private final StringProperty title = new SimpleStringProperty(); private final WeakListenerHelper helper = new WeakListenerHelper(); private Profile profile; @@ -69,25 +67,10 @@ public class GameAdvancedListItem extends AdvancedListItem2 { String version = profile.getSelectedVersion(); File iconFile = profile.getRepository().getVersionIcon(version); if (iconFile.exists()) - image.set(new Image("file:" + iconFile.getAbsolutePath())); + imageProperty().set(new Image("file:" + iconFile.getAbsolutePath())); else - image.set(new Image("/assets/img/grass.png")); + imageProperty().set(new Image("/assets/img/grass.png")); - title.set(version); - } - - @Override - public ObjectProperty imageProperty() { - return image; - } - - @Override - public StringProperty titleProperty() { - return title; - } - - @Override - public StringProperty subtitleProperty() { - return null; + titleProperty().set(version); } }