Skip to content

Commit

Permalink
Adding more token information to GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed May 4, 2024
1 parent d92045c commit 01629f7
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 56 deletions.
16 changes: 2 additions & 14 deletions convex-gui/src/main/java/convex/gui/components/BalanceLabel.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

import convex.api.Convex;
import convex.core.ErrorCodes;
import convex.core.Result;
import convex.core.data.ACell;
Expand Down Expand Up @@ -91,9 +90,9 @@ public void setBalance(AInteger a) {
setText("");
String cs=Text.toFriendlyNumber(coins.longValue());
append(cs,balanceColour,size);
append(".",SILVER,size);
String ch=Text.zeroPad(change,decimals);
int decimalSize=size*2/3;
append(decimals>0?".":" ",SILVER,decimalSize);
for (int i=0; i<decimals; i+=3) {
Color c=changeColour(i);
String chs=ch.substring(i,Math.min(decimals,i+3));
Expand Down Expand Up @@ -121,25 +120,14 @@ private static Color changeColour(int i) {
private static BigInteger getUnit(int decimals) {
return BigInteger.TEN.pow(decimals);
}

public void setBalance(Convex convex) {
try {
Long bal=convex.getBalance();
if (bal!=null) {
setBalance(bal);
}
} catch (Exception e) {
setText("<Can't get balance>");
}
}

@Override
public void setText(String s) {
super.setText("");
append(s,Color.ORANGE,getFont().getSize());
}

public void setBalance(Result r) {
public void setFromResult(Result r) {
ACell bal=r.getValue();
ACell error=r.getErrorCode();
if (error!=null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,8 @@ public void contentsChanged(ListDataEvent e) {
}
});
}

public Component[] getListComponents() {
return listPanel.getComponents();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public void updateBalance() {
private void updateBalance(Address a) {
try {
convex.query(Special.get("*balance*"),a).thenAccept(r-> {
balanceLabel.setBalance(r);
balanceLabel.setFromResult(r);
});
} catch (Throwable t) {
balanceLabel.setText(t.getClass().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ public AccountOverview(Convex convex) {
add(al);
}

{ // Coin Balance
try { // Coin Balance
//add(new JLabel(Toolkit.CONVEX)); // convex icon
balance.setFont(bigfont);
balance.setBalance(convex);
balance.setBalance(convex.getBalance());
} catch (Exception e) {
// nothing
} finally {
add(balance);
}
//add(KeyPairCombo.forConvex(convex));
Expand Down
20 changes: 16 additions & 4 deletions convex-gui/src/main/java/convex/gui/wallet/TokenComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,30 @@
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import convex.api.Convex;
import convex.core.data.ACell;
import convex.core.data.prim.AInteger;
import convex.gui.components.ActionButton;
import convex.gui.components.BalanceLabel;
import convex.gui.components.CodeLabel;
import convex.gui.utils.SymbolIcon;
import convex.gui.utils.Toolkit;
import convex.gui.wallet.WalletPanel.TokenInfo;
import net.miginfocom.swing.MigLayout;

@SuppressWarnings("serial")
public class TokenComponent extends JPanel {

protected Convex convex;

protected BalanceLabel balanceLabel;
private TokenInfo token;

private static SymbolIcon DEFAULT_ICON=SymbolIcon.get(0xf041, Toolkit.ICON_SIZE);

public TokenComponent(Convex convex, TokenInfo token) {
this.convex=convex;
this.token=token;

this.setLayout(new MigLayout("","["+(Toolkit.ICON_SIZE+10)+"][200][300][300]push"));
this.setBorder(Toolkit.createEmptyBorder(20));
Expand All @@ -45,15 +47,25 @@ public TokenComponent(Convex convex, TokenInfo token) {
balanceLabel.setFont(Toolkit.MONO_FONT);
balanceLabel.setBalance(0);
balanceLabel.setToolTipText("Account balance for "+symbolName);
add(balanceLabel);
add(balanceLabel,"align right");

JPanel actions=new JPanel();
actions.add(new ActionButton(0xe5d5,e->{
// refresh
refresh(convex);
}));
actions.add(new ActionButton(0xe872,e->{
WalletPanel.model.removeElement(token);
}));
add(actions,"dock east");
SwingUtilities.invokeLater(()->refresh(convex));
}

public void refresh(Convex convex) {
AInteger bal=token.getBalance(convex);
if (bal!=null) {
balanceLabel.setBalance(bal);
} else {
balanceLabel.setBalance(null);
}
}
}
73 changes: 73 additions & 0 deletions convex-gui/src/main/java/convex/gui/wallet/TokenInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package convex.gui.wallet;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import convex.api.Convex;
import convex.core.Result;
import convex.core.data.ACell;
import convex.core.data.Address;
import convex.core.data.Cells;
import convex.core.data.prim.AInteger;
import convex.core.data.prim.CVMLong;

public class TokenInfo {
private ACell id;

public TokenInfo(ACell tokenID) {
this.id=tokenID;
}

public ACell getID() {
return id;
}

public String symbol() {
return (id==null)?"CVM":"???";
}

public int decimals() {
return (id==null)?9:2;
}

public static TokenInfo forID(ACell tokenID) {
TokenInfo tokenInfo=new TokenInfo(tokenID);
return tokenInfo;
}

@Override
public boolean equals(Object a) {
if (a instanceof TokenInfo) {
return Cells.equals(id, ((TokenInfo)a).id);
} else {
return false;
}
}

public AInteger getBalance(Convex convex) {
try {
if (id==null) {
long convexBalance=convex.getBalance();
return CVMLong.create(convexBalance);
} else {
Result r=convex.querySync("("+getFungibleAddress(convex)+"/balance "+id+")");
if (!r.isError()) {
ACell val=r.getValue();
if (val instanceof AInteger) {
return (AInteger) val;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

static Address fungibleAddress=null;
private static Address getFungibleAddress(Convex convex) throws TimeoutException, IOException {
if (fungibleAddress!=null) return fungibleAddress;
fungibleAddress=convex.querySync("(import convex.fungible)").getValue();
return fungibleAddress;
}
}
59 changes: 24 additions & 35 deletions convex-gui/src/main/java/convex/gui/wallet/WalletPanel.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package convex.gui.wallet;

import java.awt.Color;
import java.awt.Component;

import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import convex.api.Convex;
import convex.core.data.ACell;
import convex.core.data.Cells;
import convex.core.lang.Reader;
import convex.core.util.ThreadUtils;
import convex.gui.components.ActionButton;
import convex.gui.components.ActionPanel;
import convex.gui.components.ScrollyList;
Expand All @@ -18,40 +19,6 @@

@SuppressWarnings("serial")
public class WalletPanel extends JPanel {
public static class TokenInfo {
private ACell id;

public TokenInfo(ACell tokenID) {
this.id=tokenID;
}

public ACell getID() {
return id;
}

public String symbol() {
return (id==null)?"CVM":"???";
}

public int decimals() {
return (id==null)?9:2;
}

public static TokenInfo forID(ACell tokenID) {
TokenInfo tokenInfo=new TokenInfo(tokenID);
return tokenInfo;
}

@Override
public boolean equals(Object a) {
if (a instanceof TokenInfo) {
return Cells.equals(id, ((TokenInfo)a).id);
} else {
return false;
}
}
}

protected ScrollyList<TokenInfo> list;

protected Convex convex;
Expand Down Expand Up @@ -93,5 +60,27 @@ public WalletPanel(Convex convex) {
list.refreshList();
}));
add(ap,"dock south");

ThreadUtils.runVirtual(this::updateLoop);
}

public void updateLoop() {
while (true) {
try {
if (isShowing()) {
Component[] comps=list.getListComponents();
for (Component c: comps) {
if ((c instanceof TokenComponent)&&c.isShowing()) {
((TokenComponent)c).refresh(convex);
}
}
}
Thread.sleep(1000);
} catch (InterruptedException e) {
return;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

0 comments on commit 01629f7

Please sign in to comment.