Skip to content

Commit

Permalink
Add token components to wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed May 4, 2024
1 parent 6a43164 commit d92045c
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 12 deletions.
13 changes: 8 additions & 5 deletions convex-gui/src/main/java/convex/gui/components/ActionButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
@SuppressWarnings("serial")
public class ActionButton extends JButton {

public ActionButton(String text, int iconCode, ActionListener al) {
public ActionButton(String text, int iconCode, ActionListener action) {
super(text,(iconCode>0)?Toolkit.menuIcon(iconCode):null);
this.addActionListener(al);
this.addActionListener(action);
}

public ActionButton(String text, ActionListener al) {
super(text);
this.addActionListener(al);
public ActionButton(String text, ActionListener action) {
this(text,0,action);
}

public ActionButton(int icon, ActionListener action) {
this(null,icon,action);
}
}
17 changes: 15 additions & 2 deletions convex-gui/src/main/java/convex/gui/components/BalanceLabel.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public BalanceMenu() {
public static final Color COPPER=new Color(150,80,30);

protected int decimals=9;
private Color balanceColour=GOLD;

public BalanceLabel() {
this.setEditable(false);
Expand All @@ -61,6 +62,14 @@ public BalanceLabel() {
public void setBalance(long a) {
setBalance(CVMLong.create(a));
}

public void setDecimals(int decimals) {
this.decimals=decimals;
}

public void setBalanceColour(Color c) {
this.balanceColour=c;
}

public void setBalance(AInteger a) {
try {
Expand All @@ -81,13 +90,17 @@ public void setBalance(AInteger a) {

setText("");
String cs=Text.toFriendlyNumber(coins.longValue());
append(cs,GOLD,size);
append(cs,balanceColour,size);
append(".",SILVER,size);
String ch=Text.zeroPad(change,decimals);
int decimalSize=size*2/3;
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,size*2/3);
append(chs,c,decimalSize);
}
for (int i=decimals; i<9; i++) {
append(" ",balanceColour,decimalSize);
}

Toolkit.addPopupMenu(this, new BalanceMenu());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class ScrollyList<E> extends JScrollPane {
private final ListModel<E> model;
private final ScrollablePanel listPanel = new ScrollablePanel();

private void refreshList() {
public void refreshList() {
EventQueue.invokeLater(()->{;
listPanel.removeAll();
int n = model.getSize();
Expand Down Expand Up @@ -72,7 +72,7 @@ public ScrollyList(ListModel<E> model, Function<E, Component> builder) {
super();
this.builder = builder;
this.model = model;
this.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
// this.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

listPanel.setLayout(new MigLayout("wrap 1"));
setViewportView(listPanel);
Expand Down Expand Up @@ -102,7 +102,5 @@ public void contentsChanged(ListDataEvent e) {
refreshList();
}
});

refreshList();
}
}
59 changes: 59 additions & 0 deletions convex-gui/src/main/java/convex/gui/wallet/TokenComponent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package convex.gui.wallet;

import java.awt.Font;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JPanel;

import convex.api.Convex;
import convex.core.data.ACell;
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 static SymbolIcon DEFAULT_ICON=SymbolIcon.get(0xf041, Toolkit.ICON_SIZE);

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

this.setLayout(new MigLayout("","["+(Toolkit.ICON_SIZE+10)+"][200][300][300]push"));
this.setBorder(Toolkit.createEmptyBorder(20));

ACell tokenID=token.getID();
Icon icon=(tokenID==null)?Toolkit.CONVEX:DEFAULT_ICON;
add(new JButton(icon));

String symbolName=token.symbol();
CodeLabel symLabel=new CodeLabel(symbolName);
symLabel.setFont(Toolkit.MONO_FONT.deriveFont(Font.BOLD));
symLabel.setToolTipText(symbolName+" has Token ID: "+tokenID);
add(symLabel);

balanceLabel = new BalanceLabel();
balanceLabel.setFont(Toolkit.MONO_FONT);
balanceLabel.setBalance(0);
balanceLabel.setToolTipText("Account balance for "+symbolName);
add(balanceLabel);

JPanel actions=new JPanel();
actions.add(new ActionButton(0xe5d5,e->{
// refresh
}));
actions.add(new ActionButton(0xe872,e->{
WalletPanel.model.removeElement(token);
}));
add(actions,"dock east");
}
}
80 changes: 79 additions & 1 deletion convex-gui/src/main/java/convex/gui/wallet/WalletPanel.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,97 @@
package convex.gui.wallet;

import java.awt.Color;

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.gui.components.ActionButton;
import convex.gui.components.ActionPanel;
import convex.gui.components.ScrollyList;
import convex.gui.components.Toast;
import net.miginfocom.swing.MigLayout;

@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;

static DefaultListModel<TokenInfo> model= new DefaultListModel<TokenInfo>();

public WalletPanel(Convex convex) {
this.convex=convex;
setLayout(new MigLayout("fill"));

list=new ScrollyList<>(model, token->{
return new TokenComponent(convex,token);
});

add(list,"dock center");
// add(new AccountOverview(convex),"dock north");

model.addElement(new TokenInfo(null));


// add(new AccountChooserPanel(convex),"dock south");

ActionPanel ap=new ActionPanel();
ap.add(new ActionButton("Track Token",0xe145,e->{
String newID=JOptionPane.showInputDialog(WalletPanel.this, "Enter Token ID");
if (newID==null) return;
try {
ACell tokenID=newID.isBlank()?null:Reader.read(newID);
TokenInfo token=TokenInfo.forID(tokenID);
if (model.contains(token)) {
Toast.display(WalletPanel.this, "Token already added",Color.ORANGE);
} else {
model.addElement(token);
}
} catch (Exception ex) {
Toast.display(WalletPanel.this, "Error adding token: "+ex.getMessage(),Color.ORANGE);
}
}));
ap.add(new ActionButton("Refresh",0xe5d5,e->{
list.refreshList();
}));
add(ap,"dock south");
}
}

0 comments on commit d92045c

Please sign in to comment.