From 07c53560213e02b8ca400fb6451fa0a8b5dde794 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Thu, 9 Apr 2020 16:12:25 +0200 Subject: [PATCH] add copyable label controls --- .../sparrow/control/CopyableIdLabel.java | 16 ++++++++ .../sparrow/control/CopyableLabel.java | 24 ++++++++++++ .../sparrow/control/TextUtils.java | 37 +++++++++++++++++++ .../transaction/HeadersController.java | 18 +++++---- .../sparrow/transaction/InputController.java | 14 ++++--- .../sparrow/transaction/InputsController.java | 8 ++-- .../sparrow/transaction/OutputController.java | 12 +++--- .../transaction/OutputsController.java | 6 +-- .../sparrow/transaction/headers.fxml | 21 ++++++----- .../sparrow/transaction/input.fxml | 14 ++++--- .../sparrow/transaction/inputs.fxml | 7 ++-- .../sparrow/transaction/output.fxml | 8 ++-- .../sparrow/transaction/outputs.fxml | 7 ++-- 13 files changed, 140 insertions(+), 52 deletions(-) create mode 100644 src/main/java/com/sparrowwallet/sparrow/control/CopyableIdLabel.java create mode 100644 src/main/java/com/sparrowwallet/sparrow/control/CopyableLabel.java create mode 100644 src/main/java/com/sparrowwallet/sparrow/control/TextUtils.java diff --git a/src/main/java/com/sparrowwallet/sparrow/control/CopyableIdLabel.java b/src/main/java/com/sparrowwallet/sparrow/control/CopyableIdLabel.java new file mode 100644 index 00000000..dba332a5 --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/control/CopyableIdLabel.java @@ -0,0 +1,16 @@ +package com.sparrowwallet.sparrow.control; + +import javafx.scene.text.Font; + +import java.awt.*; + +public class CopyableIdLabel extends CopyableLabel { + public CopyableIdLabel() { + this(""); + } + + public CopyableIdLabel(String text) { + super(text); + setFont(Font.font("Courier")); + } +} diff --git a/src/main/java/com/sparrowwallet/sparrow/control/CopyableLabel.java b/src/main/java/com/sparrowwallet/sparrow/control/CopyableLabel.java new file mode 100644 index 00000000..f9ce751a --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/control/CopyableLabel.java @@ -0,0 +1,24 @@ +package com.sparrowwallet.sparrow.control; + +import javafx.scene.control.TextField; + +public class CopyableLabel extends TextField { + public CopyableLabel() { + this(""); + } + + public CopyableLabel(String text) { + super(text); + + this.setEditable(false); + this.getStyleClass().add("copyable-label"); + this.setPrefWidth(10); + this.textProperty().addListener((ob, o, n) -> { + // expand the textfield + double width = TextUtils.computeTextWidth(this.getFont(), this.getText(), 0.0D) + 2; + this.setPrefWidth(width); + this.setMaxWidth(width); + }); + } + +} diff --git a/src/main/java/com/sparrowwallet/sparrow/control/TextUtils.java b/src/main/java/com/sparrowwallet/sparrow/control/TextUtils.java new file mode 100644 index 00000000..b9828259 --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/control/TextUtils.java @@ -0,0 +1,37 @@ +package com.sparrowwallet.sparrow.control; + +import javafx.scene.text.Font; +import javafx.scene.text.Text; +import javafx.scene.text.TextBoundsType; + +public class TextUtils { + static final Text helper; + static final double DEFAULT_WRAPPING_WIDTH; + static final double DEFAULT_LINE_SPACING; + static final String DEFAULT_TEXT; + static final TextBoundsType DEFAULT_BOUNDS_TYPE; + + static { + helper = new Text(); + DEFAULT_WRAPPING_WIDTH = helper.getWrappingWidth(); + DEFAULT_LINE_SPACING = helper.getLineSpacing(); + DEFAULT_TEXT = helper.getText(); + DEFAULT_BOUNDS_TYPE = helper.getBoundsType(); + } + + public static double computeTextWidth(Font font, String text, double help0) { + helper.setText(text); + helper.setFont(font); + + helper.setWrappingWidth(0.0D); + helper.setLineSpacing(0.0D); + double d = Math.min(helper.prefWidth(-1.0D), help0); + helper.setWrappingWidth((int) Math.ceil(d)); + d = Math.ceil(helper.getLayoutBounds().getWidth()); + + helper.setWrappingWidth(DEFAULT_WRAPPING_WIDTH); + helper.setLineSpacing(DEFAULT_LINE_SPACING); + helper.setText(DEFAULT_TEXT); + return d; + } +} diff --git a/src/main/java/com/sparrowwallet/sparrow/transaction/HeadersController.java b/src/main/java/com/sparrowwallet/sparrow/transaction/HeadersController.java index 7eecf036..99e4814a 100644 --- a/src/main/java/com/sparrowwallet/sparrow/transaction/HeadersController.java +++ b/src/main/java/com/sparrowwallet/sparrow/transaction/HeadersController.java @@ -2,6 +2,8 @@ package com.sparrowwallet.sparrow.transaction; import com.sparrowwallet.drongo.protocol.Transaction; import com.sparrowwallet.sparrow.EventManager; +import com.sparrowwallet.sparrow.control.CopyableIdLabel; +import com.sparrowwallet.sparrow.control.CopyableLabel; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.*; @@ -19,13 +21,13 @@ public class HeadersController extends TransactionFormController implements Init private HeadersForm headersForm; @FXML - private TextField id; + private CopyableIdLabel id; @FXML private Spinner version; @FXML - private TextField segwit; + private CopyableLabel segwit; @FXML private ToggleGroup locktimeToggleGroup; @@ -61,16 +63,16 @@ public class HeadersController extends TransactionFormController implements Init private DateTimePicker locktimeDate; @FXML - private TextField fee; + private CopyableLabel size; @FXML - private TextField size; + private CopyableLabel virtualSize; @FXML - private TextField virtualSize; + private CopyableLabel fee; @FXML - private TextField feeRateField; + private CopyableLabel feeRate; @Override public void initialize(URL location, ResourceBundle resources) { @@ -175,8 +177,8 @@ public class HeadersController extends TransactionFormController implements Init if(feeAmt != null) { fee.setText(feeAmt + " sats"); - double feeRate = feeAmt.doubleValue() / tx.getVirtualSize(); - feeRateField.setText(String.format("%.2f", feeRate) + " sats/vByte"); + double feeRateAmt = feeAmt.doubleValue() / tx.getVirtualSize(); + feeRate.setText(String.format("%.2f", feeRateAmt) + " sats/vByte"); } else { fee.setText("Unknown"); } diff --git a/src/main/java/com/sparrowwallet/sparrow/transaction/InputController.java b/src/main/java/com/sparrowwallet/sparrow/transaction/InputController.java index b18b37f6..dddd228e 100644 --- a/src/main/java/com/sparrowwallet/sparrow/transaction/InputController.java +++ b/src/main/java/com/sparrowwallet/sparrow/transaction/InputController.java @@ -6,6 +6,8 @@ import com.sparrowwallet.drongo.crypto.ECKey; import com.sparrowwallet.drongo.protocol.*; import com.sparrowwallet.drongo.psbt.PSBTInput; import com.sparrowwallet.sparrow.EventManager; +import com.sparrowwallet.sparrow.control.CopyableIdLabel; +import com.sparrowwallet.sparrow.control.CopyableLabel; import com.sparrowwallet.sparrow.control.RelativeTimelockSpinner; import javafx.fxml.FXML; import javafx.fxml.Initializable; @@ -32,19 +34,19 @@ public class InputController extends TransactionFormController implements Initia private Fieldset inputFieldset; @FXML - private TextField outpoint; + private CopyableIdLabel outpoint; @FXML private Button outpointSelect; @FXML - private TextField spends; + private CopyableLabel spends; @FXML - private Label from; + private CopyableLabel from; @FXML - private TextField address; + private CopyableIdLabel address; @FXML private CodeArea scriptSigArea; @@ -68,7 +70,7 @@ public class InputController extends TransactionFormController implements Initia private CodeArea witnessesArea; @FXML - private TextField signatures; + private CopyableLabel signatures; @FXML private ToggleSwitch rbf; @@ -95,7 +97,7 @@ public class InputController extends TransactionFormController implements Initia private Field locktimeRelativeField; @FXML - private TextField locktimeAbsolute; + private CopyableLabel locktimeAbsolute; @FXML private Spinner locktimeRelativeBlocks; diff --git a/src/main/java/com/sparrowwallet/sparrow/transaction/InputsController.java b/src/main/java/com/sparrowwallet/sparrow/transaction/InputsController.java index 50561340..e4c73575 100644 --- a/src/main/java/com/sparrowwallet/sparrow/transaction/InputsController.java +++ b/src/main/java/com/sparrowwallet/sparrow/transaction/InputsController.java @@ -2,10 +2,10 @@ package com.sparrowwallet.sparrow.transaction; import com.sparrowwallet.drongo.protocol.*; import com.sparrowwallet.drongo.psbt.PSBTInput; +import com.sparrowwallet.sparrow.control.CopyableLabel; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.chart.PieChart; -import javafx.scene.control.TextField; import java.net.URL; import java.util.ArrayList; @@ -16,13 +16,13 @@ public class InputsController extends TransactionFormController implements Initi private InputsForm inputsForm; @FXML - private TextField count; + private CopyableLabel count; @FXML - private TextField total; + private CopyableLabel total; @FXML - private TextField signatures; + private CopyableLabel signatures; @FXML private PieChart inputsPie; diff --git a/src/main/java/com/sparrowwallet/sparrow/transaction/OutputController.java b/src/main/java/com/sparrowwallet/sparrow/transaction/OutputController.java index 4768c9ae..2d3d92d1 100644 --- a/src/main/java/com/sparrowwallet/sparrow/transaction/OutputController.java +++ b/src/main/java/com/sparrowwallet/sparrow/transaction/OutputController.java @@ -3,10 +3,10 @@ package com.sparrowwallet.sparrow.transaction; import com.sparrowwallet.drongo.address.Address; import com.sparrowwallet.drongo.protocol.NonStandardScriptException; import com.sparrowwallet.drongo.protocol.TransactionOutput; +import com.sparrowwallet.sparrow.control.CopyableIdLabel; +import com.sparrowwallet.sparrow.control.CopyableLabel; import javafx.fxml.FXML; import javafx.fxml.Initializable; -import javafx.scene.control.Label; -import javafx.scene.control.TextField; import org.fxmisc.richtext.CodeArea; import tornadofx.control.Fieldset; @@ -20,13 +20,13 @@ public class OutputController extends TransactionFormController implements Initi private Fieldset outputFieldset; @FXML - private TextField value; + private CopyableLabel value; @FXML - private Label to; + private CopyableLabel to; @FXML - private TextField address; + private CopyableIdLabel address; @FXML private CodeArea scriptPubKeyArea; @@ -42,7 +42,7 @@ public class OutputController extends TransactionFormController implements Initi outputFieldset.setText("Output #" + txOutput.getIndex()); value.setText(txOutput.getValue() + " sats"); - + to.setVisible(false); try { Address[] addresses = txOutput.getScript().getToAddresses(); to.setVisible(true); diff --git a/src/main/java/com/sparrowwallet/sparrow/transaction/OutputsController.java b/src/main/java/com/sparrowwallet/sparrow/transaction/OutputsController.java index 4b1e631b..89f50057 100644 --- a/src/main/java/com/sparrowwallet/sparrow/transaction/OutputsController.java +++ b/src/main/java/com/sparrowwallet/sparrow/transaction/OutputsController.java @@ -2,10 +2,10 @@ package com.sparrowwallet.sparrow.transaction; import com.sparrowwallet.drongo.protocol.Transaction; import com.sparrowwallet.drongo.protocol.TransactionOutput; +import com.sparrowwallet.sparrow.control.CopyableLabel; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.chart.PieChart; -import javafx.scene.control.TextField; import java.net.URL; import java.util.ResourceBundle; @@ -14,10 +14,10 @@ public class OutputsController extends TransactionFormController implements Init private OutputsForm outputsForm; @FXML - private TextField count; + private CopyableLabel count; @FXML - private TextField total; + private CopyableLabel total; @FXML private PieChart outputsPie; diff --git a/src/main/resources/com/sparrowwallet/sparrow/transaction/headers.fxml b/src/main/resources/com/sparrowwallet/sparrow/transaction/headers.fxml index d2556d08..65b7b925 100644 --- a/src/main/resources/com/sparrowwallet/sparrow/transaction/headers.fxml +++ b/src/main/resources/com/sparrowwallet/sparrow/transaction/headers.fxml @@ -2,7 +2,6 @@ - @@ -11,8 +10,10 @@ - + + + @@ -31,7 +32,7 @@
- +
@@ -44,7 +45,7 @@ - + @@ -80,10 +81,10 @@
- + - +
@@ -91,10 +92,10 @@
- + - +
@@ -104,10 +105,10 @@
- + - +
diff --git a/src/main/resources/com/sparrowwallet/sparrow/transaction/input.fxml b/src/main/resources/com/sparrowwallet/sparrow/transaction/input.fxml index d329f62b..9b1864f2 100644 --- a/src/main/resources/com/sparrowwallet/sparrow/transaction/input.fxml +++ b/src/main/resources/com/sparrowwallet/sparrow/transaction/input.fxml @@ -12,6 +12,8 @@ + + @@ -30,7 +32,7 @@
- + - -
@@ -93,7 +95,7 @@
- + @@ -128,7 +130,7 @@ - + diff --git a/src/main/resources/com/sparrowwallet/sparrow/transaction/inputs.fxml b/src/main/resources/com/sparrowwallet/sparrow/transaction/inputs.fxml index a7ab81b1..04660a8f 100644 --- a/src/main/resources/com/sparrowwallet/sparrow/transaction/inputs.fxml +++ b/src/main/resources/com/sparrowwallet/sparrow/transaction/inputs.fxml @@ -5,6 +5,7 @@ + @@ -22,10 +23,10 @@
- + - +
@@ -33,7 +34,7 @@
- +
diff --git a/src/main/resources/com/sparrowwallet/sparrow/transaction/output.fxml b/src/main/resources/com/sparrowwallet/sparrow/transaction/output.fxml index eb5c1fa8..546829dc 100644 --- a/src/main/resources/com/sparrowwallet/sparrow/transaction/output.fxml +++ b/src/main/resources/com/sparrowwallet/sparrow/transaction/output.fxml @@ -11,6 +11,8 @@ + + @@ -29,9 +31,9 @@
- -
diff --git a/src/main/resources/com/sparrowwallet/sparrow/transaction/outputs.fxml b/src/main/resources/com/sparrowwallet/sparrow/transaction/outputs.fxml index e7b44807..bae25fcc 100644 --- a/src/main/resources/com/sparrowwallet/sparrow/transaction/outputs.fxml +++ b/src/main/resources/com/sparrowwallet/sparrow/transaction/outputs.fxml @@ -4,8 +4,9 @@ - + + @@ -22,10 +23,10 @@
- + - +