Skip to content

Commit

Permalink
More wallet edits
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Sep 4, 2024
1 parent f49b4da commit af30554
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 25 deletions.
11 changes: 11 additions & 0 deletions convex-gui/src/main/java/convex/gui/components/BalanceLabel.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package convex.gui.components;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.math.BigInteger;

import javax.swing.JMenuItem;
Expand Down Expand Up @@ -64,6 +66,15 @@ public void setDecimals(int decimals) {
public void setBalanceColour(Color c) {
this.balanceColour=c;
}

@Override
public Dimension getPreferredSize() {
Dimension d=super.getPreferredSize();
FontMetrics font=getFontMetrics(getFont());
int pw=font.charWidth('0')*(15+decimals);
if (d.width<pw) d.width=pw;
return d;
}

public void setBalance(AInteger a) {
if (Utils.equals(a, balance)) return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package convex.gui.components;

import java.awt.Dimension;
import java.awt.FontMetrics;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;
Expand All @@ -26,7 +28,7 @@ public DecimalAmountField(int decimals) {
super("0");
this.decimals=decimals;
this.setHorizontalAlignment(SwingConstants.RIGHT);
setColumns(20);
//setColumns(10+decimals);
}

public static DecimalFormat getNumberFormat(int decimals) {
Expand All @@ -39,6 +41,15 @@ protected Document createDefaultModel() {
return new DecimalDocument();
}

@Override
public Dimension getPreferredSize() {
Dimension d=super.getPreferredSize();
FontMetrics font=getFontMetrics(getFont());
int pw=font.charWidth('0')*(10+decimals);
if (d.width<pw) d.width=pw;
return d;
}

public class DecimalDocument extends PlainDocument {
@Override
public void insertString(int offset, String s, AttributeSet a) throws BadLocationException {
Expand Down
16 changes: 7 additions & 9 deletions convex-gui/src/main/java/convex/gui/wallet/SwapPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,13 @@ public SwapPanel(Convex convex, TokenInfo token1, TokenInfo token2) {
}

protected void addSwapComponents() {
swapPanel.setLayout(new MigLayout("fill,wrap 3","[150][grow]"));
swapPanel.removeAll();

swapPanel.setLayout(new MigLayout("fill,wrap 3"));

swapPanel.add(new JLabel("Amount:"));
amountField=new DecimalAmountField(token1.getDecimals());
amountField.setFont(Toolkit.BIG_FONT);
swapPanel.add(new TokenButton(token1));
swapPanel.add(amountField,"span");
amountField.getDocument().addDocumentListener(new DocumentListener() {
@Override
Expand All @@ -92,10 +93,7 @@ public void removeUpdate(DocumentEvent e) {
public void changedUpdate(DocumentEvent e) {}
});
amountField.setToolTipText("Input amount of "+token1.getSymbol()+" to swap into "+token2.getSymbol());

swapPanel.add(new JLabel("From:"));
swapPanel.add(new TokenButton(token1),"wrap,grow");


JButton switchButton=new JButton(SymbolIcon.get(0xe8d5,Toolkit.ICON_SIZE));
switchButton.addActionListener(e-> {
TokenInfo temp=token1;
Expand All @@ -113,13 +111,13 @@ public void changedUpdate(DocumentEvent e) {}
swapPanel.add(new JLabel()); // spacer
swapPanel.add(switchButton,"center,span");
swapPanel.add(new JLabel("To:"));
swapPanel.add(new TokenButton(token2),"wrap,grow");
swapPanel.add(new TokenButton(token2));

// Receive anount line
swapPanel.add(new JLabel("You receive:"));
// Receive amount line
receiveLabel = new BalanceLabel();
receiveLabel.setFont(Toolkit.BIG_FONT);
receiveLabel.setDecimals(token2.getDecimals());
receiveLabel.setToolTipText("Amount you will receive when swap occurs");
swapPanel.add(receiveLabel);

swapPanel.validate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public TokenComponent(Convex convex, TokenInfo token) {
this.convex=convex;
this.token=token;

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

tokenButton=new TokenButton(token);
Expand All @@ -38,8 +38,6 @@ public TokenComponent(Convex convex, TokenInfo token) {
balanceLabel.setToolTipText("Account balance for "+token.getSymbol());
add(balanceLabel,"align right");



// Action buttons
JPanel actions=new JPanel();
actions.add(ActionButton.build(0xe88e,e->{
Expand Down
26 changes: 14 additions & 12 deletions convex-gui/src/main/java/convex/gui/wallet/TransferPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class TransferPanel extends AbstractGUI {
protected BalanceLabel balanceLabel;
private DecimalAmountField amountField;
private AddressCombo addressCombo;
private Address address;

private static ComboModel<Address> model= new ComboModel<>();

Expand All @@ -47,7 +48,7 @@ public TransferPanel(Convex convex, TokenInfo token) {
this("Token Transfer for account "+convex.getAddress());
this.convex=convex;
this.token=token;
Address address=convex.getAddress();
address=convex.getAddress();
if (address==null) {
throw new IllegalStateException("Must be a valid address to transfer from");
}
Expand All @@ -64,7 +65,7 @@ public TransferPanel(Convex convex, TokenInfo token) {
ActionPanel actionPanel=new ActionPanel();
actionPanel.add(new ActionButton("Transfer!",0xe933,e->{
try {
Result done = executeTrade();
Result done = executeTransfer();
if (done==null) {
return;
} else if (done.isError()) {
Expand All @@ -83,36 +84,37 @@ public TransferPanel(Convex convex, TokenInfo token) {
}

protected void addTransferComponents(JPanel panel) {
panel.setLayout(new MigLayout("fill,wrap 3","[150][grow]"));
panel.removeAll();
panel.setLayout(new MigLayout("fill,wrap 3","[150][grow]"));

panel.add(new JLabel("Token:"));
panel.add(new TokenButton(token),"span");
panel.add(new JLabel("Amount to transfer: "));
panel.add(new TokenButton(token));
amountField=new DecimalAmountField(token.getDecimals());
amountField.setFont(Toolkit.BIG_FONT);
panel.add(amountField,"span");

panel.add(new JLabel("Balance:"));
panel.add(new JLabel("Balance:"),"span 2");
balanceLabel=new BalanceLabel();
balanceLabel.setDecimals(token.getDecimals());
balanceLabel.setBalance(token.getBalance(convex).join());
balanceLabel.setToolTipText("Current balance available in account "+address);
panel.add(balanceLabel,"span");


panel.add(new JLabel("Destination:"));
panel.add(new JLabel("Destination account: "),"span 2");
addressCombo=new AddressCombo(model);
addressCombo.setFont(Toolkit.BIG_FONT);
addressCombo.setToolTipText("Destination account that will receive asset after transfer");
panel.add(addressCombo,"span");

panel.add(new JLabel("Amount:"));
amountField=new DecimalAmountField(token.getDecimals());
amountField.setFont(Toolkit.BIG_FONT);
panel.add(amountField,"span");
amountField.setToolTipText("Input amount of "+token.getSymbol()+" to transfer to the destination account");

panel.validate();
panel.repaint();
}


protected Result executeTrade() throws InterruptedException {
protected Result executeTransfer() throws InterruptedException {
AInteger amount=amountField.getAmount();
if (amount==null) {
JOptionPane.showMessageDialog(this, "Please specify a valid amount to transfer");
Expand Down

0 comments on commit af30554

Please sign in to comment.