package wallettemplate.utils; import org.bitcoinj.core.*; import javafx.application.Platform; import javafx.beans.property.ReadOnlyDoubleProperty; import javafx.beans.property.ReadOnlyObjectProperty; import javafx.beans.property.SimpleDoubleProperty; import javafx.beans.property.SimpleObjectProperty; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import java.util.Date; /** * A class that exposes relevant bitcoin stuff as JavaFX bindable properties. */ public class BitcoinUIModel { private final long CHUNK_SIZE = 1000000; private SimpleObjectProperty<Address> address = new SimpleObjectProperty<>(); private SimpleObjectProperty<Coin> balance = new SimpleObjectProperty<>(Coin.ZERO); private SimpleDoubleProperty syncProgress = new SimpleDoubleProperty(-1); private ProgressBarUpdater syncProgressUpdater = new ProgressBarUpdater(); private ObservableList<TransactionOutput> outputs = FXCollections.observableArrayList(); public BitcoinUIModel() { } public BitcoinUIModel(Wallet wallet) { setWallet(wallet); } public void setWallet(Wallet wallet) { wallet.addEventListener(new AbstractWalletEventListener() { @Override public void onWalletChanged(Wallet wallet) { super.onWalletChanged(wallet); update(wallet); } }, Platform::runLater); update(wallet); } private void update(Wallet wallet) { balance.set(wallet.getBalance()); address.set(wallet.currentReceiveAddress()); outputs.setAll(wallet.calculateAllSpendCandidates(true)); for (Object obj : outputs.toArray()) { TransactionOutput output = (TransactionOutput)obj; if (output.getValue().getValue() < CHUNK_SIZE) outputs.remove(obj); } } private class ProgressBarUpdater extends DownloadProgressTracker { @Override protected void progress(double pct, int blocksLeft, Date date) { super.progress(pct, blocksLeft, date); Platform.runLater(() -> syncProgress.set(pct / 100.0)); } @Override protected void doneDownload() { super.doneDownload(); Platform.runLater(() -> syncProgress.set(1.0)); } } public DownloadProgressTracker getDownloadProgressTracker() { return syncProgressUpdater; } public ReadOnlyDoubleProperty syncProgressProperty() { return syncProgress; } public ReadOnlyObjectProperty<Address> addressProperty() { return address; } public ReadOnlyObjectProperty<Coin> balanceProperty() { return balance; } public ObservableList<TransactionOutput> getOutputs() { return outputs; } }