Skip to content

Commit

Permalink
Improvements to GUI accounts table and buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed May 1, 2024
1 parent b3fc52a commit f598a56
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 25 deletions.
24 changes: 24 additions & 0 deletions convex-gui/src/main/java/convex/gui/components/ActionButton.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package convex.gui.components;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import convex.gui.utils.Toolkit;

/**
* Standard button for actions
*/
@SuppressWarnings("serial")
public class ActionButton extends JButton {

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

public ActionButton(String text, ActionListener al) {
super(text);
this.addActionListener(al);
}
}
10 changes: 10 additions & 0 deletions convex-gui/src/main/java/convex/gui/components/BalanceLabel.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

import convex.api.Convex;
import convex.core.ErrorCodes;
Expand Down Expand Up @@ -46,6 +49,13 @@ 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);
}

public void setBalance(long a) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
*/
@SuppressWarnings("serial")
public class BaseTextPane extends JTextPane {
private static final StyleContext sc = StyleContext.getDefaultStyleContext();
protected static final StyleContext styleContext = StyleContext.getDefaultStyleContext();


public void append(String text, Color c, Integer size) {
AttributeSet aset = SimpleAttributeSet.EMPTY;
if (c!=null) {
aset=sc.addAttribute(aset, StyleConstants.Foreground, c);
aset=styleContext.addAttribute(aset, StyleConstants.Foreground, c);
}
if (size!=null) {
aset=sc.addAttribute(aset, StyleConstants.FontSize, size);
aset=styleContext.addAttribute(aset, StyleConstants.FontSize, size);
}

StyledDocument d=getStyledDocument();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.datatransfer.Clipboard;
Expand All @@ -20,6 +21,7 @@
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

import convex.api.Convex;
import convex.core.State;
Expand All @@ -28,7 +30,9 @@
import convex.core.data.Address;
import convex.core.util.Utils;
import convex.gui.actor.AccountWindow;
import convex.gui.components.ActionButton;
import convex.gui.components.ActionPanel;
import convex.gui.components.BalanceLabel;
import convex.gui.components.Identicon;
import convex.gui.components.models.AccountsTableModel;
import convex.gui.components.models.StateModel;
Expand Down Expand Up @@ -61,6 +65,22 @@ public void setValue(Object value) {
setIcon(Identicon.createIcon((AArrayBlob) value,24));
}
}

static class BalanceRenderer extends BalanceLabel implements TableCellRenderer {

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
this.setAlignmentX(RIGHT_ALIGNMENT);
this.setSize(table.getColumnModel().getColumn(column).getWidth(), getHeight());
if (value==null) {
setBalance(0);
} else {
this.setBalance((Long)value);
}
return this;
}
}

