Skip to content

Commit

Permalink
Colourful new balance labels
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Apr 30, 2024
1 parent 5496cef commit 81f5355
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 10 deletions.
18 changes: 18 additions & 0 deletions convex-core/src/main/java/convex/core/text/Text.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<digits) {
s=ZEROS_9.substring(0,digits-n)+s;
}
return s;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import convex.core.data.Address;
import convex.core.data.prim.AInteger;
import convex.core.lang.ops.Special;
import convex.core.text.Text;
import convex.gui.components.account.AddressCombo;
import convex.gui.components.account.KeyPairCombo;
import net.miginfocom.swing.MigLayout;
Expand All @@ -29,7 +28,7 @@ public class AccountChooserPanel extends JPanel {
public final AddressCombo addressCombo;
public final KeyPairCombo keyCombo;

private JLabel balanceLabel;
private BalanceLabel balanceLabel;

protected Convex convex;

Expand Down Expand Up @@ -82,7 +81,7 @@ public AccountChooserPanel(Convex convex) {

// Balance Info
mp.add(new JLabel("Balance: "));
balanceLabel = new JLabel("0");
balanceLabel = new BalanceLabel();
balanceLabel.setToolTipText("Convex Coin balance of the currently selected Account");
mp.add(balanceLabel);
updateBalance(getAddress());
Expand Down Expand Up @@ -129,12 +128,14 @@ private void updateBalance(Address a) {
ACell bal=r.getValue();
String s="<unknown>";
if (bal instanceof AInteger) {
s=Text.toFriendlyBalance(((AInteger)bal).longValue());
balanceLabel.setBalance((AInteger)bal);
} else {
balanceLabel.setBalance(null);
}
if (r.getErrorCode()!=null) {
s="<"+r.getErrorCode()+">";

balanceLabel.setText(s);
}
balanceLabel.setText(s);
});
} catch (Throwable t) {
balanceLabel.setText(t.getClass().getName());
Expand Down
61 changes: 61 additions & 0 deletions convex-gui/src/main/java/convex/gui/components/BalanceLabel.java
Original file line number Diff line number Diff line change
@@ -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("<No balance>");
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<decimals; i+=3) {
Color c=changeColour(i);
String chs=ch.substring(i,Math.min(decimals,i+3));
append(chs,c,getFont().getSize()*2/3);
}
} catch (Throwable e) {
e.printStackTrace();
setText(e.getMessage());
}

}

private static Color[] CCOLS=new Color[] {new Color(200,200,230), new Color(180,120,60), new Color(150,80,30)};

private static Color changeColour(int i) {
return CCOLS[(i/3)%3];
}

private static BigInteger getUnit(int decimals) {
return BigInteger.TEN.pow(decimals);
}
}
15 changes: 11 additions & 4 deletions convex-gui/src/main/java/convex/gui/components/BaseTextPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ public class BaseTextPane extends JTextPane {
private static final StyleContext sc = StyleContext.getDefaultStyleContext();


public void append(String text, Color c) {
AttributeSet aset = sc.getEmptySet();
public void append(String text, Color c, Integer size) {
AttributeSet aset = SimpleAttributeSet.EMPTY;
if (c!=null) {
aset=sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);
aset=sc.addAttribute(aset, StyleConstants.Foreground, c);
}
if (size!=null) {
aset=sc.addAttribute(aset, StyleConstants.FontSize, size);
}

StyledDocument d=getStyledDocument();
Expand All @@ -38,7 +41,11 @@ public void append(String text, Color c) {
repaint();
}

public void append(String text, Color c) {
append(text,c,null);
}

public void append(String text) {
append(text,null);
append(text,null,null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public PeerWindow(ConvexLocal peer) {
tabbedPane.addTab("REPL", null, new REPLPanel(peer), null);
} catch (Throwable t) {
String msg=("Failed to connect to Peer: "+t);
t.printStackTrace();
log.warn(msg);
tabbedPane.addTab("REPL Error", null, new JLabel(msg), null);
return;
Expand Down

0 comments on commit 81f5355

Please sign in to comment.