Skip to content

Commit

Permalink
Improvements to wallet panels
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Sep 2, 2024
1 parent 1cd67a2 commit 4e23d02
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 49 deletions.
69 changes: 49 additions & 20 deletions convex-gui/src/main/java/convex/gui/components/AbstractGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@


import java.awt.EventQueue;
import java.awt.Rectangle;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.concurrent.CompletableFuture;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import convex.core.util.Utils;
import convex.gui.MainGUI;
import convex.gui.utils.Toolkit;
import net.miginfocom.swing.MigLayout;
Expand All @@ -19,7 +25,10 @@
@SuppressWarnings("serial")
public abstract class AbstractGUI extends JPanel implements Runnable {

protected JFrame frame=new JFrame();
private static final Logger log = LoggerFactory.getLogger(AbstractGUI.class.getName());


protected JFrame frame;
private String title;

public AbstractGUI(String title) {
Expand All @@ -37,26 +46,44 @@ public final void run() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
frame.setTitle(title);
frame.setIconImage(Toolkit.getDefaultToolkit()
.getImage(MainGUI.class.getResource("/images/Convex.png")));


frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
finished.complete("Closed");
}
});

setupFrame(frame);
frame.pack();
frame.setVisible(true);
EventQueue.invokeLater(AbstractGUI.this::afterRun);
try {
showFrame();
EventQueue.invokeLater(AbstractGUI.this::afterRun);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

public void runNonModal(JComponent parent) {
showFrame();
if (parent!=null) {
Rectangle b=parent.getBounds();
frame.setLocation(b.x+100, b.y+100);
}

}

public void showFrame() {
frame=new JFrame();
frame.setTitle(title);
frame.setIconImage(Toolkit.getDefaultToolkit()
.getImage(MainGUI.class.getResource("/images/Convex.png")));

frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
finished.completeAsync(()->"Closed");
}
});

setupFrame(frame);
frame.pack();
frame.setVisible(true);
log.info("GUI displayed: "+Utils.getClassName(this));
}

/**
* Runs this GUI element until it is closed
*/
Expand All @@ -75,14 +102,16 @@ public void setupFrame(JFrame frame) {
}

/**
* Called after the wallet is run
* Called after the GUI interface is run
*/
public void afterRun() {

}

public void waitForClose() {
finished.join();
if (finished!=null) {
finished.join();
}
close();
}

Expand All @@ -91,7 +120,7 @@ public JFrame getFrame() {
}

public void close() {
// nothing to do
// nothing to do by default
}


