Skip to content

Commit

Permalink
Enhancement to Convex Desktop visuals
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Apr 27, 2024
1 parent b662d71 commit c07858d
Show file tree
Hide file tree
Showing 15 changed files with 148 additions and 80 deletions.
2 changes: 2 additions & 0 deletions convex-core/src/main/java/convex/core/store/AStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,6 @@ protected ACell decodeImpl(Blob encoding) throws BadFormatException {
* @return Stored Ref, or null if not found (may still be in persistent store)
*/
public abstract <T extends ACell> Ref<T> checkCache(Hash h);

public abstract String shortName();
}
6 changes: 6 additions & 0 deletions convex-core/src/main/java/convex/core/store/MemoryStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.util.HashMap;
import java.util.Objects;
import java.util.function.Consumer;

import org.slf4j.Logger;
Expand Down Expand Up @@ -144,4 +145,9 @@ public void close() {
public <T extends ACell> Ref<T> checkCache(Hash h) {
return refForHash(h);
}

@Override
public String shortName() {
return "Memory Store "+Objects.toIdentityString(this);
}
}
26 changes: 26 additions & 0 deletions convex-core/src/main/java/convex/core/text/Text.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,32 @@ public static void writeEscapedByte(BlobBuilder sb, byte b) {
}
}

public static int lineCount(String text) {
if (text==null) return 0;

int n=1;
for (int i=0; i<text.length(); i++) {
if (text.charAt(i)=='\n') n++;
}
return n;
}

public static int columnCount(String text) {
if (text==null) return 0;

int result=0;
int n=0;
for (int i=0; i<text.length(); i++) {
if (text.charAt(i)=='\n') {
n=0;
} else {
n++;
if (n>result) result=n;
}
}
return result;
}



}
2 changes: 1 addition & 1 deletion convex-core/src/main/java/etch/Etch.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private Etch(File dataFile) throws IOException {
if (!dataFile.exists()) dataFile.createNewFile();
this.data=new RandomAccessFile(dataFile,"rw");

this.fileName = dataFile.getCanonicalPath();
this.fileName = dataFile.getName();

// Try to exclusively lock the Etch database file
FileChannel fileChannel=this.data.getChannel();
Expand Down
6 changes: 6 additions & 0 deletions convex-core/src/main/java/etch/EtchStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,10 @@ public Etch getEtch() {
return etch;
}

@Override
public String shortName() {
// TODO Auto-generated method stub
return "Etch: "+etch.getFileName();
}

}
2 changes: 0 additions & 2 deletions convex-gui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,5 @@
<version>${slf4j.version}</version>
<scope>test</scope>
</dependency>


</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package convex.gui.components;

import javax.swing.Icon;
import javax.swing.JButton;

@SuppressWarnings("serial")
public class BaseImageButton extends JButton {

public BaseImageButton(Icon icon) {
super(icon);
this.setAlignmentX(JButton.CENTER_ALIGNMENT);
}
}
3 changes: 3 additions & 0 deletions convex-gui/src/main/java/convex/gui/components/CodeLabel.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import javax.swing.JTextArea;

import convex.core.text.Text;
import convex.gui.utils.Toolkit;

