Skip to content

Commit

Permalink
Fixes for swap panel number formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Sep 2, 2024
1 parent 4e23d02 commit 6bef122
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import convex.core.data.prim.AInteger;
import convex.core.text.Text;

/**
* Component displaying a decimal quantity in an editable text field, constrained to a decimal with a maximum number of digits
*/
@SuppressWarnings("serial")
public class DecimalAmountField extends JTextField {

Expand Down Expand Up @@ -60,8 +63,10 @@ public void insertString(int offset, String s, AttributeSet a) throws BadLocatio
dotPos=i;
continue;
}
return; // not valid so exit function early
n=i; // end of valid input
break;
}
if (n==0) return;

if (newChars[0]=='.') {
if (dotPos>=0) {
Expand All @@ -82,8 +87,12 @@ public void insertString(int offset, String s, AttributeSet a) throws BadLocatio
}
}

// String to insert
String insertS=new String(newChars);
if (n<newChars.length) insertS=insertS.substring(0,n);

// Everything valid, so just insert as normal
super.insertString(offset, new String(newChars), a);
super.insertString(offset, insertS, a);
DecimalAmountField.this.setCaretPosition(offset+n);
}
}
Expand All @@ -95,12 +104,7 @@ public AInteger getAmount() {
}

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

static AInteger parse(String text, int decimals, boolean exact) {
Expand Down
3 changes: 2 additions & 1 deletion convex-gui/src/main/java/convex/gui/wallet/SwapPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public void changedUpdate(DocumentEvent e) {}
System.err.println("Switching amount: "+amt);
addSwapComponents();
amountField.setText(amt);
System.err.println("Amount Set: "+amountField.getAmount());
refreshRates();
});

Expand Down Expand Up @@ -147,7 +148,7 @@ protected void refreshRates() {
}

convex.query(qs).thenAccept(r->{
System.err.println(r);
System.err.println(qs + " => " + r);
ACell val=r.getValue();
if (val instanceof AInteger) {
receiveLabel.setBalance((AInteger) val);
Expand Down
29 changes: 29 additions & 0 deletions convex-gui/src/test/java/convex/gui/components/ComponentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,34 @@ public class ComponentTest {
af.setText(" 3.11111111");
assertEquals(3111,af.getAmount().longValue());

af.setText("401 ");
assertEquals(401000,af.getAmount().longValue());

af.setText(" 401.2 ");
assertEquals(401200,af.getAmount().longValue());

af.setText(" fghrt ");
assertNull(af.getAmount());
}

@Test public void testDecimalAmountFieldNoDecimals() {
DecimalAmountField af=new DecimalAmountField(0);
assertEquals(CVMLong.ZERO,af.getAmount());

af.setText(" 3.444");
assertEquals(3,af.getAmount().longValue());

af.setText("1234.0");
assertEquals(1234,af.getAmount().longValue());

af.setText(" 0.0001 ");
assertEquals(0,af.getAmount().longValue());

af.setText(".");
assertNull(af.getAmount());

af.setText("13.");
assertEquals(13,af.getAmount().longValue());

}
}

0 comments on commit 6bef122

Please sign in to comment.