public AccountsPanel(Convex convex,StateModel<State> model) {
setLayout(new BorderLayout());
Expand Down Expand Up @@ -102,10 +122,10 @@ public AccountsPanel(Convex convex,StateModel<State> model) {
}

{
CellRenderer cr=new CellRenderer(JLabel.RIGHT);
BalanceRenderer cr=new BalanceRenderer();
cr.setToolTipText("Balance of the account in Convex Coins");
table.getColumnModel().getColumn(3).setCellRenderer(cr);
table.getColumnModel().getColumn(3).setPreferredWidth(180);
table.getColumnModel().getColumn(3).setPreferredWidth(200);
}

{
Expand Down Expand Up @@ -165,9 +185,7 @@ public void mousePressed(MouseEvent e) {
JPanel actionPanel = new ActionPanel();
add(actionPanel, BorderLayout.SOUTH);

JButton btnActor = new JButton("Examine Account...");
actionPanel.add(btnActor);
btnActor.addActionListener(e -> {
JButton btnActor = new ActionButton("Examine Account...",0xf5e1,e -> {
long ix=table.getSelectedRow();
if (ix<0) return;
AccountStatus as = tableModel.getEntry(ix);
Expand All @@ -176,6 +194,7 @@ public void mousePressed(MouseEvent e) {
AccountWindow pw = new AccountWindow(convex, model, addr);
pw.run();
});
actionPanel.add(btnActor);

// Turn off auto-resize, since we want a scrollable table
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public Object getValueAt(int rowIndex, int columnIndex) {
return (seq>=0)?seq:"";
}
case 3:
return Text.toFriendlyBalance(as.getBalance());
return as.getBalance();
case 4: {
ACell o = as.getHoldings().get(Init.REGISTRY_ADDRESS);
if (o == null) return "";
Expand Down
13 changes: 6 additions & 7 deletions convex-gui/src/main/java/convex/gui/keys/KeyGenPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import convex.core.data.Blob;
import convex.core.data.Blobs;
import convex.core.util.Utils;
import convex.gui.components.ActionButton;
import convex.gui.components.ActionPanel;
import convex.gui.components.Identicon;
import convex.gui.components.RightCopyMenu;
Expand Down Expand Up @@ -321,29 +322,27 @@ public KeyGenPanel(PeerGUI manager) {
JPanel actionPanel = new ActionPanel();
add(actionPanel, BorderLayout.SOUTH);

JButton btnRecreate = new JButton("Generate");
actionPanel.add(btnRecreate);
btnRecreate.addActionListener(e -> {
JButton btnRecreate = new ActionButton("Generate",0xe5d5,e -> {
Integer wc=(Integer) numSpinner.getValue();
mnemonicArea.setText(BIP39.createSecureMnemonic(wc));
updateMnemonic();
});
actionPanel.add(btnRecreate);

numSpinner = new JSpinner();
numSpinner.setModel(new SpinnerNumberModel(12, 3, 30, 1));
actionPanel.add(numSpinner);

JButton btnNewButton = new JButton("Export...");
JButton btnNewButton = new ActionButton("Export...",0xebbe,e->{});
actionPanel.add(btnNewButton);

{ // Button to Normalise Mnemonic string
JButton btnNormalise = new JButton("Normalise Mnemonic");
actionPanel.add(btnNormalise);
btnNormalise.addActionListener(e -> {
JButton btnNormalise = new ActionButton("Normalise Mnemonic",0xf0ff,e -> {
String s=mnemonicArea.getText();
mnemonicArea.setText(BIP39.normalise(s));
updateMnemonic();
});
actionPanel.add(btnNormalise);
}

actionPanel.add(addWalletButton);
Expand Down
8 changes: 4 additions & 4 deletions convex-gui/src/main/java/convex/gui/keys/KeyRingPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static void addWalletEntry(AWalletEntry we) {
public KeyRingPanel() {
setLayout(new MigLayout());

JTextArea note=Toolkit.makeNote("This is a list of currentltly loaded crytographic keys. Locked keys cannot be used until unlocked.");
JTextArea note=Toolkit.makeNote("These are currently loaded keys. Locked keys cannot be used until unlocked. Keys that are unlocked are accessible to users with control over the local machine - DO NOT unlock high value keys unless you are confident that your machine is secure.");
add(note,"dock north");

// Scrollable list of wallet entries
Expand All @@ -57,7 +57,7 @@ public KeyRingPanel() {
JPanel toolBar = new ActionPanel();

// new wallet button
JButton btnNew = new JButton("New Keypair");
JButton btnNew = new JButton("New Keypair",Toolkit.menuIcon(0xe145));
btnNew.setToolTipText("Create a new hot wallet keypair. Use for temporary purposes. Remember to save the seed if you want to re-use!");
toolBar.add(btnNew);
btnNew.addActionListener(e -> {
Expand All @@ -71,11 +71,11 @@ public KeyRingPanel() {
});

// new wallet button
JButton btnImportSeed = new JButton("Import Seed....");
JButton btnImportSeed = new JButton("Import Seed....",Toolkit.menuIcon(0xe890));
btnImportSeed.setToolTipText("Import a key pair using an Ed25519 seed");
toolBar.add(btnImportSeed);
btnImportSeed.addActionListener(e -> {
String sd=JOptionPane.showInputDialog("Enter Ed25519 Seed");
String sd=(String) JOptionPane.showInputDialog(this,"Enter Ed25519 Seed","Import private key",JOptionPane.QUESTION_MESSAGE,Toolkit.menuIcon(0xe890),null,"");
if (sd==null) return;
Blob seed=Blob.parse(sd);
if (seed==null) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public MessageFormatPanel(PeerGUI manager) {
buttonPanel = new ActionPanel();
add(buttonPanel, BorderLayout.SOUTH);

clearButton = new JButton("Clear");
clearButton = new JButton("Clear",Toolkit.menuIcon(0xe835));
clearButton.setToolTipText("Press to clear the input areas");
clearButton.addActionListener(e -> {
dataArea.setText("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import javax.swing.table.TableColumnModel;

import convex.core.State;
import convex.gui.components.ActionButton;
import convex.gui.components.ActionPanel;
import convex.gui.components.models.TorusTableModel;
import convex.gui.peer.PeerGUI;
Expand Down Expand Up @@ -72,11 +73,10 @@ public void mousePressed(MouseEvent e) {
JPanel actionPanel = new ActionPanel();
add(actionPanel, BorderLayout.SOUTH);

JButton btnCopy = new JButton("New Token");
actionPanel.add(btnCopy);
btnCopy.addActionListener(e -> {
JButton btnCopy = new ActionButton("New Token",0xe145,e -> {
// TODO:;
});
actionPanel.add(btnCopy);

// Turn off auto-resize, since we want a scrollable table
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ private static String getString(ACell a) {
} else if (a instanceof ACountable) {
return Utils.getClass(a).getSimpleName();
} else {
return RT.print(a).toString();
AString s=RT.print(a);
return (s==null)?"<too big>":s.toString();
}
}

Expand Down

0 comments on commit f598a56

Please sign in to comment.