From e88d6265b41d384c4a3dbf180485f9fed133df01 Mon Sep 17 00:00:00 2001 From: Jimbo Date: Fri, 2 Apr 2021 06:29:15 -0400 Subject: [PATCH] Adding export button for saving addresses as CSV --- .../sparrow/wallet/AddressesController.java | 45 +++++++++++++++++++ .../sparrow/wallet/addresses.fxml | 9 +++- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/AddressesController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/AddressesController.java index 2fd91901..5ab5f51d 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/AddressesController.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/AddressesController.java @@ -1,19 +1,30 @@ package com.sparrowwallet.sparrow.wallet; +import com.csvreader.CsvWriter; import com.google.common.eventbus.Subscribe; import com.sparrowwallet.drongo.KeyPurpose; import com.sparrowwallet.drongo.wallet.WalletNode; +import com.sparrowwallet.sparrow.AppServices; import com.sparrowwallet.sparrow.EventManager; import com.sparrowwallet.sparrow.control.AddressTreeTable; import com.sparrowwallet.sparrow.event.*; +import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; +import javafx.stage.FileChooser; +import javafx.stage.Stage; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.io.*; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.util.List; import java.util.ResourceBundle; public class AddressesController extends WalletFormController implements Initializable { + private static final Logger log = LoggerFactory.getLogger(AddressesController.class); + @FXML private AddressTreeTable receiveTable; @@ -83,4 +94,38 @@ public class AddressesController extends WalletFormController implements Initial changeTable.updateAll(getWalletForm().getNodeEntry(KeyPurpose.CHANGE)); } } + + public void exportReceiveAddresses(ActionEvent event) { + exportFile(); + } + + private void exportFile() { + Stage window = new Stage(); + + FileChooser fileChooser = new FileChooser(); + fileChooser.setTitle("Export Addresses File"); + String extension = "txt"; + fileChooser.setInitialFileName(getWalletForm().getWallet().getName() + "-" + + "addresses" + + (extension == null || extension.isEmpty() ? "" : "." + extension)); + + File file = fileChooser.showSaveDialog(window); + if(file != null) { + try(FileOutputStream outputStream = new FileOutputStream(file)) { + CsvWriter writer = new CsvWriter(outputStream, ',', StandardCharsets.UTF_8); + writer.writeRecord(new String[] {"Index", "Payment Address", "Derivation"}); + for(Entry entry : getWalletForm().getNodeEntry(KeyPurpose.RECEIVE).getChildren()) { + NodeEntry childEntry = (NodeEntry)entry; + writer.write(childEntry.getNode().getIndex() + ""); + writer.write(childEntry.getNode().toString()); + writer.write(childEntry.getNode().getDerivationPath()); + writer.endRecord(); + } + writer.close(); + } catch(IOException e) { + log.error("Error exporting addresses as CSV", e); + AppServices.showErrorDialog("Error exporting addresses as CSV", e.getMessage()); + } + } + } } diff --git a/src/main/resources/com/sparrowwallet/sparrow/wallet/addresses.fxml b/src/main/resources/com/sparrowwallet/sparrow/wallet/addresses.fxml index 5a0eac81..8d869ce1 100644 --- a/src/main/resources/com/sparrowwallet/sparrow/wallet/addresses.fxml +++ b/src/main/resources/com/sparrowwallet/sparrow/wallet/addresses.fxml @@ -21,7 +21,14 @@ -