WalletTemplate: allow sending of arbitrary amounts.

This commit is contained in:
Mike Hearn 2014-11-14 15:35:45 +01:00
parent 1e8d249ce7
commit 3a1c156d4e
3 changed files with 50 additions and 18 deletions

View File

@ -1,5 +1,6 @@
package wallettemplate;
import javafx.scene.layout.HBox;
import org.bitcoinj.core.*;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
@ -9,6 +10,8 @@ import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import org.spongycastle.crypto.params.KeyParameter;
import wallettemplate.controls.BitcoinAddressValidator;
import wallettemplate.utils.TextFieldValidator;
import wallettemplate.utils.WTUtils;
import static com.google.common.base.Preconditions.checkState;
import static wallettemplate.utils.GuiUtils.*;
@ -18,6 +21,8 @@ public class SendMoneyController {
public Button cancelBtn;
public TextField address;
public Label titleLabel;
public TextField amountEdit;
public Label btcLabel;
public Main.OverlayUI overlayUI;
@ -26,8 +31,12 @@ public class SendMoneyController {
// Called by FXMLLoader
public void initialize() {
checkState(!Main.bitcoin.wallet().getBalance().isZero());
Coin balance = Main.bitcoin.wallet().getBalance();
checkState(!balance.isZero());
new BitcoinAddressValidator(Main.params, address, sendBtn);
new TextFieldValidator(amountEdit, text ->
!WTUtils.didThrow(() -> checkState(Coin.parseCoin(text).compareTo(balance) <= 0)));
amountEdit.setText(balance.toPlainString());
}
public void cancel(ActionEvent event) {
@ -37,8 +46,9 @@ public class SendMoneyController {
public void send(ActionEvent event) {
// Address exception cannot happen as we validated it beforehand.
try {
Coin amount = Coin.parseCoin(amountEdit.getText());
Address destination = new Address(Main.params, address.getText());
Wallet.SendRequest req = Wallet.SendRequest.emptyWallet(destination);
Wallet.SendRequest req = Wallet.SendRequest.to(destination, amount);
req.aesKey = aesKey;
sendResult = Main.bitcoin.wallet().sendCoins(req);
Futures.addCallback(sendResult.broadcastComplete, new FutureCallback<Transaction>() {
@ -60,6 +70,8 @@ public class SendMoneyController {
});
sendBtn.setDisable(true);
address.setDisable(true);
((HBox)amountEdit.getParent()).getChildren().remove(amountEdit);
((HBox)btcLabel.getParent()).getChildren().remove(btcLabel);
updateTitleForBroadcast();
} catch (InsufficientMoneyException e) {
informationalAlert("Could not empty the wallet",

View File

@ -61,7 +61,7 @@
<HBox spacing="20" AnchorPane.rightAnchor="14.0" AnchorPane.topAnchor="17.0">
<children>
<Button onAction="#settingsClicked" style="-fx-base: white;" styleClass="fat-button" text="Settings" />
<Button id="sendMoneyOut" fx:id="sendMoneyOutBtn" alignment="CENTER" mnemonicParsing="false" onAction="#sendMoneyOut" style="-fx-base: lightgreen;" styleClass="fat-button" text="Send money out" />
<Button id="sendMoneyOut" fx:id="sendMoneyOutBtn" alignment="CENTER" mnemonicParsing="false" onAction="#sendMoneyOut" style="-fx-base: lightgreen; -fx-font-weight: bold" styleClass="fat-button" text="Send money out" />
</children>
</HBox>
</children>

View File

@ -1,29 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="wallettemplate.SendMoneyController">
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="wallettemplate.SendMoneyController">
<children>
<VBox alignment="CENTER" layoutY="100.0" prefHeight="200.0" prefWidth="600.0" spacing="20.0" style="-fx-background-color: white;" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
<VBox alignment="CENTER_LEFT" layoutY="100.0" prefHeight="200.0" prefWidth="600.0" spacing="20.0" style="-fx-background-color: white;" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
<children>
<Label fx:id="titleLabel" text="Send all money to ...">
<font>
<Font size="25.0" />
</font>
</Label>
<TextField fx:id="address" prefWidth="354.0" promptText="1EZEqFBd8yuc9ir2761987q7k3VcALC8YQ">
<VBox.margin>
<Insets left="40.0" right="40.0" />
</VBox.margin>
</TextField>
<HBox alignment="CENTER" fillHeight="true" prefHeight="30.0" prefWidth="600.0" spacing="50.0" VBox.vgrow="NEVER">
<HBox fx:id="topHBox" alignment="CENTER_LEFT" spacing="15.0">
<children>
<Label fx:id="titleLabel" text="Send">
<font>
<Font size="25.0" />
</font>
</Label>
<TextField fx:id="amountEdit" />
<Label fx:id="btcLabel" text="BTC">
<font>
<Font size="24.0" />
</font>
</Label>
</children>
</HBox>
<HBox alignment="CENTER_LEFT" spacing="15.0">
<children>
<Label text="to">
<font>
<Font size="24.0" />
</font>
</Label>
<TextField fx:id="address" maxWidth="1.7976931348623157E308" promptText="1EZEqFBd8yuc9ir2761987q7k3VcALC8YQ" HBox.hgrow="ALWAYS">
<VBox.margin>
<Insets />
</VBox.margin>
</TextField>
</children>
</HBox>
<HBox alignment="CENTER_RIGHT" fillHeight="true" prefHeight="30.0" prefWidth="600.0" spacing="15.0" VBox.vgrow="NEVER">
<children>
<Button fx:id="cancelBtn" cancelButton="true" mnemonicParsing="false" onAction="#cancel" prefWidth="79.0" text="Cancel" />
<Button fx:id="sendBtn" defaultButton="true" mnemonicParsing="false" onAction="#send" prefWidth="79.0" text="Send" />
@ -33,6 +50,9 @@
<effect>
<DropShadow />
</effect>
<padding>
<Insets bottom="30.0" left="30.0" right="30.0" top="30.0" />
</padding>
</VBox>
</children>
</AnchorPane>