diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/Account.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/Account.java index 8eaf538cd..585f45e80 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/Account.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/Account.java @@ -17,8 +17,13 @@ */ package org.jackhuang.hmcl.auth; +import org.jackhuang.hmcl.util.ObservableHelper; import org.jackhuang.hmcl.util.ToStringBuilder; +import javafx.application.Platform; +import javafx.beans.InvalidationListener; +import javafx.beans.Observable; + import java.util.Map; import java.util.Optional; import java.util.UUID; @@ -27,7 +32,7 @@ import java.util.UUID; * * @author huangyuhui */ -public abstract class Account { +public abstract class Account implements Observable { /** * @return the name of the account who owns the character @@ -66,6 +71,26 @@ public abstract class Account { public abstract void clearCache(); + private ObservableHelper helper = new ObservableHelper(this); + + @Override + public void addListener(InvalidationListener listener) { + helper.addListener(listener); + } + + @Override + public void removeListener(InvalidationListener listener) { + helper.removeListener(listener); + } + + /** + * Called when the account has changed. + * This method can be called from any thread. + */ + protected void invalidate() { + Platform.runLater(helper::invalidate); + } + @Override public String toString() { return new ToStringBuilder(this) diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/yggdrasil/YggdrasilAccount.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/yggdrasil/YggdrasilAccount.java index 112bdfc3c..4d47c1f8f 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/yggdrasil/YggdrasilAccount.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/yggdrasil/YggdrasilAccount.java @@ -114,6 +114,7 @@ public class YggdrasilAccount extends Account { } this.characterUUID = this.session.getSelectedProfile().getId(); + invalidate(); } @Override diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/ObservableHelper.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/ObservableHelper.java new file mode 100644 index 000000000..c4b8e3dd9 --- /dev/null +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/ObservableHelper.java @@ -0,0 +1,65 @@ +/* + * Hello Minecraft! Launcher. + * Copyright (C) 2018 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.util; + +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + +import javafx.beans.InvalidationListener; +import javafx.beans.Observable; + +/** + * Helper class for implementing {@link Observable}. + * + * @author yushijinhun + */ +public class ObservableHelper implements Observable, InvalidationListener { + + private List listeners = new CopyOnWriteArrayList<>(); + private Observable source; + + public ObservableHelper(Observable source) { + this.source = source; + } + + /** + * This method can be called from any thread. + */ + @Override + public void addListener(InvalidationListener listener) { + listeners.add(listener); + } + + /** + * This method can be called from any thread. + */ + @Override + public void removeListener(InvalidationListener listener) { + listeners.remove(listener); + } + + public void invalidate() { + listeners.forEach(it -> it.invalidated(source)); + } + + @Override + public void invalidated(Observable observable) { + this.invalidate(); + } + +}