From 81f53551f2a6a899fdd3c704f81200d0a6f8bb03 Mon Sep 17 00:00:00 2001 From: mikera Date: Tue, 30 Apr 2024 11:10:00 +0100 Subject: [PATCH] Colourful new balance labels --- .../src/main/java/convex/core/text/Text.java | 18 ++++++ .../gui/components/AccountChooserPanel.java | 13 ++-- .../convex/gui/components/BalanceLabel.java | 61 +++++++++++++++++++ .../convex/gui/components/BaseTextPane.java | 15 +++-- .../convex/gui/peer/windows/PeerWindow.java | 1 + 5 files changed, 98 insertions(+), 10 deletions(-) create mode 100644 convex-gui/src/main/java/convex/gui/components/BalanceLabel.java diff --git a/convex-core/src/main/java/convex/core/text/Text.java b/convex-core/src/main/java/convex/core/text/Text.java index d8d9d54b6..bb0f2d5b8 100644 --- a/convex-core/src/main/java/convex/core/text/Text.java +++ b/convex-core/src/main/java/convex/core/text/Text.java @@ -1,5 +1,6 @@ package convex.core.text; +import java.math.BigInteger; import java.text.DecimalFormat; import java.time.Instant; import java.time.format.DateTimeFormatter; @@ -155,6 +156,23 @@ public static int columnCount(String text) { return result; } + /** + * Zero pads a positive integer out to the specified number of digits + * @param change + * @param digits + * @return + */ + public static String zeroPad(BigInteger b, int digits) { + if (digits>9) throw new IllegalArgumentException("Too many digits!!"); + if (b.signum()<0) throw new IllegalArgumentException("Negative number!"); + String s=b.toString(); + int n=s.length(); + if (n"; + + balanceLabel.setText(s); } - balanceLabel.setText(s); }); } catch (Throwable t) { balanceLabel.setText(t.getClass().getName()); diff --git a/convex-gui/src/main/java/convex/gui/components/BalanceLabel.java b/convex-gui/src/main/java/convex/gui/components/BalanceLabel.java new file mode 100644 index 000000000..fa40e24b3 --- /dev/null +++ b/convex-gui/src/main/java/convex/gui/components/BalanceLabel.java @@ -0,0 +1,61 @@ +package convex.gui.components; + +import java.awt.Color; +import java.math.BigInteger; + +import convex.core.data.prim.AInteger; +import convex.core.data.prim.CVMLong; +import convex.core.text.Text; + +@SuppressWarnings("serial") +public class BalanceLabel extends BaseTextPane { + + protected int decimals=9; + + public BalanceLabel() { + + } + + public void setBalance(long a) { + setBalance(CVMLong.create(a)); + } + + public void setBalance(AInteger a) { + try { + if (a==null) { + setText(""); + return; + } + + BigInteger unit=getUnit(decimals); + BigInteger bi=a.big(); + BigInteger change=bi.remainder(unit); + BigInteger coins=bi.divide(unit); + + setText(""); + String cs=Text.toFriendlyNumber(coins.longValue()); + append(cs,Color.YELLOW); + append("."); + String ch=Text.zeroPad(change,decimals); + for (int i=0; i