Skip to content

Commit

Permalink
Add load / save for peer data in GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Jan 13, 2025
1 parent 0dd353e commit 647ab20
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 116 deletions.
2 changes: 2 additions & 0 deletions convex-core/src/main/java/convex/core/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,6 @@ public static File getFile(String path) {





}
12 changes: 12 additions & 0 deletions convex-core/src/main/java/convex/core/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -1362,6 +1365,15 @@ public static String getVersion() {
return v;
}

public static String timeString() {
return timeString(Instant.now());
}

private static String timeString(Instant timeStamp) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss").withZone(ZoneId.from(ZoneOffset.UTC));
return formatter.format(timeStamp);
}




Expand Down
1 change: 1 addition & 0 deletions convex-core/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@
requires org.bouncycastle.util;
requires org.slf4j;
requires java.base;
requires java.desktop;
}
43 changes: 43 additions & 0 deletions convex-gui/src/main/java/convex/gui/peer/PeerComponent.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package convex.gui.peer;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Objects;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

Expand All @@ -20,8 +24,12 @@
import convex.core.cvm.PeerStatus;
import convex.core.cvm.State;
import convex.core.data.ACell;
import convex.core.data.AMap;
import convex.core.data.AccountKey;
import convex.core.data.Keyword;
import convex.core.text.Text;
import convex.core.util.FileUtils;
import convex.core.util.Utils;
import convex.etch.EtchStore;
import convex.gui.components.BaseImageButton;
import convex.gui.components.BaseListComponent;
Expand Down Expand Up @@ -166,6 +174,15 @@ public PeerComponent(ConvexLocal value) {
new WalletApp(connectLocalControllerWallet(server)).run();
});
popupMenu.add(walletButton);

JMenuItem saveButton = new JMenuItem("Save Peer Data...",Toolkit.menuIcon(0xe161));
saveButton.addActionListener(e -> savePeerData(this.convex));
popupMenu.add(saveButton);

JMenuItem loadButton = new JMenuItem("Load Peer Data...",Toolkit.menuIcon(0xeaf3));
loadButton.addActionListener(e -> loadPeerData(this.convex));
popupMenu.add(loadButton);



DropdownMenu dm = new DropdownMenu(popupMenu);
Expand Down Expand Up @@ -193,6 +210,32 @@ public PeerComponent(ConvexLocal value) {
updateDescription();
}

private void loadPeerData(ConvexLocal convex) {
JFileChooser chooser=Toolkit.createCAD3Chooser(null);
int result=chooser.showOpenDialog(this);
if (result==JFileChooser.APPROVE_OPTION) {
System.out.println("Loading: "+chooser.getSelectedFile());
}
}

private void savePeerData(ConvexLocal convex) {
Peer p=convex.getLocalServer().getPeer();
AMap<Keyword, ACell> data = p.toData();

String fileName="peer-data-"+p.getPeerKey().toHexString(8)+"-"+Utils.timeString()+".cad3";
JFileChooser chooser=Toolkit.createCAD3Chooser(fileName);

int result=chooser.showSaveDialog(this);
if (result==JFileChooser.APPROVE_OPTION) {
Path path =chooser.getSelectedFile().toPath();
try {
FileUtils.writeCAD3(path, data);
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "Peer data save failed:\n "+e,"Save Error",JOptionPane.ERROR_MESSAGE);
}
}
}

protected void updateDescription() {
String current=description.getText();
String updated=getPeerDescription();
Expand Down
Loading

0 comments on commit 647ab20

Please sign in to comment.