Skip to content

Commit

Permalink
GUI refactoring, experimental IP utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed May 5, 2024
1 parent 53c1da1 commit d03f311
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 30 deletions.
12 changes: 6 additions & 6 deletions convex-gui/src/main/java/convex/gui/MainGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
public class MainGUI extends AbstractGUI {
public MainGUI() {
super ("Convex Desktop");
MigLayout layout=new MigLayout("center");
MigLayout layout=new MigLayout("wrap 1","[fill]");
setLayout(layout);

add(new HomePanel(),"dock center");
add(new HomePanel());

ActionPanel actionPanel=new ActionPanel();
actionPanel.setLayout(new MigLayout("center,align center,fillx"));
Expand All @@ -55,7 +55,7 @@ public MainGUI() {
JComponent www=createLaunchButton("convex.world",Toolkit.WWW_ICON,this::launchWebsite);
actionPanel.add(www);

add(actionPanel,"dock south");
add(actionPanel);
}

public void launchDLFS() {
Expand Down Expand Up @@ -103,9 +103,9 @@ public JPanel createLaunchButton(String label, ImageIcon icon, Runnable cmd) {
lab.setHorizontalAlignment(SwingConstants.CENTER);

JPanel panel=new JPanel();
panel.setLayout(new MigLayout());
panel.add(butt,"dock center");
panel.add(lab,"dock south");
panel.setLayout(new MigLayout("center, wrap 1","[align center]"));
panel.add(butt);
panel.add(lab);
return panel;
}

Expand Down
11 changes: 7 additions & 4 deletions convex-gui/src/main/java/convex/gui/components/AbstractGUI.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package convex.gui.components;


import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
Expand All @@ -12,6 +11,7 @@

import convex.gui.MainGUI;
import convex.gui.utils.Toolkit;
import net.miginfocom.swing.MigLayout;

/**
* Base class for Convex GUI apps
Expand Down Expand Up @@ -41,12 +41,15 @@ public void run() {
frame.setTitle(title);
frame.setIconImage(Toolkit.getDefaultToolkit()
.getImage(MainGUI.class.getResource("/images/Convex.png")));
frame.setBounds(50, 50, 1200, 920);

// Prefer not to have explicit frame size / position?
// frame.setBounds(50, 50, 1200, 920);

Toolkit.closeIfFirstFrame(frame);

frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(AbstractGUI.this, BorderLayout.CENTER);
frame.getContentPane().setLayout(new MigLayout());
frame.getContentPane().add(AbstractGUI.this);
frame.pack();
frame.setVisible(true);

} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import convex.core.data.prim.CVMLong;
import convex.core.lang.RT;
import convex.core.text.Text;
import convex.core.util.Utils;
import convex.gui.utils.SymbolIcon;
import convex.gui.utils.Toolkit;

Expand Down Expand Up @@ -56,6 +57,7 @@ public BalanceLabel() {
//StyleConstants.setAlignment(attribs, StyleConstants.ALIGN_RIGHT);
//StyleConstants.setFontFamily(attribs, Font.PLAIN);
this.setParagraphAttributes(attribs, true);
this.setFocusable(false);
}

public void setBalance(long a) {
Expand All @@ -71,6 +73,7 @@ public void setBalanceColour(Color c) {
}

public void setBalance(AInteger a) {
if (Utils.equals(a, balance)) return;
try {
if (a==null) {
setText("<No balance>");
Expand Down
16 changes: 11 additions & 5 deletions convex-gui/src/main/java/convex/gui/components/WorldPanel.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package convex.gui.components;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
Expand All @@ -16,6 +17,9 @@

import convex.gui.utils.RobinsonProjection;

/**
* Panel displaying world image an optional locations
*/
@SuppressWarnings("serial")
public class WorldPanel extends JPanel {
static BufferedImage image;
Expand All @@ -42,6 +46,7 @@ public class WorldPanel extends JPanel {

public WorldPanel() {
setBackground(Color.BLACK);
setPreferredSize(new Dimension(image.getWidth(),image.getHeight()));
}

@Override
Expand All @@ -57,13 +62,14 @@ public void paintComponent(Graphics g) {
int dh = Math.min(h, w * sh / sw);

int y = (h - dh) / 2;
int x = (w - dw) / 2;

g.drawImage(image, 0, y, dw, y + dh, 0, 0, sw, sh, null);
g.drawImage(image, x, y, x+dw, y + dh, 0, 0, sw, sh, null);

paintDot(g, 51.5073219, -0.1276474, 0, y, dw, dh); // London
paintDot(g, -33.928992, 18.417396, 0, y, dw, dh); // Cape Town
paintDot(g, 35.6828387, 139.7594549, 0, y, dw, dh); // Tokyo
paintDot(g, 23.135305, -82.3589631, 0, y, dw, dh); // Havana
paintDot(g, 51.5073219, -0.1276474, x, y, dw, dh); // London
paintDot(g, -33.928992, 18.417396, x, y, dw, dh); // Cape Town
paintDot(g, 35.6828387, 139.7594549, x, y, dw, dh); // Tokyo
paintDot(g, 23.135305, -82.3589631, x, y, dw, dh); // Havana
}

private void paintDot(Graphics g, double latitude, double longitude, int x, int y, int dw, int dh) {
Expand Down
20 changes: 7 additions & 13 deletions convex-gui/src/main/java/convex/gui/panels/HomePanel.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package convex.gui.panels;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import convex.gui.components.WorldPanel;

import java.awt.Font;
import net.miginfocom.swing.MigLayout;

@SuppressWarnings("serial")
public class HomePanel extends JPanel {
Expand All @@ -18,19 +16,15 @@ public class HomePanel extends JPanel {
* Create the panel.
*/
public HomePanel() {
setPreferredSize(new Dimension(800,600));
setLayout(new BorderLayout(0, 0));

JPanel panel = new JPanel();
add(panel);
panel.setLayout(new BorderLayout(0, 0));
// setPreferredSize(new Dimension(800,600));
setLayout(new MigLayout());

JLabel lblWelome = new JLabel("Welcome to Convex");
lblWelome.setFont(new Font("Monospaced", Font.PLAIN, 24));
lblWelome.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(lblWelome, BorderLayout.NORTH);
add(lblWelome,"dock north");

panel.add(new WorldPanel(), BorderLayout.CENTER);
add(new WorldPanel(),"align center");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public String getPeerDescription() {
AccountKey paddr=server.getPeerKey();
// sb.append("0x"+paddr.toChecksumHex()+"\n");
if (server.isLive()) {
sb.append("Local peer on: " + server.getHostAddress() + " with store "+server.getStore().shortName()+"\n");
sb.append("Local peer on port: " + server.getPort() + " with store "+server.getStore().shortName()+"\n");
} else {
sb.append("Inactive Peer\n");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ public AccountOverview(Convex convex) {
private void updateLoop() {
while (true) {
try {
Thread.sleep(1000);
if (isShowing()) {
balance.setBalance(convex.getBalance());
}
Thread.sleep(1000);
} catch (InterruptedException e) {
return;
} catch (Exception e) {
Expand Down
20 changes: 20 additions & 0 deletions convex-gui/src/main/java/convex/gui/wallet/SwapPanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package convex.gui.wallet;

import convex.api.Convex;
import convex.gui.components.AbstractGUI;

@SuppressWarnings("serial")
public class SwapPanel extends AbstractGUI {

protected Convex convex;

private SwapPanel(String title) {
super(title);
}

public SwapPanel(Convex convex) {
this("Token Swap for account "+convex.getAddress());
this.convex=convex;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public TokenComponent(Convex convex, TokenInfo token) {
actions.add(new ActionButton(0xe872,e->{
WalletPanel.model.removeElement(token);
}));
actions.add(new ActionButton(0xe933,e->{
// Token swap
new SwapPanel(convex).run();
}));

add(actions,"dock east");
SwingUtilities.invokeLater(()->refresh(convex));
}
Expand Down
50 changes: 50 additions & 0 deletions convex-peer/src/main/java/convex/net/IPUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package convex.net;

import java.io.IOException;
import java.net.InetAddress;
import java.net.URI;
import java.net.UnknownHostException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class IPUtils {

public static InetAddress tryGetIP() throws InterruptedException {

String s=tryGetWTF().trim();
if (s!=null) {
try {
InetAddress i= InetAddress.getByName(s);
return i;
} catch (UnknownHostException e) {
// continue
}
}
return null;
}

public static String tryGetWTF() throws InterruptedException {
HttpClient client = HttpClient.newHttpClient();

HttpRequest request = HttpRequest.newBuilder(
URI.create("https://wtfismyip.com/text"))
.header("accept", "text/plain")
.build();

HttpResponse<String> response;
try {
response = client.send(request, HttpResponse.BodyHandlers.ofString());
String text=response.body();
return text;
} catch (IOException e) {
return null;
}
}

public static void main(String[] args) throws InterruptedException {
System.out.println(tryGetIP());

System.out.println(tryGetIP());
}
}

0 comments on commit d03f311

Please sign in to comment.