Expand Down
23 changes: 14 additions & 9 deletions convex-gui/src/main/java/convex/gui/components/BalanceLabel.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,6 @@ public BalanceMenu() {

public BalanceLabel() {
this.setEditable(false);

AttributeSet attribs = new SimpleAttributeSet();
attribs=styleContext.addAttribute(attribs, StyleConstants.Alignment, StyleConstants.ALIGN_RIGHT);
attribs=styleContext.addAttribute(attribs, StyleConstants.FontFamily, getFont().getFamily());
//StyleConstants.setAlignment(attribs, StyleConstants.ALIGN_RIGHT);
//StyleConstants.setFontFamily(attribs, Font.PLAIN);
this.setParagraphAttributes(attribs, true);
this.setFocusable(false);
}

Expand Down Expand Up @@ -91,6 +84,14 @@ public void setBalance(AInteger a) {
BigInteger coins=bi.divide(unit);

setText("");
AttributeSet attribs = new SimpleAttributeSet();
attribs=styleContext.addAttribute(attribs, StyleConstants.Alignment, StyleConstants.ALIGN_RIGHT);
attribs=styleContext.addAttribute(attribs, StyleConstants.FontFamily, getFont().getFamily());
//StyleConstants.setAlignment(attribs, StyleConstants.ALIGN_RIGHT);
//StyleConstants.setFontFamily(attribs, Font.PLAIN);
this.setParagraphAttributes(attribs, true);


String cs=Text.toFriendlyNumber(coins.longValue());
append(cs,balanceColour,size);
String ch=Text.zeroPad(change,decimals);
Expand Down Expand Up @@ -130,8 +131,12 @@ private static BigInteger getUnit(int decimals) {

@Override
public void setText(String s) {
super.setText("");
append(s,Color.ORANGE,getFont().getSize());
AInteger val=DecimalAmountField.parse(s,decimals,false);
if (val==null) {
super.setText("");
} else {
setBalance(val);
}
}

public void setFromResult(Result r) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,27 @@ public class DecimalDocument extends PlainDocument {
public void insertString(int offset, String s, AttributeSet a) throws BadLocationException {
if (s == null) return;

char[] cs = s.toCharArray();
int n=cs.length;
char[] newChars = s.toCharArray();
int n=newChars.length;
if (n==0) return;

String text=super.getText(0, super.getLength());
int tlen=text.length();
int dotPos=text.indexOf('.');

for (int i = 0; i < n; i++ ) {
char c=cs[i];
char c=newChars[i];
if (Text.isASCIIDigit(c)) continue;
if ((i==0)&&(c=='.')) continue;
if ((c=='.')&&(dotPos<0)) {
// found first do
dotPos=i;
continue;
}
return; // not valid so exit function early
}

if (cs[0]=='.') {
if (newChars[0]=='.') {
if (dotPos>=0) {
super.remove(dotPos,tlen-dotPos);
offset=dotPos;
Expand All @@ -72,28 +77,44 @@ public void insertString(int offset, String s, AttributeSet a) throws BadLocatio
int digits=(offset+n-dotPos)-1;
if (digits>decimals) {
int newN=n-digits+decimals;
cs=Arrays.copyOfRange(cs, 0, newN);
newChars=Arrays.copyOfRange(newChars, 0, newN);
n=newN;
}
}

// Everything valid, so just insert as normal
super.insertString(offset, new String(cs), a);
super.insertString(offset, new String(newChars), a);
DecimalAmountField.this.setCaretPosition(offset+n);
}
}

public AInteger getAmount() {
String text=getText();
if (text.isBlank()) return null;
return parse(text,decimals,true);
}

public void setText(String text) {
AInteger amt=DecimalAmountField.parse(text,decimals,false);
if (amt!=null) {
super.setText(text.trim());
} else {
// no change
}
}

static AInteger parse(String text, int decimals, boolean exact) {
try {
text=text.trim();
BigDecimal dec=new BigDecimal(text);
if (decimals>0) {
dec=dec.multiply(new BigDecimal(BigInteger.TEN.pow(decimals)));
}
BigInteger bi=dec.toBigIntegerExact();
BigInteger bi=(exact?dec.toBigIntegerExact():dec.toBigInteger());
return AInteger.create(bi);
} catch (Exception e) {
} catch (NumberFormatException e) {
return null;
}catch (Exception e) {
e.printStackTrace();
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public AccountChooserPanel(Convex convex) {
keyCombo.setToolTipText("Select a key pair from your Keyring. This will be used to sign transactions.");
keyCombo.addItemListener(e->{
if (e.getStateChange()==ItemEvent.DESELECTED) {
// key pair was deselected and/or set to null
convex.setKeyPair(null);
return;
};
Expand Down
1 change: 1 addition & 0 deletions convex-gui/src/main/java/convex/gui/utils/Toolkit.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class Toolkit {
public static final float DEFAULT_FONT_SIZE=13*SCALE;

public static Font DEFAULT_FONT = new Font(Font.SANS_SERIF,Font.PLAIN,(int)DEFAULT_FONT_SIZE);
public static Font BIG_FONT = new Font(Font.SANS_SERIF, Font.BOLD, (int)(DEFAULT_FONT_SIZE*1.8));
public static Font MONO_FONT = new Font(Font.MONOSPACED, Font.BOLD, (int)(DEFAULT_FONT_SIZE));
public static Font BIG_MONO_FONT = new Font(Font.MONOSPACED, Font.BOLD, (int)(DEFAULT_FONT_SIZE*1.5));
public static Font SMALL_MONO_FONT = new Font(Font.MONOSPACED, Font.PLAIN, (int)(DEFAULT_FONT_SIZE*0.8));
Expand Down
39 changes: 29 additions & 10 deletions convex-gui/src/main/java/convex/gui/wallet/SwapPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ public class SwapPanel extends AbstractGUI {
protected TokenInfo token1;
protected TokenInfo token2;
private DecimalAmountField amountField;

/**
* Panel for swap display components
*/
private JPanel swapPanel;
private JPanel tradePanel;
private BalanceLabel receiveLabel;

private SwapPanel(String title) {
Expand All @@ -52,12 +55,7 @@ public SwapPanel(Convex convex, TokenInfo token1, TokenInfo token2) {
addSwapComponents();
add(swapPanel,"align center,span,growx");

tradePanel=new JPanel();
tradePanel.setLayout(new MigLayout("fill,wrap 3","[150][grow]"));
tradePanel.add(new JLabel("You receive:"));
receiveLabel = new BalanceLabel();
tradePanel.add(receiveLabel);
add(tradePanel);


// Main action buttons
ActionPanel actionPanel=new ActionPanel();
Expand All @@ -72,14 +70,16 @@ public SwapPanel(Convex convex, TokenInfo token1, TokenInfo token2) {
refreshRates();
}));
add(actionPanel,"dock south");
refreshRates();
}

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

swapPanel.add(new JLabel("Amount:"));
amountField=new DecimalAmountField(token1.getDecimals());
amountField.setFont(amountField.getFont().deriveFont(30f));
amountField.setFont(Toolkit.BIG_FONT);
swapPanel.add(amountField,"span");
amountField.getDocument().addDocumentListener(new DocumentListener() {
@Override
Expand All @@ -97,18 +97,32 @@ public void changedUpdate(DocumentEvent e) {}

swapPanel.add(new JLabel("From:"));
swapPanel.add(new TokenButton(token1),"wrap,grow");
JButton switchButton=new JButton(SymbolIcon.get(0xe8d5,Toolkit.SMALL_ICON_SIZE));

JButton switchButton=new JButton(SymbolIcon.get(0xe8d5,Toolkit.ICON_SIZE));
switchButton.addActionListener(e-> {
TokenInfo temp=token1;
token1=token2;
token2=temp;

String amt=receiveLabel.getText();
System.err.println("Switching amount: "+amt);
addSwapComponents();
amountField.setText(amt);
refreshRates();
});

swapPanel.add(new JLabel()); // spacer
swapPanel.add(switchButton,"span");
swapPanel.add(switchButton,"center,span");
swapPanel.add(new JLabel("To:"));
swapPanel.add(new TokenButton(token2),"wrap,grow");

// Receive anount line
swapPanel.add(new JLabel("You receive:"));
receiveLabel = new BalanceLabel();
receiveLabel.setFont(Toolkit.BIG_FONT);
receiveLabel.setDecimals(token2.getDecimals());
swapPanel.add(receiveLabel);

swapPanel.validate();
swapPanel.repaint();
}
Expand All @@ -118,6 +132,10 @@ protected void refreshRates() {
ACell torus=TokenInfo.getTorusAddress(convex);
receiveLabel.setDecimals(token2.getDecimals());
AInteger amount=amountField.getAmount();
if (amount==null) {
receiveLabel.setText("-");
return;
}
String qs;
if (token1.isConvex()) {
System.err.println(amount +" :dec "+token1.getDecimals());
Expand All @@ -129,6 +147,7 @@ protected void refreshRates() {
}

convex.query(qs).thenAccept(r->{
System.err.println(r);
ACell val=r.getValue();
if (val instanceof AInteger) {
receiveLabel.setBalance((AInteger) val);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package convex.gui.wallet;

import java.awt.Font;

import javax.swing.JPanel;

import convex.api.Convex;
Expand All @@ -14,6 +16,8 @@ public class TokenComponent extends JPanel {
protected Convex convex;
protected BalanceLabel balanceLabel;
private TokenInfo token;

static final Font BALANCE_FONT=Toolkit.BIG_FONT;
TokenButton tokenButton;

public TokenComponent(Convex convex, TokenInfo token) {
Expand All @@ -28,7 +32,7 @@ public TokenComponent(Convex convex, TokenInfo token) {

balanceLabel = new BalanceLabel();
balanceLabel.setDecimals(token.getDecimals());
balanceLabel.setFont(Toolkit.BIG_MONO_FONT);
balanceLabel.setFont(BALANCE_FONT);
balanceLabel.setToolTipText("Account balance for "+token.getSymbol());
add(balanceLabel,"align right");

Expand All @@ -46,7 +50,8 @@ public TokenComponent(Convex convex, TokenInfo token) {

actions.add(ActionButton.build(0xe933,e->{
// Token swap
new SwapPanel(convex,token,TokenListPanel.getDefaultToken()).run();
TokenInfo with= TokenListPanel.getOtherToken(token);
new SwapPanel(convex,token,with).runNonModal(this);
},"Open token swap window for this token"));
actions.add(ActionButton.build(0xe5d5,e->{
refresh(convex);
Expand Down
Loading

0 comments on commit 4e23d02

Please sign in to comment.