/**
Expand All @@ -11,8 +12,10 @@
public class CodeLabel extends JTextArea {

public CodeLabel(String text) {
super(Text.lineCount(text),Text.columnCount(text)+1);
this.setText(text);
this.setEditable(false);
this.setFont(Toolkit.SMALL_MONO_FONT);

}
}
14 changes: 7 additions & 7 deletions convex-gui/src/main/java/convex/gui/components/DropdownMenu.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package convex.gui.components;

import javax.swing.JButton;
import javax.swing.JPopupMenu;

import convex.gui.utils.SymbolIcon;
import convex.gui.utils.Toolkit;

/**
* A dropdown menu that can be used wherever an embedded menu is needed.
*/
@SuppressWarnings("serial")
public class DropdownMenu extends JButton {
public class DropdownMenu extends BaseImageButton {

private JPopupMenu popupMenu;

public DropdownMenu(JPopupMenu popupMenu) {
super();
super(SymbolIcon.get(0xe8b8, Toolkit.SYMBOL_SIZE));
this.popupMenu = popupMenu;
this.setIcon(Toolkit.COG);
this.addActionListener(e -> {
popupMenu.show(this, 0, this.getHeight());
// setIconTextGap(0);
this.addActionListener(e->{
popupMenu.show(DropdownMenu.this, 0, DropdownMenu.this.getHeight());
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public static ImageIcon createIcon(AArrayBlob hash, int size) {
public Identicon(AArrayBlob a) {
super();
setKey(a);
setFont(Toolkit.SMALL_MONO_FONT);
setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));

Toolkit.addPopupMenu(this,new JPopupMenu() {
Expand Down
114 changes: 58 additions & 56 deletions convex-gui/src/main/java/convex/gui/components/PeerComponent.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package convex.gui.components;

import java.awt.BorderLayout;
import java.io.IOException;
import java.util.concurrent.TimeoutException;

Expand All @@ -9,7 +8,6 @@
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

import convex.api.Convex;
import convex.api.ConvexLocal;
Expand All @@ -32,6 +30,7 @@
import convex.peer.ConnectionManager;
import convex.peer.Server;
import etch.EtchStore;
import net.miginfocom.swing.MigLayout;

@SuppressWarnings("serial")
public class PeerComponent extends BaseListComponent {
Expand Down Expand Up @@ -63,73 +62,69 @@ public void launchExploreWindow(Convex peer) {
public PeerComponent(ConvexLocal value) {
this.convex = value;

setLayout(new BorderLayout(0, 0));
setLayout(new MigLayout());

// setPreferredSize(new Dimension(1000, 90));

JButton button = new JButton("");
button.setBorder(null);
add(button, BorderLayout.WEST);
button.setIcon(Toolkit.CONVEX);
// Convex Button
JButton button = new BaseImageButton(Toolkit.CONVEX);
button.addActionListener(e -> {
launchPeerWindow(this.convex);
});
button.setToolTipText("Launch Peer management window");
add(button, "dock west");

// Central area

JPanel panel = new JPanel();
panel.setBorder(new EmptyBorder(5, 5, 5, 5));
add(panel);
panel.setLayout(new BorderLayout(0, 0));

description = new JTextArea((convex == null) ? "No peer" : convex.toString());
JPanel centralPanel = new JPanel();
centralPanel.setLayout(new MigLayout("fill, wrap 2","[][grow]"));

Server server=convex.getLocalServer();
AccountKey peerKey=server.getPeerKey();
Identicon identicon=new Identicon(peerKey);
centralPanel.add(identicon);
centralPanel.add(new CodeLabel("0x"+peerKey.toChecksumHex()),"span");

description = new CodeLabel(getPeerDescription());
description.setFont(Toolkit.SMALL_MONO_FONT);
description.setEditable(false);
description.setBorder(null);
description.setBackground(null);
panel.add(description, BorderLayout.CENTER);
centralPanel.add(description, "span 2");
add(centralPanel,"dock center");

// Setup popup menu for peer
JPopupMenu popupMenu = new JPopupMenu();
Server server=convex.getLocalServer();
if (server!=null) {
JMenuItem closeButton = new JMenuItem("Shutdown Peer");
closeButton.addActionListener(e -> {
try {
server.shutdown();
} catch (Exception e1) {
// ignore
}
});
popupMenu.add(closeButton);

JMenuItem exploreButton = new JMenuItem("Explore state");
exploreButton.addActionListener(e -> {
launchExploreWindow(convex);
});
popupMenu.add(exploreButton);

if (server.getStore() instanceof EtchStore) {
JMenuItem storeButton = new JMenuItem("Explore Etch store");
storeButton.addActionListener(e -> {
launchEtchWindow(convex);
});
popupMenu.add(storeButton);
JMenuItem closeButton = new JMenuItem("Shutdown Peer");
closeButton.addActionListener(e -> {
try {
server.shutdown();
} catch (Exception e1) {
// ignore
}


JMenuItem killConn = new JMenuItem("Kill Connections");
killConn.addActionListener(e -> {
server.getConnectionManager().closeAllConnections();
});
popupMenu.add(killConn);

} else {
JMenuItem closeButton = new JMenuItem("Close connection");
closeButton.addActionListener(e -> {
convex.close();
});
popupMenu.add(closeButton);

JMenuItem exploreButton = new JMenuItem("Explore state");
exploreButton.addActionListener(e -> {
launchExploreWindow(convex);
});
popupMenu.add(exploreButton);

if (server.getStore() instanceof EtchStore) {
JMenuItem storeButton = new JMenuItem("Explore Etch store");
storeButton.addActionListener(e -> {
launchEtchWindow(convex);
});
popupMenu.add(closeButton);
popupMenu.add(storeButton);
}

JMenuItem killConn = new JMenuItem("Kill Connections");
killConn.addActionListener(e -> {
server.getConnectionManager().closeAllConnections();
});
popupMenu.add(killConn);

JMenuItem replButton = new JMenuItem("Launch REPL");
replButton.addActionListener(e -> launchPeerWindow(this.convex));
Expand All @@ -139,12 +134,13 @@ public PeerComponent(ConvexLocal value) {
clientButton.addActionListener(e -> launchClientWindow(convex));
popupMenu.add(clientButton);

JPanel blockView = new BlockViewComponent(convex);
add(blockView, BorderLayout.SOUTH);

DropdownMenu dm = new DropdownMenu(popupMenu);
add(dm, BorderLayout.EAST);
add(dm, "dock east");

// Block view at bottom

JPanel blockView = new BlockViewComponent(convex);
StateModel<Peer> model=PeerGUI.getStateModel(convex);
if (model!=null) {
model.addPropertyChangeListener(e->{
Expand All @@ -156,6 +152,10 @@ public PeerComponent(ConvexLocal value) {
updateDescription();
});
}
add(blockView, "dock south");



updateDescription();
}

Expand Down Expand Up @@ -187,17 +187,19 @@ public String getPeerDescription() {
if (server != null) {
State state=server.getPeer().getConsensusState();
AccountKey paddr=server.getPeerKey();
sb.append("0x"+paddr.toChecksumHex()+"\n");
sb.append("Local peer on: " + server.getHostAddress() + " with store "+server.getStore()+"\n");
// sb.append("0x"+paddr.toChecksumHex()+"\n");
sb.append("Local peer on: " + server.getHostAddress() + " with store "+server.getStore().shortName()+"\n");

PeerStatus ps=state.getPeer(paddr);
if (ps!=null) {
sb.append("Peer Stake: "+Text.toFriendlyBalance(ps.getPeerStake()));
sb.append(" ");
sb.append("Delegated Stake: "+Text.toFriendlyBalance(ps.getDelegatedStake()));
sb.append(" ");
} else {
sb.append("Not currently a rgistered peer ");
}
ConnectionManager cm=server.getConnectionManager();
sb.append("\n");
sb.append("Connections: "+cm.getConnectionCount());
} else if (convex != null) {
sb.append(convex.toString());
Expand Down
9 changes: 5 additions & 4 deletions convex-gui/src/main/java/convex/gui/keys/WalletComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class WalletComponent extends BaseListComponent {

AWalletEntry walletEntry;

JPanel buttons = new JPanel();
JPanel buttons;

private CodeLabel infoLabel;

Expand Down Expand Up @@ -60,7 +60,9 @@ public WalletComponent(AWalletEntry initialWalletEntry) {
add(cPanel,"dock center"); // add to MigLayout

//////////// Buttons

buttons = new JPanel();
buttons.setLayout(new MigLayout());

// lock button
lockButton = new JButton("");
buttons.add(lockButton);
Expand Down Expand Up @@ -109,8 +111,7 @@ public WalletComponent(AWalletEntry initialWalletEntry) {
});
menu.add(m3);


DropdownMenu menuButton=new DropdownMenu(menu);
DropdownMenu menuButton=new DropdownMenu(menu);
buttons.add(menuButton);

// panel of buttons on right
Expand Down
Loading

0 comments on commit c07858d

Please sign in to comment.