From 4c31e8a627d81f6b52a1840ea28a30d0125de842 Mon Sep 17 00:00:00 2001 From: David Bohn <david@cancrisoft.net> Date: Wed, 15 Jun 2016 18:37:34 +0200 Subject: [PATCH] Refactored setup dialog into own class --- src/main/java/fucoin/MainRemote.java | 104 +----------------- .../fucoin/setup/NetworkInterfaceReader.java | 37 +++++++ .../setup/SelectableNetworkInterface.java | 3 - .../java/fucoin/setup/SetupDialogPanel.java | 78 +++++++++++++ 4 files changed, 121 insertions(+), 101 deletions(-) create mode 100644 src/main/java/fucoin/setup/NetworkInterfaceReader.java create mode 100644 src/main/java/fucoin/setup/SetupDialogPanel.java diff --git a/src/main/java/fucoin/MainRemote.java b/src/main/java/fucoin/MainRemote.java index a00a464..9eea194 100644 --- a/src/main/java/fucoin/MainRemote.java +++ b/src/main/java/fucoin/MainRemote.java @@ -7,26 +7,18 @@ import akka.util.Timeout; import com.typesafe.config.Config; import com.typesafe.config.ConfigFactory; import fucoin.setup.SelectableNetworkInterface; +import fucoin.setup.SetupDialogPanel; import fucoin.wallet.WalletImpl; import scala.concurrent.Await; import scala.concurrent.duration.FiniteDuration; import javax.swing.*; -import javax.swing.event.AncestorEvent; -import javax.swing.event.AncestorListener; -import java.awt.*; import java.io.File; import java.net.*; -import java.util.*; -import java.util.List; import java.util.concurrent.TimeUnit; public class MainRemote { - private static JTextField walletNameField = new JTextField(10); - private static JTextField pathField = new JTextField(10); - private static JComboBox<SelectableNetworkInterface> hostnameField = new JComboBox<>(); - public static void main(String[] args) throws InterruptedException { String hostname = "127.0.0.1"; @@ -50,7 +42,8 @@ public class MainRemote { //Init System Actor System ActorSystem system = ActorSystem.create("Remote", ConfigFactory.parseString("akka.remote.netty.tcp.hostname=" + hostname).withFallback(config)); - JPanel dialogPanel = createDialogPanel(hostname); + //JPanel dialogPanel = createDialogPanel(hostname); + SetupDialogPanel dialogPanel = new SetupDialogPanel(hostname); Timeout timeout = new Timeout(5, TimeUnit.SECONDS); ActorRef preknownNeighbour = null; @@ -70,7 +63,7 @@ public class MainRemote { return; } - SelectableNetworkInterface selectedHostname = (SelectableNetworkInterface) hostnameField.getSelectedItem(); + SelectableNetworkInterface selectedHostname = dialogPanel.getNetworkInterface(); if (!selectedHostname.getHostName().equals(hostname)) { if (system != null) { @@ -85,8 +78,8 @@ public class MainRemote { system = ActorSystem.create("Remote", ConfigFactory.parseString("akka.remote.netty.tcp.hostname=" + hostname).withFallback(config)); } - walletName = walletNameField.getText(); - path = pathField.getText(); + walletName = dialogPanel.getWalletName(); + path = dialogPanel.getAddressOfNeighbour(); // check input if (path.equals("") || walletName.equals("")) { @@ -107,89 +100,4 @@ public class MainRemote { system.actorOf(WalletImpl.props(preknownNeighbour, walletName), walletName); } - - /** - * Create the setup UI for the remote application - * This provides an input for a wallet name, the initially known neighbour and a combo box to select - * the used network interface for the client. - * - * @param defaultHostname This is the default hostname that should be selected. - */ - private static JPanel createDialogPanel(String defaultHostname) { - JPanel dialogPanel = new JPanel(); - dialogPanel.setLayout(new GridLayout(3, 1)); - JLabel walletLabel = new JLabel("Pick your wallet name: ", SwingConstants.LEFT); - dialogPanel.add(walletLabel); - dialogPanel.add(walletNameField); - dialogPanel.add(new JLabel("Enter a neighbour node address: ", SwingConstants.LEFT)); - dialogPanel.add(pathField); - dialogPanel.add(new JLabel("Select your reachable IP address: ", SwingConstants.LEFT)); - - selectWalletNameField(); - - List<SelectableNetworkInterface> interfaces = fetchNetworkInterfaces(); - - for (SelectableNetworkInterface netint : interfaces) { - hostnameField.addItem(netint); - } - - hostnameField.setSelectedItem(new SelectableNetworkInterface(defaultHostname, "default")); - dialogPanel.add(hostnameField); - return dialogPanel; - } - - /** - * This method makes sure, that the Wallet name field is selected on display of the dialog - */ - private static void selectWalletNameField() { - walletNameField.addAncestorListener(new AncestorListener() { - @Override - public void ancestorAdded(AncestorEvent event) { - final AncestorListener al= this; - SwingUtilities.invokeLater(() -> { - JComponent component = event.getComponent(); - component.requestFocusInWindow(); - component.removeAncestorListener( al ); - }); - } - - @Override - public void ancestorRemoved(AncestorEvent event) { - - } - - @Override - public void ancestorMoved(AncestorEvent event) { - - } - }); - } - - /** - * Grab a list of all network interfaces and the associated IP addresses to prevent some strange behaviours of - * the <code>InetAddress.getLocalHost().getHostAddress();</code> call - * - * @return The list contains SelectableNetworkInterfaces, that contain the interface name and the associated IP address - */ - private static List<SelectableNetworkInterface> fetchNetworkInterfaces() { - List<SelectableNetworkInterface> map = new ArrayList<>(); - - try { - Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces(); - - for (NetworkInterface networkInterface : Collections.list(nets)) { - Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses(); - // To ease the setup, currently only IPv4 addresses are supported - Optional<InetAddress> picked = Collections.list(inetAddresses).stream().filter(inetAddress -> !(inetAddress instanceof Inet6Address)).findFirst(); - if (picked.isPresent()) { - String hostAddress = picked.get().getHostAddress(); - map.add(new SelectableNetworkInterface(hostAddress, networkInterface.getDisplayName())); - } - } - } catch (SocketException e) { - e.printStackTrace(); - } - - return map; - } } diff --git a/src/main/java/fucoin/setup/NetworkInterfaceReader.java b/src/main/java/fucoin/setup/NetworkInterfaceReader.java new file mode 100644 index 0000000..2aa5db4 --- /dev/null +++ b/src/main/java/fucoin/setup/NetworkInterfaceReader.java @@ -0,0 +1,37 @@ +package fucoin.setup; + +import java.net.Inet6Address; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.util.*; + +public class NetworkInterfaceReader { + /** + * Grab a list of all network interfaces and the associated IP addresses to prevent some strange behaviours of + * the <code>InetAddress.getLocalHost().getHostAddress();</code> call + * + * @return The list contains SelectableNetworkInterfaces, that contain the interface name and the associated IP address + */ + public static List<SelectableNetworkInterface> fetchNetworkInterfaces() { + List<SelectableNetworkInterface> map = new ArrayList<>(); + + try { + Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces(); + + for (NetworkInterface networkInterface : Collections.list(nets)) { + Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses(); + // To ease the setup, currently only IPv4 addresses are supported + Optional<InetAddress> picked = Collections.list(inetAddresses).stream().filter(inetAddress -> !(inetAddress instanceof Inet6Address)).findFirst(); + if (picked.isPresent()) { + String hostAddress = picked.get().getHostAddress(); + map.add(new SelectableNetworkInterface(hostAddress, networkInterface.getDisplayName())); + } + } + } catch (SocketException e) { + e.printStackTrace(); + } + + return map; + } +} diff --git a/src/main/java/fucoin/setup/SelectableNetworkInterface.java b/src/main/java/fucoin/setup/SelectableNetworkInterface.java index 0e023c0..8d21975 100644 --- a/src/main/java/fucoin/setup/SelectableNetworkInterface.java +++ b/src/main/java/fucoin/setup/SelectableNetworkInterface.java @@ -1,8 +1,5 @@ package fucoin.setup; -/** - * @author davidbohn - */ public class SelectableNetworkInterface { String hostName; diff --git a/src/main/java/fucoin/setup/SetupDialogPanel.java b/src/main/java/fucoin/setup/SetupDialogPanel.java new file mode 100644 index 0000000..96e2cc7 --- /dev/null +++ b/src/main/java/fucoin/setup/SetupDialogPanel.java @@ -0,0 +1,78 @@ +package fucoin.setup; + +import javax.swing.*; +import javax.swing.event.AncestorEvent; +import javax.swing.event.AncestorListener; +import java.awt.*; +import java.util.List; + +public class SetupDialogPanel extends JPanel { + private JTextField walletNameField = new JTextField(10); + private JTextField pathField = new JTextField(10); + private JComboBox<SelectableNetworkInterface> hostnameField = new JComboBox<>(); + + public SetupDialogPanel(String defaultHostname) { + super(); + + createComponents(defaultHostname); + } + + private void createComponents(String defaultHostname) { + this.setLayout(new GridLayout(3, 1)); + this.add(new JLabel("Pick your wallet name: ", SwingConstants.LEFT)); + this.add(walletNameField); + this.add(new JLabel("Enter a neighbour node address: ", SwingConstants.LEFT)); + this.add(pathField); + this.add(new JLabel("Select your reachable IP address: ", SwingConstants.LEFT)); + + selectWalletNameField(); + + List<SelectableNetworkInterface> interfaces = NetworkInterfaceReader.fetchNetworkInterfaces(); + + for (SelectableNetworkInterface netint : interfaces) { + hostnameField.addItem(netint); + } + + hostnameField.setSelectedItem(new SelectableNetworkInterface(defaultHostname, "default")); + this.add(hostnameField); + } + + /** + * This method makes sure, that the Wallet name field is selected on display of the dialog + */ + private void selectWalletNameField() { + walletNameField.addAncestorListener(new AncestorListener() { + @Override + public void ancestorAdded(AncestorEvent event) { + final AncestorListener al= this; + SwingUtilities.invokeLater(() -> { + JComponent component = event.getComponent(); + component.requestFocusInWindow(); + component.removeAncestorListener( al ); + }); + } + + @Override + public void ancestorRemoved(AncestorEvent event) { + + } + + @Override + public void ancestorMoved(AncestorEvent event) { + + } + }); + } + + public String getWalletName() { + return walletNameField.getText(); + } + + public String getAddressOfNeighbour() { + return pathField.getText(); + } + + public SelectableNetworkInterface getNetworkInterface() { + return (SelectableNetworkInterface) hostnameField.getSelectedItem(); + } +} -- GitLab