From c3cd0dd8a082cffcb728cab39161f0a4d7d11ae8 Mon Sep 17 00:00:00 2001 From: huangyuhui Date: Fri, 7 Sep 2018 01:03:59 +0800 Subject: [PATCH] Refactor ProfileList & AccountList --- .../java/org/jackhuang/hmcl/ui/ListPage.java | 40 ++++++++++ ...AccountListSkin.java => ListPageSkin.java} | 10 +-- .../hmcl/ui/account/AccountList.java | 22 ++---- .../hmcl/ui/profile/ProfileList.java | 22 ++---- .../hmcl/ui/profile/ProfileListSkin.java | 77 ------------------- 5 files changed, 56 insertions(+), 115 deletions(-) create mode 100644 HMCL/src/main/java/org/jackhuang/hmcl/ui/ListPage.java rename HMCL/src/main/java/org/jackhuang/hmcl/ui/{account/AccountListSkin.java => ListPageSkin.java} (89%) delete mode 100644 HMCL/src/main/java/org/jackhuang/hmcl/ui/profile/ProfileListSkin.java diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/ListPage.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/ListPage.java new file mode 100644 index 000000000..effce1734 --- /dev/null +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/ListPage.java @@ -0,0 +1,40 @@ +/* + * Hello Minecraft! Launcher. + * Copyright (C) 2017 huangyuhui + * + * 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.ui; + +import javafx.beans.property.ListProperty; +import javafx.beans.property.SimpleListProperty; +import javafx.collections.FXCollections; +import javafx.scene.Node; +import javafx.scene.control.Control; +import javafx.scene.control.Skin; + +public abstract class ListPage extends Control { + private final ListProperty items = new SimpleListProperty<>(FXCollections.observableArrayList()); + + public abstract void add(); + + public ListProperty itemsProperty() { + return items; + } + + @Override + protected Skin createDefaultSkin() { + return new ListPageSkin(this); + } +} diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/AccountListSkin.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/ListPageSkin.java similarity index 89% rename from HMCL/src/main/java/org/jackhuang/hmcl/ui/account/AccountListSkin.java rename to HMCL/src/main/java/org/jackhuang/hmcl/ui/ListPageSkin.java index d4a2fd206..dfed6b147 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/AccountListSkin.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/ListPageSkin.java @@ -15,7 +15,7 @@ * 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.ui.account; +package org.jackhuang.hmcl.ui; import com.jfoenix.controls.JFXButton; import com.jfoenix.controls.JFXScrollPane; @@ -27,12 +27,10 @@ import javafx.scene.control.SkinBase; import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; import org.jackhuang.hmcl.setting.Theme; -import org.jackhuang.hmcl.ui.FXUtils; -import org.jackhuang.hmcl.ui.SVG; -public class AccountListSkin extends SkinBase { +public class ListPageSkin extends SkinBase { - public AccountListSkin(AccountList skinnable) { + public ListPageSkin(ListPage skinnable) { super(skinnable); StackPane root = new StackPane(); @@ -65,7 +63,7 @@ public class AccountListSkin extends SkinBase { btnAdd.getStyleClass().setAll("jfx-button-raised-round"); btnAdd.setButtonType(JFXButton.ButtonType.RAISED); btnAdd.setGraphic(SVG.plus(Theme.whiteFillBinding(), -1, -1)); - btnAdd.setOnMouseClicked(e -> skinnable.addNewAccount()); + btnAdd.setOnMouseClicked(e -> skinnable.add()); vBox.getChildren().setAll(btnAdd); } diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/AccountList.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/AccountList.java index f3fa11fdd..946018a1d 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/AccountList.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/AccountList.java @@ -20,29 +20,27 @@ package org.jackhuang.hmcl.ui.account; import javafx.beans.property.*; import javafx.collections.FXCollections; import javafx.collections.ObservableList; -import javafx.scene.control.Control; -import javafx.scene.control.Skin; import javafx.scene.control.ToggleGroup; import org.jackhuang.hmcl.auth.Account; import org.jackhuang.hmcl.ui.Controllers; +import org.jackhuang.hmcl.ui.ListPage; import org.jackhuang.hmcl.ui.decorator.DecoratorPage; import org.jackhuang.hmcl.util.MappedObservableList; import static org.jackhuang.hmcl.ui.FXUtils.onInvalidating; import static org.jackhuang.hmcl.util.i18n.I18n.i18n; -public class AccountList extends Control implements DecoratorPage { +public class AccountList extends ListPage implements DecoratorPage { private final ReadOnlyStringWrapper title = new ReadOnlyStringWrapper(i18n("account.manage")); - private final ListProperty items = new SimpleListProperty<>(FXCollections.observableArrayList()); private ObjectProperty selectedAccount = new SimpleObjectProperty() { { - items.addListener(onInvalidating(this::invalidated)); + itemsProperty().addListener(onInvalidating(this::invalidated)); } @Override protected void invalidated() { Account selected = get(); - items.forEach(item -> item.selectedProperty().set(item.getAccount() == selected)); + itemsProperty().forEach(item -> item.selectedProperty().set(item.getAccount() == selected)); } }; private final ListProperty accounts = new SimpleListProperty<>(FXCollections.observableArrayList()); @@ -57,7 +55,7 @@ public class AccountList extends Control implements DecoratorPage { accountsProperty(), account -> new AccountListItem(toggleGroup, account)); - items.bindContent(accountItems); + itemsProperty().bindContent(accountItems); toggleGroup.selectedToggleProperty().addListener((o, a, toggle) -> { if (toggle == null || toggle.getUserData() == null) return; @@ -74,18 +72,10 @@ public class AccountList extends Control implements DecoratorPage { } @Override - protected Skin createDefaultSkin() { - return new AccountListSkin(this); - } - - public void addNewAccount() { + public void add() { Controllers.dialog(new AddAccountPane()); } - public ListProperty itemsProperty() { - return items; - } - @Override public ReadOnlyStringProperty titleProperty() { return title.getReadOnlyProperty(); 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 bf089248c..5fdabd4bd 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 @@ -20,29 +20,27 @@ package org.jackhuang.hmcl.ui.profile; import javafx.beans.property.*; import javafx.collections.FXCollections; import javafx.collections.ObservableList; -import javafx.scene.control.Control; -import javafx.scene.control.Skin; 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.MappedObservableList; import static org.jackhuang.hmcl.ui.FXUtils.onInvalidating; import static org.jackhuang.hmcl.util.i18n.I18n.i18n; -public class ProfileList extends Control implements DecoratorPage { +public class ProfileList extends ListPage implements DecoratorPage { private final ReadOnlyStringWrapper title = new ReadOnlyStringWrapper(i18n("profile.manage")); - private final ListProperty items = new SimpleListProperty<>(FXCollections.observableArrayList()); private ObjectProperty selectedProfile = new SimpleObjectProperty() { { - items.addListener(onInvalidating(this::invalidated)); + itemsProperty().addListener(onInvalidating(this::invalidated)); } @Override protected void invalidated() { Profile selected = get(); - items.forEach(item -> item.selectedProperty().set(item.getProfile() == selected)); + itemsProperty().forEach(item -> item.selectedProperty().set(item.getProfile() == selected)); } }; private final ListProperty profiles = new SimpleListProperty<>(FXCollections.observableArrayList()); @@ -57,7 +55,7 @@ public class ProfileList extends Control implements DecoratorPage { profilesProperty(), profile -> new ProfileListItem(toggleGroup, profile)); - items.bindContent(profileItems); + itemsProperty().bindContent(profileItems); toggleGroup.selectedToggleProperty().addListener((o, a, toggle) -> { if (toggle == null || toggle.getUserData() == null) return; @@ -74,18 +72,10 @@ public class ProfileList extends Control implements DecoratorPage { } @Override - protected Skin createDefaultSkin() { - return new ProfileListSkin(this); - } - - public void addNewProfile() { + public void add() { Controllers.navigate(new ProfilePage(null)); } - public ListProperty itemsProperty() { - return items; - } - @Override public ReadOnlyStringProperty titleProperty() { return title.getReadOnlyProperty(); diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/profile/ProfileListSkin.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/profile/ProfileListSkin.java deleted file mode 100644 index 588fda41f..000000000 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/profile/ProfileListSkin.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Hello Minecraft! Launcher. - * Copyright (C) 2017 huangyuhui - * - * 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.ui.profile; - -import com.jfoenix.controls.JFXButton; -import com.jfoenix.controls.JFXScrollPane; -import javafx.beans.binding.Bindings; -import javafx.geometry.Insets; -import javafx.geometry.Pos; -import javafx.scene.control.ScrollPane; -import javafx.scene.control.SkinBase; -import javafx.scene.layout.StackPane; -import javafx.scene.layout.VBox; -import org.jackhuang.hmcl.setting.Theme; -import org.jackhuang.hmcl.ui.FXUtils; -import org.jackhuang.hmcl.ui.SVG; - -public class ProfileListSkin extends SkinBase { - - public ProfileListSkin(ProfileList skinnable) { - super(skinnable); - - StackPane root = new StackPane(); - - ScrollPane scrollPane = new ScrollPane(); - { - scrollPane.setFitToWidth(true); - - VBox accountList = new VBox(); - accountList.maxWidthProperty().bind(scrollPane.widthProperty()); - accountList.setSpacing(10); - accountList.setStyle("-fx-padding: 10 10 10 10;"); - - Bindings.bindContent(accountList.getChildren(), skinnable.itemsProperty()); - - scrollPane.setContent(accountList); - JFXScrollPane.smoothScrolling(scrollPane); - } - - VBox vBox = new VBox(); - { - vBox.setAlignment(Pos.BOTTOM_RIGHT); - vBox.setPickOnBounds(false); - vBox.setPadding(new Insets(15)); - vBox.setSpacing(15); - - JFXButton btnAdd = new JFXButton(); - FXUtils.setLimitWidth(btnAdd, 40); - FXUtils.setLimitHeight(btnAdd, 40); - btnAdd.getStyleClass().setAll("jfx-button-raised-round"); - btnAdd.setButtonType(JFXButton.ButtonType.RAISED); - btnAdd.setGraphic(SVG.plus(Theme.whiteFillBinding(), -1, -1)); - btnAdd.setOnMouseClicked(e -> skinnable.addNewProfile()); - - vBox.getChildren().setAll(btnAdd); - } - - root.getChildren().setAll(scrollPane, vBox); - - getChildren().setAll(root); - } -}