package wallettemplate; import com.google.bitcoin.core.*; import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.Futures; import javafx.application.Platform; import javafx.event.ActionEvent; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import wallettemplate.controls.BitcoinAddressValidator; import static wallettemplate.utils.GuiUtils.crashAlert; import static wallettemplate.utils.GuiUtils.informationalAlert; public class SendMoneyController { public Button sendBtn; public Button cancelBtn; public TextField address; public Label titleLabel; public Main.OverlayUI overlayUi; private Wallet.SendResult sendResult; // Called by FXMLLoader public void initialize() { new BitcoinAddressValidator(Main.params, address, sendBtn); } public void cancel(ActionEvent event) { overlayUi.done(); } public void send(ActionEvent event) { try { Address destination = new Address(Main.params, address.getText()); Wallet.SendRequest req = Wallet.SendRequest.emptyWallet(destination); sendResult = Main.bitcoin.wallet().sendCoins(req); if (sendResult == null) { // We couldn't empty the wallet for some reason. TODO: When bitcoinj issue 425 is fixed, be more helpful informationalAlert("Could not empty the wallet", "You may have too little money left in the wallet to make a transaction."); overlayUi.done(); return; } Futures.addCallback(sendResult.broadcastComplete, new FutureCallback<Transaction>() { @Override public void onSuccess(Transaction result) { Platform.runLater(overlayUi::done); } @Override public void onFailure(Throwable t) { // We died trying to empty the wallet. crashAlert(t); } }); sendResult.tx.getConfidence().addEventListener((tx, reason) -> { if (reason == TransactionConfidence.Listener.ChangeReason.SEEN_PEERS) updateTitleForBroadcast(); }); sendBtn.setDisable(true); address.setDisable(true); updateTitleForBroadcast(); } catch (AddressFormatException e) { // Cannot happen because we already validated it when the text field changed. throw new RuntimeException(e); } } private void updateTitleForBroadcast() { final int peers = sendResult.tx.getConfidence().numBroadcastPeers(); titleLabel.setText(String.format("Broadcasting ... seen by %d peers", peers)); } }