Craig Raw
5 years ago
13 changed files with 202 additions and 31 deletions
@ -1 +1 @@ |
|||
Subproject commit 2f5655708df1b7722dfb4098cd9c79aad1a35886 |
|||
Subproject commit 14767c3250281d52c1b7da21c1ab0dc83076fda8 |
@ -0,0 +1,79 @@ |
|||
package com.sparrowwallet.sparrow.control; |
|||
|
|||
import com.sparrowwallet.drongo.Utils; |
|||
import com.sparrowwallet.drongo.address.Address; |
|||
import javafx.beans.property.ObjectProperty; |
|||
import javafx.beans.property.SimpleObjectProperty; |
|||
import javafx.scene.control.ContextMenu; |
|||
import javafx.scene.control.MenuItem; |
|||
import javafx.scene.control.Tooltip; |
|||
import javafx.scene.input.Clipboard; |
|||
import javafx.scene.input.ClipboardContent; |
|||
|
|||
public class AddressLabel extends IdLabel { |
|||
private final ObjectProperty<Address> addressProperty = new SimpleObjectProperty<>(); |
|||
|
|||
private Tooltip tooltip; |
|||
private AddressContextMenu contextMenu; |
|||
|
|||
public AddressLabel() { |
|||
this(""); |
|||
} |
|||
|
|||
public AddressLabel(String text) { |
|||
super(text); |
|||
addressProperty().addListener((observable, oldValue, newValue) -> { |
|||
setAddressAsText(newValue); |
|||
contextMenu.copyHex.setText("Copy " + newValue.getOutputScriptDataType()); |
|||
}); |
|||
|
|||
tooltip = new Tooltip(); |
|||
contextMenu = new AddressContextMenu(); |
|||
} |
|||
|
|||
public final ObjectProperty<Address> addressProperty() { |
|||
return addressProperty; |
|||
} |
|||
|
|||
public final Address getAddress() { |
|||
return addressProperty.get(); |
|||
} |
|||
|
|||
public final void setAddress(Address value) { |
|||
this.addressProperty.set(value); |
|||
} |
|||
|
|||
private void setAddressAsText(Address address) { |
|||
if(address != null) { |
|||
tooltip.setText(address.getScriptType() + " " + Utils.bytesToHex(address.getOutputScriptData())); |
|||
setTooltip(tooltip); |
|||
setContextMenu(contextMenu); |
|||
setText(address.toString()); |
|||
} |
|||
} |
|||
|
|||
private class AddressContextMenu extends ContextMenu { |
|||
private MenuItem copyAddress; |
|||
private MenuItem copyHex; |
|||
|
|||
public AddressContextMenu() { |
|||
copyAddress = new MenuItem("Copy Address"); |
|||
copyAddress.setOnAction(AE -> { |
|||
hide(); |
|||
ClipboardContent content = new ClipboardContent(); |
|||
content.putString(getAddress().toString()); |
|||
Clipboard.getSystemClipboard().setContent(content); |
|||
}); |
|||
|
|||
copyHex = new MenuItem("Copy Script Output Bytes"); |
|||
copyHex.setOnAction(AE -> { |
|||
hide(); |
|||
ClipboardContent content = new ClipboardContent(); |
|||
content.putString(Utils.bytesToHex(getAddress().getOutputScriptData())); |
|||
Clipboard.getSystemClipboard().setContent(content); |
|||
}); |
|||
|
|||
getItems().addAll(copyAddress, copyHex); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,88 @@ |
|||
package com.sparrowwallet.sparrow.control; |
|||
|
|||
import com.sparrowwallet.drongo.protocol.Transaction; |
|||
import javafx.beans.property.LongProperty; |
|||
import javafx.beans.property.SimpleLongProperty; |
|||
import javafx.scene.control.ContextMenu; |
|||
import javafx.scene.control.MenuItem; |
|||
import javafx.scene.control.Tooltip; |
|||
import javafx.scene.input.Clipboard; |
|||
import javafx.scene.input.ClipboardContent; |
|||
|
|||
import java.text.DecimalFormat; |
|||
import java.text.DecimalFormatSymbols; |
|||
import java.util.Locale; |
|||
|
|||
public class CoinLabel extends CopyableLabel { |
|||
public static final int MAX_SATS_SHOWN = 1000000; |
|||
|
|||
private DecimalFormat btcFormat = new DecimalFormat("0", DecimalFormatSymbols.getInstance(Locale.ENGLISH)); |
|||
|
|||
private final LongProperty value = new SimpleLongProperty(); |
|||
private Tooltip tooltip; |
|||
private CoinContextMenu contextMenu; |
|||
|
|||
public CoinLabel() { |
|||
this("Unknown"); |
|||
} |
|||
|
|||
public CoinLabel(String text) { |
|||
super(text); |
|||
btcFormat.setMaximumFractionDigits(8); |
|||
valueProperty().addListener((observable, oldValue, newValue) -> setValueAsText((Long)newValue)); |
|||
tooltip = new Tooltip(); |
|||
contextMenu = new CoinContextMenu(); |
|||
} |
|||
|
|||
public final LongProperty valueProperty() { |
|||
return value; |
|||
} |
|||
|
|||
public final long getValue() { |
|||
return value.get(); |
|||
} |
|||
|
|||
public final void setValue(long value) { |
|||
this.value.set(value); |
|||
} |
|||
|
|||
private void setValueAsText(Long value) { |
|||
setTooltip(tooltip); |
|||
setContextMenu(contextMenu); |
|||
|
|||
String satsValue = String.format(Locale.ENGLISH, "%,d",value) + " sats"; |
|||
String btcValue = btcFormat.format(value.doubleValue() / Transaction.SATOSHIS_PER_BITCOIN) + " BTC"; |
|||
if(value > MAX_SATS_SHOWN) { |
|||
tooltip.setText(satsValue); |
|||
setText(btcValue); |
|||
} else { |
|||
tooltip.setText(btcValue); |
|||
setText(satsValue); |
|||
} |
|||
} |
|||
|
|||
private class CoinContextMenu extends ContextMenu { |
|||
private MenuItem copySatsValue; |
|||
private MenuItem copyBtcValue; |
|||
|
|||
public CoinContextMenu() { |
|||
copySatsValue = new MenuItem("Copy Value in Satoshis"); |
|||
copySatsValue.setOnAction(AE -> { |
|||
hide(); |
|||
ClipboardContent content = new ClipboardContent(); |
|||
content.putString(Long.toString(getValue())); |
|||
Clipboard.getSystemClipboard().setContent(content); |
|||
}); |
|||
|
|||
copyBtcValue = new MenuItem("Copy Value in BTC"); |
|||
copyBtcValue.setOnAction(AE -> { |
|||
hide(); |
|||
ClipboardContent content = new ClipboardContent(); |
|||
content.putString(btcFormat.format((double)getValue() / Transaction.SATOSHIS_PER_BITCOIN)); |
|||
Clipboard.getSystemClipboard().setContent(content); |
|||
}); |
|||
|
|||
getItems().addAll(copySatsValue, copyBtcValue); |
|||
} |
|||
} |
|||
} |
Loading…
Reference in new issue