From d92045c279f1ae150f94ee81b78e8a616c20b1c9 Mon Sep 17 00:00:00 2001 From: mikera Date: Sat, 4 May 2024 12:06:21 +0100 Subject: [PATCH] Add token components to wallet --- .../convex/gui/components/ActionButton.java | 13 +-- .../convex/gui/components/BalanceLabel.java | 17 +++- .../convex/gui/components/ScrollyList.java | 6 +- .../convex/gui/wallet/TokenComponent.java | 59 ++++++++++++++ .../java/convex/gui/wallet/WalletPanel.java | 80 ++++++++++++++++++- 5 files changed, 163 insertions(+), 12 deletions(-) create mode 100644 convex-gui/src/main/java/convex/gui/wallet/TokenComponent.java diff --git a/convex-gui/src/main/java/convex/gui/components/ActionButton.java b/convex-gui/src/main/java/convex/gui/components/ActionButton.java index 09fe652d1..6bcf3fd98 100644 --- a/convex-gui/src/main/java/convex/gui/components/ActionButton.java +++ b/convex-gui/src/main/java/convex/gui/components/ActionButton.java @@ -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); } } diff --git a/convex-gui/src/main/java/convex/gui/components/BalanceLabel.java b/convex-gui/src/main/java/convex/gui/components/BalanceLabel.java index 517f43e9e..90ae58326 100644 --- a/convex-gui/src/main/java/convex/gui/components/BalanceLabel.java +++ b/convex-gui/src/main/java/convex/gui/components/BalanceLabel.java @@ -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); @@ -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 { @@ -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 extends JScrollPane { private final ListModel model; private final ScrollablePanel listPanel = new ScrollablePanel(); - private void refreshList() { + public void refreshList() { EventQueue.invokeLater(()->{; listPanel.removeAll(); int n = model.getSize(); @@ -72,7 +72,7 @@ public ScrollyList(ListModel model, Function 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); @@ -102,7 +102,5 @@ public void contentsChanged(ListDataEvent e) { refreshList(); } }); - - refreshList(); } } diff --git a/convex-gui/src/main/java/convex/gui/wallet/TokenComponent.java b/convex-gui/src/main/java/convex/gui/wallet/TokenComponent.java new file mode 100644 index 000000000..65e3b6d11 --- /dev/null +++ b/convex-gui/src/main/java/convex/gui/wallet/TokenComponent.java @@ -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"); + } +} diff --git a/convex-gui/src/main/java/convex/gui/wallet/WalletPanel.java b/convex-gui/src/main/java/convex/gui/wallet/WalletPanel.java index f88252113..acd58e0f8 100644 --- a/convex-gui/src/main/java/convex/gui/wallet/WalletPanel.java +++ b/convex-gui/src/main/java/convex/gui/wallet/WalletPanel.java @@ -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 list; + + protected Convex convex; + + static DefaultListModel model= new DefaultListModel(); 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"); } }