/* * This file is part of Bitsquare. * * Bitsquare is free software: you can redistribute it and/or modify it * under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or (at * your option) any later version. * * Bitsquare 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 Affero General Public * License for more details. * * You should have received a copy of the GNU Affero General Public License * along with Bitsquare. If not, see <http://www.gnu.org/licenses/>. */ package io.bitsquare.gui.components; import io.bitsquare.btc.WalletService; import io.bitsquare.btc.listeners.AddressConfidenceListener; import io.bitsquare.btc.listeners.BalanceListener; import io.bitsquare.gui.components.indicator.TxConfidenceIndicator; import io.bitsquare.gui.util.BSFormatter; import javafx.scene.control.TextField; import javafx.scene.control.Tooltip; import javafx.scene.effect.BlurType; import javafx.scene.effect.DropShadow; import javafx.scene.effect.Effect; import javafx.scene.layout.AnchorPane; import javafx.scene.paint.Color; import org.bitcoinj.core.Address; import org.bitcoinj.core.Coin; import org.bitcoinj.core.Transaction; import org.bitcoinj.core.TransactionConfidence; public class BalanceWithConfirmationTextField extends AnchorPane { private static WalletService walletService; private BalanceListener balanceListener; private AddressConfidenceListener confidenceListener; public static void setWalletService(WalletService walletService) { BalanceWithConfirmationTextField.walletService = walletService; } private final TextField textField; private final Tooltip progressIndicatorTooltip; private final TxConfidenceIndicator txConfidenceIndicator; private final Effect fundedEffect = new DropShadow(BlurType.THREE_PASS_BOX, Color.GREEN, 4, 0.0, 0, 0); private final Effect notFundedEffect = new DropShadow(BlurType.THREE_PASS_BOX, Color.ORANGERED, 4, 0.0, 0, 0); private BSFormatter formatter; /////////////////////////////////////////////////////////////////////////////////////////// // Constructor /////////////////////////////////////////////////////////////////////////////////////////// public BalanceWithConfirmationTextField() { textField = new TextField(); textField.setFocusTraversable(false); textField.setEditable(false); txConfidenceIndicator = new TxConfidenceIndicator(); txConfidenceIndicator.setFocusTraversable(false); txConfidenceIndicator.setPrefSize(24, 24); txConfidenceIndicator.setId("funds-confidence"); txConfidenceIndicator.setLayoutY(1); txConfidenceIndicator.setProgress(0); txConfidenceIndicator.setVisible(false); progressIndicatorTooltip = new Tooltip("-"); Tooltip.install(txConfidenceIndicator, progressIndicatorTooltip); AnchorPane.setRightAnchor(txConfidenceIndicator, 0.0); AnchorPane.setRightAnchor(textField, 55.0); AnchorPane.setLeftAnchor(textField, 0.0); getChildren().addAll(textField, txConfidenceIndicator); } public void cleanup() { walletService.removeBalanceListener(balanceListener); walletService.removeAddressConfidenceListener(confidenceListener); } public void setup(Address address, BSFormatter formatter) { this.formatter = formatter; confidenceListener = new AddressConfidenceListener(address) { @Override public void onTransactionConfidenceChanged(TransactionConfidence confidence) { updateConfidence(confidence); } }; walletService.addAddressConfidenceListener(confidenceListener); updateConfidence(walletService.getConfidenceForAddress(address)); balanceListener = new BalanceListener(address) { @Override public void onBalanceChanged(Coin balance, Transaction tx) { updateBalance(balance); } }; walletService.addBalanceListener(balanceListener); updateBalance(walletService.getBalanceForAddress(address)); } /////////////////////////////////////////////////////////////////////////////////////////// // Private methods /////////////////////////////////////////////////////////////////////////////////////////// private void updateConfidence(TransactionConfidence confidence) { if (confidence != null) { switch (confidence.getConfidenceType()) { case UNKNOWN: progressIndicatorTooltip.setText("Unknown transaction status"); txConfidenceIndicator.setProgress(0); break; case PENDING: progressIndicatorTooltip.setText( "Seen by " + confidence.numBroadcastPeers() + " peer(s) / 0 " + "confirmations"); txConfidenceIndicator.setProgress(-1.0); break; case BUILDING: progressIndicatorTooltip.setText("Confirmed in " + confidence.getDepthInBlocks() + " block(s)"); txConfidenceIndicator.setProgress(Math.min(1, (double) confidence.getDepthInBlocks() / 6.0)); break; case DEAD: progressIndicatorTooltip.setText("Transaction is invalid."); txConfidenceIndicator.setProgress(0); break; } if (txConfidenceIndicator.getProgress() != 0) { txConfidenceIndicator.setVisible(true); AnchorPane.setRightAnchor(txConfidenceIndicator, 0.0); AnchorPane.setRightAnchor(textField, 35.0); } } } private void updateBalance(Coin balance) { textField.setText(formatter.formatCoinWithCode(balance)); if (balance.isPositive()) textField.setEffect(fundedEffect); else textField.setEffect(notFundedEffect); } }